| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cmd |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"flag" |
| 11 |
"fmt" |
| 12 |
"log" |
| 13 |
"os" |
| 14 |
"slices" |
| 15 |
"unicode/utf8" |
| 16 |
|
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol/semtok" |
| 19 |
"golang.org/x/tools/gopls/internal/settings" |
| 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 |
type semanticToken struct { |
| 46 |
app *Application |
| 47 |
} |
| 48 |
|
| 49 |
func (c *semanticToken) Name() string { return "semtok" } |
| 50 |
func (c *semanticToken) Parent() string { return c.app.Name() } |
| 51 |
func (c *semanticToken) Usage() string { return "<filename>" } |
| 52 |
func (c *semanticToken) ShortHelp() string { return "show semantic tokens for the specified file" } |
| 53 |
func (c *semanticToken) DetailedHelp(f *flag.FlagSet) { |
| 54 |
fmt.Fprint(f.Output(), ` |
| 55 |
Example: show the semantic tokens for this file: |
| 56 |
|
| 57 |
$ gopls semtok internal/cmd/semtok.go |
| 58 |
`) |
| 59 |
printFlagDefaults(f) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
func (c *semanticToken) Run(ctx context.Context, args ...string) error { |
| 65 |
if len(args) != 1 { |
| 66 |
return fmt.Errorf("expected one file name, got %d", len(args)) |
| 67 |
} |
| 68 |
|
| 69 |
origOptions := c.app.options |
| 70 |
c.app.options = func(opts *settings.Options) { |
| 71 |
if origOptions != nil { |
| 72 |
origOptions(opts) |
| 73 |
} |
| 74 |
opts.SemanticTokens = true |
| 75 |
} |
| 76 |
cli, _, err := c.app.connect(ctx) |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
defer cli.terminate(ctx) |
| 81 |
uri := protocol.URIFromPath(args[0]) |
| 82 |
file, err := cli.openFile(ctx, uri) |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
|
| 87 |
lines := bytes.Split(file.mapper.Content, []byte{'\n'}) |
| 88 |
params := &protocol.SemanticTokensRangeParams{ |
| 89 |
TextDocument: protocol.TextDocumentIdentifier{ |
| 90 |
URI: uri, |
| 91 |
}, |
| 92 |
Range: protocol.Range{Start: protocol.Position{Line: 0, Character: 0}, |
| 93 |
End: protocol.Position{ |
| 94 |
Line: uint32(len(lines) - 1), |
| 95 |
Character: uint32(len(lines[len(lines)-1]))}, |
| 96 |
}, |
| 97 |
} |
| 98 |
resp, err := cli.server.SemanticTokensRange(ctx, params) |
| 99 |
if err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
legend := cli.initializeResult.Capabilities.SemanticTokensProvider.(protocol.SemanticTokensOptions).Legend |
| 103 |
return decorate(legend, file, resp.Data) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
type mark struct { |
| 109 |
line, offset int |
| 110 |
len int |
| 111 |
typ semtok.Type |
| 112 |
mods []semtok.Modifier |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
const ( |
| 117 |
SemanticLeft = "/*⇐" |
| 118 |
SemanticRight = "/*⇒" |
| 119 |
) |
| 120 |
|
| 121 |
func markLine(m mark, lines [][]byte) { |
| 122 |
l := lines[m.line-1] |
| 123 |
length := utf8.RuneCount(l[m.offset-1 : m.offset-1+m.len]) |
| 124 |
splitAt := m.offset - 1 |
| 125 |
insert := "" |
| 126 |
if m.typ == "namespace" && m.offset-1+m.len < len(l) && l[m.offset-1+m.len] == '"' { |
| 127 |
|
| 128 |
|
| 129 |
insert = fmt.Sprintf("%s%d,namespace,[]*/", SemanticLeft, length) |
| 130 |
splitAt = m.offset + m.len |
| 131 |
} else { |
| 132 |
|
| 133 |
spacer := "" |
| 134 |
if splitAt-1 >= 0 && l[splitAt-1] == '/' { |
| 135 |
spacer = " " |
| 136 |
} |
| 137 |
insert = fmt.Sprintf("%s%s%d,%s,%v*/", spacer, SemanticRight, length, m.typ, m.mods) |
| 138 |
} |
| 139 |
x := append([]byte(insert), l[splitAt:]...) |
| 140 |
l = append(l[:splitAt], x...) |
| 141 |
lines[m.line-1] = l |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
func decorate(legend protocol.SemanticTokensLegend, file *cmdFile, data []uint32) error { |
| 147 |
marks := newMarks(legend, file, data) |
| 148 |
if len(marks) == 0 { |
| 149 |
return nil |
| 150 |
} |
| 151 |
lines := bytes.Split(file.mapper.Content, []byte{'\n'}) |
| 152 |
for _, mark := range slices.Backward(marks) { |
| 153 |
markLine(mark, lines) |
| 154 |
} |
| 155 |
os.Stdout.Write(bytes.Join(lines, []byte{'\n'})) |
| 156 |
return nil |
| 157 |
} |
| 158 |
|
| 159 |
func newMarks(legend protocol.SemanticTokensLegend, file *cmdFile, data []uint32) []mark { |
| 160 |
ans := []mark{} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
lspLine := make([]uint32, len(data)/5) |
| 165 |
lspChar := make([]uint32, len(data)/5) |
| 166 |
var line, char uint32 |
| 167 |
for i := 0; 5*i < len(data); i++ { |
| 168 |
lspLine[i] = line + data[5*i+0] |
| 169 |
if data[5*i+0] > 0 { |
| 170 |
char = 0 |
| 171 |
} |
| 172 |
lspChar[i] = char + data[5*i+1] |
| 173 |
char = lspChar[i] |
| 174 |
line = lspLine[i] |
| 175 |
} |
| 176 |
|
| 177 |
for i := 0; 5*i < len(data); i++ { |
| 178 |
pr := protocol.Range{ |
| 179 |
Start: protocol.Position{ |
| 180 |
Line: lspLine[i], |
| 181 |
Character: lspChar[i], |
| 182 |
}, |
| 183 |
End: protocol.Position{ |
| 184 |
Line: lspLine[i], |
| 185 |
Character: lspChar[i] + data[5*i+2], |
| 186 |
}, |
| 187 |
} |
| 188 |
spn, err := file.rangeSpan(pr) |
| 189 |
if err != nil { |
| 190 |
log.Fatal(err) |
| 191 |
} |
| 192 |
|
| 193 |
var mods []semtok.Modifier |
| 194 |
{ |
| 195 |
n := int(data[5*i+4]) |
| 196 |
for i, mod := range legend.TokenModifiers { |
| 197 |
if (n & (1 << i)) != 0 { |
| 198 |
mods = append(mods, semtok.Modifier(mod)) |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
m := mark{ |
| 204 |
line: spn.Start().Line(), |
| 205 |
offset: spn.Start().Column(), |
| 206 |
len: spn.End().Column() - spn.Start().Column(), |
| 207 |
typ: semtok.Type(legend.TokenTypes[data[5*i+3]]), |
| 208 |
mods: mods, |
| 209 |
} |
| 210 |
ans = append(ans, m) |
| 211 |
} |
| 212 |
return ans |
| 213 |
} |