Path: blob/main/src/vs/workbench/browser/quickaccess.ts
5223 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Disposable } from '../../base/common/lifecycle.js';6import { getIEditor } from '../../editor/browser/editorBrowser.js';7import { ICodeEditorViewState, IDiffEditorViewState } from '../../editor/common/editorCommon.js';8import { localize } from '../../nls.js';9import { ICommandHandler } from '../../platform/commands/common/commands.js';10import { ContextKeyExpr, RawContextKey } from '../../platform/contextkey/common/contextkey.js';11import { IResourceEditorInput, ITextResourceEditorInput } from '../../platform/editor/common/editor.js';12import { IKeybindingService } from '../../platform/keybinding/common/keybinding.js';13import { IQuickInputService } from '../../platform/quickinput/common/quickInput.js';14import { IEditorPane, IUntitledTextResourceEditorInput, IUntypedEditorInput } from '../common/editor.js';15import { EditorInput } from '../common/editor/editorInput.js';16import { IEditorGroup, IEditorGroupsService } from '../services/editor/common/editorGroupsService.js';17import { PreferredGroup, IEditorService } from '../services/editor/common/editorService.js';1819export const inQuickPickContextKeyValue = 'inQuickOpen';20export const InQuickPickContextKey = new RawContextKey<boolean>(inQuickPickContextKeyValue, false, localize('inQuickOpen', "Whether keyboard focus is inside the quick open control"));21export const inQuickPickContext = ContextKeyExpr.has(inQuickPickContextKeyValue);2223export const defaultQuickAccessContextKeyValue = 'inFilesPicker';24export const defaultQuickAccessContext = ContextKeyExpr.and(inQuickPickContext, ContextKeyExpr.has(defaultQuickAccessContextKeyValue));2526export interface IWorkbenchQuickAccessConfiguration {27readonly workbench: {28readonly commandPalette: {29readonly history: number;30readonly preserveInput: boolean;31readonly showAskInChat: boolean;32readonly experimental: {33readonly suggestCommands: boolean;34readonly enableNaturalLanguageSearch: boolean;35readonly askChatLocation: 'quickChat' | 'chatView';36};37};38readonly quickOpen: {39readonly enableExperimentalNewVersion: boolean;40readonly preserveInput: boolean;41};42};43}4445export function getQuickNavigateHandler(id: string, next?: boolean): ICommandHandler {46return accessor => {47const keybindingService = accessor.get(IKeybindingService);48const quickInputService = accessor.get(IQuickInputService);4950const keys = keybindingService.lookupKeybindings(id);51const quickNavigate = { keybindings: keys };5253quickInputService.navigate(!!next, quickNavigate);54};55}5657export class PickerEditorState extends Disposable {5859private editorViewState: {60editor: EditorInput;61group: IEditorGroup;62state: ICodeEditorViewState | IDiffEditorViewState | undefined;63} | undefined = undefined;6465private readonly openedTransientEditors = new Set<EditorInput>(); // editors that were opened between set and restore6667constructor(68@IEditorService private readonly editorService: IEditorService,69@IEditorGroupsService private readonly editorGroupsService: IEditorGroupsService70) {71super();72}7374set(): void {75if (this.editorViewState) {76return; // return early if already done77}7879const activeEditorPane = this.editorService.activeEditorPane;80if (activeEditorPane) {81this.editorViewState = {82group: activeEditorPane.group,83editor: activeEditorPane.input,84state: getIEditor(activeEditorPane.getControl())?.saveViewState() ?? undefined,85};86}87}8889/**90* Open a transient editor such that it may be closed when the state is restored.91* Note that, when the state is restored, if the editor is no longer transient, it will not be closed.92*/93async openTransientEditor(editor: IResourceEditorInput | ITextResourceEditorInput | IUntitledTextResourceEditorInput | IUntypedEditorInput, group?: PreferredGroup): Promise<IEditorPane | undefined> {94editor.options = { ...editor.options, transient: true };9596const editorPane = await this.editorService.openEditor(editor, group);97if (editorPane?.input && editorPane.input !== this.editorViewState?.editor && editorPane.group.isTransient(editorPane.input)) {98this.openedTransientEditors.add(editorPane.input);99}100101return editorPane;102}103104async restore(): Promise<void> {105if (this.editorViewState) {106for (const editor of this.openedTransientEditors) {107if (editor.isDirty()) {108continue;109}110111for (const group of this.editorGroupsService.groups) {112if (group.isTransient(editor)) {113await group.closeEditor(editor, { preserveFocus: true });114}115}116}117118await this.editorViewState.group.openEditor(this.editorViewState.editor, {119viewState: this.editorViewState.state,120preserveFocus: true // important to not close the picker as a result121});122123this.reset();124}125}126127reset() {128this.editorViewState = undefined;129this.openedTransientEditors.clear();130}131132override dispose(): void {133super.dispose();134135this.reset();136}137}138139140