src/vs/editor/common/commands/trimTrailingWhitespaceCommand.ts
126 LOC · 32 covered · 94 uncovered · 4 ranges · 12 concepts · 1 introducers · 7 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
languageConfigurationExtensionPoint.ts ×19
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as strings from '../../../base/common/strings.js';
import { EditOperation, ISingleEditOperation } from '../core/editOperation.js';
import { Position } from '../core/position.js';
import { Range } from '../core/range.js';
import { Selection } from '../core/selection.js';
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../editorCommon.js';
import { StandardTokenType } from '../encodedTokenAttributes.js';
import { ITextModel } from '../model.js';
export class TrimTrailingWhitespaceCommand implements ICommand {
private readonly _selection: Selection;
private _selectionId: string | null;
private readonly _cursors: Position[];
private readonly _trimInRegexesAndStrings: boolean;
constructor(selection: Selection, cursors: Position[], trimInRegexesAndStrings: boolean) {
this._selection = selection;
this._cursors = cursors;
this._selectionId = null;
this._trimInRegexesAndStrings = trimInRegexesAndStrings;
}
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
const ops = trimTrailingWhitespace(model, this._cursors, this._trimInRegexesAndStrings);
for (let i = 0, len = ops.length; i < len; i++) {
const op = ops[i];
builder.addEditOperation(op.range, op.text);
}
this._selectionId = builder.trackSelection(this._selection);
}
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
return helper.getTrackedSelection(this._selectionId!);
}
/**
* Generate commands for trimming trailing whitespace on a model and ignore lines on which cursors are sitting.
*/
export function trimTrailingWhitespace(model: ITextModel, cursors: Position[], trimInRegexesAndStrings: boolean): ISingleEditOperation[] {
// Sort cursors ascending
cursors.sort((a, b) => {
if (a.lineNumber === b.lineNumber) {
return a.column - b.column;
}
return a.lineNumber - b.lineNumber;
});
// Reduce multiple cursors on the same line and only keep the last one on the line
for (let i = cursors.length - 2; i >= 0; i--) {
if (cursors[i].lineNumber === cursors[i + 1].lineNumber) {
// Remove cursor at `i`
cursors.splice(i, 1);
}
}
const r: ISingleEditOperation[] = [];
let rLen = 0;
let cursorIndex = 0;
const cursorLen = cursors.length;
for (let lineNumber = 1, lineCount = model.getLineCount(); lineNumber <= lineCount; lineNumber++) {
const lineContent = model.getLineContent(lineNumber);
const maxLineColumn = lineContent.length + 1;
let minEditColumn = 0;
if (cursorIndex < cursorLen && cursors[cursorIndex].lineNumber === lineNumber) {
minEditColumn = cursors[cursorIndex].column;
cursorIndex++;
if (minEditColumn === maxLineColumn) {
// The cursor is at the end of the line => no edits for sure on this line
continue;
}
}
if (lineContent.length === 0) {
continue;
}
const lastNonWhitespaceIndex = strings.lastNonWhitespaceIndex(lineContent);
let fromColumn = 0;
if (lastNonWhitespaceIndex === -1) {
// Entire line is whitespace
fromColumn = 1;
} else if (lastNonWhitespaceIndex !== lineContent.length - 1) {
// There is trailing whitespace
fromColumn = lastNonWhitespaceIndex + 2;
} else {
// There is no trailing whitespace
continue;
}
if (!trimInRegexesAndStrings) {
if (!model.tokenization.hasAccurateTokensForLine(lineNumber)) {
// We don't want to force line tokenization, as that can be expensive, but we also don't want to trim
// trailing whitespace in lines that are not tokenized yet, as that can be wrong and trim whitespace from
// lines that the user requested we don't. So we bail out if the tokens are not accurate for this line.
continue;
}
const lineTokens = model.tokenization.getLineTokens(lineNumber);
const fromColumnType = lineTokens.getStandardTokenType(lineTokens.findTokenIndexAtOffset(fromColumn));
if (fromColumnType === StandardTokenType.String || fromColumnType === StandardTokenType.RegEx) {
continue;
}
}
fromColumn = Math.max(minEditColumn, fromColumn);
r[rLen++] = EditOperation.delete(new Range(
lineNumber, fromColumn,
lineNumber, maxLineColumn
));
}
return r;
}