-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.ts
More file actions
344 lines (304 loc) · 18.6 KB
/
Copy pathextension.ts
File metadata and controls
344 lines (304 loc) · 18.6 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { workspace, ExtensionContext, window } from 'vscode';
import { ConnectionsPanel } from './features/connectionsPanel'
import { ConnectionManager } from './features/connectionManager'
import { QueryEditor } from './features/queryEditor'
import { ResultsViewer } from './features/resultsViewer'
import { CompositeChartProvider } from './features/compositeChartProvider'
import { ChartEditorProvider } from './features/chartEditorProvider'
import { DataTableProvider } from './features/dataTableProvider'
import * as copilot from './features/copilot'
import { ConnectionStatusBar } from './features/connectionStatusBar'
import * as dotnet from './features/dotnet'
import { Clipboard } from './features/clipboard'
import { ScratchPadManager, SCRATCH_PAD_SCHEME } from './features/scratchPadManager'
import { ScratchPadPanel } from './features/scratchPadPanel'
import { HistoryManager } from './features/historyManager'
import { HistoryPanel } from './features/historyPanel'
import { Importer } from './features/importer'
import { ImportManager } from './features/importManager'
import { EntityDefinitionProvider, ENTITY_DEFINITION_SCHEME } from './features/entityDefinitionProvider'
import { Server, NullServer } from './features/server'
import type { IServer } from './features/server'
import
{
LanguageClient,
LanguageClientOptions,
ServerOptions,
Executable
} from 'vscode-languageclient/node';
let client: LanguageClient;
export async function activate(context: ExtensionContext)
{
// Create output channel early so dotnet activation can log to it
const outputChannel = window.createOutputChannel('Kusto');
// ─── Language server (optional — requires .NET and Server.dll) ───
let server: IServer = new NullServer();
const serverDll = path.join(context.extensionPath, 'server', 'Server.dll');
const dotnetPath = fs.existsSync(serverDll) ? await dotnet.activate(outputChannel) : undefined;
if (dotnetPath) {
const serverExecutable: Executable = {
command: dotnetPath,
args: [serverDll, "vscode"]
};
const serverOptions: ServerOptions = {
run: serverExecutable,
debug: serverExecutable
};
const clientOptions: LanguageClientOptions = {
documentSelector: [
{ scheme: 'file', language: 'kusto' },
{ scheme: ENTITY_DEFINITION_SCHEME, language: 'kusto' },
{ scheme: SCRATCH_PAD_SCHEME, language: 'kusto' }
],
synchronize: {
fileEvents: workspace.createFileSystemWatcher('**/*.{kql,csl,kusto}')
},
outputChannel: outputChannel,
middleware: {
provideCompletionItem: async (document, position, context, token, next) => {
const result = await next(document, position, context, token);
const globalCommitChars = client?.initializeResult?.capabilities
?.completionProvider?.allCommitCharacters ?? [];
return fixCompletionCommit(result, globalCommitChars);
}
}
};
client = new LanguageClient(
'kustoLanguageServer',
'Kusto Language Server',
serverOptions,
clientOptions
);
try {
await client.start();
server = new Server(client, context);
} catch (error) {
outputChannel.appendLine(`Failed to start language server: ${error instanceof Error ? error.message : String(error)}`);
window.showErrorMessage('Kusto language server failed to start. Language features will be unavailable.');
}
}
// ─── UI features (always registered so integration tests will run) ────────────────────
const clipboard = new Clipboard();
const scratchPadManager = new ScratchPadManager(context);
// Register "Go to Definition" provider for kusto-entity:// URIs
const entityDefinitionProvider = new EntityDefinitionProvider(server);
context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(ENTITY_DEFINITION_SCHEME, entityDefinitionProvider),
entityDefinitionProvider
);
// Register command to fix doubled commit characters after completion acceptance
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.fixCommitCharDoubling', fixCommitCharDoubling)
);
// activate results viewer
const chartProvider = new CompositeChartProvider();
const chartEditorProvider = new ChartEditorProvider();
const dataTableProvider = new DataTableProvider(server, clipboard);
const resultsViewer = new ResultsViewer(context, server, clipboard, chartProvider, chartEditorProvider, dataTableProvider);
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.copyChart', () => resultsViewer.copyChart()),
vscode.commands.registerCommand('msKustoExplorer.toggleChartEditor', () => resultsViewer.toggleChartEditor()),
vscode.commands.registerCommand('msKustoExplorer.saveSingletonResults', () => resultsViewer.saveCurrentResults()),
vscode.commands.registerCommand('msKustoExplorer.moveViewToMain', () => resultsViewer.moveResultsTabToMain()),
vscode.commands.registerCommand('msKustoExplorer.toggleSearch', () => resultsViewer.toggleSearch()),
vscode.commands.registerCommand('msKustoExplorer.removeChart', () => resultsViewer.removeChart()),
vscode.commands.registerCommand('msKustoExplorer.copyData', () => resultsViewer.copyData()),
vscode.commands.registerCommand('msKustoExplorer.copyDataAsMarkdown', () => resultsViewer.copyDataAsMarkdown()),
vscode.commands.registerCommand('msKustoExplorer.copyDataAsHtml', () => resultsViewer.copyDataAsHtml()),
vscode.commands.registerCommand('msKustoExplorer.copyTableAsDatatable', () => resultsViewer.copyTableAsDatatable()),
vscode.commands.registerCommand('msKustoExplorer.savePanelResults', () => resultsViewer.saveCurrentResults()),
vscode.commands.registerCommand('msKustoExplorer.chartPanelResults', () => resultsViewer.openChartFromBottomView()),
vscode.commands.registerCommand('msKustoExplorer.rerunQuery', () => resultsViewer.rerunQuery()),
);
// Track Kusto session state
const updateKustoContextFn = () => {
const hasKustoDocument = vscode.workspace.textDocuments.some(doc => doc.languageId === 'kusto');
const hasSingletonView = resultsViewer.hasSingletonView();
const isKustoActive = hasKustoDocument || hasSingletonView;
vscode.commands.executeCommand('setContext', 'msKustoExplorer.hasActiveDocument', isKustoActive);
vscode.commands.executeCommand('setContext', 'msKustoExplorer.hasSingletonView', hasSingletonView);
const activeEditor = vscode.window.activeTextEditor;
vscode.commands.executeCommand('setContext', 'msKustoExplorer.isEntityDefinition', activeEditor?.document.uri.scheme === ENTITY_DEFINITION_SCHEME);
vscode.commands.executeCommand('setContext', 'msKustoExplorer.isScratchPad', activeEditor?.document.uri.scheme === SCRATCH_PAD_SCHEME);
};
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.singletonViewStateChanged', () => updateKustoContextFn())
);
updateKustoContextFn();
context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument(() => updateKustoContextFn()),
vscode.workspace.onDidCloseTextDocument(() => updateKustoContextFn()),
vscode.window.onDidChangeActiveTextEditor(() => updateKustoContextFn())
);
// activate connections data layer
const connectionManager = new ConnectionManager(context, server);
// activate scratch pad documents
const scratchPadPanel = new ScratchPadPanel(context, scratchPadManager, connectionManager);
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.newScratchPad', () => scratchPadPanel.createScratchPad()),
vscode.commands.registerCommand('msKustoExplorer.openScratchPad', (item) => scratchPadPanel.openScratchPad(item)),
vscode.commands.registerCommand('msKustoExplorer.deleteScratchPad', (item) => scratchPadPanel.deleteScratchPad(item)),
vscode.commands.registerCommand('msKustoExplorer.renameScratchPad', (item) => scratchPadPanel.renameScratchPad(item)),
vscode.commands.registerCommand('msKustoExplorer.saveScratchPadAs', () => scratchPadPanel.saveScratchPadAs()),
);
// Register Kusto Explorer import commands
const importManager = new ImportManager(scratchPadManager, connectionManager);
const importer = new Importer(importManager, connectionManager);
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.importConnectionsFromKustoExplorer', () => importer.importConnections()),
vscode.commands.registerCommand('msKustoExplorer.importScratchPadsFromKustoExplorer', () => importer.importScratchPads()),
);
// activate connections panel and related features
const connectionsPanel = new ConnectionsPanel(context, server, clipboard, importer, connectionManager);
await connectionsPanel.initialize();
// No-op commands assigned to tree items to suppress auto-expand on click
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.selectServer', () => {}),
vscode.commands.registerCommand('msKustoExplorer.selectDatabase', () => {}),
vscode.commands.registerCommand('msKustoExplorer.selectEntity', () => {}),
vscode.commands.registerCommand('msKustoExplorer.addServer', () => connectionsPanel.addServer()),
vscode.commands.registerCommand('msKustoExplorer.addServerToGroup', (item) => connectionsPanel.addServerToGroup(item)),
vscode.commands.registerCommand('msKustoExplorer.addServerGroup', () => connectionsPanel.addServerGroup()),
vscode.commands.registerCommand('msKustoExplorer.removeServer', (item) => connectionsPanel.removeServer(item)),
vscode.commands.registerCommand('msKustoExplorer.removeServerGroup', (item) => connectionsPanel.removeServerGroup(item)),
vscode.commands.registerCommand('msKustoExplorer.moveServer', (item) => connectionsPanel.moveServer(item)),
vscode.commands.registerCommand('msKustoExplorer.editServer', (item) => connectionsPanel.editServer(item)),
vscode.commands.registerCommand('msKustoExplorer.renameServer', (item) => connectionsPanel.renameServer(item)),
vscode.commands.registerCommand('msKustoExplorer.renameServerGroup', (item) => connectionsPanel.renameServerGroup(item)),
vscode.commands.registerCommand('msKustoExplorer.refreshServer', (item) => connectionsPanel.refreshServer(item)),
vscode.commands.registerCommand('msKustoExplorer.refreshDatabase', (item) => connectionsPanel.refreshDatabase(item)),
vscode.commands.registerCommand('msKustoExplorer.copyEntityAsCommand', (item) => connectionsPanel.copyEntityAsCommand(item)),
vscode.commands.registerCommand('msKustoExplorer.copyEntityAsExpression', (item) => connectionsPanel.copyEntityAsExpression(item)),
vscode.commands.registerCommand('msKustoExplorer.viewEntityDefinition', (item) => connectionsPanel.viewEntityDefinition(item)),
);
// Create status bar item showing the active document's cluster and database connection.
new ConnectionStatusBar(context, connectionManager);
// activate query history
const historyManager = new HistoryManager(context, server);
// activate query execution features
const historyPanel = new HistoryPanel(context, historyManager, resultsViewer);
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.openHistoryItem', (item) => historyPanel.openHistoryItem(item)),
vscode.commands.registerCommand('msKustoExplorer.deleteHistoryItem', (item) => historyPanel.deleteHistoryItem(item)),
vscode.commands.registerCommand('msKustoExplorer.clearHistory', () => historyPanel.clearHistory()),
);
const queryEditor = new QueryEditor(context, server, clipboard, historyManager, connectionManager, resultsViewer, historyPanel);
context.subscriptions.push(
vscode.commands.registerCommand('msKustoExplorer.noop', () => {}),
vscode.commands.registerCommand('msKustoExplorer.runQuery', (startLine?: number, startChar?: number, endLine?: number, endChar?: number) => queryEditor.runQuery(startLine, startChar, endLine, endChar)),
vscode.commands.registerCommand('msKustoExplorer.runQuery.running', () => {}),
vscode.commands.registerCommand('msKustoExplorer.copyClientRequestId', (clientRequestId?: string) => queryEditor.copyClientRequestId(clientRequestId)),
vscode.commands.registerCommand('msKustoExplorer.copyQuery', (startLine?: number, startChar?: number, endLine?: number, endChar?: number) => queryEditor.copyQuery(startLine, startChar, endLine, endChar)),
vscode.commands.registerCommand('msKustoExplorer.copyQueryTransparent', (startLine?: number, startChar?: number, endLine?: number, endChar?: number) => queryEditor.copyQuery(startLine, startChar, endLine, endChar, true)),
vscode.commands.registerCommand('msKustoExplorer.formatQuery', (startLine?: number, startChar?: number, endLine?: number, endChar?: number) => queryEditor.formatQuery(startLine, startChar, endLine, endChar)),
vscode.commands.registerCommand('msKustoExplorer.selectQuery', (startLine: number, startChar: number, endLine: number, endChar: number) => queryEditor.selectRange(startLine, startChar, endLine, endChar)),
vscode.commands.registerCommand('msKustoExplorer.showResults', (startLine: number, startChar: number) => queryEditor.showHistoryResults(startLine, startChar)),
vscode.commands.registerCommand('msKustoExplorer.refreshDocumentSchema', () => queryEditor.refreshDocumentSchema()),
);
// activate copilot hooks
copilot.activate(context, server, connectionManager, resultsViewer);
// Expose internal components for integration tests
return { historyManager, connectionManager, resultsViewer, server };
}
export function deactivate(): Thenable<void> | undefined
{
if (!client)
{
return undefined;
}
return client.stop();
}
/**
* Identifies completion items where a commit character also appears in the insertText,
* which would cause the character to be doubled when used to commit the completion.
* Attaches a post-completion command that detects and removes the duplicate.
* @param globalCommitChars Commit characters defined in server capabilities (apply to all items)
*/
function fixCompletionCommit(
result: vscode.CompletionItem[] | vscode.CompletionList | null | undefined,
globalCommitChars: string[]
): vscode.CompletionItem[] | vscode.CompletionList | null | undefined {
if (!result) {
return result;
}
const items = Array.isArray(result) ? result : result.items;
for (const item of items) {
// Use per-item commit characters if set, otherwise fall back to global
const commitChars = (item.commitCharacters && item.commitCharacters.length > 0)
? item.commitCharacters
: globalCommitChars;
if (commitChars.length === 0) {
continue;
}
const insertText = typeof item.insertText === 'string'
? item.insertText
: item.insertText instanceof vscode.SnippetString
? item.insertText.value
: undefined;
if (!insertText) {
continue;
}
// Find commit characters that appear in the insert text and could be doubled
const conflicting = commitChars.filter(ch => insertText.includes(ch));
if (conflicting.length > 0) {
// Attach a command that runs after the completion is accepted.
// It checks if the typed commit character was doubled and removes the duplicate.
item.command = {
title: '',
command: 'msKustoExplorer.fixCommitCharDoubling',
arguments: [conflicting, item.command]
};
}
}
return result;
}
/**
* Handles the post-completion fix for doubled commit characters.
* The command fires before the commit character is inserted into the document,
* so we listen for the next document change and use the change event to find
* where the commit character was inserted, then check if it caused a doubling.
*/
async function fixCommitCharDoubling(commitChars: string[], originalCommand?: vscode.Command): Promise<void> {
const editor = vscode.window.activeTextEditor;
if (editor && editor.document.languageId === 'kusto') {
const disposable = vscode.workspace.onDidChangeTextDocument(async (e) => {
if (e.document !== editor.document) {
return;
}
disposable.dispose();
// Find a single-character insertion that matches a conflicting commit char
for (const change of e.contentChanges) {
if (change.text.length === 1 && commitChars.includes(change.text)) {
// The commit char was inserted at change.range.start
// Check if the character just before it is the same
const insertPos = change.range.start;
if (insertPos.character >= 1) {
const charBefore = e.document.getText(
new vscode.Range(insertPos.translate(0, -1), insertPos)
);
if (charBefore === change.text) {
// Delete the duplicate (the newly inserted one, now at insertPos)
await editor.edit(eb => {
eb.delete(new vscode.Range(insertPos, insertPos.translate(0, 1)));
}, { undoStopBefore: false, undoStopAfter: false });
}
}
break;
}
}
});
// Safety: dispose the listener if no change comes within 1 second.
// Note: dispose() is idempotent, so calling it from both the handler and
// this timeout is harmless. JS is single-threaded, so there is no race.
setTimeout(() => disposable.dispose(), 1000);
}
// Preserve any original command from the server
if (originalCommand) {
await vscode.commands.executeCommand(originalCommand.command, ...(originalCommand.arguments ?? []));
}
}