-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
editor-manager.ts
192 lines (167 loc) · 6.78 KB
/
editor-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (C) 2017 TypeFox and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
import { injectable, inject } from "inversify";
import URI from "@theia/core/lib/common/uri";
import { Emitter, Event, RecursivePartial, SelectionService } from '@theia/core/lib/common';
import { OpenHandler, FrontendApplication } from "@theia/core/lib/browser";
import { EditorWidget } from "./editor-widget";
import { TextEditorProvider, Range, Position } from "./editor";
import { WidgetFactory, WidgetManager } from '@theia/core/lib/browser/widget-manager';
import { Widget } from '@phosphor/widgets';
import { FileIconProvider } from '@theia/filesystem/lib/browser/icons/file-icons';
export const EditorManager = Symbol("EditorManager");
export interface EditorManager extends OpenHandler {
/**
* All opened editors.
*/
readonly editors: EditorWidget[];
/**
* Open an editor for the given uri and input.
* Reject if the given input is not an editor input or an editor cannot be opened.
*/
open(uri: URI, input?: EditorInput): Promise<EditorWidget>;
/**
* The most recently focused editor.
*/
readonly currentEditor: EditorWidget | undefined;
/**
* Emit when the current editor changed.
*/
readonly onCurrentEditorChanged: Event<EditorWidget | undefined>;
/**
* The currently focused editor.
*/
readonly activeEditor: EditorWidget | undefined;
/**
* Emit when the active editor changed.
*/
readonly onActiveEditorChanged: Event<EditorWidget | undefined>;
}
export interface EditorInput {
revealIfVisible?: boolean;
selection?: RecursivePartial<Range>;
}
@injectable()
export class EditorManagerImpl implements EditorManager, WidgetFactory {
readonly id = "code-editor-opener";
readonly label = "Code Editor";
protected readonly currentObserver: EditorManagerImpl.Observer;
protected readonly activeObserver: EditorManagerImpl.Observer;
constructor(
@inject(TextEditorProvider) protected readonly editorProvider: TextEditorProvider,
@inject(SelectionService) protected readonly selectionService: SelectionService,
@inject(FrontendApplication) protected readonly app: FrontendApplication,
@inject(WidgetManager) protected readonly widgetManager: WidgetManager,
@inject(FileIconProvider) protected readonly iconProvider: FileIconProvider
) {
this.currentObserver = new EditorManagerImpl.Observer('current', app);
this.activeObserver = new EditorManagerImpl.Observer('active', app);
}
get editors() {
return this.widgetManager.getWidgets(this.id) as EditorWidget[];
}
get currentEditor() {
return this.currentObserver.getEditor();
}
get onCurrentEditorChanged() {
return this.currentObserver.onEditorChanged();
}
get activeEditor() {
return this.activeObserver.getEditor();
}
get onActiveEditorChanged() {
return this.activeObserver.onEditorChanged();
}
canHandle(uri: URI, input?: EditorInput): number {
return 100;
}
open(uri: URI, input?: EditorInput): Promise<EditorWidget> {
return this.widgetManager.getOrCreateWidget<EditorWidget>(this.id, uri.toString()).then(editor => {
if (!editor.isAttached) {
this.app.shell.addToMainArea(editor);
}
this.revealIfVisible(editor, input);
this.revealSelection(editor, input);
return editor;
});
}
// don't call directly, but use WidgetManager
createWidget(uriAsString: string): Promise<Widget> {
const uri = new URI(uriAsString);
return this.createEditor(uri);
}
protected createEditor(uri: URI): Promise<EditorWidget> {
return this.editorProvider(uri).then(textEditor => {
const newEditor = new EditorWidget(textEditor, this.selectionService);
newEditor.id = this.id + ":" + uri.toString();
newEditor.title.closable = true;
newEditor.title.label = uri.path.base;
newEditor.title.iconClass = this.iconProvider.getFileIconForURI(textEditor.uri);
return newEditor;
});
}
protected revealIfVisible(editor: EditorWidget, input?: EditorInput): void {
if (input === undefined || input.revealIfVisible === undefined || input.revealIfVisible) {
this.app.shell.activateMain(editor.id);
}
}
protected revealSelection(widget: EditorWidget, input?: EditorInput): void {
if (input && input.selection) {
const editor = widget.editor;
const selection = this.getSelection(input.selection);
if (Position.is(selection)) {
editor.cursor = selection;
editor.revealPosition(selection);
} else if (Range.is(selection)) {
editor.cursor = selection.end;
editor.selection = selection;
editor.revealRange(selection);
}
}
}
protected getSelection(selection: RecursivePartial<Range>): Range | Position | undefined {
const { start, end } = selection;
if (start && start.line !== undefined && start.line >= 0 &&
start.character !== undefined && start.character >= 0) {
if (end && end.line !== undefined && end.line >= 0 &&
end.character !== undefined && end.character >= 0) {
return selection as Range;
}
return start as Position;
}
return undefined;
}
}
export namespace EditorManagerImpl {
export class Observer {
protected readonly onEditorChangedEmitter = new Emitter<EditorWidget | undefined>();
constructor(
protected readonly kind: 'current' | 'active',
protected readonly app: FrontendApplication
) {
const key = this.kind === 'current' ? 'currentChanged' : 'activeChanged';
app.shell[key].connect((shell, arg) => {
if (arg.newValue instanceof EditorWidget || arg.oldValue instanceof EditorWidget) {
this.onEditorChangedEmitter.fire(this.getEditor());
}
});
}
getEditor(): EditorWidget | undefined {
if (this.app) {
const key = this.kind === 'current' ? 'currentWidget' : 'activeWidget';
const widget = this.app.shell[key];
if (widget instanceof EditorWidget) {
return widget;
}
}
return undefined;
}
onEditorChanged() {
return this.onEditorChangedEmitter.event;
}
}
}