1
0
mirror of https://github.com/golang/go synced 2024-10-01 09:38:36 -06:00
go/internal/lsp/snippet/snippet_builder_test.go
Muir Manders 45abd40133 internal/lsp: introduce snippet builder object
*snippet.Builder helps you construct lsp completion snippet strings.
It handles escaping for you, and it gives a convenient interface for
writing nested placeholders.

Note that the builder does not support snippet "variables" or
associated features. They add a lot of complexity and don't seem very
useful at this point (plus they aren't supported in many editors).

Change-Id: I492ab2f6f0e08ed952154cbc0a17c86f32abf40a
GitHub-Last-Rev: 35a3f5d1cd0b6fda81b2c942a02aa1bd25c90acd
GitHub-Pull-Request: golang/tools#90
Reviewed-on: https://go-review.googlesource.com/c/tools/+/173661
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-04-25 18:37:29 +00:00

50 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)) {
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"})
})
}