Path: blob/main/src/vs/workbench/common/editor/editorOptions.ts
3296 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 { IRange } from '../../../editor/common/core/range.js';6import { ICodeEditorViewState, IDiffEditorViewState, IEditor, ScrollType } from '../../../editor/common/editorCommon.js';7import { ITextEditorOptions, TextEditorSelectionRevealType, TextEditorSelectionSource } from '../../../platform/editor/common/editor.js';8import { isTextEditorViewState } from '../editor.js';910export function applyTextEditorOptions(options: ITextEditorOptions, editor: IEditor, scrollType: ScrollType): boolean {11let applied = false;1213// Restore view state if any14const viewState = massageEditorViewState(options);15if (isTextEditorViewState(viewState)) {16editor.restoreViewState(viewState);1718applied = true;19}2021// Restore selection if any22if (options.selection) {23const range: IRange = {24startLineNumber: options.selection.startLineNumber,25startColumn: options.selection.startColumn,26endLineNumber: options.selection.endLineNumber ?? options.selection.startLineNumber,27endColumn: options.selection.endColumn ?? options.selection.startColumn28};2930// Apply selection with a source so that listeners can31// distinguish this selection change from others.32// If no source is provided, set a default source to33// signal this navigation.34editor.setSelection(range, options.selectionSource ?? TextEditorSelectionSource.NAVIGATION);3536// Reveal selection37if (options.selectionRevealType === TextEditorSelectionRevealType.NearTop) {38editor.revealRangeNearTop(range, scrollType);39} else if (options.selectionRevealType === TextEditorSelectionRevealType.NearTopIfOutsideViewport) {40editor.revealRangeNearTopIfOutsideViewport(range, scrollType);41} else if (options.selectionRevealType === TextEditorSelectionRevealType.CenterIfOutsideViewport) {42editor.revealRangeInCenterIfOutsideViewport(range, scrollType);43} else {44editor.revealRangeInCenter(range, scrollType);45}4647applied = true;48}4950return applied;51}5253function massageEditorViewState(options: ITextEditorOptions): object | undefined {5455// Without a selection or view state, just return immediately56if (!options.selection || !options.viewState) {57return options.viewState;58}5960// Diff editor: since we have an explicit selection, clear the61// cursor state from the modified side where the selection62// applies. This avoids a redundant selection change event.63const candidateDiffViewState = options.viewState as IDiffEditorViewState;64if (candidateDiffViewState.modified) {65candidateDiffViewState.modified.cursorState = [];6667return candidateDiffViewState;68}6970// Code editor: since we have an explicit selection, clear the71// cursor state. This avoids a redundant selection change event.72const candidateEditorViewState = options.viewState as ICodeEditorViewState;73if (candidateEditorViewState.cursorState) {74candidateEditorViewState.cursorState = [];75}7677return candidateEditorViewState;78}798081