Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/standalone/browser/standaloneCodeEditorService.ts
3294 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { windowOpenNoOpener } from '../../../base/browser/dom.js';
7
import { Schemas } from '../../../base/common/network.js';
8
import { URI } from '../../../base/common/uri.js';
9
import { ICodeEditor } from '../../browser/editorBrowser.js';
10
import { AbstractCodeEditorService } from '../../browser/services/abstractCodeEditorService.js';
11
import { ICodeEditorService } from '../../browser/services/codeEditorService.js';
12
import { IRange } from '../../common/core/range.js';
13
import { ScrollType } from '../../common/editorCommon.js';
14
import { ITextModel } from '../../common/model.js';
15
import { IContextKey, IContextKeyService } from '../../../platform/contextkey/common/contextkey.js';
16
import { ITextResourceEditorInput } from '../../../platform/editor/common/editor.js';
17
import { InstantiationType, registerSingleton } from '../../../platform/instantiation/common/extensions.js';
18
import { IThemeService } from '../../../platform/theme/common/themeService.js';
19
20
export class StandaloneCodeEditorService extends AbstractCodeEditorService {
21
22
private readonly _editorIsOpen: IContextKey<boolean>;
23
private _activeCodeEditor: ICodeEditor | null;
24
25
constructor(
26
@IContextKeyService contextKeyService: IContextKeyService,
27
@IThemeService themeService: IThemeService,
28
) {
29
super(themeService);
30
this._register(this.onCodeEditorAdd(() => this._checkContextKey()));
31
this._register(this.onCodeEditorRemove(() => this._checkContextKey()));
32
this._editorIsOpen = contextKeyService.createKey('editorIsOpen', false);
33
this._activeCodeEditor = null;
34
35
this._register(this.registerCodeEditorOpenHandler(async (input, source, sideBySide) => {
36
if (!source) {
37
return null;
38
}
39
return this.doOpenEditor(source, input);
40
}));
41
}
42
43
private _checkContextKey(): void {
44
let hasCodeEditor = false;
45
for (const editor of this.listCodeEditors()) {
46
if (!editor.isSimpleWidget) {
47
hasCodeEditor = true;
48
break;
49
}
50
}
51
this._editorIsOpen.set(hasCodeEditor);
52
}
53
54
public setActiveCodeEditor(activeCodeEditor: ICodeEditor | null): void {
55
this._activeCodeEditor = activeCodeEditor;
56
}
57
58
public getActiveCodeEditor(): ICodeEditor | null {
59
return this._activeCodeEditor;
60
}
61
62
63
private doOpenEditor(editor: ICodeEditor, input: ITextResourceEditorInput): ICodeEditor | null {
64
const model = this.findModel(editor, input.resource);
65
if (!model) {
66
if (input.resource) {
67
68
const schema = input.resource.scheme;
69
if (schema === Schemas.http || schema === Schemas.https) {
70
// This is a fully qualified http or https URL
71
windowOpenNoOpener(input.resource.toString());
72
return editor;
73
}
74
}
75
return null;
76
}
77
78
const selection = <IRange>(input.options ? input.options.selection : null);
79
if (selection) {
80
if (typeof selection.endLineNumber === 'number' && typeof selection.endColumn === 'number') {
81
editor.setSelection(selection);
82
editor.revealRangeInCenter(selection, ScrollType.Immediate);
83
} else {
84
const pos = {
85
lineNumber: selection.startLineNumber,
86
column: selection.startColumn
87
};
88
editor.setPosition(pos);
89
editor.revealPositionInCenter(pos, ScrollType.Immediate);
90
}
91
}
92
93
return editor;
94
}
95
96
private findModel(editor: ICodeEditor, resource: URI): ITextModel | null {
97
const model = editor.getModel();
98
if (model && model.uri.toString() !== resource.toString()) {
99
return null;
100
}
101
102
return model;
103
}
104
}
105
106
registerSingleton(ICodeEditorService, StandaloneCodeEditorService, InstantiationType.Eager);
107
108