Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set active editor on startup #6152

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions packages/plugin-ext/src/main/browser/text-editor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { Event, Emitter } from '@theia/core';
import { Emitter, Event } from '@theia/core';
import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
import { injectable, inject } from 'inversify';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import { inject, injectable } from 'inversify';

export const TextEditorService = Symbol('TextEditorService');
/**
Expand All @@ -38,25 +38,19 @@ export class TextEditorServiceImpl implements TextEditorService {
onTextEditorAdd: Event<MonacoEditor> = this.onTextEditorAddEmitter.event;
onTextEditorRemove: Event<MonacoEditor> = this.onTextEditorRemoveEmitter.event;

private editors = new Map<string, MonacoEditor>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always nice to remove duplicate state! 😊


constructor(@inject(EditorManager) private editorManager: EditorManager) {
editorManager.onCurrentEditorChanged(this.onEditorChanged);
editorManager.onCreated(w => this.onEditorCreated(w));
editorManager.all.forEach(w => this.onEditorCreated(w));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tolusha thanks for tackling this! 🙏

in general this should work, but we could actually clean up the editorManager.onCurrentEditorChanged(this.onEditorChanged) here, no?

WDYT of renaming onEditorCreated to connectEditor or something like that, adjusting onCurrentEditorChanged handler, and now calling connectEditor for editorManager.currentEditor once?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a bit off topic as this is not related to the issue in scope of this PR, but before I forget to mention it: I think we should remove this.editors map which holds references to MonacoEditor instances without a real reason.

e.g. getActiveEditor uses editorManager an so could listTextEditors be implemented as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexTugarev
I've done some refactoring as you mentioned.
I am not sure if changes regarding connectEditor make sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok with me. let's test with many opened editors to see how it works with many events being fired.

}

listTextEditors(): MonacoEditor[] {
return Array.from(this.editors.values());
return this.editorManager.all.map(w => MonacoEditor.get(w)!).filter(editor => editor !== undefined);
}

getActiveEditor(): EditorWidget | undefined {
return this.editorManager.activeEditor;
}

private onEditorChanged(editor: EditorWidget | undefined): void {
// console.log(`Current Editor Changed: ${editor}`);
}

private onEditorCreated(editor: EditorWidget): void {
const monacoEditor = MonacoEditor.get(editor);
if (monacoEditor) {
Expand All @@ -66,16 +60,10 @@ export class TextEditorServiceImpl implements TextEditorService {
}

private onEditorAdded(editor: MonacoEditor): void {
if (!this.editors.has(editor.getControl().getId())) {
this.editors.set(editor.getControl().getId(), editor);
this.onTextEditorAddEmitter.fire(editor);
}
this.onTextEditorAddEmitter.fire(editor);
}

private onEditorRemoved(editor: MonacoEditor): void {
if (this.editors.has(editor.getControl().getId())) {
this.editors.delete(editor.getControl().getId());
this.onTextEditorRemoveEmitter.fire(editor);
}
this.onTextEditorRemoveEmitter.fire(editor);
}

}