fmt: keep comments after imports (#8483)
							parent
							
								
									44ec9e3ebc
								
							
						
					
					
						commit
						8755f40430
					
				| 
						 | 
					@ -284,6 +284,7 @@ pub:
 | 
				
			||||||
	alias_pos token.Position
 | 
						alias_pos token.Position
 | 
				
			||||||
pub mut:
 | 
					pub mut:
 | 
				
			||||||
	syms     []ImportSymbol // the list of symbols in `import {symbol1, symbol2}`
 | 
						syms     []ImportSymbol // the list of symbols in `import {symbol1, symbol2}`
 | 
				
			||||||
 | 
						comments []Comment
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// import symbol,for import {symbol} syntax
 | 
					// import symbol,for import {symbol} syntax
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -105,6 +105,34 @@ pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn (mut f Fmt) import_comments(comments []ast.Comment, options CommentsOptions) {
 | 
				
			||||||
 | 
						if comments.len == 0 {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if options.inline {
 | 
				
			||||||
 | 
							mut i := 0
 | 
				
			||||||
 | 
							for i = f.out_imports.len - 1; i >= 0; i-- {
 | 
				
			||||||
 | 
								if !f.out_imports.buf[i].is_space() { // != `\n` {
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							f.out_imports.go_back(f.out_imports.len - i - 1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for c in comments {
 | 
				
			||||||
 | 
							ctext := c.text.trim_left('\x01')
 | 
				
			||||||
 | 
							if ctext == '' {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mut out_s := if options.inline { ' ' } else { '' }
 | 
				
			||||||
 | 
							out_s += '//'
 | 
				
			||||||
 | 
							if is_first_char_alphanumeric(ctext) {
 | 
				
			||||||
 | 
								out_s += ' '
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							out_s += ctext
 | 
				
			||||||
 | 
							f.out_imports.writeln(out_s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn is_first_char_alphanumeric(s string) bool {
 | 
					fn is_first_char_alphanumeric(s string) bool {
 | 
				
			||||||
	return match s[0] {
 | 
						return match s[0] {
 | 
				
			||||||
		`a`...`z`, `A`...`Z`, `0`...`9` { true }
 | 
							`a`...`z`, `A`...`Z`, `0`...`9` { true }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,7 +22,6 @@ pub mut:
 | 
				
			||||||
	table              &table.Table
 | 
						table              &table.Table
 | 
				
			||||||
	out_imports        strings.Builder
 | 
						out_imports        strings.Builder
 | 
				
			||||||
	out                strings.Builder
 | 
						out                strings.Builder
 | 
				
			||||||
	out_save           strings.Builder
 | 
					 | 
				
			||||||
	indent             int
 | 
						indent             int
 | 
				
			||||||
	empty_line         bool
 | 
						empty_line         bool
 | 
				
			||||||
	line_len           int
 | 
						line_len           int
 | 
				
			||||||
| 
						 | 
					@ -272,6 +271,7 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		already_imported[import_text] = true
 | 
							already_imported[import_text] = true
 | 
				
			||||||
		f.out_imports.writeln(import_text)
 | 
							f.out_imports.writeln(import_text)
 | 
				
			||||||
 | 
							f.import_comments(imp.comments, inline: true)
 | 
				
			||||||
		num_imports++
 | 
							num_imports++
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if num_imports > 0 {
 | 
						if num_imports > 0 {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +0,0 @@
 | 
				
			||||||
import semver
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// as semver
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn main() {
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,4 +0,0 @@
 | 
				
			||||||
import semver// as semver
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn main() {
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					import semver // as sv
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum Abc {
 | 
					enum Abc {
 | 
				
			||||||
	a
 | 
						a
 | 
				
			||||||
	b // after a value
 | 
						b // after a value
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +0,0 @@
 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn main() {
 | 
					 | 
				
			||||||
	os.system('echo hi')
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1907,6 +1907,7 @@ fn (mut p Parser) import_stmt() ast.Import {
 | 
				
			||||||
			return import_node
 | 
								return import_node
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						import_node.comments = p.eat_line_end_comments()
 | 
				
			||||||
	p.imports[mod_alias] = mod_name
 | 
						p.imports[mod_alias] = mod_name
 | 
				
			||||||
	// if mod_name !in p.table.imports {
 | 
						// if mod_name !in p.table.imports {
 | 
				
			||||||
	p.table.imports << mod_name
 | 
						p.table.imports << mod_name
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue