Skip to content

Commit

Permalink
Undo context key service changes because of regression, (#160329)
Browse files Browse the repository at this point in the history
* Undo context key service changes because of regression,

This reverts commit 77a89ef.

This fixes #160296

* fix compile

* next try...
  • Loading branch information
jrieken committed Sep 8, 2022
1 parent 2734456 commit bcf14fe
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 248 deletions.
120 changes: 62 additions & 58 deletions src/vs/editor/browser/widget/codeEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2028,31 +2028,31 @@ export class EditorModeContext extends Disposable {

constructor(
private readonly _editor: CodeEditorWidget,
contextKeyService: IContextKeyService,
private readonly _contextKeyService: IContextKeyService,
private readonly _languageFeaturesService: ILanguageFeaturesService,
) {
super();

this._langId = EditorContextKeys.languageId.bindTo(contextKeyService);
this._hasCompletionItemProvider = EditorContextKeys.hasCompletionItemProvider.bindTo(contextKeyService);
this._hasCodeActionsProvider = EditorContextKeys.hasCodeActionsProvider.bindTo(contextKeyService);
this._hasCodeLensProvider = EditorContextKeys.hasCodeLensProvider.bindTo(contextKeyService);
this._hasDefinitionProvider = EditorContextKeys.hasDefinitionProvider.bindTo(contextKeyService);
this._hasDeclarationProvider = EditorContextKeys.hasDeclarationProvider.bindTo(contextKeyService);
this._hasImplementationProvider = EditorContextKeys.hasImplementationProvider.bindTo(contextKeyService);
this._hasTypeDefinitionProvider = EditorContextKeys.hasTypeDefinitionProvider.bindTo(contextKeyService);
this._hasHoverProvider = EditorContextKeys.hasHoverProvider.bindTo(contextKeyService);
this._hasDocumentHighlightProvider = EditorContextKeys.hasDocumentHighlightProvider.bindTo(contextKeyService);
this._hasDocumentSymbolProvider = EditorContextKeys.hasDocumentSymbolProvider.bindTo(contextKeyService);
this._hasReferenceProvider = EditorContextKeys.hasReferenceProvider.bindTo(contextKeyService);
this._hasRenameProvider = EditorContextKeys.hasRenameProvider.bindTo(contextKeyService);
this._hasSignatureHelpProvider = EditorContextKeys.hasSignatureHelpProvider.bindTo(contextKeyService);
this._hasInlayHintsProvider = EditorContextKeys.hasInlayHintsProvider.bindTo(contextKeyService);
this._hasDocumentFormattingProvider = EditorContextKeys.hasDocumentFormattingProvider.bindTo(contextKeyService);
this._hasDocumentSelectionFormattingProvider = EditorContextKeys.hasDocumentSelectionFormattingProvider.bindTo(contextKeyService);
this._hasMultipleDocumentFormattingProvider = EditorContextKeys.hasMultipleDocumentFormattingProvider.bindTo(contextKeyService);
this._hasMultipleDocumentSelectionFormattingProvider = EditorContextKeys.hasMultipleDocumentSelectionFormattingProvider.bindTo(contextKeyService);
this._isInWalkThrough = EditorContextKeys.isInWalkThroughSnippet.bindTo(contextKeyService);
this._langId = EditorContextKeys.languageId.bindTo(_contextKeyService);
this._hasCompletionItemProvider = EditorContextKeys.hasCompletionItemProvider.bindTo(_contextKeyService);
this._hasCodeActionsProvider = EditorContextKeys.hasCodeActionsProvider.bindTo(_contextKeyService);
this._hasCodeLensProvider = EditorContextKeys.hasCodeLensProvider.bindTo(_contextKeyService);
this._hasDefinitionProvider = EditorContextKeys.hasDefinitionProvider.bindTo(_contextKeyService);
this._hasDeclarationProvider = EditorContextKeys.hasDeclarationProvider.bindTo(_contextKeyService);
this._hasImplementationProvider = EditorContextKeys.hasImplementationProvider.bindTo(_contextKeyService);
this._hasTypeDefinitionProvider = EditorContextKeys.hasTypeDefinitionProvider.bindTo(_contextKeyService);
this._hasHoverProvider = EditorContextKeys.hasHoverProvider.bindTo(_contextKeyService);
this._hasDocumentHighlightProvider = EditorContextKeys.hasDocumentHighlightProvider.bindTo(_contextKeyService);
this._hasDocumentSymbolProvider = EditorContextKeys.hasDocumentSymbolProvider.bindTo(_contextKeyService);
this._hasReferenceProvider = EditorContextKeys.hasReferenceProvider.bindTo(_contextKeyService);
this._hasRenameProvider = EditorContextKeys.hasRenameProvider.bindTo(_contextKeyService);
this._hasSignatureHelpProvider = EditorContextKeys.hasSignatureHelpProvider.bindTo(_contextKeyService);
this._hasInlayHintsProvider = EditorContextKeys.hasInlayHintsProvider.bindTo(_contextKeyService);
this._hasDocumentFormattingProvider = EditorContextKeys.hasDocumentFormattingProvider.bindTo(_contextKeyService);
this._hasDocumentSelectionFormattingProvider = EditorContextKeys.hasDocumentSelectionFormattingProvider.bindTo(_contextKeyService);
this._hasMultipleDocumentFormattingProvider = EditorContextKeys.hasMultipleDocumentFormattingProvider.bindTo(_contextKeyService);
this._hasMultipleDocumentSelectionFormattingProvider = EditorContextKeys.hasMultipleDocumentSelectionFormattingProvider.bindTo(_contextKeyService);
this._isInWalkThrough = EditorContextKeys.isInWalkThroughSnippet.bindTo(_contextKeyService);

const update = () => this._update();

Expand Down Expand Up @@ -2086,23 +2086,25 @@ export class EditorModeContext extends Disposable {
}

reset() {
this._langId.reset();
this._hasCompletionItemProvider.reset();
this._hasCodeActionsProvider.reset();
this._hasCodeLensProvider.reset();
this._hasDefinitionProvider.reset();
this._hasDeclarationProvider.reset();
this._hasImplementationProvider.reset();
this._hasTypeDefinitionProvider.reset();
this._hasHoverProvider.reset();
this._hasDocumentHighlightProvider.reset();
this._hasDocumentSymbolProvider.reset();
this._hasReferenceProvider.reset();
this._hasRenameProvider.reset();
this._hasDocumentFormattingProvider.reset();
this._hasDocumentSelectionFormattingProvider.reset();
this._hasSignatureHelpProvider.reset();
this._isInWalkThrough.reset();
this._contextKeyService.bufferChangeEvents(() => {
this._langId.reset();
this._hasCompletionItemProvider.reset();
this._hasCodeActionsProvider.reset();
this._hasCodeLensProvider.reset();
this._hasDefinitionProvider.reset();
this._hasDeclarationProvider.reset();
this._hasImplementationProvider.reset();
this._hasTypeDefinitionProvider.reset();
this._hasHoverProvider.reset();
this._hasDocumentHighlightProvider.reset();
this._hasDocumentSymbolProvider.reset();
this._hasReferenceProvider.reset();
this._hasRenameProvider.reset();
this._hasDocumentFormattingProvider.reset();
this._hasDocumentSelectionFormattingProvider.reset();
this._hasSignatureHelpProvider.reset();
this._isInWalkThrough.reset();
});
}

private _update() {
Expand All @@ -2111,26 +2113,28 @@ export class EditorModeContext extends Disposable {
this.reset();
return;
}
this._langId.set(model.getLanguageId());
this._hasCompletionItemProvider.set(this._languageFeaturesService.completionProvider.has(model));
this._hasCodeActionsProvider.set(this._languageFeaturesService.codeActionProvider.has(model));
this._hasCodeLensProvider.set(this._languageFeaturesService.codeLensProvider.has(model));
this._hasDefinitionProvider.set(this._languageFeaturesService.definitionProvider.has(model));
this._hasDeclarationProvider.set(this._languageFeaturesService.declarationProvider.has(model));
this._hasImplementationProvider.set(this._languageFeaturesService.implementationProvider.has(model));
this._hasTypeDefinitionProvider.set(this._languageFeaturesService.typeDefinitionProvider.has(model));
this._hasHoverProvider.set(this._languageFeaturesService.hoverProvider.has(model));
this._hasDocumentHighlightProvider.set(this._languageFeaturesService.documentHighlightProvider.has(model));
this._hasDocumentSymbolProvider.set(this._languageFeaturesService.documentSymbolProvider.has(model));
this._hasReferenceProvider.set(this._languageFeaturesService.referenceProvider.has(model));
this._hasRenameProvider.set(this._languageFeaturesService.renameProvider.has(model));
this._hasSignatureHelpProvider.set(this._languageFeaturesService.signatureHelpProvider.has(model));
this._hasInlayHintsProvider.set(this._languageFeaturesService.inlayHintsProvider.has(model));
this._hasDocumentFormattingProvider.set(this._languageFeaturesService.documentFormattingEditProvider.has(model) || this._languageFeaturesService.documentRangeFormattingEditProvider.has(model));
this._hasDocumentSelectionFormattingProvider.set(this._languageFeaturesService.documentRangeFormattingEditProvider.has(model));
this._hasMultipleDocumentFormattingProvider.set(this._languageFeaturesService.documentFormattingEditProvider.all(model).length + this._languageFeaturesService.documentRangeFormattingEditProvider.all(model).length > 1);
this._hasMultipleDocumentSelectionFormattingProvider.set(this._languageFeaturesService.documentRangeFormattingEditProvider.all(model).length > 1);
this._isInWalkThrough.set(model.uri.scheme === Schemas.walkThroughSnippet);
this._contextKeyService.bufferChangeEvents(() => {
this._langId.set(model.getLanguageId());
this._hasCompletionItemProvider.set(this._languageFeaturesService.completionProvider.has(model));
this._hasCodeActionsProvider.set(this._languageFeaturesService.codeActionProvider.has(model));
this._hasCodeLensProvider.set(this._languageFeaturesService.codeLensProvider.has(model));
this._hasDefinitionProvider.set(this._languageFeaturesService.definitionProvider.has(model));
this._hasDeclarationProvider.set(this._languageFeaturesService.declarationProvider.has(model));
this._hasImplementationProvider.set(this._languageFeaturesService.implementationProvider.has(model));
this._hasTypeDefinitionProvider.set(this._languageFeaturesService.typeDefinitionProvider.has(model));
this._hasHoverProvider.set(this._languageFeaturesService.hoverProvider.has(model));
this._hasDocumentHighlightProvider.set(this._languageFeaturesService.documentHighlightProvider.has(model));
this._hasDocumentSymbolProvider.set(this._languageFeaturesService.documentSymbolProvider.has(model));
this._hasReferenceProvider.set(this._languageFeaturesService.referenceProvider.has(model));
this._hasRenameProvider.set(this._languageFeaturesService.renameProvider.has(model));
this._hasSignatureHelpProvider.set(this._languageFeaturesService.signatureHelpProvider.has(model));
this._hasInlayHintsProvider.set(this._languageFeaturesService.inlayHintsProvider.has(model));
this._hasDocumentFormattingProvider.set(this._languageFeaturesService.documentFormattingEditProvider.has(model) || this._languageFeaturesService.documentRangeFormattingEditProvider.has(model));
this._hasDocumentSelectionFormattingProvider.set(this._languageFeaturesService.documentRangeFormattingEditProvider.has(model));
this._hasMultipleDocumentFormattingProvider.set(this._languageFeaturesService.documentFormattingEditProvider.all(model).length + this._languageFeaturesService.documentRangeFormattingEditProvider.all(model).length > 1);
this._hasMultipleDocumentSelectionFormattingProvider.set(this._languageFeaturesService.documentRangeFormattingEditProvider.all(model).length > 1);
this._isInWalkThrough.set(model.uri.scheme === Schemas.walkThroughSnippet);
});
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/vs/platform/contextkey/browser/contextKeyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Emitter, Event, MicrotaskEmitter } from 'vs/base/common/event';
import { Emitter, Event, PauseableEmitter } from 'vs/base/common/event';
import { Iterable } from 'vs/base/common/iterator';
import { DisposableStore, IDisposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { TernarySearchTree } from 'vs/base/common/map';
Expand Down Expand Up @@ -270,7 +270,7 @@ export abstract class AbstractContextKeyService implements IContextKeyService {
protected _isDisposed: boolean;
protected _myContextId: number;

protected _onDidChangeContext = new MicrotaskEmitter<IContextKeyChangeEvent>({ merge: input => new CompositeContextKeyChangeEvent(input) });
protected _onDidChangeContext = new PauseableEmitter<IContextKeyChangeEvent>({ merge: input => new CompositeContextKeyChangeEvent(input) });
readonly onDidChangeContext = this._onDidChangeContext.event;

constructor(myContextId: number) {
Expand All @@ -291,6 +291,16 @@ export abstract class AbstractContextKeyService implements IContextKeyService {
return new ContextKey(this, key, defaultValue);
}


bufferChangeEvents(callback: Function): void {
this._onDidChangeContext.pause();
try {
callback();
} finally {
this._onDidChangeContext.resume();
}
}

public createScoped(domNode: IContextKeyServiceTarget): IContextKeyService {
if (this._isDisposed) {
throw new Error(`AbstractContextKeyService has been disposed`);
Expand Down Expand Up @@ -534,6 +544,10 @@ class OverlayContextKeyService implements IContextKeyService {
this.overlay = new Map(overlay);
}

bufferChangeEvents(callback: Function): void {
this.parent.bufferChangeEvents(callback);
}

createKey<T extends ContextKeyValue>(): IContextKey<T> {
throw new Error('Not supported.');
}
Expand Down
1 change: 1 addition & 0 deletions src/vs/platform/contextkey/common/contextkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ export interface IContextKeyService {
dispose(): void;

onDidChangeContext: Event<IContextKeyChangeEvent>;
bufferChangeEvents(callback: Function): void;

createKey<T extends ContextKeyValue>(key: string, defaultValue: T | undefined): IContextKey<T>;
contextMatchesRules(rules: ContextKeyExpression | undefined): boolean;
Expand Down
17 changes: 10 additions & 7 deletions src/vs/platform/contextkey/test/browser/contextkey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ suite('ContextKeyService', () => {

let fired = false;
const event = child.onDidChangeContext(e => fired = true);

root.setContext('testA', 10);
root.setContext('testB', 20);
root.setContext('testD', 30);
root.bufferChangeEvents(() => {
root.setContext('testA', 10);
root.setContext('testB', 20);
root.setContext('testD', 30);
});

assert.strictEqual(fired, false, 'Should not fire event when overridden key is updated in parent');
event.dispose();
Expand Down Expand Up @@ -138,9 +139,11 @@ suite('ContextKeyService', () => {
def.complete(undefined);
});

root.setContext('testA', 10);
root.setContext('testB', 20);
root.setContext('testC', 30);
root.bufferChangeEvents(() => {
root.setContext('testA', 10);
root.setContext('testB', 20);
root.setContext('testC', 30);
});

return def.p;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ suite('AbstractKeybindingService', () => {
_serviceBrand: undefined,
dispose: undefined!,
onDidChangeContext: undefined!,
bufferChangeEvents() { },
createKey: undefined!,
contextMatchesRules: undefined!,
getContextKeyValue: undefined!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class MockContextKeyService implements IContextKeyService {
public get onDidChangeContext(): Event<IContextKeyChangeEvent> {
return Event.None;
}
public bufferChangeEvents(callback: () => void) { callback(); }
public getContextKeyValue(key: string) {
const value = this._keys.get(key);
if (value) {
Expand Down
24 changes: 15 additions & 9 deletions src/vs/platform/list/browser/listService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ export class WorkbenchList<T> extends List<T> {
const selection = this.getSelection();
const focus = this.getFocus();

this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.listMultiSelection.set(selection.length > 1);
this.listDoubleSelection.set(selection.length === 2);
this.contextKeyService.bufferChangeEvents(() => {
this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.listMultiSelection.set(selection.length > 1);
this.listDoubleSelection.set(selection.length === 2);
});
}));
this.disposables.add(this.onDidChangeFocus(() => {
const selection = this.getSelection();
Expand Down Expand Up @@ -555,9 +557,11 @@ export class WorkbenchTable<TRow> extends Table<TRow> {
const selection = this.getSelection();
const focus = this.getFocus();

this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.listMultiSelection.set(selection.length > 1);
this.listDoubleSelection.set(selection.length === 2);
this.contextKeyService.bufferChangeEvents(() => {
this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.listMultiSelection.set(selection.length > 1);
this.listDoubleSelection.set(selection.length === 2);
});
}));
this.disposables.add(this.onDidChangeFocus(() => {
const selection = this.getSelection();
Expand Down Expand Up @@ -1209,9 +1213,11 @@ class WorkbenchTreeInternals<TInput, T, TFilterData> {
const selection = tree.getSelection();
const focus = tree.getFocus();

this.hasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.hasMultiSelection.set(selection.length > 1);
this.hasDoubleSelection.set(selection.length === 2);
this.contextKeyService.bufferChangeEvents(() => {
this.hasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.hasMultiSelection.set(selection.length > 1);
this.hasDoubleSelection.set(selection.length === 2);
});
}),
tree.onDidChangeFocus(() => {
const selection = tree.getSelection();
Expand Down
20 changes: 11 additions & 9 deletions src/vs/workbench/browser/parts/editor/titleControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,21 @@ export abstract class TitleControl extends Themable {
this.editorToolBarMenuDisposables.clear();

// Update contexts
const activeEditor = this.group.activeEditor;
this.contextKeyService.bufferChangeEvents(() => {
const activeEditor = this.group.activeEditor;

this.resourceContext.set(withUndefinedAsNull(EditorResourceAccessor.getOriginalUri(activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY })));
this.resourceContext.set(withUndefinedAsNull(EditorResourceAccessor.getOriginalUri(activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY })));

this.editorPinnedContext.set(activeEditor ? this.group.isPinned(activeEditor) : false);
this.editorIsFirstContext.set(activeEditor ? this.group.isFirst(activeEditor) : false);
this.editorIsLastContext.set(activeEditor ? this.group.isLast(activeEditor) : false);
this.editorStickyContext.set(activeEditor ? this.group.isSticky(activeEditor) : false);
this.editorPinnedContext.set(activeEditor ? this.group.isPinned(activeEditor) : false);
this.editorIsFirstContext.set(activeEditor ? this.group.isFirst(activeEditor) : false);
this.editorIsLastContext.set(activeEditor ? this.group.isLast(activeEditor) : false);
this.editorStickyContext.set(activeEditor ? this.group.isSticky(activeEditor) : false);

this.editorCanSplitInGroupContext.set(activeEditor ? activeEditor.hasCapability(EditorInputCapabilities.CanSplitInGroup) : false);
this.sideBySideEditorContext.set(activeEditor?.typeId === SideBySideEditorInput.ID);
this.editorCanSplitInGroupContext.set(activeEditor ? activeEditor.hasCapability(EditorInputCapabilities.CanSplitInGroup) : false);
this.sideBySideEditorContext.set(activeEditor?.typeId === SideBySideEditorInput.ID);

this.groupLockedContext.set(this.group.isLocked);
this.groupLockedContext.set(this.group.isLocked);
});

// Editor actions require the editor control to be there, so we retrieve it via service
const activeEditorPane = this.group.activeEditorPane;
Expand Down
8 changes: 5 additions & 3 deletions src/vs/workbench/browser/parts/views/treeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,11 @@ abstract class AbstractTreeView extends Disposable implements ITreeView {
// Remember when adding to this method that it isn't called until the the view is visible, meaning that
// properties could be set and events could be fired before we're initialized and that this needs to be handled.

this.initializeShowCollapseAllAction();
this.initializeCollapseAllToggle();
this.initializeShowRefreshAction();
this.contextKeyService.bufferChangeEvents(() => {
this.initializeShowCollapseAllAction();
this.initializeCollapseAllToggle();
this.initializeShowRefreshAction();
});

this.treeViewDnd = this.instantiationService.createInstance(CustomTreeViewDragAndDrop, this.id);
if (this._dragAndDropController) {
Expand Down

0 comments on commit bcf14fe

Please sign in to comment.