From c1c24b388db0bdaee35573ada84f0530b8996c40 Mon Sep 17 00:00:00 2001 From: Alex Tugarev Date: Wed, 18 Apr 2018 09:47:03 +0200 Subject: [PATCH] [outline] debounce updates 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 --- .../browser/monaco-outline-contribution.ts | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/monaco/src/browser/monaco-outline-contribution.ts b/packages/monaco/src/browser/monaco-outline-contribution.ts index c3ae9b587b293..2bf05f358f194 100644 --- a/packages/monaco/src/browser/monaco-outline-contribution.ts +++ b/packages/monaco/src/browser/monaco-outline-contribution.ts @@ -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 { @@ -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) { @@ -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;