Skip to content

Commit 73d6794

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/template: fix completion token boundary conditions
The template completion logic checked to see if the cursor position was inside a logical {{ }} block, but the boundary conditions in the relevant checks was wrong: in one case, a panic would occur if the cursor was in between the pair of left braces. In another, it would panic even if the cursor was at the start of a pair of left braces. Fix the boundary conditions, with a test. Fixes golang/go#57621 Change-Id: I826349906ee1ae67b2c5378e1b59d56e94c14fb2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/622875 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 91421d7 commit 73d6794

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

gopls/internal/template/completion.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ func inTemplate(fc *Parsed, pos protocol.Position) int {
8484
offset := fc.FromPosition(pos)
8585
// this could be a binary search, as the tokens are ordered
8686
for _, tk := range fc.tokens {
87-
if tk.Start < offset && offset <= tk.End {
87+
if tk.Start+len(Left) <= offset && offset+len(Right) <= tk.End {
8888
return tk.Start
8989
}
9090
}
9191
for _, x := range fc.elided {
92-
if x > offset {
93-
// fc.elided is sorted
92+
if x+len(Left) > offset {
93+
// fc.elided is sorted, and x is the position where a '{{' was replaced
94+
// by ' '. We consider only cases where the replaced {{ is to the left
95+
// of the cursor.
9496
break
9597
}
9698
// If the interval [x,offset] does not contain Left or Right

gopls/internal/test/integration/template/template_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,30 @@ func shorten(fn protocol.DocumentURI) string {
228228
return pieces[j-2] + "/" + pieces[j-1]
229229
}
230230

231+
func TestCompletionPanic_Issue57621(t *testing.T) {
232+
const src = `
233+
-- go.mod --
234+
module mod.com
235+
236+
go 1.12
237+
-- hello.tmpl --
238+
{{range .Planets}}
239+
Hello {{
240+
{{end}}
241+
`
242+
Run(t, src, func(t *testing.T, env *Env) {
243+
env.OpenFile("hello.tmpl")
244+
// None of these should panic.
245+
env.Completion(env.RegexpSearch("hello.tmpl", `Hello ()\{\{`))
246+
env.Completion(env.RegexpSearch("hello.tmpl", `Hello \{()\{`))
247+
env.Completion(env.RegexpSearch("hello.tmpl", `Hello \{\{()`))
248+
env.Completion(env.RegexpSearch("hello.tmpl", `()\{\{range`))
249+
env.Completion(env.RegexpSearch("hello.tmpl", `\{()\{range`))
250+
env.Completion(env.RegexpSearch("hello.tmpl", `\{\{()range`))
251+
env.Completion(env.RegexpSearch("hello.tmpl", `Planets()}}`))
252+
env.Completion(env.RegexpSearch("hello.tmpl", `Planets}()}`))
253+
env.Completion(env.RegexpSearch("hello.tmpl", `Planets}}()`))
254+
})
255+
}
256+
231257
// Hover needs tests

0 commit comments

Comments
 (0)