Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/common/editor/editorOptions.ts
3296 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 { IRange } from '../../../editor/common/core/range.js';
7
import { ICodeEditorViewState, IDiffEditorViewState, IEditor, ScrollType } from '../../../editor/common/editorCommon.js';
8
import { ITextEditorOptions, TextEditorSelectionRevealType, TextEditorSelectionSource } from '../../../platform/editor/common/editor.js';
9
import { isTextEditorViewState } from '../editor.js';
10
11
export function applyTextEditorOptions(options: ITextEditorOptions, editor: IEditor, scrollType: ScrollType): boolean {
12
let applied = false;
13
14
// Restore view state if any
15
const viewState = massageEditorViewState(options);
16
if (isTextEditorViewState(viewState)) {
17
editor.restoreViewState(viewState);
18
19
applied = true;
20
}
21
22
// Restore selection if any
23
if (options.selection) {
24
const range: IRange = {
25
startLineNumber: options.selection.startLineNumber,
26
startColumn: options.selection.startColumn,
27
endLineNumber: options.selection.endLineNumber ?? options.selection.startLineNumber,
28
endColumn: options.selection.endColumn ?? options.selection.startColumn
29
};
30
31
// Apply selection with a source so that listeners can
32
// distinguish this selection change from others.
33
// If no source is provided, set a default source to
34
// signal this navigation.
35
editor.setSelection(range, options.selectionSource ?? TextEditorSelectionSource.NAVIGATION);
36
37
// Reveal selection
38
if (options.selectionRevealType === TextEditorSelectionRevealType.NearTop) {
39
editor.revealRangeNearTop(range, scrollType);
40
} else if (options.selectionRevealType === TextEditorSelectionRevealType.NearTopIfOutsideViewport) {
41
editor.revealRangeNearTopIfOutsideViewport(range, scrollType);
42
} else if (options.selectionRevealType === TextEditorSelectionRevealType.CenterIfOutsideViewport) {
43
editor.revealRangeInCenterIfOutsideViewport(range, scrollType);
44
} else {
45
editor.revealRangeInCenter(range, scrollType);
46
}
47
48
applied = true;
49
}
50
51
return applied;
52
}
53
54
function massageEditorViewState(options: ITextEditorOptions): object | undefined {
55
56
// Without a selection or view state, just return immediately
57
if (!options.selection || !options.viewState) {
58
return options.viewState;
59
}
60
61
// Diff editor: since we have an explicit selection, clear the
62
// cursor state from the modified side where the selection
63
// applies. This avoids a redundant selection change event.
64
const candidateDiffViewState = options.viewState as IDiffEditorViewState;
65
if (candidateDiffViewState.modified) {
66
candidateDiffViewState.modified.cursorState = [];
67
68
return candidateDiffViewState;
69
}
70
71
// Code editor: since we have an explicit selection, clear the
72
// cursor state. This avoids a redundant selection change event.
73
const candidateEditorViewState = options.viewState as ICodeEditorViewState;
74
if (candidateEditorViewState.cursorState) {
75
candidateEditorViewState.cursorState = [];
76
}
77
78
return candidateEditorViewState;
79
}
80
81