Skip to content

Commit

Permalink
fix: Added debounce delay for saving indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
agg23 committed Oct 10, 2022
1 parent 42871f1 commit a417b5f
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ const selector: DocumentSelector = [
{ scheme: 'file', language: 'verilog' }
];

let indexer: SystemVerilogIndexer | undefined = undefined;
let saveIndexTimeout: NodeJS.Timeout | undefined;

function queuedSaveIndex(context: ExtensionContext) {
if (saveIndexTimeout !== undefined) {
clearTimeout(saveIndexTimeout);
}

saveIndexTimeout = setTimeout(function () {
saveIndex(context);
}, 10000);
}

function saveIndex(context: ExtensionContext): void {
saveIndexTimeout = undefined;

const syms = [...indexer.symbols];
context.workspaceState.update('symbols', syms);
}

export function activate(context: ExtensionContext) {
// Output Channel
const outputChannel = window.createOutputChannel('SystemVerilog');
Expand All @@ -35,7 +55,7 @@ export function activate(context: ExtensionContext) {

// Back-end Classes
const parser = new SystemVerilogParser();
const indexer = new SystemVerilogIndexer(statusBar, parser, outputChannel);
indexer = new SystemVerilogIndexer(statusBar, parser, outputChannel);

// Providers
const docProvider = new SystemVerilogDocumentSymbolProvider(parser, indexer);
Expand All @@ -56,7 +76,7 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(languages.registerReferenceProvider(selector, referenceProvider));

const buildHandler = () => {
indexer.build_index().then((_) => saveIndex());
indexer.build_index().then((_) => queuedSaveIndex(context));
};
const instantiateHandler = () => {
moduleInstantiator.instantiateModule();
Expand All @@ -70,34 +90,34 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(
workspace.onDidSaveTextDocument((doc) => {
indexer.onChange(doc);
saveIndex();
queuedSaveIndex(context);
})
);
context.subscriptions.push(
window.onDidChangeActiveTextEditor((editor) => {
if (editor !== undefined) {
indexer.onChange(editor.document);
saveIndex();
queuedSaveIndex(context);
}
})
);
const watcher = workspace.createFileSystemWatcher('**/*.{sv,v,svh,vh}', false, false, false);
context.subscriptions.push(
watcher.onDidCreate((uri) => {
indexer.onCreate(uri);
saveIndex();
queuedSaveIndex(context);
})
);
context.subscriptions.push(
watcher.onDidDelete((uri) => {
indexer.onDelete(uri);
saveIndex();
queuedSaveIndex(context);
})
);
context.subscriptions.push(
watcher.onDidChange((uri) => {
indexer.onDelete(uri);
saveIndex();
queuedSaveIndex(context);
})
);
context.subscriptions.push(watcher);
Expand All @@ -113,11 +133,6 @@ export function activate(context: ExtensionContext) {
}
}

function saveIndex(): void {
const syms = [...indexer.symbols];
context.workspaceState.update('symbols', syms);
}

function loadIndex(): void {
const symbols: Array<any> = context.workspaceState.get('symbols');
let numSymbols = 0;
Expand Down Expand Up @@ -243,9 +258,12 @@ export function activate(context: ExtensionContext) {
});
}

export function deactivate(): Thenable<void> | undefined {
export function deactivate(context: ExtensionContext): Thenable<void> | undefined {
if (!client) {
return undefined;
}

queuedSaveIndex(context);

return client.stop();
}

0 comments on commit a417b5f

Please sign in to comment.