diff --git a/internal/lsp/cache/parse.go b/internal/lsp/cache/parse.go index 816b89abed..6d26674f76 100644 --- a/internal/lsp/cache/parse.go +++ b/internal/lsp/cache/parse.go @@ -328,12 +328,19 @@ func fixMissingCurlies(f *ast.File, b *ast.BlockStmt, parent ast.Node, tok *toke } } + parentLine := tok.Line(parent.Pos()) + + if parentLine >= tok.LineCount() { + // If we are the last line in the file, no need to fix anything. + return nil + } + // Insert curlies at the end of parent's starting line. The parent // is the statement that contains the block, e.g. *ast.IfStmt. The // block's Pos()/End() can't be relied upon because they are based // on the (missing) curly braces. We assume the statement is a // single line for now and try sticking the curly braces at the end. - insertPos := tok.LineStart(tok.Line(parent.Pos())+1) - 1 + insertPos := tok.LineStart(parentLine+1) - 1 // Scootch position backwards until it's not in a comment. For example: // diff --git a/internal/lsp/testdata/lsp/primarymod/danglingstmt/dangling_if_eof.go b/internal/lsp/testdata/lsp/primarymod/danglingstmt/dangling_if_eof.go new file mode 100644 index 0000000000..3454c9fa63 --- /dev/null +++ b/internal/lsp/testdata/lsp/primarymod/danglingstmt/dangling_if_eof.go @@ -0,0 +1,8 @@ +package danglingstmt + +func bar5() bool { //@item(danglingBar5, "bar5", "func() bool", "func") + return true +} + +func _() { + if b //@rank(" //", danglingBar5) diff --git a/internal/lsp/testdata/lsp/summary.txt.golden b/internal/lsp/testdata/lsp/summary.txt.golden index 8681655380..d9120ea7ac 100644 --- a/internal/lsp/testdata/lsp/summary.txt.golden +++ b/internal/lsp/testdata/lsp/summary.txt.golden @@ -5,7 +5,7 @@ CompletionSnippetCount = 67 UnimportedCompletionsCount = 11 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8 -RankedCompletionsCount = 98 +RankedCompletionsCount = 99 CaseSensitiveCompletionsCount = 4 DiagnosticsCount = 38 FoldingRangesCount = 2