1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
// Copyright 2022 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 misc
import (
"testing"
"golang.org/x/tools/gopls/internal/lsp/protocol"
. "golang.org/x/tools/gopls/internal/lsp/regtest"
"golang.org/x/tools/gopls/internal/lsp/source"
)
func TestWorkspaceSymbolMissingMetadata(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.17
-- a.go --
package p
const C1 = "a.go"
-- exclude.go --
//go:build exclude
// +build exclude
package exclude
const C2 = "exclude.go"
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
syms := env.WorkspaceSymbol("C")
if got, want := len(syms), 1; got != want {
t.Errorf("got %d symbols, want %d", got, want)
}
// Opening up an ignored file will result in an overlay with missing
// metadata, but this shouldn't break workspace symbols requests.
env.OpenFile("exclude.go")
syms = env.WorkspaceSymbol("C")
if got, want := len(syms), 1; got != want {
t.Errorf("got %d symbols, want %d", got, want)
}
})
}
func TestWorkspaceSymbolSorting(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.17
-- a/a.go --
package a
const (
Foo = iota
FooBar
Fooey
Fooex
Fooest
)
`
var symbolMatcher = string(source.SymbolFastFuzzy)
WithOptions(
Settings{"symbolMatcher": symbolMatcher},
).Run(t, files, func(t *testing.T, env *Env) {
want := []string{
"Foo", // prefer exact segment matches first
"FooBar", // ...followed by exact word matches
"Fooex", // shorter than Fooest, FooBar, lexically before Fooey
"Fooey", // shorter than Fooest, Foobar
"Fooest",
}
got := env.WorkspaceSymbol("Foo")
compareSymbols(t, got, want)
})
}
func TestWorkspaceSymbolSpecialPatterns(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.17
-- a/a.go --
package a
const (
AxxBxxCxx
ABC
)
`
var symbolMatcher = string(source.SymbolFastFuzzy)
WithOptions(
Settings{"symbolMatcher": symbolMatcher},
).Run(t, files, func(t *testing.T, env *Env) {
compareSymbols(t, env.WorkspaceSymbol("ABC"), []string{"ABC", "AxxBxxCxx"})
compareSymbols(t, env.WorkspaceSymbol("'ABC"), []string{"ABC"})
compareSymbols(t, env.WorkspaceSymbol("^mod.com"), []string{"mod.com/a.ABC", "mod.com/a.AxxBxxCxx"})
compareSymbols(t, env.WorkspaceSymbol("^mod.com Axx"), []string{"mod.com/a.AxxBxxCxx"})
compareSymbols(t, env.WorkspaceSymbol("C$"), []string{"ABC"})
})
}
func compareSymbols(t *testing.T, got []protocol.SymbolInformation, want []string) {
t.Helper()
if len(got) != len(want) {
t.Errorf("got %d symbols, want %d", len(got), len(want))
}
for i := range got {
if got[i].Name != want[i] {
t.Errorf("got[%d] = %q, want %q", i, got[i].Name, want[i])
}
}
}
|