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.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/editor/common/commands/shiftCommand.ts · 278 LOCcommands/shiftCommand.tssrc/vs/editor/common/languages/enterAction.ts · 65 LOClanguages/enterAction.tssrc/vs/editor/common/languages/supports/indentationLineProcessor.ts · 236 LOCsupports/indentationLine…src/vs/editor/contrib/indentation/common/indentation.ts · 115 LOCcommon/indentation.tssrc/vs/workbench/contrib/codeEditor/common/languageConfigurationExtensionPoint.ts · 888 LOCcommon/languageConfigura…JavaScript Issue #25437|occurrence=1, JavaScript Issue #141816|occurrence=1 · 0 introduced LOCJavaScript Issue #25437|…indentation.ts ×1 · 2 introduced LOCindentation.ts ×1textModelTokens.ts ×4 · 12 introduced LOCtextModelTokens.ts ×4JavaScript Enriching the hover|occurrence=1 · 0 introduced LOCJavaScript Enriching the…JavaScript Issue #86176|occurrence=1 · 0 introduced LOCJavaScript Issue #86176|…indentation.ts ×4 · 41 introduced LOCindentation.ts ×4indentation.ts ×1 · 2 introduced LOCindentation.ts ×1indentation.ts ×6 · 27 introduced LOCindentation.ts ×6languageConfigurationExtensionPoint.ts ×38 · 309 introduced LOClanguageConfigurationExt…languageConfigurationExtensionPoint.ts ×6 · 12 introduced LOClanguageConfigurationExt…shiftCommand.ts ×1 · 5 introduced LOCshiftCommand.ts ×1languageConfigurationExtensionPoint.ts ×19 · 775 introduced LOClanguageConfigurationExt…JavaScript Enriching the hover|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/codeEditor/test/node/autoindent.test|title=Auto-Reindentation - TypeScript/JavaScript Enriching the hover|occurrence=1JavaScript Enriching the…JavaScript Issue #141816|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/codeEditor/test/node/autoindent.test|title=Auto-Reindentation - TypeScript/JavaScript Issue #141816|occurrence=1JavaScript Issue #141816…JavaScript Issue #209859: do not do reindentation for tokens inside of a string|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/codeEditor/test/node/autoindent.test|title=Auto-Reindentation - TypeScript/JavaScript Issue #209859: do not do reindentation for tokens inside of a string|occurrence=1JavaScript Issue #209859…JavaScript Issue #25437|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/codeEditor/test/node/autoindent.test|title=Auto-Reindentation - TypeScript/JavaScript Issue #25437|occurrence=1JavaScript Issue #25437|…JavaScript Issue #29886|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/codeEditor/test/node/autoindent.test|title=Auto-Reindentation - TypeScript/JavaScript Issue #29886|occurrence=1JavaScript Issue #29886|…JavaScript Issue #86176|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/codeEditor/test/node/autoindent.test|title=Auto-Reindentation - TypeScript/JavaScript Issue #86176|occurrence=1JavaScript Issue #86176|…autoindent.test|title=Language Configuration Parsing Folding markers support object regex syntax with flags|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/codeEditor/test/node/autoindent.test|title=Language Configuration Parsing Folding markers support object regex syntax with flags|occurrence=1autoindent.test|title=La…Focused file · src/vs/editor/common/commands/trimTrailingWhitespaceCommand.ts · 126 LOCcommands/trimTrailingWhi…

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.

1 > /*--------------------------------------------------------------------------------------------- languageConfigurationExtensionPoint.ts ×19
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import * as strings from '../../../base/common/strings.js';
7 > import { EditOperation, ISingleEditOperation } from '../core/editOperation.js';
8 > import { Position } from '../core/position.js';
9 > import { Range } from '../core/range.js';
10 > import { Selection } from '../core/selection.js';
11 > import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../editorCommon.js';
12 > import { StandardTokenType } from '../encodedTokenAttributes.js';
13 > import { ITextModel } from '../model.js';
14 >
15 > export class TrimTrailingWhitespaceCommand implements ICommand {
16 >
17 > private readonly _selection: Selection;
18 > private _selectionId: string | null;
19 > private readonly _cursors: Position[];
20 > private readonly _trimInRegexesAndStrings: boolean;
21 >
22 > constructor(selection: Selection, cursors: Position[], trimInRegexesAndStrings: boolean) {
23 this._selection = selection;
24 this._cursors = cursors;
25 this._selectionId = null;
26 this._trimInRegexesAndStrings = trimInRegexesAndStrings;
27 }
29 > public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
30 const ops = trimTrailingWhitespace(model, this._cursors, this._trimInRegexesAndStrings);
31 for (let i = 0, len = ops.length; i < len; i++) {
32 const op = ops[i];
33
34 builder.addEditOperation(op.range, op.text);
35 }
36
37 this._selectionId = builder.trackSelection(this._selection);
38 }
40 > public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
41 return helper.getTrackedSelection(this._selectionId!);
42 }
44 >
45 > /**
46 > * Generate commands for trimming trailing whitespace on a model and ignore lines on which cursors are sitting.
47 > */
48 > export function trimTrailingWhitespace(model: ITextModel, cursors: Position[], trimInRegexesAndStrings: boolean): ISingleEditOperation[] {
49 // Sort cursors ascending
50 cursors.sort((a, b) => {
51 if (a.lineNumber === b.lineNumber) {
52 return a.column - b.column;
53 }
54 return a.lineNumber - b.lineNumber;
55 });
56
57 // Reduce multiple cursors on the same line and only keep the last one on the line
58 for (let i = cursors.length - 2; i >= 0; i--) {
59 if (cursors[i].lineNumber === cursors[i + 1].lineNumber) {
60 // Remove cursor at `i`
61 cursors.splice(i, 1);
62 }
63 }
64
65 const r: ISingleEditOperation[] = [];
66 let rLen = 0;
67 let cursorIndex = 0;
68 const cursorLen = cursors.length;
69
70 for (let lineNumber = 1, lineCount = model.getLineCount(); lineNumber <= lineCount; lineNumber++) {
71 const lineContent = model.getLineContent(lineNumber);
72 const maxLineColumn = lineContent.length + 1;
73 let minEditColumn = 0;
74
75 if (cursorIndex < cursorLen && cursors[cursorIndex].lineNumber === lineNumber) {
76 minEditColumn = cursors[cursorIndex].column;
77 cursorIndex++;
78 if (minEditColumn === maxLineColumn) {
79 // The cursor is at the end of the line => no edits for sure on this line
80 continue;
81 }
82 }
83
84 if (lineContent.length === 0) {
85 continue;
86 }
87
88 const lastNonWhitespaceIndex = strings.lastNonWhitespaceIndex(lineContent);
89
90 let fromColumn = 0;
91 if (lastNonWhitespaceIndex === -1) {
92 // Entire line is whitespace
93 fromColumn = 1;
94 } else if (lastNonWhitespaceIndex !== lineContent.length - 1) {
95 // There is trailing whitespace
96 fromColumn = lastNonWhitespaceIndex + 2;
97 } else {
98 // There is no trailing whitespace
99 continue;
100 }
101
102 if (!trimInRegexesAndStrings) {
103 if (!model.tokenization.hasAccurateTokensForLine(lineNumber)) {
104 // We don't want to force line tokenization, as that can be expensive, but we also don't want to trim
105 // trailing whitespace in lines that are not tokenized yet, as that can be wrong and trim whitespace from
106 // lines that the user requested we don't. So we bail out if the tokens are not accurate for this line.
107 continue;
108 }
109
110 const lineTokens = model.tokenization.getLineTokens(lineNumber);
111 const fromColumnType = lineTokens.getStandardTokenType(lineTokens.findTokenIndexAtOffset(fromColumn));
112
113 if (fromColumnType === StandardTokenType.String || fromColumnType === StandardTokenType.RegEx) {
114 continue;
115 }
116 }
117
118 fromColumn = Math.max(minEditColumn, fromColumn);
119 r[rLen++] = EditOperation.delete(new Range(
120 lineNumber, fromColumn,
121 lineNumber, maxLineColumn
122 ));
123 }
124
125 return r;
126 }