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 s := node.text.trim_left('\x01') | ||||||
| 		mut out_s := '//' | 		mut out_s := '//' | ||||||
| 		if s != '' { | 		if s != '' { | ||||||
| 			match s[0] { | 			if is_first_char_alphanumeric(s) { | ||||||
| 				`a`...`z`, `A`...`Z`, `0`...`9` { out_s += ' ' } | 				out_s += ' ' | ||||||
| 				else {} |  | ||||||
| 			} | 			} | ||||||
| 			out_s += s | 			out_s += s | ||||||
| 		} | 		} | ||||||
|  | @ -57,12 +56,21 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { | ||||||
| 		f.write(out_s) | 		f.write(out_s) | ||||||
| 	} else { | 	} else { | ||||||
| 		lines := node.text.trim_space().split_into_lines() | 		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 { | 		for line in lines { | ||||||
| 			f.writeln(line) | 			f.writeln(line) | ||||||
| 			f.empty_line = false | 			f.empty_line = false | ||||||
| 		} | 		} | ||||||
|  | 		if no_new_lines { | ||||||
|  | 			f.remove_new_line() | ||||||
|  | 		} else { | ||||||
| 			f.empty_line = true | 			f.empty_line = true | ||||||
|  | 		} | ||||||
| 		f.write('*/') | 		f.write('*/') | ||||||
| 	} | 	} | ||||||
| 	if options.level == .indent { | 	if options.level == .indent { | ||||||
|  | @ -96,3 +104,10 @@ pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) { | ||||||
| 		f.indent-- | 		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
 | 	// empty return
 | ||||||
| 	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
 |   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{ | 	_ := User{ | ||||||
| 		// Just a comment
 | 		// Just a comment
 | ||||||
| 	} | 	} | ||||||
|  | 	//////
 | ||||||
|  | 	// /
 | ||||||
|  | 	// 123
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn assign_comments() { | ||||||
| 	a := 123 // comment after assign
 | 	a := 123 // comment after assign
 | ||||||
| 	b := 'foo' // also comment after assign
 | 	b := 'foo' // also comment after assign
 | ||||||
| 	c := true | 	c := true | ||||||
| 	// Between two assigns
 | 	// Between two assigns
 | ||||||
| 	d := false | 	d := false | ||||||
| 	//////
 | 	// at the end
 | ||||||
| 	// /
 | } | ||||||
| 	// 123
 | 
 | ||||||
|  | fn linebreaks_in_block_comments() { | ||||||
| 	/* | 	/* | ||||||
| 	block | 	block | ||||||
| 	*/ | 	*/ | ||||||
| 	println('hello world') | 	/***** | ||||||
|  | 	Want a long line of stars? | ||||||
|  | 	no problem | ||||||
|  | 	*****/ | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue