-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.ts
146 lines (126 loc) · 4.81 KB
/
extension.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
// src/extension.ts
import * as vscode from 'vscode';
import { SymbolProvider } from './symbolProvider';
import { DefinitionProvider } from './definitionProvider';
import { provideDocumentFormattingEdits } from './formattingProvider';
import { initOutputChannel, log } from './log';
// Debounce utility to prevent rapid consecutive calls
function debounce(func: Function, wait: number) {
let timeout: NodeJS.Timeout;
return (...args: any[]) => {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), wait);
};
}
// Add this function near the top, after imports
function shouldProcessDocument(document: vscode.TextDocument): boolean {
return document.uri.scheme === 'file' && document.languageId === 'shards';
}
export async function activate(context: vscode.ExtensionContext) {
initOutputChannel();
log('Shards Extension is now active!');
// Register the formatting provider
vscode.languages.registerDocumentFormattingEditProvider('shards', { provideDocumentFormattingEdits });
// Initialize the symbol provider
const symbolProvider = new SymbolProvider();
// Initialize providers
const documentSymbolProvider = vscode.languages.registerDocumentSymbolProvider(
{ scheme: 'file', language: 'shards' },
symbolProvider,
{ label: 'Shards' }
);
// Add workspace symbol provider
const workspaceSymbolProvider = vscode.languages.registerWorkspaceSymbolProvider({
provideWorkspaceSymbols: async (query: string): Promise<vscode.SymbolInformation[]> => {
const allSymbols = symbolProvider.getAllSymbols();
const filteredSymbols = allSymbols.filter(symbol =>
symbol.name.toLowerCase().includes(query.toLowerCase())
);
return filteredSymbols.map(symbol => new vscode.SymbolInformation(
symbol.name,
symbol.kind,
'',
new vscode.Location(
vscode.Uri.file(symbol.path),
symbol.range
)
));
}
});
const definitionProvider = new DefinitionProvider(symbolProvider);
const defProviderDisposable = vscode.languages.registerDefinitionProvider(
{ scheme: 'file', language: 'shards' },
definitionProvider
);
// Register the reload command
const reloadCommand = vscode.commands.registerCommand('shards.reload', async () => {
symbolProvider.clearCache();
// Reparse all open documents
for (const document of vscode.workspace.textDocuments) {
if (shouldProcessDocument(document)) {
await symbolProvider.updateDocumentSymbols(document);
}
}
vscode.window.showInformationMessage('Shards Extension reloaded.');
});
// Listen to document changes and trigger AST parsing
const debouncedParse = debounce(async (document: vscode.TextDocument) => {
if (!shouldProcessDocument(document)) return;
log('Parsing document:', document.fileName);
try {
await symbolProvider.updateDocumentSymbols(document);
} catch (error) {
console.error('Error updating document symbols:', error);
vscode.window.showErrorMessage('Shards Extension: Failed to update symbols.');
}
}, 500);
// Parse all open documents immediately
const parseOpenDocuments = async () => {
log("Parsing all open documents...");
const docs = vscode.workspace.textDocuments;
for (const doc of docs) {
if (shouldProcessDocument(doc)) {
log("Initial parse of:", doc.fileName);
await symbolProvider.updateDocumentSymbols(doc);
}
}
};
// Call it immediately
parseOpenDocuments();
// Also parse when workspace folders change
const workspaceFoldersChange = vscode.workspace.onDidChangeWorkspaceFolders(async () => {
await parseOpenDocuments();
});
// Listen to document open events - parse immediately without debounce
const openSubscription = vscode.workspace.onDidOpenTextDocument(async (document) => {
if (shouldProcessDocument(document)) {
log('Document opened, parsing immediately:', document.fileName);
await symbolProvider.updateDocumentSymbols(document);
}
});
// Save events - parse immediately without debounce
const saveSubscription = vscode.workspace.onDidSaveTextDocument(async (document) => {
if (shouldProcessDocument(document)) {
log('Document saved, parsing immediately:', document.fileName);
await symbolProvider.updateDocumentSymbols(document);
}
});
// Changes can remain debounced
const changeSubscription = vscode.workspace.onDidChangeTextDocument((event) => {
debouncedParse(event.document);
});
// Add workspaceFoldersChange to subscriptions
context.subscriptions.push(
documentSymbolProvider,
workspaceSymbolProvider,
defProviderDisposable,
reloadCommand,
changeSubscription,
saveSubscription,
openSubscription,
workspaceFoldersChange
);
}
export function deactivate() {
log('Shards Extension is now deactivated.');
}