1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CharCode } from 'vs/base/common/charCode';
import * as strings from 'vs/base/common/strings';
import { CursorColumns } from 'vs/editor/common/core/cursorColumns';
import { Range } from 'vs/editor/common/core/range';
import { Selection, SelectionDirection } from 'vs/editor/common/core/selection';
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from 'vs/editor/common/editorCommon';
import { ITextModel } from 'vs/editor/common/model';
import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
import { getEnterAction } from 'vs/editor/common/languages/enterAction';
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
export interface IShiftCommandOpts {
isUnshift: boolean;
tabSize: number;
indentSize: number;
insertSpaces: boolean;
useTabStops: boolean;
autoIndent: EditorAutoIndentStrategy;
}
const repeatCache: { [str: string]: string[] } = Object.create(null);
export function cachedStringRepeat(str: string, count: number): string {
if (count <= 0) {
return '';
}
if (!repeatCache[str]) {
repeatCache[str] = ['', str];
}
const cache = repeatCache[str];
for (let i = cache.length; i <= count; i++) {
cache[i] = cache[i - 1] + str;
}
return cache[count];
}
export class ShiftCommand implements ICommand {
public static unshiftIndent(line: string, column: number, tabSize: number, indentSize: number, insertSpaces: boolean): string {
// Determine the visible column where the content starts
const contentStartVisibleColumn = CursorColumns.visibleColumnFromColumn(line, column, tabSize);
if (insertSpaces) {
const indent = cachedStringRepeat(' ', indentSize);
const desiredTabStop = CursorColumns.prevIndentTabStop(contentStartVisibleColumn, indentSize);
const indentCount = desiredTabStop / indentSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
} else {
const indent = '\t';
const desiredTabStop = CursorColumns.prevRenderTabStop(contentStartVisibleColumn, tabSize);
const indentCount = desiredTabStop / tabSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
}
}
public static shiftIndent(line: string, column: number, tabSize: number, indentSize: number, insertSpaces: boolean): string {
// Determine the visible column where the content starts
const contentStartVisibleColumn = CursorColumns.visibleColumnFromColumn(line, column, tabSize);
if (insertSpaces) {
const indent = cachedStringRepeat(' ', indentSize);
const desiredTabStop = CursorColumns.nextIndentTabStop(contentStartVisibleColumn, indentSize);
const indentCount = desiredTabStop / indentSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
} else {
const indent = '\t';
const desiredTabStop = CursorColumns.nextRenderTabStop(contentStartVisibleColumn, tabSize);
const indentCount = desiredTabStop / tabSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
}
}
private readonly _opts: IShiftCommandOpts;
private readonly _selection: Selection;
private _selectionId: string | null;
private _useLastEditRangeForCursorEndPosition: boolean;
private _selectionStartColumnStaysPut: boolean;
constructor(
range: Selection,
opts: IShiftCommandOpts,
@ILanguageConfigurationService private readonly _languageConfigurationService: ILanguageConfigurationService
) {
this._opts = opts;
this._selection = range;
this._selectionId = null;
this._useLastEditRangeForCursorEndPosition = false;
this._selectionStartColumnStaysPut = false;
}
private _addEditOperation(builder: IEditOperationBuilder, range: Range, text: string) {
if (this._useLastEditRangeForCursorEndPosition) {
builder.addTrackedEditOperation(range, text);
} else {
builder.addEditOperation(range, text);
}
}
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
const startLine = this._selection.startLineNumber;
let endLine = this._selection.endLineNumber;
if (this._selection.endColumn === 1 && startLine !== endLine) {
endLine = endLine - 1;
}
const { tabSize, indentSize, insertSpaces } = this._opts;
const shouldIndentEmptyLines = (startLine === endLine);
if (this._opts.useTabStops) {
// if indenting or outdenting on a whitespace only line
if (this._selection.isEmpty()) {
if (/^\s*$/.test(model.getLineContent(startLine))) {
this._useLastEditRangeForCursorEndPosition = true;
}
}
// keep track of previous line's "miss-alignment"
let previousLineExtraSpaces = 0, extraSpaces = 0;
for (let lineNumber = startLine; lineNumber <= endLine; lineNumber++, previousLineExtraSpaces = extraSpaces) {
extraSpaces = 0;
const lineText = model.getLineContent(lineNumber);
let indentationEndIndex = strings.firstNonWhitespaceIndex(lineText);
if (this._opts.isUnshift && (lineText.length === 0 || indentationEndIndex === 0)) {
// empty line or line with no leading whitespace => nothing to do
continue;
}
if (!shouldIndentEmptyLines && !this._opts.isUnshift && lineText.length === 0) {
// do not indent empty lines => nothing to do
continue;
}
if (indentationEndIndex === -1) {
// the entire line is whitespace
indentationEndIndex = lineText.length;
}
if (lineNumber > 1) {
const contentStartVisibleColumn = CursorColumns.visibleColumnFromColumn(lineText, indentationEndIndex + 1, tabSize);
if (contentStartVisibleColumn % indentSize !== 0) {
// The current line is "miss-aligned", so let's see if this is expected...
// This can only happen when it has trailing commas in the indent
if (model.tokenization.isCheapToTokenize(lineNumber - 1)) {
const enterAction = getEnterAction(this._opts.autoIndent, model, new Range(lineNumber - 1, model.getLineMaxColumn(lineNumber - 1), lineNumber - 1, model.getLineMaxColumn(lineNumber - 1)), this._languageConfigurationService);
if (enterAction) {
extraSpaces = previousLineExtraSpaces;
if (enterAction.appendText) {
for (let j = 0, lenJ = enterAction.appendText.length; j < lenJ && extraSpaces < indentSize; j++) {
if (enterAction.appendText.charCodeAt(j) === CharCode.Space) {
extraSpaces++;
} else {
break;
}
}
}
if (enterAction.removeText) {
extraSpaces = Math.max(0, extraSpaces - enterAction.removeText);
}
// Act as if `prefixSpaces` is not part of the indentation
for (let j = 0; j < extraSpaces; j++) {
if (indentationEndIndex === 0 || lineText.charCodeAt(indentationEndIndex - 1) !== CharCode.Space) {
break;
}
indentationEndIndex--;
}
}
}
}
}
if (this._opts.isUnshift && indentationEndIndex === 0) {
// line with no leading whitespace => nothing to do
continue;
}
let desiredIndent: string;
if (this._opts.isUnshift) {
desiredIndent = ShiftCommand.unshiftIndent(lineText, indentationEndIndex + 1, tabSize, indentSize, insertSpaces);
} else {
desiredIndent = ShiftCommand.shiftIndent(lineText, indentationEndIndex + 1, tabSize, indentSize, insertSpaces);
}
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), desiredIndent);
if (lineNumber === startLine && !this._selection.isEmpty()) {
// Force the startColumn to stay put because we're inserting after it
this._selectionStartColumnStaysPut = (this._selection.startColumn <= indentationEndIndex + 1);
}
}
} else {
// if indenting or outdenting on a whitespace only line
if (!this._opts.isUnshift && this._selection.isEmpty() && model.getLineLength(startLine) === 0) {
this._useLastEditRangeForCursorEndPosition = true;
}
const oneIndent = (insertSpaces ? cachedStringRepeat(' ', indentSize) : '\t');
for (let lineNumber = startLine; lineNumber <= endLine; lineNumber++) {
const lineText = model.getLineContent(lineNumber);
let indentationEndIndex = strings.firstNonWhitespaceIndex(lineText);
if (this._opts.isUnshift && (lineText.length === 0 || indentationEndIndex === 0)) {
// empty line or line with no leading whitespace => nothing to do
continue;
}
if (!shouldIndentEmptyLines && !this._opts.isUnshift && lineText.length === 0) {
// do not indent empty lines => nothing to do
continue;
}
if (indentationEndIndex === -1) {
// the entire line is whitespace
indentationEndIndex = lineText.length;
}
if (this._opts.isUnshift && indentationEndIndex === 0) {
// line with no leading whitespace => nothing to do
continue;
}
if (this._opts.isUnshift) {
indentationEndIndex = Math.min(indentationEndIndex, indentSize);
for (let i = 0; i < indentationEndIndex; i++) {
const chr = lineText.charCodeAt(i);
if (chr === CharCode.Tab) {
indentationEndIndex = i + 1;
break;
}
}
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), '');
} else {
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, 1), oneIndent);
if (lineNumber === startLine && !this._selection.isEmpty()) {
// Force the startColumn to stay put because we're inserting after it
this._selectionStartColumnStaysPut = (this._selection.startColumn === 1);
}
}
}
}
this._selectionId = builder.trackSelection(this._selection);
}
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
if (this._useLastEditRangeForCursorEndPosition) {
const lastOp = helper.getInverseEditOperations()[0];
return new Selection(lastOp.range.endLineNumber, lastOp.range.endColumn, lastOp.range.endLineNumber, lastOp.range.endColumn);
}
const result = helper.getTrackedSelection(this._selectionId!);
if (this._selectionStartColumnStaysPut) {
// The selection start should not move
const initialStartColumn = this._selection.startColumn;
const resultStartColumn = result.startColumn;
if (resultStartColumn <= initialStartColumn) {
return result;
}
if (result.getDirection() === SelectionDirection.LTR) {
return new Selection(result.startLineNumber, initialStartColumn, result.endLineNumber, result.endColumn);
}
return new Selection(result.endLineNumber, result.endColumn, result.startLineNumber, initialStartColumn);
}
return result;
}
}
|