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