@@ -612,47 +612,31 @@ func typeReferencesToCompletionItems(
612612 }
613613 }
614614 }
615- var lastImportLine uint32
615+ var lastImportLine int64
616616 currentImportPaths := map [string ]struct {}{}
617617 for currentFileImport := range seq .Values (current .ir .Imports ()) {
618- l := currentFileImport .Decl .Span ().EndLoc ().Line
619- if l < 0 || l > math .MaxUint32 {
620- continue // skip this import; exceptional case.
621- }
622- lastImportLine = max (uint32 (l ), lastImportLine )
618+ lastImportLine = max (lastImportLine , int64 (currentFileImport .Decl .Span ().EndLoc ().Line ))
623619 currentImportPaths [currentFileImport .Path ()] = struct {}{}
624620 }
625- var insertPosition protocol.Position
626- if lastImportLine != 0 {
627- // add an additionalTextEdit to the end of the current imports, which can then be tidied by
628- // `buf format` to go in it's proper location.
629- insertPosition = protocol.Position {
630- Line : lastImportLine ,
631- Character : 0 ,
632- }
633- } else {
621+ if lastImportLine == 0 {
634622 // If lastImportLine is 0, we have no imports in this file; put it after `package`, if
635623 // package exists. Otherwise, after `syntax`, if it exists. Otherwise, balk.
636624 // NOTE: We simply want to add the import on the next line (which may not be how `buf
637625 // format` would format the file); we leave the overall file formatting to `buf format`.
638- var line int
639626 switch {
640627 case ! current .ir .AST ().Package ().IsZero ():
641- line = current .ir .AST ().Package ().Span ().EndLoc ().Line
628+ lastImportLine = int64 ( current .ir .AST ().Package ().Span ().EndLoc ().Line )
642629 case ! current .ir .AST ().Syntax ().IsZero ():
643- line = current .ir .AST ().Syntax ().Span ().EndLoc ().Line
644- default :
645- line = 0
646- }
647- if line < 0 || line > math .MaxUint32 {
648- // Nothing to do; exceptional case.
649- } else {
650- insertPosition = protocol.Position {
651- Line : uint32 (line ),
652- Character : 0 ,
653- }
630+ lastImportLine = int64 (current .ir .AST ().Syntax ().Span ().EndLoc ().Line )
654631 }
655632 }
633+ if lastImportLine < 0 || lastImportLine > math .MaxUint32 {
634+ lastImportLine = 0 // Default to insert at top of page.
635+ }
636+ importInsertPosition := protocol.Position {
637+ Line : uint32 (lastImportLine ),
638+ Character : 0 ,
639+ }
656640 parentPrefix := string (parentFullName ) + "."
657641 packagePrefix := string (current .ir .Package ()) + "."
658642 return func (yield func (protocol.CompletionItem ) bool ) {
@@ -700,8 +684,8 @@ func typeReferencesToCompletionItems(
700684 additionalTextEdits = append (additionalTextEdits , protocol.TextEdit {
701685 NewText : "import " + `"` + symbolFile + `";` + "\n " ,
702686 Range : protocol.Range {
703- Start : insertPosition ,
704- End : insertPosition ,
687+ Start : importInsertPosition ,
688+ End : importInsertPosition ,
705689 },
706690 })
707691 }
0 commit comments