src/vs/base/common/jsonEdit.ts
176 LOC · 166 covered · 10 uncovered · 64 ranges · 2316 concepts · 32 introducers · 1161 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.
/*---------------------------------------------------------------------------------------------
jsonEdit.ts ×5
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { findNodeAtLocation, JSONPath, Node, ParseError, parseTree, Segment } from './json.js';
import { Edit, format, FormattingOptions, isEOL } from './jsonFormatter.js';
export function removeProperty(text: string, path: JSONPath, formattingOptions: FormattingOptions): Edit[] {
}
export function setProperty(text: string, originalPath: JSONPath, value: unknown, formattingOptions: FormattingOptions, getInsertionIndex?: (properties: string[]) => number): Edit[] {
const errors: ParseError[] = [];
const root = parseTree(text, errors);
let parent: Node | undefined = undefined;
let lastSegment: Segment | undefined = undefined;
while (path.length > 0) {
lastSegment = path.pop();
parent = findNodeAtLocation(root, path);
if (parent === undefined && value !== undefined) {
value = { [lastSegment]: value };
} else {
value = [value];
}
break;
}
}
if (!parent) {
if (value === undefined) { // delete
return []; // property does not exist, nothing to do
}
return withFormatting(text, { offset: root ? root.offset : 0, length: root ? root.length : 0, content: JSON.stringify(value) }, formattingOptions);
jsonEdit.ts ×2
} else if (parent.type === 'object' && typeof lastSegment === 'string' && Array.isArray(parent.children)) {
jsonEdit.ts ×5
if (existing !== undefined) {
throw new Error('Malformed AST');
}
let removeBegin: number;
let removeEnd = existing.parent.offset + existing.parent.length;
if (propertyIndex > 0) {
const previous = parent.children[propertyIndex - 1];
removeBegin = previous.offset + previous.length;
if (parent.children.length > 1) {
const next = parent.children[1];
removeEnd = next.offset;
}
return withFormatting(text, { offset: removeBegin, length: removeEnd - removeBegin, content: '' }, formattingOptions);
jsonEdit.ts ×4
return withFormatting(text, { offset: existing.offset, length: existing.length, content: JSON.stringify(value) }, formattingOptions);
}
}
const index = getInsertionIndex ? getInsertionIndex(parent.children.map(p => p.children![0].value)) : parent.children.length;
jsonEdit.ts ×2
let edit: Edit;
if (index > 0) {
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
} else {
}
}
} else if (parent.type === 'array' && typeof lastSegment === 'number' && Array.isArray(parent.children)) {
jsonEdit.ts ×5
const newProperty = `${JSON.stringify(value)}`;
let edit: Edit;
if (parent.children.length === 0 || lastSegment === 0) {
edit = { offset: parent.offset + 1, length: 0, content: parent.children.length === 0 ? newProperty : newProperty + ',' };
jsonEdit.ts ×1
const index = lastSegment === -1 || lastSegment > parent.children.length ? parent.children.length : lastSegment;
jsonEdit.ts ×1
const previous = parent.children[index - 1];
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
}
const removalIndex = lastSegment;
const toRemove = parent.children[removalIndex];
let edit: Edit;
if (parent.children.length === 1) {
edit = { offset: parent.offset + 1, length: parent.length - 2, content: '' };
const previous = parent.children[removalIndex - 1];
const offset = previous.offset + previous.length;
const parentEndOffset = parent.offset + parent.length;
edit = { offset, length: parentEndOffset - 2 - offset, content: '' };
edit = { offset: toRemove.offset, length: parent.children[removalIndex + 1].offset - toRemove.offset, content: '' };
jsonEdit.ts ×1
}
}
throw new Error(`Can not add ${typeof lastSegment !== 'number' ? 'index' : 'property'} to parent of type ${parent.type}`);
}
export function withFormatting(text: string, edit: Edit, formattingOptions: FormattingOptions): Edit[] {
let newText = applyEdit(text, edit);
// format the new text
let begin = edit.offset;
let end = edit.offset + edit.content.length;
if (edit.length === 0 || edit.content.length === 0) { // insert or remove
begin--;
}
while (end < newText.length && !isEOL(newText, end)) {
}
const edits = format(newText, { offset: begin, length: end - begin }, formattingOptions);
// apply the formatting edits and track the begin and end offsets of the changes
for (let i = edits.length - 1; i >= 0; i--) {
newText = applyEdit(newText, curr);
begin = Math.min(begin, curr.offset);
end = Math.max(end, curr.offset + curr.length);
end += curr.content.length - curr.length;
}
const editLength = text.length - (newText.length - end) - begin;
return [{ offset: begin, length: editLength, content: newText.substring(begin, end) }];
}
export function applyEdit(text: string, edit: Edit): string {
return text.substring(0, edit.offset) + edit.content + text.substring(edit.offset + edit.length);
jsonEdit.ts ×4
}
export function applyEdits(text: string, edits: Edit[]): string {
if (diff === 0) {
return a.length - b.length;
}
let lastModifiedOffset = text.length;
for (let i = sortedEdits.length - 1; i >= 0; i--) {
const e = sortedEdits[i];
if (e.offset + e.length <= lastModifiedOffset) {
text = applyEdit(text, e);
} else {
throw new Error('Overlapping edit');
}
}
return text;
}