Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/browser/quickaccess.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 { localize } from '../../nls.js';
7
import { ContextKeyExpr, RawContextKey } from '../../platform/contextkey/common/contextkey.js';
8
import { ICommandHandler } from '../../platform/commands/common/commands.js';
9
import { IKeybindingService } from '../../platform/keybinding/common/keybinding.js';
10
import { IQuickInputService } from '../../platform/quickinput/common/quickInput.js';
11
import { Disposable } from '../../base/common/lifecycle.js';
12
import { getIEditor } from '../../editor/browser/editorBrowser.js';
13
import { ICodeEditorViewState, IDiffEditorViewState } from '../../editor/common/editorCommon.js';
14
import { IResourceEditorInput, ITextResourceEditorInput } from '../../platform/editor/common/editor.js';
15
import { EditorInput } from '../common/editor/editorInput.js';
16
import { IEditorGroup, IEditorGroupsService } from '../services/editor/common/editorGroupsService.js';
17
import { ACTIVE_GROUP_TYPE, AUX_WINDOW_GROUP_TYPE, IEditorService, SIDE_GROUP_TYPE } from '../services/editor/common/editorService.js';
18
import { IUntitledTextResourceEditorInput, IUntypedEditorInput, GroupIdentifier, IEditorPane } from '../common/editor.js';
19
20
export const inQuickPickContextKeyValue = 'inQuickOpen';
21
export const InQuickPickContextKey = new RawContextKey<boolean>(inQuickPickContextKeyValue, false, localize('inQuickOpen', "Whether keyboard focus is inside the quick open control"));
22
export const inQuickPickContext = ContextKeyExpr.has(inQuickPickContextKeyValue);
23
24
export const defaultQuickAccessContextKeyValue = 'inFilesPicker';
25
export const defaultQuickAccessContext = ContextKeyExpr.and(inQuickPickContext, ContextKeyExpr.has(defaultQuickAccessContextKeyValue));
26
27
export interface IWorkbenchQuickAccessConfiguration {
28
readonly workbench: {
29
readonly commandPalette: {
30
readonly history: number;
31
readonly preserveInput: boolean;
32
readonly experimental: {
33
readonly suggestCommands: boolean;
34
readonly enableNaturalLanguageSearch: boolean;
35
readonly askChatLocation: 'quickChat' | 'chatView';
36
};
37
};
38
readonly quickOpen: {
39
readonly enableExperimentalNewVersion: boolean;
40
readonly preserveInput: boolean;
41
};
42
};
43
}
44
45
export function getQuickNavigateHandler(id: string, next?: boolean): ICommandHandler {
46
return accessor => {
47
const keybindingService = accessor.get(IKeybindingService);
48
const quickInputService = accessor.get(IQuickInputService);
49
50
const keys = keybindingService.lookupKeybindings(id);
51
const quickNavigate = { keybindings: keys };
52
53
quickInputService.navigate(!!next, quickNavigate);
54
};
55
}
56
export class PickerEditorState extends Disposable {
57
private _editorViewState: {
58
editor: EditorInput;
59
group: IEditorGroup;
60
state: ICodeEditorViewState | IDiffEditorViewState | undefined;
61
} | undefined = undefined;
62
63
private readonly openedTransientEditors = new Set<EditorInput>(); // editors that were opened between set and restore
64
65
constructor(
66
@IEditorService private readonly editorService: IEditorService,
67
@IEditorGroupsService private readonly editorGroupsService: IEditorGroupsService
68
) {
69
super();
70
}
71
72
set(): void {
73
if (this._editorViewState) {
74
return; // return early if already done
75
}
76
77
const activeEditorPane = this.editorService.activeEditorPane;
78
if (activeEditorPane) {
79
this._editorViewState = {
80
group: activeEditorPane.group,
81
editor: activeEditorPane.input,
82
state: getIEditor(activeEditorPane.getControl())?.saveViewState() ?? undefined,
83
};
84
}
85
}
86
87
/**
88
* Open a transient editor such that it may be closed when the state is restored.
89
* Note that, when the state is restored, if the editor is no longer transient, it will not be closed.
90
*/
91
async openTransientEditor(editor: IResourceEditorInput | ITextResourceEditorInput | IUntitledTextResourceEditorInput | IUntypedEditorInput, group?: IEditorGroup | GroupIdentifier | SIDE_GROUP_TYPE | ACTIVE_GROUP_TYPE | AUX_WINDOW_GROUP_TYPE): Promise<IEditorPane | undefined> {
92
editor.options = { ...editor.options, transient: true };
93
94
const editorPane = await this.editorService.openEditor(editor, group);
95
if (editorPane?.input && editorPane.input !== this._editorViewState?.editor && editorPane.group.isTransient(editorPane.input)) {
96
this.openedTransientEditors.add(editorPane.input);
97
}
98
99
return editorPane;
100
}
101
102
async restore(): Promise<void> {
103
if (this._editorViewState) {
104
for (const editor of this.openedTransientEditors) {
105
if (editor.isDirty()) {
106
continue;
107
}
108
109
for (const group of this.editorGroupsService.groups) {
110
if (group.isTransient(editor)) {
111
await group.closeEditor(editor, { preserveFocus: true });
112
}
113
}
114
}
115
116
await this._editorViewState.group.openEditor(this._editorViewState.editor, {
117
viewState: this._editorViewState.state,
118
preserveFocus: true // important to not close the picker as a result
119
});
120
121
this.reset();
122
}
123
}
124
125
reset() {
126
this._editorViewState = undefined;
127
this.openedTransientEditors.clear();
128
}
129
130
override dispose(): void {
131
super.dispose();
132
133
this.reset();
134
}
135
}
136
137