Skip to content

Commit 8283f95

Browse files
committed
1 parent 8e89400 commit 8283f95

4 files changed

Lines changed: 89 additions & 14 deletions

File tree

src/vs/editor/common/commands/shiftCommand.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import * as strings from 'vs/base/common/strings';
88
import { CursorColumns } from 'vs/editor/common/controller/cursorCommon';
99
import { Range } from 'vs/editor/common/core/range';
10-
import { Selection } from 'vs/editor/common/core/selection';
10+
import { Selection, SelectionDirection } from 'vs/editor/common/core/selection';
1111
import { ICommand, ICursorStateComputerData, IEditOperationBuilder, ITokenizedModel } from 'vs/editor/common/editorCommon';
1212
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
1313
import { CharCode } from 'vs/base/common/charCode';
@@ -45,11 +45,13 @@ export class ShiftCommand implements ICommand {
4545
private _selection: Selection;
4646
private _selectionId: string;
4747
private _useLastEditRangeForCursorEndPosition: boolean;
48+
private _selectionStartColumnStaysPut: boolean;
4849

4950
constructor(range: Selection, opts: IShiftCommandOpts) {
5051
this._opts = opts;
5152
this._selection = range;
5253
this._useLastEditRangeForCursorEndPosition = false;
54+
this._selectionStartColumnStaysPut = false;
5355
}
5456

5557
private _addEditOperation(builder: IEditOperationBuilder, range: Range, text: string) {
@@ -156,6 +158,10 @@ export class ShiftCommand implements ICommand {
156158
}
157159

158160
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), indents[desiredIndentCount]);
161+
if (lineNumber === startLine) {
162+
// Force the startColumn to stay put because we're inserting after it
163+
this._selectionStartColumnStaysPut = (this._selection.startColumn <= indentationEndIndex + 1);
164+
}
159165
}
160166
} else {
161167

@@ -197,6 +203,10 @@ export class ShiftCommand implements ICommand {
197203
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), '');
198204
} else {
199205
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, 1), oneIndent);
206+
if (lineNumber === startLine) {
207+
// Force the startColumn to stay put because we're inserting after it
208+
this._selectionStartColumnStaysPut = (this._selection.startColumn === 1);
209+
}
200210
}
201211
}
202212
}
@@ -209,6 +219,22 @@ export class ShiftCommand implements ICommand {
209219
let lastOp = helper.getInverseEditOperations()[0];
210220
return new Selection(lastOp.range.endLineNumber, lastOp.range.endColumn, lastOp.range.endLineNumber, lastOp.range.endColumn);
211221
}
212-
return helper.getTrackedSelection(this._selectionId);
222+
const result = helper.getTrackedSelection(this._selectionId);
223+
224+
if (this._selectionStartColumnStaysPut) {
225+
// The selection start should not move
226+
let initialStartColumn = this._selection.startColumn;
227+
let resultStartColumn = result.startColumn;
228+
if (resultStartColumn <= initialStartColumn) {
229+
return result;
230+
}
231+
232+
if (result.getDirection() === SelectionDirection.LTR) {
233+
return new Selection(result.startLineNumber, initialStartColumn, result.endLineNumber, result.endColumn);
234+
}
235+
return new Selection(result.endLineNumber, result.endColumn, result.startLineNumber, initialStartColumn);
236+
}
237+
238+
return result;
213239
}
214240
}

src/vs/editor/common/model/modelLine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,11 @@ export class ModelLine {
416416

417417
// var markers = this._markers;
418418

419-
// var printMarker = (m:ILineMarker) => {
419+
// var printMarker = (m:LineMarker) => {
420420
// if (m.stickToPreviousCharacter) {
421-
// return '|' + m.column;
421+
// return '|' + m.position.column;
422422
// }
423-
// return m.column + '|';
423+
// return m.position.column + '|';
424424
// };
425425
// return '[' + markers.map(printMarker).join(', ') + ']';
426426
// }

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ suite('Editor Commands - ShiftCommand', () => {
107107
'',
108108
'123'
109109
],
110-
new Selection(1, 2, 1, 2)
110+
new Selection(1, 1, 1, 2)
111111
);
112112
});
113113

@@ -130,7 +130,7 @@ suite('Editor Commands - ShiftCommand', () => {
130130
'',
131131
'123'
132132
],
133-
new Selection(1, 4, 1, 2)
133+
new Selection(1, 4, 1, 1)
134134
);
135135
});
136136

@@ -153,7 +153,7 @@ suite('Editor Commands - ShiftCommand', () => {
153153
'',
154154
'123'
155155
],
156-
new Selection(1, 2, 1, 4)
156+
new Selection(1, 1, 1, 4)
157157
);
158158
});
159159

@@ -176,7 +176,7 @@ suite('Editor Commands - ShiftCommand', () => {
176176
'',
177177
'123'
178178
],
179-
new Selection(1, 2, 2, 1)
179+
new Selection(1, 1, 2, 1)
180180
);
181181
});
182182

@@ -199,7 +199,7 @@ suite('Editor Commands - ShiftCommand', () => {
199199
'',
200200
'123'
201201
],
202-
new Selection(1, 2, 2, 1)
202+
new Selection(1, 1, 2, 1)
203203
);
204204

205205
testShiftCommand(
@@ -312,7 +312,7 @@ suite('Editor Commands - ShiftCommand', () => {
312312
'',
313313
'\t123'
314314
],
315-
new Selection(1, 2, 5, 3)
315+
new Selection(1, 1, 5, 3)
316316
);
317317

318318
testShiftCommand(
@@ -333,7 +333,7 @@ suite('Editor Commands - ShiftCommand', () => {
333333
'\t',
334334
'123'
335335
],
336-
new Selection(4, 2, 5, 1)
336+
new Selection(4, 1, 5, 1)
337337
);
338338
});
339339

@@ -538,7 +538,7 @@ suite('Editor Commands - ShiftCommand', () => {
538538
'',
539539
'\t123'
540540
],
541-
new Selection(1, 2, 5, 5)
541+
new Selection(1, 1, 5, 5)
542542
);
543543
});
544544

@@ -703,7 +703,7 @@ suite('Editor Commands - ShiftCommand', () => {
703703
' eleven | 11',
704704
'',
705705
],
706-
new Selection(1, 5, 13, 1)
706+
new Selection(1, 1, 13, 1)
707707
);
708708
});
709709

@@ -839,6 +839,28 @@ suite('Editor Commands - ShiftCommand', () => {
839839
);
840840
});
841841

842+
test('issue Microsoft/monaco-editor#443: Indentation of a single row deletes selected text in some cases', () => {
843+
testCommand(
844+
[
845+
'Hello world!',
846+
'another line'
847+
],
848+
null,
849+
new Selection(1, 1, 1, 13),
850+
(sel) => new ShiftCommand(sel, {
851+
isUnshift: false,
852+
tabSize: 4,
853+
oneIndent: '\t',
854+
useTabStops: true
855+
}),
856+
[
857+
'\tHello world!',
858+
'another line'
859+
],
860+
new Selection(1, 1, 1, 14)
861+
);
862+
});
863+
842864
test('bug #16815:Shift+Tab doesn\'t go back to tabstop', () => {
843865

844866
var repeatStr = (str: string, cnt: number): string => {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,33 @@ class IndentRulesMode extends MockMode {
11311131
}
11321132

11331133
suite('Editor Controller - Regression tests', () => {
1134+
1135+
test('issue Microsoft/monaco-editor#443: Indentation of a single row deletes selected text in some cases', () => {
1136+
let model = Model.createFromString(
1137+
[
1138+
'Hello world!',
1139+
'another line'
1140+
].join('\n'),
1141+
{
1142+
defaultEOL: DefaultEndOfLine.LF,
1143+
detectIndentation: false,
1144+
insertSpaces: false,
1145+
tabSize: 4,
1146+
trimAutoWhitespace: false
1147+
},
1148+
);
1149+
1150+
withMockCodeEditor(null, { model: model }, (editor, cursor) => {
1151+
cursor.setSelections('test', [new Selection(1, 1, 1, 13)]);
1152+
1153+
// Check that indenting maintains the selection start at column 1
1154+
CoreEditingCommands.Tab.runEditorCommand(null, editor, null);
1155+
assert.deepEqual(cursor.getSelection(), new Selection(1, 1, 1, 14));
1156+
});
1157+
1158+
model.dispose();
1159+
});
1160+
11341161
test('Bug 9121: Auto indent + undo + redo is funky', () => {
11351162
let model = Model.createFromString(
11361163
[

0 commit comments

Comments
 (0)