1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:14:43 -07:00
go/internal/lsp/snippet/snippet_builder_test.go
Muir Manders dd2b5c81c5 internal/lsp: simplify snippet config/generation
I moved the "usePlaceholders" config field on to CompletionOptions.
This way the completion code generates a single snippet with a little
conditional logic based on the "WantPlaceholders" option instead of
juggling the generation of two almost identical "plain" and
"placeholder" snippets at the same time. It also reduces the work done
generating completion candidates a little.

I also made a minor tweak to the snippet builder where empty
placeholders are now always represented as e.g "${1:}" instead of
"${1}" or "${1:}", depending on if you passed a callback to
WritePlaceholder() or not.

Change-Id: Ib84cc0cd729a11b9e13ad3ac4b6fd2d82460acd5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193697
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-09-10 04:45:52 +00:00

52 lines
1.1 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package snippet
import (
"testing"
)
func TestSnippetBuilder(t *testing.T) {
expect := func(expected string, fn func(*Builder)) {
t.Helper()
var b Builder
fn(&b)
if got := b.String(); got != expected {
t.Errorf("got %q, expected %q", got, expected)
}
}
expect("", func(b *Builder) {})
expect(`hi { \} \$ | " , / \\`, func(b *Builder) {
b.WriteText(`hi { } $ | " , / \`)
})
expect("${1:}", func(b *Builder) {
b.WritePlaceholder(nil)
})
expect("hi ${1:there}", func(b *Builder) {
b.WriteText("hi ")
b.WritePlaceholder(func(b *Builder) {
b.WriteText("there")
})
})
expect(`${1:id=${2:{your id\}}}`, func(b *Builder) {
b.WritePlaceholder(func(b *Builder) {
b.WriteText("id=")
b.WritePlaceholder(func(b *Builder) {
b.WriteText("{your id}")
})
})
})
expect(`${1|one,{ \} \$ \| " \, / \\,three|}`, func(b *Builder) {
b.WriteChoice([]string{"one", `{ } $ | " , / \`, "three"})
})
}