fmt: only insert a space after // if the 3rd char is alphanumeric (#7330)

pull/7342/head
Lukas Neubert 2020-12-15 04:26:28 +01:00 committed by GitHub
parent 8ab59c5f0f
commit c922565525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -1204,16 +1204,19 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
if !node.text.contains('\n') {
is_separate_line := !options.inline || node.text.starts_with('\x01')
mut s := if node.text.starts_with('\x01') { node.text[1..] } else { node.text }
if s == '' {
s = '//'
} else {
s = '// ' + s
mut out_s := '//'
if s != '' {
match s[0] {
`a`...`z`, `A`...`Z`, `0`...`9` { out_s += ' ' }
else {}
}
out_s += s
}
if !is_separate_line && f.indent > 0 {
f.remove_new_line() // delete the generated \n
f.write(' ')
}
f.write(s)
f.write(out_s)
return
}
lines := node.text.split_into_lines()

View File

@ -30,6 +30,9 @@ fn main() {
a := 123 // comment after assign
b := 'foo' // also comment after assign
c := true
// between two assigns
// Between two assigns
d := false
//////
// /
// 123
}

View File

@ -926,7 +926,7 @@ fn (mut s Scanner) text_scan() token.Token {
}
if s.should_parse_comment() {
s.line_comment = s.text[start + 1..comment_line_end]
mut comment := s.line_comment.trim_space()
mut comment := s.line_comment
// Find out if this comment is on its own line (for vfmt)
mut is_separate_line_comment := true
for j := start - 2; j >= 0 && s.text[j] != `\n`; j-- {