Skip to content

Commit

Permalink
[outline] debounce updates
Browse files Browse the repository at this point in the history
on relaod of the browser it happens that updates are triggered quite often, which causes cancellation of previous
requests. `DocumentSymbolProvider.provideDocumentSymbols` calls will throw on cancellation.

Signed-off-by: Alex Tugarev <alex.tugarev@typefox.io>
  • Loading branch information
AlexTugarev committed Apr 25, 2018
1 parent e869d72 commit c1c24b3
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions packages/monaco/src/browser/monaco-outline-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { OutlineSymbolInformationNode } from '@theia/outline-view/lib/browser/ou
import URI from "@theia/core/lib/common/uri";
import { MonacoEditor } from './monaco-editor';

import debounce = require('lodash.debounce');

@injectable()
export class MonacoOutlineContribution implements FrontendApplicationContribution {

Expand All @@ -35,15 +37,13 @@ export class MonacoOutlineContribution implements FrontendApplicationContributio
this.outlineViewService.onDidChangeOpenState(async isOpen => {
this.updateOutline();
});
// let's skip the initial current Editor change event, as on reload it comes before the language sevrers have started,
// let's skip the initial current Editor change event, as on reload it comes before the language server have started,
// resulting in an empty outline.
setTimeout(() => {
this.editorManager.onCurrentEditorChanged(widget => this.updateOutlineForEditor(widget));
this.editorManager.onCurrentEditorChanged(debounce(widget => this.updateOutlineForEditor(widget), 50));
}, 3000);

DocumentSymbolProviderRegistry.onDidChange(event => {
this.updateOutline();
});
DocumentSymbolProviderRegistry.onDidChange(debounce(event => this.updateOutline()));

this.outlineViewService.onDidSelect(async node => {
if (MonacoOutlineSymbolInformationNode.is(node) && node.parent) {
Expand Down Expand Up @@ -102,13 +102,18 @@ export class MonacoOutlineContribution implements FrontendApplicationContributio
this.cancellationSource = new CancellationTokenSource();
const token = this.cancellationSource.token;
for (const documentSymbolProvider of documentSymbolProviders) {
const symbolInformation = await documentSymbolProvider.provideDocumentSymbols(model, token);
if (token.isCancellationRequested) {
try {
const symbolInformation = await documentSymbolProvider.provideDocumentSymbols(model, token);
if (token.isCancellationRequested) {
return [];
}
if (Array.isArray(symbolInformation)) {
entries.push(...symbolInformation);
}
} catch {
// happens if `provideDocumentSymbols` promise is rejected.
return [];
}
if (Array.isArray(symbolInformation)) {
entries.push(...symbolInformation);
}
}

return entries;
Expand Down

0 comments on commit c1c24b3

Please sign in to comment.