-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
210 lines (186 loc) · 6.88 KB
/
server.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
209
210
import {
createConnection,
DefinitionParams,
DidChangeConfigurationNotification,
DidChangeTextDocumentParams,
InitializeParams,
InitializeResult,
Location,
Position,
ProposedFeatures,
TextDocumentContentChangeEvent,
TextDocumentSyncKind,
} from 'vscode-languageserver/node';
import {
Range
} from 'vscode';
import TextMirror from './TextMirror';
import * as Parser from 'web-tree-sitter';
import { readFile } from 'fs';
import { loadParser } from './Parser';
import { analyze, documents, mightBeFunctionCall, queryFunction, parse } from './Analysis';
// Connect to the extension client via IPC
let connection = createConnection(ProposedFeatures.all);
let hasConfigCap = false;
let hasWorkspaceFolderCap = false;
let hasDiagnosticCap = false;
function posToPoint(position: Position): Parser.Point {
return {row: position.line, column: position.character};
}
connection.onInitialize(async (params: InitializeParams) => {
console.log('AZSL LSP client initializing');
await loadParser();
const caps = params.capabilities;
hasConfigCap = !!(caps.workspace && !! caps.workspace.configuration);
hasWorkspaceFolderCap = !!(caps.workspace && !!caps.workspace.workspaceFolders);
hasDiagnosticCap = !!(caps.textDocument && caps.textDocument.publishDiagnostics && caps.textDocument.publishDiagnostics.relatedInformation);
const result: InitializeResult = {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
definitionProvider: true,
// Advertise code completion capabilities
// completionProvider: {
// resolveProvider: true,
// }
hoverProvider: true,
}
};
if (hasWorkspaceFolderCap) {
result.capabilities.workspace = {
workspaceFolders: {
supported: true,
}
};
}
return result;
});
connection.onInitialized(() => {
if (hasConfigCap) {
connection.client.register(DidChangeConfigurationNotification.type);
}
if (hasWorkspaceFolderCap) {
connection.workspace.onDidChangeWorkspaceFolders(_event => {
connection.console.log('Workspace folder change event received');
});
}
console.log('AZSL LSP server connection initialized');
});
connection.onDidOpenTextDocument(async (params) => {
const doc = params.textDocument;
console.debug(`${doc.uri} opened`);
let data = documents.get(doc.uri);
if (!data || data.version < params.textDocument.version) {
const tree = await parse(doc.uri, doc.text);
documents.set(doc.uri, {
uri: doc.uri,
tree,
version: doc.version,
text: new TextMirror(doc.text),
symbolTable: await analyze(doc.uri, tree),
});
}
});
connection.onDidCloseTextDocument((params) => {
console.debug(`${params.textDocument.uri} closed`);
});
// Emitted on doc change and open
connection.onDidChangeTextDocument(async (params: DidChangeTextDocumentParams) => {
const doc = params.textDocument;
let docData = documents.get(doc.uri);
for (let i = 0; i !== params.contentChanges.length; ++i) {
const change: TextDocumentContentChangeEvent = params.contentChanges[i];
if (TextDocumentContentChangeEvent.isFull(change)) {
const tree = await parse(doc.uri, change.text);
documents.set(doc.uri, {
uri: doc.uri,
tree,
version: params.textDocument.version,
text: new TextMirror(change.text),
symbolTable: await analyze(doc.uri, tree),
});
} else if (docData && docData.tree && TextDocumentContentChangeEvent.isIncremental(change)) {
// Incremental parse update
const { range, text } = change as {range: Range, text: string};
const startIndex = docData.text.getOffset(range.start);
const oldEndIndex = docData.text.getOffset(range.end);
const newEndIndex = startIndex + text.length;
docData.tree.edit({
startIndex,
oldEndIndex,
newEndIndex,
startPosition: { row: range.start.line, column: range.start.character },
oldEndPosition: docData.tree.rootNode.descendantForIndex(startIndex, oldEndIndex).endPosition,
newEndPosition: { row: range.end.line, column: range.end.character },
});
docData.text.applyChange(change);
docData.tree = await parse(doc.uri, docData.text.text, docData.tree);
docData.symbolTable = await analyze(doc.uri, docData.tree);
} else {
readFile(doc.uri, 'utf8', async (err, data) => {
if (!err) {
const tree = await parse(doc.uri, data);
documents.set(doc.uri, {
uri: doc.uri,
tree,
version: params.textDocument.version,
text: new TextMirror(data),
symbolTable: await analyze(doc.uri, tree),
});
}
});
return;
}
}
});
connection.onDefinition((params: DefinitionParams, token, workDoneProgress, resultProgress): Location | null => {
const doc = documents.get(params.textDocument.uri);
if (!doc || !doc.symbolTable) {
return null;
}
const node = doc.tree.rootNode.namedDescendantForPosition(posToPoint(params.position));
if (mightBeFunctionCall(node)) {
const result = queryFunction(node.text, doc);
if (result) {
let pos = {
line: result.fn.node.startPosition.row,
character: result.fn.node.startPosition.column,
};
return Location.create(result.uri, {start: pos, end: pos});
}
}
return null;
});
connection.onHover((params, token, workDoneProgress, resultProgress) => {
const doc = documents.get(params.textDocument.uri);
if (!doc || !doc.symbolTable) {
return null;
}
const node = doc.tree.rootNode.namedDescendantForPosition(posToPoint(params.position));
if (mightBeFunctionCall(node)) {
// Treat this node as a function
const result = queryFunction(node.text, doc);
if (result && result.fn) {
let value = result.fn.text;
if (result.fn.comment) {
value = `${result.fn.comment}\n${value}`;
}
return {
contents: {
kind: 'markdown',
value: `\`\`\`hlsl\n${value}\n\`\`\``,
},
};
}
}
return null;
// Uncomment below to examine AST node data
/*
return {
contents: {
kind: 'markdown',
value: node.type,
}
};
*/
});
connection.listen();