-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
208 lines (177 loc) · 6 KB
/
index.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { spawn } from "child_process";
import { readFileSync } from "fs";
import * as lsp from "vscode-languageserver-protocol/node.js";
let proc = spawn("gopls")
let conn = lsp.createProtocolConnection(
new lsp.StreamMessageReader(proc.stdout),
new lsp.StreamMessageWriter(proc.stdin)
);
conn.listen();
const SemTokenTypes = [];
for (const type in lsp.SemanticTokenTypes) {
SemTokenTypes.push(type);
}
const SemTokenModifiers = [];
for (const modifier in lsp.SemanticTokenModifiers) {
SemTokenModifiers.push(modifier);
}
const initReq: lsp.InitializeParams = {
rootUri: "file://G:/code/go/btKeeper",
workspaceFolders: [
{
uri: "file://G:/code/go/btKeeper",
name: "btKeeper"
}
],
processId: process.pid,
capabilities: {
textDocument: {
synchronization: {
dynamicRegistration: false,
didSave: false,
willSave: false,
willSaveWaitUntil: false
},
hover: {
dynamicRegistration: false,
contentFormat: [lsp.MarkupKind.Markdown, lsp.MarkupKind.PlainText]
},
documentSymbol: {
dynamicRegistration: false,
symbolKind: {
valueSet: [
lsp.SymbolKind.File,
lsp.SymbolKind.Module,
lsp.SymbolKind.Namespace,
lsp.SymbolKind.Package,
lsp.SymbolKind.Class,
lsp.SymbolKind.Method,
lsp.SymbolKind.Property,
lsp.SymbolKind.Field,
lsp.SymbolKind.Constructor,
lsp.SymbolKind.Enum,
lsp.SymbolKind.Interface,
lsp.SymbolKind.Function,
lsp.SymbolKind.Variable,
lsp.SymbolKind.Constant,
lsp.SymbolKind.String,
lsp.SymbolKind.Number,
lsp.SymbolKind.Boolean,
lsp.SymbolKind.Array,
lsp.SymbolKind.Object,
lsp.SymbolKind.Key,
lsp.SymbolKind.Null,
lsp.SymbolKind.EnumMember,
lsp.SymbolKind.Struct,
lsp.SymbolKind.Event,
lsp.SymbolKind.Operator,
lsp.SymbolKind.TypeParameter
]
},
hierarchicalDocumentSymbolSupport: true,
labelSupport: true
},
semanticTokens: {
dynamicRegistration: false,
requests: {
range: true,
full: true
},
tokenTypes: SemTokenTypes,
tokenModifiers: SemTokenModifiers,
formats: ["relative"],
overlappingTokenSupport: true,
}
},
window: {
workDoneProgress: true
}
},
initializationOptions: {
semanticTokens: true
},
trace: "verbose"
};
console.log("send init");
let initRes = await conn.sendRequest(lsp.InitializeRequest.type, initReq);
console.log("init: ", initRes);
conn.onNotification(lsp.LogTraceNotification.type, (msg) => {
console.log("log: ", msg);
})
conn.onNotification(lsp.ShowMessageNotification.type, (msg) => {
console.log("msg: ", msg);
})
conn.onRequest(lsp.WorkDoneProgressCreateRequest.type, (req) => {
console.log("create work done: ", req);
conn.onProgress(lsp.WorkDoneProgress.type, req.token, (ev) => {
console.log(`process ${req.token}: ${ev.kind} ${ev.message}`);
})
})
console.log("send inited");
await conn.sendNotification(lsp.InitializedNotification.type, {});
const codeText = readFileSync("G:/code/go/btKeeper/main.go").toString("utf-8");
const codeLines = codeText.split("\r\n");
console.log(codeLines);
const openReq: lsp.DidOpenTextDocumentParams = {
textDocument: {
uri: "file://G:/code/go/btKeeper/main.go",
languageId: "go",
version: 0,
text: codeText
}
}
console.log("send didOpen");
await conn.sendNotification(lsp.DidOpenTextDocumentNotification.type, openReq);
const symReq: lsp.DocumentSymbolParams = {
textDocument: {
uri: "file://G:/code/go/btKeeper/main.go"
}
}
console.log("send sym")
let symRes = await conn.sendRequest(lsp.DocumentSymbolRequest.type, symReq);
console.log("symbol: ", symRes);
const semReq: lsp.SemanticTokensParams = {
textDocument: {
uri: "file://G:/code/go/btKeeper/main.go"
}
}
console.log("send sem");
let semRes = await conn.sendRequest(lsp.SemanticTokensRequest.type, semReq);
if (semRes !== null) {
const data = semRes.data;
let curLine = 0;
let curStart = 0;
for (let i = 0; i < data.length; i += 5) {
const deltaLine = data[i];
curLine += deltaLine;
if (deltaLine !== 0) {
curStart = 0;
}
const deltaStart = data[i + 1];
curStart += deltaStart;
const length = data[i + 2];
const code = codeLines[curLine].substring(curStart, curStart + length);
const tokenModifiers = [];
const modifier = data[i + 4];
for (let j = 0; j < SemTokenModifiers.length; j++) {
if ((modifier & (1 << j)) !== 0) {
tokenModifiers.push(SemTokenModifiers[j]);
}
}
console.log(`${code} tokenTypes: ${SemTokenTypes[data[i + 3]]}; tokenModifier: ${data[i + 4]} ${JSON.stringify(tokenModifiers)}`);
}
}
const hoverReq: lsp.HoverParams = {
textDocument: {
uri: "file://G:/code/go/btKeeper/main.go"
},
position: {
line: 16,
character: 17
}
}
console.log("send hover");
let hoverRes = await conn.sendRequest(lsp.HoverRequest.type, hoverReq);
console.log("hover: ", hoverRes);
await conn.sendNotification(lsp.ExitNotification.type);
conn.end();