Skip to content

Commit dcbe78a

Browse files
committed
The cursor gets passed in the ViewModel
1 parent 7a76532 commit dcbe78a

9 files changed

Lines changed: 150 additions & 224 deletions

File tree

src/vs/editor/common/commonCodeEditor.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ServiceCollection } from 'vs/platform/instantiation/common/serviceColle
1313
import { IContextKey, IContextKeyServiceTarget, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
1414
import { CommonEditorConfiguration } from 'vs/editor/common/config/commonEditorConfig';
1515
import { Cursor } from 'vs/editor/common/controller/cursor';
16-
import { CursorColumns, IViewModelHelper, ICursors, CursorConfiguration } from 'vs/editor/common/controller/cursorCommon';
16+
import { CursorColumns, ICursors, CursorConfiguration } from 'vs/editor/common/controller/cursorCommon';
1717
import { Position, IPosition } from 'vs/editor/common/core/position';
1818
import { Range, IRange } from 'vs/editor/common/core/range';
1919
import { Selection, ISelection } from 'vs/editor/common/core/selection';
@@ -863,23 +863,6 @@ export abstract class CommonCodeEditor extends Disposable implements editorCommo
863863

864864
this.viewModel = new ViewModel(this.id, this._configuration, this.model);
865865

866-
let viewModelHelper: IViewModelHelper = {
867-
viewModel: this.viewModel,
868-
coordinatesConverter: this.viewModel.coordinatesConverter,
869-
getScrollTop: (): number => {
870-
return this.viewModel.viewLayout.getScrollTop();
871-
},
872-
getCompletelyVisibleViewRange: (): Range => {
873-
return this.viewModel.getCompletelyVisibleViewRange();
874-
},
875-
getCompletelyVisibleViewRangeAtScrollTop: (scrollTop: number): Range => {
876-
return this.viewModel.getCompletelyVisibleViewRangeAtScrollTop(scrollTop);
877-
},
878-
getVerticalOffsetForViewLineNumber: (viewLineNumber: number): number => {
879-
return this.viewModel.viewLayout.getVerticalOffsetForLineNumber(viewLineNumber);
880-
}
881-
};
882-
883866
this.listenersToRemove.push(this.model.addBulkListener((events) => {
884867
for (let i = 0, len = events.length; i < len; i++) {
885868
let eventType = events[i].type;
@@ -917,7 +900,7 @@ export abstract class CommonCodeEditor extends Disposable implements editorCommo
917900
this.cursor = new Cursor(
918901
this._configuration,
919902
this.model,
920-
viewModelHelper
903+
this.viewModel
921904
);
922905

923906
this.viewCursor = new ViewModelCursors(

src/vs/editor/common/controller/cursor.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { DeleteOperations } from 'vs/editor/common/controller/cursorDeleteOperat
2020
import { TypeOperations } from 'vs/editor/common/controller/cursorTypeOperations';
2121
import { TextModelEventType, ModelRawContentChangedEvent, RawContentChangedType } from 'vs/editor/common/model/textModelEvents';
2222
import { CursorEventType, CursorChangeReason, ICursorPositionChangedEvent, VerticalRevealType, ICursorSelectionChangedEvent, ICursorRevealRangeEvent, CursorScrollRequest } from 'vs/editor/common/controller/cursorEvents';
23+
import { IViewModel } from "vs/editor/common/viewModel/viewModel";
2324

2425
export class Cursor extends Disposable implements ICursors {
2526

@@ -44,11 +45,29 @@ export class Cursor extends Disposable implements ICursors {
4445
private _isDoingComposition: boolean;
4546
private _columnSelectData: IColumnSelectData;
4647

47-
constructor(configuration: editorCommon.IConfiguration, model: editorCommon.IModel, viewModelHelper: IViewModelHelper) {
48+
constructor(configuration: editorCommon.IConfiguration, model: editorCommon.IModel, viewModel: IViewModel) {
4849
super();
4950
this._eventEmitter = this._register(new EventEmitter());
5051
this._configuration = configuration;
5152
this._model = model;
53+
54+
let viewModelHelper: IViewModelHelper = {
55+
viewModel: viewModel,
56+
coordinatesConverter: viewModel.coordinatesConverter,
57+
getScrollTop: (): number => {
58+
return viewModel.viewLayout.getScrollTop();
59+
},
60+
getCompletelyVisibleViewRange: (): Range => {
61+
return viewModel.getCompletelyVisibleViewRange();
62+
},
63+
getCompletelyVisibleViewRangeAtScrollTop: (scrollTop: number): Range => {
64+
return viewModel.getCompletelyVisibleViewRangeAtScrollTop(scrollTop);
65+
},
66+
getVerticalOffsetForViewLineNumber: (viewLineNumber: number): number => {
67+
return viewModel.viewLayout.getVerticalOffsetForLineNumber(viewLineNumber);
68+
}
69+
};
70+
5271
this._viewModelHelper = viewModelHelper;
5372
this.context = new CursorContext(this._configuration, this._model, this._viewModelHelper);
5473
this._cursors = new CursorCollection(this.context);

src/vs/editor/common/viewModel/viewModel.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@ export interface IViewModel {
120120
getDecorationsInViewport(visibleRange: Range): ViewModelDecoration[];
121121
getViewLineRenderingData(visibleRange: Range, lineNumber: number): ViewLineRenderingData;
122122
getMinimapLinesRenderingData(startLineNumber: number, endLineNumber: number, needed: boolean[]): MinimapLinesRenderingData;
123+
getCompletelyVisibleViewRange(): Range;
124+
getCompletelyVisibleViewRangeAtScrollTop(scrollTop: number): Range;
123125

124126
getTabSize(): number;
125127
getLineCount(): number;
126128
getLineContent(lineNumber: number): string;
127129
getLineIndentGuide(lineNumber: number): number;
128130
getLineMinColumn(lineNumber: number): number;
129131
getLineMaxColumn(lineNumber: number): number;
132+
getLineFirstNonWhitespaceColumn(lineNumber: number): number;
133+
getLineLastNonWhitespaceColumn(lineNumber: number): number;
130134
getAllOverviewRulerDecorations(): ViewModelDecoration[];
131135
getValueInRange(range: Range, eol: EndOfLinePreference): string;
132136

src/vs/editor/test/common/commands/commandTestUtils.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
'use strict';
66

77
import * as assert from 'assert';
8-
import { Cursor } from 'vs/editor/common/controller/cursor';
98
import { Range } from 'vs/editor/common/core/range';
109
import { Selection } from 'vs/editor/common/core/selection';
1110
import * as editorCommon from 'vs/editor/common/editorCommon';
1211
import { Model } from 'vs/editor/common/model/model';
1312
import { LanguageIdentifier } from 'vs/editor/common/modes';
14-
import { TestConfiguration } from 'vs/editor/test/common/mocks/testConfiguration';
15-
import { viewModelHelper } from 'vs/editor/test/common/editorTestUtils';
13+
import { withMockCodeEditor } from "vs/editor/test/common/mocks/mockCodeEditor";
1614

1715
export function testCommand(
1816
lines: string[],
@@ -22,22 +20,19 @@ export function testCommand(
2220
expectedLines: string[],
2321
expectedSelection: Selection
2422
): void {
25-
2623
let model = Model.createFromString(lines.join('\n'), undefined, languageIdentifier);
27-
let config = new TestConfiguration(null);
28-
let cursor = new Cursor(config, model, viewModelHelper(model));
24+
withMockCodeEditor(null, { model: model }, (editor, cursor) => {
2925

30-
cursor.setSelections('tests', [selection]);
26+
cursor.setSelections('tests', [selection]);
3127

32-
cursor.trigger('tests', editorCommon.Handler.ExecuteCommand, commandFactory(cursor.getSelection()));
28+
cursor.trigger('tests', editorCommon.Handler.ExecuteCommand, commandFactory(cursor.getSelection()));
3329

34-
assert.deepEqual(model.getLinesContent(), expectedLines);
30+
assert.deepEqual(model.getLinesContent(), expectedLines);
3531

36-
let actualSelection = cursor.getSelection();
37-
assert.deepEqual(actualSelection.toString(), expectedSelection.toString());
32+
let actualSelection = cursor.getSelection();
33+
assert.deepEqual(actualSelection.toString(), expectedSelection.toString());
3834

39-
cursor.dispose();
40-
config.dispose();
35+
});
4136
model.dispose();
4237
}
4338

src/vs/editor/test/common/commands/sideEditing.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,30 @@
55
'use strict';
66

77
import * as assert from 'assert';
8-
import { Cursor } from 'vs/editor/common/controller/cursor';
98
import { EditOperation } from 'vs/editor/common/core/editOperation';
109
import { Position } from 'vs/editor/common/core/position';
1110
import { Range } from 'vs/editor/common/core/range';
1211
import { Selection } from 'vs/editor/common/core/selection';
1312
import { IIdentifiedSingleEditOperation } from 'vs/editor/common/editorCommon';
14-
import { Model } from 'vs/editor/common/model/model';
1513
import { ILineEdit, ModelLine, LineMarker, MarkersTracker } from 'vs/editor/common/model/modelLine';
16-
import { TestConfiguration } from 'vs/editor/test/common/mocks/testConfiguration';
17-
import { viewModelHelper } from 'vs/editor/test/common/editorTestUtils';
14+
import { withMockCodeEditor } from "vs/editor/test/common/mocks/mockCodeEditor";
1815

1916
const NO_TAB_SIZE = 0;
2017

2118
function testCommand(lines: string[], selections: Selection[], edits: IIdentifiedSingleEditOperation[], expectedLines: string[], expectedSelections: Selection[]): void {
22-
let model = Model.createFromString(lines.join('\n'));
23-
let config = new TestConfiguration(null);
24-
let cursor = new Cursor(config, model, viewModelHelper(model));
19+
withMockCodeEditor(lines, {}, (editor, cursor) => {
20+
const model = editor.getModel();
2521

26-
cursor.setSelections('tests', selections);
22+
cursor.setSelections('tests', selections);
2723

28-
model.applyEdits(edits);
24+
model.applyEdits(edits);
2925

30-
assert.deepEqual(model.getLinesContent(), expectedLines);
26+
assert.deepEqual(model.getLinesContent(), expectedLines);
3127

32-
let actualSelections = cursor.getSelections();
33-
assert.deepEqual(actualSelections.map(s => s.toString()), expectedSelections.map(s => s.toString()));
28+
let actualSelections = cursor.getSelections();
29+
assert.deepEqual(actualSelections.map(s => s.toString()), expectedSelections.map(s => s.toString()));
3430

35-
cursor.dispose();
36-
config.dispose();
37-
model.dispose();
31+
});
3832
}
3933

4034
function testLineEditMarker(text: string, column: number, stickToPreviousCharacter: boolean, edit: ILineEdit, expectedColumn: number): void {

0 commit comments

Comments
 (0)