mirror of
https://github.com/golang/go
synced 2024-11-18 16:14:46 -07:00
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 <muir@mnd.rs> Reviewed-by: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
88be01311a
commit
c229649527
9
internal/lsp/cache/parse.go
vendored
9
internal/lsp/cache/parse.go
vendored
@ -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:
|
||||
//
|
||||
|
8
internal/lsp/testdata/lsp/primarymod/danglingstmt/dangling_if_eof.go
vendored
Normal file
8
internal/lsp/testdata/lsp/primarymod/danglingstmt/dangling_if_eof.go
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
package danglingstmt
|
||||
|
||||
func bar5() bool { //@item(danglingBar5, "bar5", "func() bool", "func")
|
||||
return true
|
||||
}
|
||||
|
||||
func _() {
|
||||
if b //@rank(" //", danglingBar5)
|
2
internal/lsp/testdata/lsp/summary.txt.golden
vendored
2
internal/lsp/testdata/lsp/summary.txt.golden
vendored
@ -5,7 +5,7 @@ CompletionSnippetCount = 67
|
||||
UnimportedCompletionsCount = 11
|
||||
DeepCompletionsCount = 5
|
||||
FuzzyCompletionsCount = 8
|
||||
RankedCompletionsCount = 98
|
||||
RankedCompletionsCount = 99
|
||||
CaseSensitiveCompletionsCount = 4
|
||||
DiagnosticsCount = 38
|
||||
FoldingRangesCount = 2
|
||||
|
Loading…
Reference in New Issue
Block a user