Skip to content

Commit

Permalink
[vscode] Support TextEditor#show() and TextEditor#hide() #11146
Browse files Browse the repository at this point in the history
* extends theia.d.ts accordingly
* adapt editor-manager to handle split editor open-request with missing
counter option by computing the counter
* add show/hide to TextEditorsMain interface

Signed-off-by: Johannes Faltermeier <jfaltermeier@eclipsesource.com>

Contributed on behalf of STMicroelectronics
  • Loading branch information
jfaltermeier committed Jun 14, 2022
1 parent 369bf42 commit b9a94e5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [plugin] moved `WebviewViewResolveContext` from `window` to `root` namespace [#11216](https://github.com/eclipse-theia/theia/pull/11216) - Contributed on behalf of STMicroelectronics
- [plugin] Add support for property `color` of `ThemeIcon`. [#11243](https://github.com/eclipse-theia/theia/pull/11243) - Contributed on behalf of STMicroelectronics
- [plugin] Add support for `TreeItemLabel` in `TreeItem`. [#11288](https://github.com/eclipse-theia/theia/pull/11288) - Contributed on behalf of STMicroelectronics
- [plugin] Support `TextEditor#show()` and `TextEditor#hide()` [#11168](https://github.com/eclipse-theia/theia/pull/11168) - Contributed on behalf of STMicroelectronics

<a name="breaking_changes_1.27.0">[Breaking Changes:](#breaking_changes_1.27.0)</a>

Expand Down
14 changes: 14 additions & 0 deletions packages/editor/src/browser/editor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {

// This override only serves to inform external callers that they can use EditorOpenerOptions.
override open(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {

/* check whether we have a split request without a counter */
if (options && options.counter === undefined && options.widgetOptions?.mode) {
switch (options.widgetOptions.mode) {
case 'split-right':
case 'split-bottom':
case 'split-top':
case 'split-left':
const counter = this.createCounterForUri(uri);
const splitOptions: EditorOpenerOptions = { counter, ...options };
return super.open(uri, splitOptions);
}
}

return super.open(uri, options);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,11 +1168,10 @@ export interface DecorationOptions {
}

export interface TextEditorsMain {
// $tryShowTextDocument(resource: UriComponents, options: TextDocumentShowOptions): Promise<string>;
$tryShowTextDocument(uri: UriComponents, options?: TextDocumentShowOptions): Promise<void>;
$registerTextEditorDecorationType(key: string, options: DecorationRenderOptions): void;
$removeTextEditorDecorationType(key: string): void;
// $tryShowEditor(id: string, position: EditorPosition): Promise<void>;
// $tryHideEditor(id: string): Promise<void>;
$tryHideEditor(id: string): Promise<void>;
$trySetOptions(id: string, options: TextEditorConfigurationUpdate): Promise<void>;
$trySetDecorations(id: string, key: string, ranges: DecorationOptions[]): Promise<void>;
$trySetDecorationsFast(id: string, key: string, ranges: number[]): Promise<void>;
Expand Down
18 changes: 16 additions & 2 deletions packages/plugin-ext/src/main/browser/editors-and-documents-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class EditorsAndDocumentsMain implements Disposable {
private readonly textEditors = new Map<string, TextEditorMain>();

private readonly modelService: EditorModelService;
private readonly editorService: EditorManager;

private readonly onTextEditorAddEmitter = new Emitter<TextEditorMain[]>();
private readonly onTextEditorRemoveEmitter = new Emitter<string[]>();
Expand All @@ -59,10 +60,10 @@ export class EditorsAndDocumentsMain implements Disposable {
constructor(rpc: RPCProtocol, container: interfaces.Container) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.EDITORS_AND_DOCUMENTS_EXT);

const editorService = container.get(EditorManager);
this.editorService = container.get(EditorManager);
this.modelService = container.get(EditorModelService);

this.stateComputer = new EditorAndDocumentStateComputer(d => this.onDelta(d), editorService, this.modelService);
this.stateComputer = new EditorAndDocumentStateComputer(d => this.onDelta(d), this.editorService, this.modelService);
this.toDispose.push(this.stateComputer);
this.toDispose.push(this.onTextEditorAddEmitter);
this.toDispose.push(this.onTextEditorRemoveEmitter);
Expand Down Expand Up @@ -168,6 +169,19 @@ export class EditorsAndDocumentsMain implements Disposable {
saveAll(includeUntitled?: boolean): Promise<boolean> {
return this.modelService.saveAll(includeUntitled);
}

hideEditor(id: string): Promise<void> {
for (const editorWidget of this.editorService.all) {
const monacoEditor = MonacoEditor.get(editorWidget);
if (monacoEditor) {
if (id === new EditorSnapshot(monacoEditor).id) {
editorWidget.close();
break;
}
}
}
return Promise.resolve();
}
}

class EditorAndDocumentStateComputer implements Disposable {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/main/browser/main-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function setUpPluginApi(rpc: RPCProtocol, container: interfaces.Container

const bulkEditService = container.get(MonacoBulkEditService);
const monacoEditorService = container.get(MonacoEditorService);
const editorsMain = new TextEditorsMainImpl(editorsAndDocuments, rpc, bulkEditService, monacoEditorService);
const editorsMain = new TextEditorsMainImpl(editorsAndDocuments, documentsMain, rpc, bulkEditService, monacoEditorService);
rpc.set(PLUGIN_RPC_CONTEXT.TEXT_EDITORS_MAIN, editorsMain);

// start listening only after all clients are subscribed to events
Expand Down
14 changes: 12 additions & 2 deletions packages/plugin-ext/src/main/browser/text-editors-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import {
DecorationRenderOptions,
ThemeDecorationInstanceRenderOptions,
DecorationOptions,
WorkspaceEditDto
WorkspaceEditDto,
DocumentsMain,
} from '../../common/plugin-api-rpc';
import { Range } from '../../common/plugin-api-rpc-model';
import { Range, TextDocumentShowOptions } from '../../common/plugin-api-rpc-model';
import { EditorsAndDocumentsMain } from './editors-and-documents-main';
import { RPCProtocol } from '../../common/rpc-protocol';
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
Expand All @@ -54,6 +55,7 @@ export class TextEditorsMainImpl implements TextEditorsMain, Disposable {

constructor(
private readonly editorsAndDocuments: EditorsAndDocumentsMain,
private readonly documents: DocumentsMain,
rpc: RPCProtocol,
private readonly bulkEditService: MonacoBulkEditService,
private readonly monacoEditorService: MonacoEditorService,
Expand Down Expand Up @@ -87,6 +89,10 @@ export class TextEditorsMainImpl implements TextEditorsMain, Disposable {
}
}

$tryShowTextDocument(uri: UriComponents, options?: TextDocumentShowOptions): Promise<void> {
return this.documents.$tryShowDocument(uri, options);
}

$trySetOptions(id: string, options: TextEditorConfigurationUpdate): Promise<void> {
if (!this.editorsAndDocuments.getEditor(id)) {
return Promise.reject(disposed(`TextEditor: ${id}`));
Expand Down Expand Up @@ -173,6 +179,10 @@ export class TextEditorsMainImpl implements TextEditorsMain, Disposable {
this.monacoEditorService.removeDecorationType(key);
}

$tryHideEditor(id: string): Promise<void> {
return this.editorsAndDocuments.hideEditor(id);
}

$trySetDecorations(id: string, key: string, ranges: DecorationOptions[]): Promise<void> {
if (!this.editorsAndDocuments.getEditor(id)) {
return Promise.reject(disposed(`TextEditor(${id})`));
Expand Down
21 changes: 16 additions & 5 deletions packages/plugin-ext/src/plugin/text-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ export class TextEditorExt implements theia.TextEditor {

// prepare data for serialization
const edits: SingleEditOperation[] = editData.edits.map(e =>
({
range: Converter.fromRange(e.range)!,
text: e.text,
forceMoveMarkers: e.forceMoveMarkers
}));
({
range: Converter.fromRange(e.range)!,
text: e.text,
forceMoveMarkers: e.forceMoveMarkers
}));

return this.proxy.$tryApplyEdits(this.id, editData.documentVersionId, edits, {
setEndOfLine: editData.setEndOfLine,
Expand All @@ -263,6 +263,17 @@ export class TextEditorExt implements theia.TextEditor {
return undefined;
});
}

show(column?: theia.ViewColumn): void {
this.proxy.$tryShowTextDocument(this.document.uri, {
viewColumn: column,
preview: true,
});
}

hide(): void {
this.proxy.$tryHideEditor(this.id);
}
}

export class TextEditorOptionsExt implements theia.TextEditorOptions {
Expand Down
16 changes: 16 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,22 @@ export module '@theia/plugin' {
* @param revealType The scrolling strategy for revealing `range`.
*/
revealRange(range: Range, revealType?: TextEditorRevealType): void;

/**
* Shows this text editor. A [column](#ViewColumn) can be provided to control where the editor is being shown. Might change the [active editor](#window.activeTextEditor).
*
* @deprecated use [window.showTextDocument](#Window.showTextDocument) instead.
*
* @param column A [view column](#ViewColumn) in which this editor should be shown.
*/
show(column?: ViewColumn): void;

/**
* Hides this text editor.
*
* @deprecated use 'workbench.action.closeActiveEditor' command instead.
*/
hide(): void;
}

/**
Expand Down

0 comments on commit b9a94e5

Please sign in to comment.