fmt: do not move single line block comments to previous line (#8046)

pull/8077/head
Lukas Neubert 2021-01-13 03:30:24 +01:00 committed by GitHub
parent 03a0534483
commit 0e490766df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 4 deletions

View File

@ -725,7 +725,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
continue continue
} }
if comment.pos.pos > field.pos.pos { if comment.pos.pos > field.pos.pos {
comments_len += '/* $comment.text */ '.len comments_len += '/* ${comment.text.trim_left('\x01')} */ '.len
} }
} }
field_aligns.add_info(comments_len + field.name.len, ft.len, field.pos.line_nr) field_aligns.add_info(comments_len + field.name.len, ft.len, field.pos.line_nr)
@ -764,7 +764,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
// Handle comments between field name and type // Handle comments between field name and type
mut comments_len := 0 mut comments_len := 0
for comm_idx < comments.len && comments[comm_idx].pos.pos < end_pos { for comm_idx < comments.len && comments[comm_idx].pos.pos < end_pos {
comment_text := '/* ${comments[comm_idx].text} */ ' // TODO handle in a function comment_text := '/* ${comments[comm_idx].text.trim_left('\x01')} */ ' // TODO handle in a function
comments_len += comment_text.len comments_len += comment_text.len
f.write(comment_text) f.write(comment_text)
comm_idx++ comm_idx++
@ -1419,7 +1419,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
} }
if !node.text.contains('\n') { if !node.text.contains('\n') {
is_separate_line := !options.inline || node.text.starts_with('\x01') is_separate_line := !options.inline || node.text.starts_with('\x01')
mut s := if node.text.starts_with('\x01') { node.text[1..] } else { node.text } mut s := node.text.trim_left('\x01')
mut out_s := '//' mut out_s := '//'
if s != '' { if s != '' {
match s[0] { match s[0] {

View File

@ -9,6 +9,18 @@ fn mr_fun() (int, int) {
return 1, 2 return 1, 2
} }
fn single_line_blocks() {
// 1
println('')
// 2
println('')
// 3
// 4
println('')
// 5
// 6
}
fn main() { fn main() {
/* /*
block1 block1

View File

@ -6,6 +6,18 @@ fn mr_fun() (int, int) {
return /* one comment */ 1, /* another comment */ 2 return /* one comment */ 1, /* another comment */ 2
} }
fn single_line_blocks() {
/* 1 */
println('')
/* 2 */
println('')
/* 3 */
/* 4 */
println('')
// 5
/* 6 */
}
fn main() { fn main() {
/* block1 /* block1
*/ */

View File

@ -959,7 +959,10 @@ fn (mut s Scanner) text_scan() token.Token {
} }
s.pos++ s.pos++
if s.should_parse_comment() { if s.should_parse_comment() {
comment := s.text[start..(s.pos - 1)].trim(' ') mut comment := s.text[start..(s.pos - 1)].trim(' ')
if !comment.contains('\n') {
comment = '\x01' + comment
}
return s.new_token(.comment, comment, comment.len + 4) return s.new_token(.comment, comment, comment.len + 4)
} }
// Skip if not in fmt mode // Skip if not in fmt mode