-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathextension.ts
More file actions
214 lines (186 loc) · 7.63 KB
/
extension.ts
File metadata and controls
214 lines (186 loc) · 7.63 KB
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
211
212
213
214
import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';
import { type ResolvedReference, Resolver, parse } from '@authzed/spicedb-parser-js';
import { getInstallCommand, languageServerBinaryPath } from './binary';
import { CheckWatchProvider } from './checkwatchprovider';
export function activate(context: vscode.ExtensionContext) {
console.log('spicedb-vscode is now active');
const checkWatchProvider = new CheckWatchProvider(context.extensionUri);
context.subscriptions.push(vscode.window.registerWebviewViewProvider(CheckWatchProvider.viewType, checkWatchProvider));
context.subscriptions.push(
vscode.commands.registerCommand('spicedb.addCheckWatch', () => {
checkWatchProvider.addWatch();
}),
);
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor((editor) => {
if (editor) {
checkWatchProvider.performUpdate(editor.document.uri.fsPath);
}
}),
);
context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument((e: vscode.TextDocumentChangeEvent) => {
if (e.document.uri.fsPath.endsWith('.zed')) {
if (vscode.window.activeTextEditor?.document.uri.fsPath === e.document.uri.fsPath) {
checkWatchProvider.setActiveSchema(e.document.uri.fsPath, e.document.getText());
}
}
if (e.document.uri.fsPath.endsWith('.zed.yaml')) {
if (vscode.window.activeTextEditor?.document.uri.fsPath === e.document.uri.fsPath) {
checkWatchProvider.setActiveYaml(e.document.uri.fsPath, e.document.getText());
}
}
}),
);
// TODO: Move this into the language server.
vscode.languages.registerDocumentSemanticTokensProvider(
'spicedb',
{
provideDocumentSemanticTokens: function (
document: vscode.TextDocument,
_token: vscode.CancellationToken,
): vscode.ProviderResult<vscode.SemanticTokens> {
const text = document.getText();
const parserResult = parse(text);
const data: number[] = [];
if (parserResult.error) {
return {
data: new Uint32Array(data),
resultId: undefined,
};
}
// Data format:
// - Line number (0-indexed, and offset from the *previous line*)
// - Column position (0-indexed)
// - Token length
// - Token type index
// - Modifier index
let prevLine = 0;
let prevChar = 0;
const appendData = (lineNumber: number, colPosition: number, length: number, tokenType: number, modifierIndex: number) => {
data.push(
lineNumber - prevLine,
prevLine === lineNumber ? colPosition - prevChar : colPosition,
length,
tokenType,
modifierIndex,
);
prevLine = lineNumber;
prevChar = colPosition;
};
// Resolve all type references and relation/permission references in expressions and color based on their kind and resolution
// status.
const resolution = new Resolver(parserResult.schema!);
resolution.resolvedReferences().forEach((resolved: ResolvedReference) => {
const lineNumber = resolved.reference.range.startIndex.line - 1; // parser ranges are 1-indexed
const colPosition = resolved.reference.range.startIndex.column - 1;
switch (resolved.kind) {
case 'type':
if (resolved.referencedTypeAndRelation === undefined) {
appendData(lineNumber, colPosition, resolved.reference.path.length, /* type.unknown */ 3, 0);
return;
}
appendData(lineNumber, colPosition, resolved.reference.path.length, /* type */ 0, 0);
if (resolved.reference.relationName) {
if (resolved.referencedTypeAndRelation.relation !== undefined) {
appendData(
lineNumber,
colPosition + 1 + resolved.reference.path.length,
resolved.reference.relationName.length,
/* member */ 2,
0,
);
} else if (resolved.referencedTypeAndRelation.permission !== undefined) {
appendData(
lineNumber,
colPosition + 1 + resolved.reference.path.length,
resolved.reference.relationName.length,
/* property */ 1,
0,
);
} else {
appendData(
lineNumber,
colPosition + 1 + resolved.reference.path.length,
resolved.reference.relationName.length,
/* member.unknown */ 3,
0,
);
}
}
break;
case 'expression':
if (resolved.resolvedRelationOrPermission === undefined) {
appendData(lineNumber, colPosition, resolved.reference.relationName.length, /* property.unknown */ 5, 0);
} else {
switch (resolved.resolvedRelationOrPermission.kind) {
case 'permission':
appendData(lineNumber, colPosition, resolved.reference.relationName.length, /* member */ 2, 0);
break;
case 'relation':
appendData(lineNumber, colPosition, resolved.reference.relationName.length, /* property */ 1, 0);
break;
}
}
break;
}
});
return {
data: new Uint32Array(data),
resultId: undefined,
};
},
},
{
tokenTypes: ['type', 'property', 'member', 'type.unknown', 'member.unknown', 'property.unknown'],
tokenModifiers: ['declaration'],
},
);
startLanguageServer(context);
}
async function startLanguageServer(context: vscode.ExtensionContext) {
// Start the LSP hooks using the language server found in the SpiceDB binary.
const serverBinary = await languageServerBinaryPath(context);
if (!serverBinary) {
const installCommand = getInstallCommand();
if (installCommand.startsWith('https://')) {
const OpenInstall = 'Open Installation Instructions';
vscode.window
.showInformationMessage('`spicedb` binary is not installed. Click below to open the installation instructions.', OpenInstall)
.then((selection) => {
if (selection === OpenInstall) {
vscode.env.openExternal(vscode.Uri.parse(installCommand));
}
});
} else {
vscode.window.showErrorMessage('`spicedb` binary is not installed. Please install it using `' + installCommand + '`');
}
console.error('Failed to find the SpiceDB language server binary');
return;
}
const serverOptions: ServerOptions = {
run: {
command: serverBinary,
args: ['lsp'],
transport: TransportKind.stdio,
},
debug: {
command: serverBinary,
args: ['lsp'],
transport: TransportKind.stdio,
},
};
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'spicedb' }],
synchronize: {
fileEvents: vscode.workspace.createFileSystemWatcher('**/.zed'),
},
};
// Create the language client and start the client.
const client = new LanguageClient('spicedbLanguageServer', 'SpiceDB Language Server', serverOptions, clientOptions);
// Start the client. This will also launch the server.
await client.start();
console.log('spicedb-vscode is now active with its LSP running');
}
export function deactivate() {}