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.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/base/common/json.ts · 1326 LOCcommon/json.tssmartSnippetInserter.ts ×1 · 3 introduced LOCsmartSnippetInserter.ts …smartSnippetInserter.ts ×4 · 15 introduced LOCsmartSnippetInserter.ts …smartSnippetInserter.test|title=SmartSnippetInserter empty array 1|occurrence=1, smartSnippetInserter.test|title=SmartSnippetInserter empty array 3|occurrence=1 · 0 introduced LOCsmartSnippetInserter.tes…smartSnippetInserter.ts ×1 · 2 introduced LOCsmartSnippetInserter.ts …smartSnippetInserter.ts ×10 · 69 introduced LOCsmartSnippetInserter.ts …smartSnippetInserter.ts ×1 · 8 introduced LOCsmartSnippetInserter.ts …smartSnippetInserter.ts ×11 · 62 introduced LOCsmartSnippetInserter.ts …smartSnippetInserter.test|title=SmartSnippetInserter empty array 1|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test|title=SmartSnippetInserter empty array 1|occurrence=1smartSnippetInserter.tes…smartSnippetInserter.test|title=SmartSnippetInserter empty array 2|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test|title=SmartSnippetInserter empty array 2|occurrence=1smartSnippetInserter.tes…smartSnippetInserter.test|title=SmartSnippetInserter empty array 3|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test|title=SmartSnippetInserter empty array 3|occurrence=1smartSnippetInserter.tes…smartSnippetInserter.test|title=SmartSnippetInserter empty text|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test|title=SmartSnippetInserter empty text|occurrence=1smartSnippetInserter.tes…smartSnippetInserter.test|title=SmartSnippetInserter one element array 1|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test|title=SmartSnippetInserter one element array 1|occurrence=1smartSnippetInserter.tes…smartSnippetInserter.test|title=SmartSnippetInserter two elements array 1|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test|title=SmartSnippetInserter two elements array 1|occurrence=1smartSnippetInserter.tes…smartSnippetInserter.test|title=SmartSnippetInserter two elements array 2|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test|title=SmartSnippetInserter two elements array 2|occurrence=1smartSnippetInserter.tes…Focused file · src/vs/workbench/contrib/preferences/common/smartSnippetInserter.ts · 157 LOCcommon/smartSnippetInser…

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 > /*--------------------------------------------------------------------------------------------- smartSnippetInserter.ts ×11
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 { JSONScanner, createScanner as createJSONScanner, SyntaxKind as JSONSyntaxKind } from '../../../../base/common/json.js';
7 > import { Position } from '../../../../editor/common/core/position.js';
8 > import { Range } from '../../../../editor/common/core/range.js';
9 > import { ITextModel } from '../../../../editor/common/model.js';
10 >
11 > export interface InsertSnippetResult {
12 > position: Position;
13 > prepend: string;
14 > append: string;
15 > }
16 >
17 > export class SmartSnippetInserter {
18 >
19 > private static hasOpenBrace(scanner: JSONScanner): boolean {
21 > while (scanner.scan() !== JSONSyntaxKind.EOF) {
22 > const kind = scanner.getToken();
23 >
24 > if (kind === JSONSyntaxKind.OpenBraceToken) {
25 > return true; smartSnippetInserter.ts ×4
26 > }
29 > return false;
32 > private static offsetToPosition(model: ITextModel, offset: number): Position {
33 > let offsetBeforeLine = 0; smartSnippetInserter.ts ×10
34 > const eolLength = model.getEOL().length;
35 > const lineCount = model.getLineCount();
36 > for (let lineNumber = 1; lineNumber <= lineCount; lineNumber++) {
37 > const lineTotalLength = model.getLineLength(lineNumber) + eolLength;
38 > const offsetAfterLine = offsetBeforeLine + lineTotalLength;
39 >
40 > if (offsetAfterLine > offset) {
41 > return new Position(
42 > lineNumber,
43 > offset - offsetBeforeLine + 1
44 > );
45 > }
46 > offsetBeforeLine = offsetAfterLine;
47 > }
48 return new Position(
49 lineCount,
50 model.getLineMaxColumn(lineCount)
51 );
54 > static insertSnippet(model: ITextModel, _position: Position): InsertSnippetResult {
55 >
56 > const desiredPosition = model.getValueLengthInRange(new Range(1, 1, _position.lineNumber, _position.column));
57 >
58 > // <INVALID> [ <BEFORE_OBJECT> { <INVALID> } <AFTER_OBJECT>, <BEFORE_OBJECT> { <INVALID> } <AFTER_OBJECT> ] <INVALID>
59 > enum State {
60 > INVALID = 0,
61 > AFTER_OBJECT = 1,
62 > BEFORE_OBJECT = 2,
63 > }
64 > let currentState = State.INVALID;
65 > let lastValidPos = -1;
66 > let lastValidState = State.INVALID;
67 >
68 > const scanner = createJSONScanner(model.getValue());
69 > let arrayLevel = 0;
70 > let objLevel = 0;
71 >
72 > const checkRangeStatus = (pos: number, state: State) => {
73 > if (state !== State.INVALID && arrayLevel === 1 && objLevel === 0) { smartSnippetInserter.ts ×10
74 > currentState = state;
75 > lastValidPos = pos;
76 > lastValidState = state;
77 > } else {
78 > if (currentState !== State.INVALID) {
79 > currentState = State.INVALID;
80 > lastValidPos = scanner.getTokenOffset();
81 > }
82 > }
83 > };
85 > while (scanner.scan() !== JSONSyntaxKind.EOF) {
86 > const currentPos = scanner.getPosition();
87 > const kind = scanner.getToken();
88 >
89 > let goodKind = false;
90 > switch (kind) {
91 > case JSONSyntaxKind.OpenBracketToken:
92 > goodKind = true; smartSnippetInserter.ts ×10
93 > arrayLevel++;
94 > checkRangeStatus(currentPos, State.BEFORE_OBJECT);
95 > break;
96 > case JSONSyntaxKind.CloseBracketToken: smartSnippetInserter.ts ×11
97 > goodKind = true; smartSnippetInserter.ts ×10
98 > arrayLevel--;
99 > checkRangeStatus(currentPos, State.INVALID);
100 > break;
101 > case JSONSyntaxKind.CommaToken: smartSnippetInserter.ts ×11
102 > goodKind = true; smartSnippetInserter.ts ×1
103 > checkRangeStatus(currentPos, State.BEFORE_OBJECT);
104 > break;
105 > case JSONSyntaxKind.OpenBraceToken: smartSnippetInserter.ts ×11
106 > goodKind = true; smartSnippetInserter.ts ×4
107 > objLevel++;
108 > checkRangeStatus(currentPos, State.INVALID);
109 > break;
110 > case JSONSyntaxKind.CloseBraceToken: smartSnippetInserter.ts ×11
111 > goodKind = true; smartSnippetInserter.ts ×4
112 > objLevel--;
113 > checkRangeStatus(currentPos, State.AFTER_OBJECT);
114 > break;
115 > case JSONSyntaxKind.Trivia: smartSnippetInserter.ts ×11
116 > case JSONSyntaxKind.LineBreakTrivia:
117 > goodKind = true;
118 > }
119 >
120 > if (currentPos >= desiredPosition && (currentState !== State.INVALID || lastValidPos !== -1)) {
121 > let acceptPosition: number; smartSnippetInserter.ts ×10
122 > let acceptState: State;
123 >
124 > if (currentState !== State.INVALID) {
125 > acceptPosition = (goodKind ? currentPos : scanner.getTokenOffset());
126 > acceptState = currentState;
127 > } else {
128 > acceptPosition = lastValidPos;
129 > acceptState = lastValidState;
130 > }
131 >
132 > if (acceptState as State === State.AFTER_OBJECT) {
134 > position: this.offsetToPosition(model, acceptPosition),
135 > prepend: ',',
136 > append: ''
137 > };
139 > scanner.setPosition(acceptPosition);
140 > return {
141 > position: this.offsetToPosition(model, acceptPosition),
142 > prepend: '',
143 > append: this.hasOpenBrace(scanner) ? ',' : ''
144 > };
145 > }
146 > }
149 > // no valid position found!
150 > const modelLineCount = model.getLineCount();
151 > return {
152 > position: new Position(modelLineCount, model.getLineMaxColumn(modelLineCount)),
153 > prepend: '\n[',
154 > append: ']'
155 > };
157 > }