Skip to content

Commit c229649

Browse files
muirdmstamblerre
authored andcommitted
internal/lsp/cache: fix crash fixing curlies near EOF
We were crashing in cases like: 1: func foo() { 2: if b<> <EOF> We were trying to get the line start position for line 3, but there is no line 3. Fix by bailing out early if we are the last line in the file because there is nothing to fix in that case. Fixes golang/go#37226. Change-Id: I4ad5746d7b55bdcc2de57c04e972c15a61084faa Reviewed-on: https://go-review.googlesource.com/c/tools/+/219498 Run-TryBot: Muir Manders <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 88be013 commit c229649

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

internal/lsp/cache/parse.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,19 @@ func fixMissingCurlies(f *ast.File, b *ast.BlockStmt, parent ast.Node, tok *toke
328328
}
329329
}
330330

331+
parentLine := tok.Line(parent.Pos())
332+
333+
if parentLine >= tok.LineCount() {
334+
// If we are the last line in the file, no need to fix anything.
335+
return nil
336+
}
337+
331338
// Insert curlies at the end of parent's starting line. The parent
332339
// is the statement that contains the block, e.g. *ast.IfStmt. The
333340
// block's Pos()/End() can't be relied upon because they are based
334341
// on the (missing) curly braces. We assume the statement is a
335342
// single line for now and try sticking the curly braces at the end.
336-
insertPos := tok.LineStart(tok.Line(parent.Pos())+1) - 1
343+
insertPos := tok.LineStart(parentLine+1) - 1
337344

338345
// Scootch position backwards until it's not in a comment. For example:
339346
//
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package danglingstmt
2+
3+
func bar5() bool { //@item(danglingBar5, "bar5", "func() bool", "func")
4+
return true
5+
}
6+
7+
func _() {
8+
if b //@rank(" //", danglingBar5)

internal/lsp/testdata/lsp/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CompletionSnippetCount = 67
55
UnimportedCompletionsCount = 11
66
DeepCompletionsCount = 5
77
FuzzyCompletionsCount = 8
8-
RankedCompletionsCount = 98
8+
RankedCompletionsCount = 99
99
CaseSensitiveCompletionsCount = 4
1010
DiagnosticsCount = 38
1111
FoldingRangesCount = 2

0 commit comments

Comments
 (0)