Skip to content

Commit f53c7ba

Browse files
committed
replace inPlace with optional ModificationOptions.formattingOptions
1 parent b905205 commit f53c7ba

File tree

4 files changed

+89
-84
lines changed

4 files changed

+89
-84
lines changed

CHANGELOG.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
1+
2.3.0 2020-07-03
2+
==================
3+
* new API `ModificationOptions.isArrayInsertion`: If `JSONPath` refers to an index of an array and `isArrayInsertion` is `true`, then `modify` will insert a new item at that location instead of overwriting its contents.
4+
* `ModificationOptions.formattingOptions` is now optional. If not set, newly inserted content will be not be formatted.
5+
6+
17
2.2.0 2019-10-25
28
==================
3-
* added *ParseOptions.allowEmptyContent*. Default is `false`.
4-
* New API *getNodeType*: Returns the type of a value returned by parse.
5-
* parse: Fix issue with empty property name
9+
* added `ParseOptions.allowEmptyContent`. Default is `false`.
10+
* new API `getNodeType`: Returns the type of a value returned by parse.
11+
* `parse`: Fix issue with empty property name
612

713
2.1.0 2019-03-29
814
==================
915
* `JSONScanner` and `JSONVisitor` return lineNumber / character.
1016

1117
2.0.0 2018-04-12
1218
==================
13-
* renamed Node.columnOffset to Node.colonOffset
14-
* new API getNodePath: Gets the JSON path of the given JSON DOM node
15-
* new API findNodeAtOffset: Finds the most inner node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
19+
* renamed `Node.columnOffset` to `Node.colonOffset`
20+
* new API `getNodePath`: Gets the JSON path of the given JSON DOM node
21+
* new API `findNodeAtOffset`: Finds the most inner node at the given offset. If `includeRightBound` is set, also finds nodes that end at the given offset.
1622

1723
1.0.3 2018-03-07
1824
==================
1925
* provide ems modules
2026

2127
1.0.2 2018-03-05
2228
==================
23-
* added the *visit.onComment* API, reported when comments are allowed.
24-
* added the *ParseErrorCode.InvalidCommentToken* enum value, reported when comments are disallowed.
29+
* added the `visit.onComment` API, reported when comments are allowed.
30+
* added the `ParseErrorCode.InvalidCommentToken` enum value, reported when comments are disallowed.
2531

2632
1.0.1
2733
==================
28-
* added the *format* API: computes edits to format a JSON document.
29-
* added the *modify* API: computes edits to insert, remove or replace a property or value in a JSON document.
30-
* added the *allyEdits* API: applies edits to a document
34+
* added the `format` API: computes edits to format a JSON document.
35+
* added the `modify` API: computes edits to insert, remove or replace a property or value in a JSON document.
36+
* added the `allyEdits` API: applies edits to a document
3137

3238
1.0.0
3339
==================
34-
* remove nls dependency (remove getParseErrorMessage)
40+
* remove nls dependency (remove `getParseErrorMessage`)
3541

3642
0.4.2 / 2017-05-05
3743
==================
38-
* added ParseError.offset & ParseError.length
44+
* added `ParseError.offset` & `ParseError.length`
3945

4046
0.4.1 / 2017-04-02
4147
==================
42-
* added ParseOptions.allowTrailingComma
48+
* added `ParseOptions.allowTrailingComma`
4349

4450
0.4.0 / 2017-02-23
4551
==================

src/impl/edit.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { Edit, FormattingOptions, ParseError, Node, JSONPath, Segment } from '../main';
7+
import { Edit, ParseError, Node, JSONPath, Segment, ModificationOptions } from '../main';
88
import { format, isEOL } from './format';
99
import { parseTree, findNodeAtLocation } from './parser';
1010

11-
export function removeProperty(text: string, path: JSONPath, formattingOptions: FormattingOptions): Edit[] {
12-
return setProperty(text, path, void 0, formattingOptions);
11+
export function removeProperty(text: string, path: JSONPath, options: ModificationOptions): Edit[] {
12+
return setProperty(text, path, void 0, options);
1313
}
1414

15-
export function setProperty(text: string, originalPath: JSONPath, value: any, formattingOptions: FormattingOptions, getInsertionIndex?: (properties: string[]) => number, isArrayInsertion: boolean = false): Edit[] {
15+
export function setProperty(text: string, originalPath: JSONPath, value: any, options: ModificationOptions): Edit[] {
1616
let path = originalPath.slice()
1717
let errors: ParseError[] = [];
1818
let root = parseTree(text, errors);
@@ -38,7 +38,7 @@ export function setProperty(text: string, originalPath: JSONPath, value: any, fo
3838
if (value === void 0) { // delete
3939
throw new Error('Can not delete in empty document');
4040
}
41-
return withFormatting(text, { offset: root ? root.offset : 0, length: root ? root.length : 0, content: JSON.stringify(value) }, formattingOptions);
41+
return withFormatting(text, { offset: root ? root.offset : 0, length: root ? root.length : 0, content: JSON.stringify(value) }, options);
4242
} else if (parent.type === 'object' && typeof lastSegment === 'string' && Array.isArray(parent.children)) {
4343
let existing = findNodeAtLocation(parent, [lastSegment]);
4444
if (existing !== void 0) {
@@ -61,17 +61,17 @@ export function setProperty(text: string, originalPath: JSONPath, value: any, fo
6161
removeEnd = next.offset;
6262
}
6363
}
64-
return withFormatting(text, { offset: removeBegin, length: removeEnd - removeBegin, content: '' }, formattingOptions);
64+
return withFormatting(text, { offset: removeBegin, length: removeEnd - removeBegin, content: '' }, options);
6565
} else {
6666
// set value of existing property
67-
return withFormatting(text, { offset: existing.offset, length: existing.length, content: JSON.stringify(value) }, formattingOptions);
67+
return withFormatting(text, { offset: existing.offset, length: existing.length, content: JSON.stringify(value) }, options);
6868
}
6969
} else {
7070
if (value === void 0) { // delete
7171
return []; // property does not exist, nothing to do
7272
}
7373
let newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`;
74-
let index = getInsertionIndex ? getInsertionIndex(parent.children.map(p => p.children![0].value)) : parent.children.length;
74+
let index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map(p => p.children![0].value)) : parent.children.length;
7575
let edit: Edit;
7676
if (index > 0) {
7777
let previous = parent.children[index - 1];
@@ -81,7 +81,7 @@ export function setProperty(text: string, originalPath: JSONPath, value: any, fo
8181
} else {
8282
edit = { offset: parent.offset + 1, length: 0, content: newProperty + ',' };
8383
}
84-
return withFormatting(text, edit, formattingOptions);
84+
return withFormatting(text, edit, options);
8585
}
8686
} else if (parent.type === 'array' && typeof lastSegment === 'number' && Array.isArray(parent.children)) {
8787
let insertIndex = lastSegment;
@@ -95,7 +95,7 @@ export function setProperty(text: string, originalPath: JSONPath, value: any, fo
9595
let previous = parent.children[parent.children.length - 1];
9696
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
9797
}
98-
return withFormatting(text, edit, formattingOptions);
98+
return withFormatting(text, edit, options);
9999
} else if (value === void 0 && parent.children.length >= 0) {
100100
// Removal
101101
let removalIndex = lastSegment;
@@ -113,12 +113,12 @@ export function setProperty(text: string, originalPath: JSONPath, value: any, fo
113113
} else {
114114
edit = { offset: toRemove.offset, length: parent.children[removalIndex + 1].offset - toRemove.offset, content: '' };
115115
}
116-
return withFormatting(text, edit, formattingOptions);
116+
return withFormatting(text, edit, options);
117117
} else if (value !== void 0) {
118118
let edit: Edit;
119119
const newProperty = `${JSON.stringify(value)}`;
120120

121-
if (!isArrayInsertion && parent.children.length > lastSegment) {
121+
if (!options.isArrayInsertion && parent.children.length > lastSegment) {
122122
let toModify = parent.children[lastSegment];
123123

124124
edit = { offset: toModify.offset, length: toModify.length, content: newProperty }
@@ -130,18 +130,18 @@ export function setProperty(text: string, originalPath: JSONPath, value: any, fo
130130
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
131131
}
132132

133-
return withFormatting(text, edit, formattingOptions);
133+
return withFormatting(text, edit, options);
134134
} else {
135-
throw new Error(`Can not ${value === void 0 ? 'remove' : (isArrayInsertion ? 'insert' : 'modify')} Array index ${insertIndex} as length is not sufficient`);
135+
throw new Error(`Can not ${value === void 0 ? 'remove' : (options.isArrayInsertion ? 'insert' : 'modify')} Array index ${insertIndex} as length is not sufficient`);
136136
}
137137
} else {
138138
throw new Error(`Can not add ${typeof lastSegment !== 'number' ? 'index' : 'property'} to parent of type ${parent.type}`);
139139
}
140140
}
141141

142-
function withFormatting(text: string, edit: Edit, formattingOptions: FormattingOptions): Edit[] {
143-
if (formattingOptions.inPlace) {
144-
return [{ ...edit }]
142+
function withFormatting(text: string, edit: Edit, options: ModificationOptions): Edit[] {
143+
if (!options.formattingOptions) {
144+
return [edit]
145145
}
146146
// apply the edit
147147
let newText = applyEdit(text, edit);
@@ -158,7 +158,7 @@ function withFormatting(text: string, edit: Edit, formattingOptions: FormattingO
158158
}
159159
}
160160

161-
let edits = format(newText, { offset: begin, length: end - begin }, formattingOptions);
161+
let edits = format(newText, { offset: begin, length: end - begin }, options.formattingOptions);
162162

163163
// apply the formatting edits and track the begin and end offsets of the changes
164164
for (let i = edits.length - 1; i >= 0; i--) {

src/main.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,6 @@ export interface FormattingOptions {
322322
* The default 'end of line' character. If not set, '\n' is used as default.
323323
*/
324324
eol?: string;
325-
/**
326-
* If true, changes within {@function format} will not be formatted and their original formatting will be preserved.
327-
* Useful for cutting down on computational time for large files.
328-
*/
329-
inPlace?: boolean;
330325
}
331326

332327
/**
@@ -350,9 +345,9 @@ export function format(documentText: string, range: Range | undefined, options:
350345
*/
351346
export interface ModificationOptions {
352347
/**
353-
* Formatting options
348+
* Formatting options. If undefined, the newly inserted code will be inserted unformatted.
354349
*/
355-
formattingOptions: FormattingOptions;
350+
formattingOptions?: FormattingOptions;
356351
/**
357352
* Default false. If `JSONPath` refers to an index of an array and {@property isArrayInsertion} is `true`, then
358353
* {@function modify} will insert a new item at that location instead of overwriting its contents.
@@ -380,7 +375,7 @@ export interface ModificationOptions {
380375
* To apply edits to an input, you can use `applyEdits`.
381376
*/
382377
export function modify(text: string, path: JSONPath, value: any, options: ModificationOptions): Edit[] {
383-
return edit.setProperty(text, path, value, options.formattingOptions, options.getInsertionIndex, options.isArrayInsertion);
378+
return edit.setProperty(text, path, value, options);
384379
}
385380

386381
/**

0 commit comments

Comments
 (0)