Skip to content

Commit

Permalink
Implement generic invokeWithinContext for all editor panes
Browse files Browse the repository at this point in the history
Continues PR #104694
  • Loading branch information
roblourens committed Sep 16, 2020
1 parent a9a9512 commit 961db33
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/vs/workbench/browser/parts/editor/editorPane.ts
Expand Up @@ -19,6 +19,7 @@ import { MementoObject } from 'vs/workbench/common/memento';
import { joinPath, IExtUri } from 'vs/base/common/resources';
import { indexOfPath } from 'vs/base/common/extpath';
import { IDisposable } from 'vs/base/common/lifecycle';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';

/**
* The base class of editors in the workbench. Editors register themselves for specific editor inputs.
Expand Down Expand Up @@ -83,6 +84,13 @@ export abstract class EditorPane extends Composite implements IEditorPane {
*/
protected abstract createEditor(parent: HTMLElement): void;

/**
* Should be overridden by editors that have their own ScopedContextKeyService
*/
invokeWithinContext<T>(fn: (accessor: ServicesAccessor) => T): T | null {
return null;
}

/**
* Note: Clients should not call this method, the workbench calls this
* method. Calling it otherwise may result in unexpected behavior.
Expand Down
15 changes: 14 additions & 1 deletion src/vs/workbench/browser/parts/editor/textDiffEditor.ts
Expand Up @@ -17,7 +17,7 @@ import { TextDiffEditorModel } from 'vs/workbench/common/editor/textDiffEditorMo
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfigurationService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { TextFileOperationError, TextFileOperationResult } from 'vs/workbench/services/textfile/common/textfiles';
import { ScrollType, IDiffEditorViewState, IDiffEditorModel } from 'vs/editor/common/editorCommon';
Expand Down Expand Up @@ -60,6 +60,19 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditorPan
this.doSaveOrClearTextDiffEditorViewState(editor);
}

invokeWithinContext<T>(fn: (accessor: ServicesAccessor) => T): T | null {
const control = this.getControl();
if (!control) {
return null;
}

if (control.getOriginalEditor().hasTextFocus()) {
return control.getOriginalEditor().invokeWithinContext(fn);
} else {
return control.getModifiedEditor().invokeWithinContext(fn);
}
}

getTitle(): string {
if (this.input) {
return this.input.getName();
Expand Down
10 changes: 9 additions & 1 deletion src/vs/workbench/browser/parts/editor/textEditor.ts
Expand Up @@ -14,7 +14,7 @@ import { EditorInput, EditorOptions, IEditorMemento, ITextEditorPane, TextEditor
import { EditorPane } from 'vs/workbench/browser/parts/editor/editorPane';
import { IEditorViewState, IEditor, ScrollType } from 'vs/editor/common/editorCommon';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfigurationService';
Expand Down Expand Up @@ -200,6 +200,14 @@ export abstract class BaseTextEditor extends EditorPane implements ITextEditorPa
// Subclasses can override
}

invokeWithinContext<T>(fn: (accessor: ServicesAccessor) => T): T | null {
if (!this.editorControl) {
return null;
}

return isCodeEditor(this.editorControl) ? this.editorControl.invokeWithinContext(fn) : null;
}

focus(): void {

// Pass on to Editor
Expand Down
5 changes: 2 additions & 3 deletions src/vs/workbench/browser/parts/editor/titleControl.ts
Expand Up @@ -13,7 +13,7 @@ import { IAction, IRunEvent, WorkbenchActionExecutedEvent, WorkbenchActionExecut
import * as arrays from 'vs/base/common/arrays';
import { ResolvedKeybinding } from 'vs/base/common/keyCodes';
import { dispose, DisposableStore } from 'vs/base/common/lifecycle';
import { getCodeEditor, isCodeEditor } from 'vs/editor/browser/editorBrowser';
import { isCodeEditor } from 'vs/editor/browser/editorBrowser';
import { localize } from 'vs/nls';
import { createAndFillInActionBarActions, createAndFillInContextMenuActions, MenuEntryActionViewItem, SubmenuEntryActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { ExecuteCommandAction, IMenu, IMenuService, MenuId, MenuItemAction, SubmenuItemAction } from 'vs/platform/actions/common/actions';
Expand Down Expand Up @@ -237,8 +237,7 @@ export abstract class TitleControl extends Themable {
// Editor actions require the editor control to be there, so we retrieve it via service
const activeEditorPane = this.group.activeEditorPane;
if (activeEditorPane instanceof EditorPane) {
const codeEditor = getCodeEditor(activeEditorPane.getControl());
const scopedContextKeyService = codeEditor?.invokeWithinContext(accessor => accessor.get(IContextKeyService)) || this.contextKeyService;
const scopedContextKeyService = activeEditorPane.invokeWithinContext(accessor => accessor.get(IContextKeyService)) || this.contextKeyService;
const titleBarMenu = this.menuService.createMenu(MenuId.EditorTitle, scopedContextKeyService);
this.editorToolBarMenuDisposables.add(titleBarMenu);
this.editorToolBarMenuDisposables.add(titleBarMenu.onDidChange(() => {
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/common/editor.ts
Expand Up @@ -123,6 +123,11 @@ export interface IEditorPane extends IComposite {
* Finds out if this editor is visible or not.
*/
isVisible(): boolean;

/**
* Should be overridden by editors that have their own ScopedContextKeyService
*/
invokeWithinContext<T>(fn: (accessor: ServicesAccessor) => T): T | null;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/services/editor/browser/editorService.ts
Expand Up @@ -790,9 +790,9 @@ export class EditorService extends Disposable implements EditorServiceImpl {
//#region invokeWithinEditorContext()

invokeWithinEditorContext<T>(fn: (accessor: ServicesAccessor) => T): T {
const activeTextEditorControl = this.activeTextEditorControl;
if (isCodeEditor(activeTextEditorControl)) {
return activeTextEditorControl.invokeWithinContext(fn);
const result = this.activeEditorPane?.invokeWithinContext(fn);
if (result) {
return result;
}

const activeGroup = this.editorGroupService.activeGroup;
Expand Down

0 comments on commit 961db33

Please sign in to comment.