src/vs/workbench/contrib/preferences/common/smartSnippetInserter.ts
157 LOC · 153 covered · 4 uncovered · 28 ranges · 7 concepts · 6 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.
/*---------------------------------------------------------------------------------------------
smartSnippetInserter.ts ×11
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { JSONScanner, createScanner as createJSONScanner, SyntaxKind as JSONSyntaxKind } from '../../../../base/common/json.js';
import { Position } from '../../../../editor/common/core/position.js';
import { Range } from '../../../../editor/common/core/range.js';
import { ITextModel } from '../../../../editor/common/model.js';
export interface InsertSnippetResult {
position: Position;
prepend: string;
append: string;
}
export class SmartSnippetInserter {
private static hasOpenBrace(scanner: JSONScanner): boolean {
while (scanner.scan() !== JSONSyntaxKind.EOF) {
const kind = scanner.getToken();
if (kind === JSONSyntaxKind.OpenBraceToken) {
}
return false;
private static offsetToPosition(model: ITextModel, offset: number): Position {
const eolLength = model.getEOL().length;
const lineCount = model.getLineCount();
for (let lineNumber = 1; lineNumber <= lineCount; lineNumber++) {
const lineTotalLength = model.getLineLength(lineNumber) + eolLength;
const offsetAfterLine = offsetBeforeLine + lineTotalLength;
if (offsetAfterLine > offset) {
return new Position(
lineNumber,
offset - offsetBeforeLine + 1
);
}
offsetBeforeLine = offsetAfterLine;
}
return new Position(
lineCount,
model.getLineMaxColumn(lineCount)
);
static insertSnippet(model: ITextModel, _position: Position): InsertSnippetResult {
const desiredPosition = model.getValueLengthInRange(new Range(1, 1, _position.lineNumber, _position.column));
// <INVALID> [ <BEFORE_OBJECT> { <INVALID> } <AFTER_OBJECT>, <BEFORE_OBJECT> { <INVALID> } <AFTER_OBJECT> ] <INVALID>
enum State {
INVALID = 0,
AFTER_OBJECT = 1,
BEFORE_OBJECT = 2,
}
let currentState = State.INVALID;
let lastValidPos = -1;
let lastValidState = State.INVALID;
const scanner = createJSONScanner(model.getValue());
let arrayLevel = 0;
let objLevel = 0;
const checkRangeStatus = (pos: number, state: State) => {
if (state !== State.INVALID && arrayLevel === 1 && objLevel === 0) {
smartSnippetInserter.ts ×10
currentState = state;
lastValidPos = pos;
lastValidState = state;
} else {
if (currentState !== State.INVALID) {
currentState = State.INVALID;
lastValidPos = scanner.getTokenOffset();
}
}
};
while (scanner.scan() !== JSONSyntaxKind.EOF) {
const currentPos = scanner.getPosition();
const kind = scanner.getToken();
let goodKind = false;
switch (kind) {
case JSONSyntaxKind.OpenBracketToken:
arrayLevel++;
checkRangeStatus(currentPos, State.BEFORE_OBJECT);
break;
arrayLevel--;
checkRangeStatus(currentPos, State.INVALID);
break;
checkRangeStatus(currentPos, State.BEFORE_OBJECT);
break;
objLevel++;
checkRangeStatus(currentPos, State.INVALID);
break;
objLevel--;
checkRangeStatus(currentPos, State.AFTER_OBJECT);
break;
case JSONSyntaxKind.LineBreakTrivia:
goodKind = true;
}
if (currentPos >= desiredPosition && (currentState !== State.INVALID || lastValidPos !== -1)) {
let acceptState: State;
if (currentState !== State.INVALID) {
acceptPosition = (goodKind ? currentPos : scanner.getTokenOffset());
acceptState = currentState;
} else {
acceptPosition = lastValidPos;
acceptState = lastValidState;
}
if (acceptState as State === State.AFTER_OBJECT) {
position: this.offsetToPosition(model, acceptPosition),
prepend: ',',
append: ''
};
scanner.setPosition(acceptPosition);
return {
position: this.offsetToPosition(model, acceptPosition),
prepend: '',
append: this.hasOpenBrace(scanner) ? ',' : ''
};
}
}
// no valid position found!
const modelLineCount = model.getLineCount();
return {
position: new Position(modelLineCount, model.getLineMaxColumn(modelLineCount)),
prepend: '\n[',
append: ']'
};
}