Skip to content

Commit b84d08b

Browse files
author
Benjamin Pasero
committed
options - make sure text editor options are proper
1 parent 17d6b13 commit b84d08b

3 files changed

Lines changed: 46 additions & 106 deletions

File tree

src/vs/platform/editor/common/editor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ export interface ITextEditorOptions extends IEditorOptions {
220220
/**
221221
* Text editor selection.
222222
*/
223-
selection?: ITextEditorSelection;
223+
readonly selection?: ITextEditorSelection;
224224

225225
/**
226226
* Text editor view state.
227227
*/
228-
viewState?: object;
228+
readonly viewState?: object;
229229

230230
/**
231231
* Option to scroll vertically or horizontally as necessary and reveal a range centered vertically only if it lies outside the viewport.
232232
*/
233-
revealInCenterIfOutsideViewport?: boolean;
233+
readonly revealInCenterIfOutsideViewport?: boolean;
234234
}

src/vs/workbench/common/editor.ts

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
import { Event, Emitter } from 'vs/base/common/event';
77
import { assign } from 'vs/base/common/objects';
8-
import { isUndefinedOrNull, withNullAsUndefined, assertIsDefined } from 'vs/base/common/types';
8+
import { withNullAsUndefined, assertIsDefined } from 'vs/base/common/types';
99
import { URI } from 'vs/base/common/uri';
1010
import { IDisposable, Disposable, toDisposable } from 'vs/base/common/lifecycle';
1111
import { IEditor as ICodeEditor, IEditorViewState, ScrollType, IDiffEditor } from 'vs/editor/common/editorCommon';
12-
import { IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput, IResourceInput, EditorActivation, EditorOpenContext } from 'vs/platform/editor/common/editor';
12+
import { IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput, IResourceInput, EditorActivation, EditorOpenContext, ITextEditorSelection } from 'vs/platform/editor/common/editor';
1313
import { IInstantiationService, IConstructorSignature0, ServicesAccessor, BrandedService } from 'vs/platform/instantiation/common/instantiation';
1414
import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1515
import { Registry } from 'vs/platform/registry/common/platform';
@@ -24,6 +24,7 @@ import { ITextFileSaveOptions, ITextFileService } from 'vs/workbench/services/te
2424
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2525
import { isEqual } from 'vs/base/common/resources';
2626
import { IPanel } from 'vs/workbench/common/panel';
27+
import { IRange } from 'vs/editor/common/core/range';
2728

2829
export const DirtyWorkingCopiesContext = new RawContextKey<boolean>('dirtyWorkingCopies', false);
2930
export const ActiveEditorContext = new RawContextKey<string | null>('activeEditor', null);
@@ -983,14 +984,22 @@ export class EditorOptions implements IEditorOptions {
983984
/**
984985
* Base Text Editor Options.
985986
*/
986-
export class TextEditorOptions extends EditorOptions {
987-
private startLineNumber: number | undefined;
988-
private startColumn: number | undefined;
989-
private endLineNumber: number | undefined;
990-
private endColumn: number | undefined;
987+
export class TextEditorOptions extends EditorOptions implements ITextEditorOptions {
991988

992-
private revealInCenterIfOutsideViewport: boolean | undefined;
993-
private editorViewState: IEditorViewState | undefined;
989+
/**
990+
* Text editor selection.
991+
*/
992+
selection: ITextEditorSelection | undefined;
993+
994+
/**
995+
* Text editor view state.
996+
*/
997+
editorViewState: IEditorViewState | undefined;
998+
999+
/**
1000+
* Option to scroll vertically or horizontally as necessary and reveal a range centered vertically only if it lies outside the viewport.
1001+
*/
1002+
revealInCenterIfOutsideViewport: boolean | undefined;
9941003

9951004
static from(input?: IBaseResourceInput): TextEditorOptions | undefined {
9961005
if (!input || !input.options) {
@@ -1017,8 +1026,12 @@ export class TextEditorOptions extends EditorOptions {
10171026
super.overwrite(options);
10181027

10191028
if (options.selection) {
1020-
const selection = options.selection;
1021-
this.selection(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn);
1029+
this.selection = {
1030+
startLineNumber: options.selection.startLineNumber,
1031+
startColumn: options.selection.startColumn,
1032+
endLineNumber: options.selection.endLineNumber ?? options.selection.startLineNumber,
1033+
endColumn: options.selection.endColumn ?? options.selection.startColumn
1034+
};
10221035
}
10231036

10241037
if (options.viewState) {
@@ -1036,19 +1049,7 @@ export class TextEditorOptions extends EditorOptions {
10361049
* Returns if this options object has objects defined for the editor.
10371050
*/
10381051
hasOptionsDefined(): boolean {
1039-
return !!this.editorViewState || (!isUndefinedOrNull(this.startLineNumber) && !isUndefinedOrNull(this.startColumn));
1040-
}
1041-
1042-
/**
1043-
* Tells the editor to set show the given selection when the editor is being opened.
1044-
*/
1045-
selection(startLineNumber: number, startColumn: number, endLineNumber: number = startLineNumber, endColumn: number = startColumn): EditorOptions {
1046-
this.startLineNumber = startLineNumber;
1047-
this.startColumn = startColumn;
1048-
this.endLineNumber = endLineNumber;
1049-
this.endColumn = endColumn;
1050-
1051-
return this;
1052+
return !!this.editorViewState || !!this.revealInCenterIfOutsideViewport || !!this.selection;
10521053
}
10531054

10541055
/**
@@ -1069,12 +1070,6 @@ export class TextEditorOptions extends EditorOptions {
10691070
* @return if something was applied
10701071
*/
10711072
apply(editor: ICodeEditor, scrollType: ScrollType): boolean {
1072-
1073-
// View state
1074-
return this.applyViewState(editor, scrollType);
1075-
}
1076-
1077-
private applyViewState(editor: ICodeEditor, scrollType: ScrollType): boolean {
10781073
let gotApplied = false;
10791074

10801075
// First try viewstate
@@ -1084,36 +1079,20 @@ export class TextEditorOptions extends EditorOptions {
10841079
}
10851080

10861081
// Otherwise check for selection
1087-
else if (!isUndefinedOrNull(this.startLineNumber) && !isUndefinedOrNull(this.startColumn)) {
1088-
1089-
// Select
1090-
if (!isUndefinedOrNull(this.endLineNumber) && !isUndefinedOrNull(this.endColumn)) {
1091-
const range = {
1092-
startLineNumber: this.startLineNumber,
1093-
startColumn: this.startColumn,
1094-
endLineNumber: this.endLineNumber,
1095-
endColumn: this.endColumn
1096-
};
1097-
editor.setSelection(range);
1098-
if (this.revealInCenterIfOutsideViewport) {
1099-
editor.revealRangeInCenterIfOutsideViewport(range, scrollType);
1100-
} else {
1101-
editor.revealRangeInCenter(range, scrollType);
1102-
}
1103-
}
1082+
else if (this.selection) {
1083+
const range: IRange = {
1084+
startLineNumber: this.selection.startLineNumber,
1085+
startColumn: this.selection.startColumn,
1086+
endLineNumber: this.selection.endLineNumber ?? this.selection.startLineNumber,
1087+
endColumn: this.selection.endColumn ?? this.selection.startColumn
1088+
};
11041089

1105-
// Reveal
1106-
else {
1107-
const pos = {
1108-
lineNumber: this.startLineNumber,
1109-
column: this.startColumn
1110-
};
1111-
editor.setPosition(pos);
1112-
if (this.revealInCenterIfOutsideViewport) {
1113-
editor.revealPositionInCenterIfOutsideViewport(pos, scrollType);
1114-
} else {
1115-
editor.revealPositionInCenter(pos, scrollType);
1116-
}
1090+
editor.setSelection(range);
1091+
1092+
if (this.revealInCenterIfOutsideViewport) {
1093+
editor.revealRangeInCenterIfOutsideViewport(range, scrollType);
1094+
} else {
1095+
editor.revealRangeInCenter(range, scrollType);
11171096
}
11181097

11191098
gotApplied = true;
@@ -1319,13 +1298,13 @@ export async function pathsToEditors(paths: IPathData[] | undefined, fileService
13191298

13201299
const exists = (typeof path.exists === 'boolean') ? path.exists : await fileService.exists(resource);
13211300

1322-
const options: ITextEditorOptions = { pinned: true };
1323-
if (exists && typeof path.lineNumber === 'number') {
1324-
options.selection = {
1301+
const options: ITextEditorOptions = (exists && typeof path.lineNumber === 'number') ? {
1302+
selection: {
13251303
startLineNumber: path.lineNumber,
13261304
startColumn: path.columnNumber || 1
1327-
};
1328-
}
1305+
},
1306+
pinned: true
1307+
} : { pinned: true };
13291308

13301309
let input: IResourceInput | IUntitledTextResourceInput;
13311310
if (!exists) {

src/vs/workbench/test/common/editor/editorOptions.test.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)