fmt: better newline handling in block comments (#8325)
							parent
							
								
									750738aa12
								
							
						
					
					
						commit
						997f56a3dc
					
				|  | @ -44,9 +44,8 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { | |||
| 		mut s := node.text.trim_left('\x01') | ||||
| 		mut out_s := '//' | ||||
| 		if s != '' { | ||||
| 			match s[0] { | ||||
| 				`a`...`z`, `A`...`Z`, `0`...`9` { out_s += ' ' } | ||||
| 				else {} | ||||
| 			if is_first_char_alphanumeric(s) { | ||||
| 				out_s += ' ' | ||||
| 			} | ||||
| 			out_s += s | ||||
| 		} | ||||
|  | @ -57,12 +56,21 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { | |||
| 		f.write(out_s) | ||||
| 	} else { | ||||
| 		lines := node.text.trim_space().split_into_lines() | ||||
| 		f.writeln('/*') | ||||
| 		expected_line_count := node.pos.last_line - node.pos.line_nr | ||||
| 		no_new_lines := lines.len > expected_line_count && !is_first_char_alphanumeric(lines[0]) | ||||
| 		f.write('/*') | ||||
| 		if !no_new_lines { | ||||
| 			f.writeln('') | ||||
| 		} | ||||
| 		for line in lines { | ||||
| 			f.writeln(line) | ||||
| 			f.empty_line = false | ||||
| 		} | ||||
| 		f.empty_line = true | ||||
| 		if no_new_lines { | ||||
| 			f.remove_new_line() | ||||
| 		} else { | ||||
| 			f.empty_line = true | ||||
| 		} | ||||
| 		f.write('*/') | ||||
| 	} | ||||
| 	if options.level == .indent { | ||||
|  | @ -96,3 +104,10 @@ pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) { | |||
| 		f.indent-- | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| fn is_first_char_alphanumeric(s string) bool { | ||||
| 	return match s[0] { | ||||
| 		`a`...`z`, `A`...`Z`, `0`...`9` { true } | ||||
| 		else { false } | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -64,3 +64,20 @@ fn main() { | |||
| 	// empty return
 | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| fn insert_space() { | ||||
| 	// abc
 | ||||
| } | ||||
| 
 | ||||
| fn linebreaks_in_block_comments() { | ||||
| 	/* | ||||
| 	foo | ||||
| 	comment goes here! | ||||
| 	bar | ||||
| 	*/ | ||||
| 	/* | ||||
| 	spam | ||||
| 	spaces make no difference there | ||||
| 	eggs | ||||
| 	*/ | ||||
| } | ||||
|  |  | |||
|  | @ -52,3 +52,16 @@ fn main() { | |||
| 	} | ||||
|   return // empty return
 | ||||
| } | ||||
| 
 | ||||
| fn insert_space() { | ||||
| 	//abc
 | ||||
| } | ||||
| 
 | ||||
| fn linebreaks_in_block_comments() { | ||||
| 	/*foo | ||||
| 	comment goes here! | ||||
| 	bar*/ | ||||
| 	/*  spam | ||||
| 	spaces make no difference there | ||||
| 	eggs */ | ||||
| } | ||||
|  |  | |||
|  | @ -35,16 +35,26 @@ fn main() { | |||
| 	_ := User{ | ||||
| 		// Just a comment
 | ||||
| 	} | ||||
| 	//////
 | ||||
| 	// /
 | ||||
| 	// 123
 | ||||
| } | ||||
| 
 | ||||
| fn assign_comments() { | ||||
| 	a := 123 // comment after assign
 | ||||
| 	b := 'foo' // also comment after assign
 | ||||
| 	c := true | ||||
| 	// Between two assigns
 | ||||
| 	d := false | ||||
| 	//////
 | ||||
| 	// /
 | ||||
| 	// 123
 | ||||
| 	// at the end
 | ||||
| } | ||||
| 
 | ||||
| fn linebreaks_in_block_comments() { | ||||
| 	/* | ||||
| 	block | ||||
| 	*/ | ||||
| 	println('hello world') | ||||
| 	/***** | ||||
| 	Want a long line of stars? | ||||
| 	no problem | ||||
| 	*****/ | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue