• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript json.parseTree函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中vs/base/common/json.parseTree函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parseTree函数的具体用法?TypeScript parseTree怎么用?TypeScript parseTree使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了parseTree函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: setProperty

export function setProperty(text: string, segments: Segment[], value: any, formattingOptions: FormattingOptions, getInsertionIndex?: (properties: string[]) => number) : Edit[] {
	let lastSegment = segments.pop();
	if (typeof lastSegment !== 'string') {
		throw new Error('Last segment must be a property name');
	}

	let errors: ParseError[] = [];
	let node = parseTree(text, errors);
	if (segments.length > 0) {
		node = findNodeAtLocation(node, segments);
		if (node === void 0) {
			throw new Error('Cannot find object');
		}
	}
	if (node && node.type === 'object') {
		let existing = findNodeAtLocation(node, [ lastSegment ]);
		if (existing !== void 0) {
			if (value === void 0) { // delete
				let propertyIndex = node.children.indexOf(existing.parent);
				let removeBegin : number;
				let removeEnd = existing.parent.offset + existing.parent.length;
				if (propertyIndex > 0) {
					// remove the comma of the previous node
					let previous = node.children[propertyIndex - 1];
					removeBegin = previous.offset + previous.length;
				} else {
					removeBegin = node.offset + 1;
					if (node.children.length > 1) {
						// remove the comma of the next node
						let next = node.children[1];
						removeEnd = next.offset;
					}
				}
				return withFormatting(text, { offset: removeBegin, length: removeEnd - removeBegin, content: '' }, formattingOptions);
			} else {
				// set value of existing property
				return [{ offset: existing.offset, length: existing.length, content: JSON.stringify(value) }];
			}
		} else {
			if (value === void 0) { // delete
				throw new Error(`Property ${lastSegment} does not exist.`);
			}
			let newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`;
			let index = getInsertionIndex ? getInsertionIndex(node.children.map(p => p.children[0].value)) : node.children.length;
			let edit: Edit;
			if (index > 0) {
				let previous = node.children[index - 1];
				edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty};
			} else if (node.children.length === 0) {
				edit = { offset: node.offset + 1, length: 0, content: newProperty};
			} else {
				edit = { offset: node.offset + 1, length: 0, content: newProperty + ','};
			}
			return withFormatting(text, edit, formattingOptions);
		}
	} else {
		throw new Error('Path does not reference an object');
	}
}
开发者ID:Buildsoftwaresphere,项目名称:vscode,代码行数:59,代码来源:jsonEdit.ts


示例2: test

	test('tree: find location', () => {
		let root = parseTree('{ "key1": { "key11": [ "val111", "val112" ] }, "key2": [ { "key21": false, "key22": 221 }, null, [{}] ] }');
		assertNodeAtLocation(root, ['key1'], { key11: ['val111', 'val112'] });
		assertNodeAtLocation(root, ['key1', 'key11'], ['val111', 'val112']);
		assertNodeAtLocation(root, ['key1', 'key11', 0], 'val111');
		assertNodeAtLocation(root, ['key1', 'key11', 1], 'val112');
		assertNodeAtLocation(root, ['key1', 'key11', 2], void 0);
		assertNodeAtLocation(root, ['key2', 0, 'key21'], false);
		assertNodeAtLocation(root, ['key2', 0, 'key22'], 221);
		assertNodeAtLocation(root, ['key2', 1], null);
		assertNodeAtLocation(root, ['key2', 2], [{}]);
		assertNodeAtLocation(root, ['key2', 2, 0], {});
	});
开发者ID:m-khosravi,项目名称:vscode,代码行数:13,代码来源:json.test.ts


示例3: assertTree

function assertTree(input: string, expected: any, expectedErrors: number[] = []): void {
	var errors: ParseError[] = [];
	var actual = parseTree(input, errors);

	assert.deepEqual(errors.map(e => e.error, expected), expectedErrors);
	let checkParent = (node: Node) => {
		if (node.children) {
			for (let child of node.children) {
				assert.equal(node, child.parent);
				delete child.parent; // delete to avoid recursion in deep equal
				checkParent(child);
			}
		}
	};
	checkParent(actual);

	assert.deepEqual(actual, expected);
}
开发者ID:m-khosravi,项目名称:vscode,代码行数:18,代码来源:json.test.ts


示例4: setProperty

export function setProperty(text: string, path: JSONPath, value: any, formattingOptions: FormattingOptions, getInsertionIndex?: (properties: string[]) => number): Edit[] {
	let errors: ParseError[] = [];
	let root = parseTree(text, errors);
	let parent: Node = void 0;

	let lastSegment: Segment = void 0;
	while (path.length > 0) {
		lastSegment = path.pop();
		parent = findNodeAtLocation(root, path);
		if (parent === void 0 && value !== void 0) {
			if (typeof lastSegment === 'string') {
				value = { [lastSegment]: value };
			} else {
				value = [value];
			}
		} else {
			break;
		}
	}

	if (!parent) {
		// empty document
		if (value === void 0) { // delete
			throw new Error('Can not delete in empty document');
		}
		return withFormatting(text, { offset: root ? root.offset : 0, length: root ? root.length : 0, content: JSON.stringify(value) }, formattingOptions);
	} else if (parent.type === 'object' && typeof lastSegment === 'string') {
		let existing = findNodeAtLocation(parent, [lastSegment]);
		if (existing !== void 0) {
			if (value === void 0) { // delete
				let propertyIndex = parent.children.indexOf(existing.parent);
				let removeBegin: number;
				let removeEnd = existing.parent.offset + existing.parent.length;
				if (propertyIndex > 0) {
					// remove the comma of the previous node
					let previous = parent.children[propertyIndex - 1];
					removeBegin = previous.offset + previous.length;
				} else {
					removeBegin = parent.offset + 1;
					if (parent.children.length > 1) {
						// remove the comma of the next node
						let next = parent.children[1];
						removeEnd = next.offset;
					}
				}
				return withFormatting(text, { offset: removeBegin, length: removeEnd - removeBegin, content: '' }, formattingOptions);
			} else {
				// set value of existing property
				return withFormatting(text, { offset: existing.offset, length: existing.length, content: JSON.stringify(value) }, formattingOptions);
			}
		} else {
			if (value === void 0) { // delete
				return []; // property does not exist, nothing to do
			}
			let newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`;
			let index = getInsertionIndex ? getInsertionIndex(parent.children.map(p => p.children[0].value)) : parent.children.length;
			let edit: Edit;
			if (index > 0) {
				let previous = parent.children[index - 1];
				edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
			} else if (parent.children.length === 0) {
				edit = { offset: parent.offset + 1, length: 0, content: newProperty };
			} else {
				edit = { offset: parent.offset + 1, length: 0, content: newProperty + ',' };
			}
			return withFormatting(text, edit, formattingOptions);
		}
	} else if (parent.type === 'array' && typeof lastSegment === 'number') {
		let insertIndex = lastSegment;
		if (insertIndex === -1) {
			// Insert
			let newProperty = `${JSON.stringify(value)}`;
			let edit: Edit;
			if (parent.children.length === 0) {
				edit = { offset: parent.offset + 1, length: 0, content: newProperty };
			} else {
				let previous = parent.children[parent.children.length - 1];
				edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
			}
			return withFormatting(text, edit, formattingOptions);
		} else {
			if (value === void 0 && parent.children.length >= 0) {
				//Removal
				let removalIndex = lastSegment;
				let toRemove = parent.children[removalIndex];
				let edit: Edit;
				if (parent.children.length === 1) {
					// only item
					edit = { offset: parent.offset + 1, length: parent.length - 2, content: '' };
				} else if (parent.children.length - 1 === removalIndex) {
					// last item
					let previous = parent.children[removalIndex - 1];
					let offset = previous.offset + previous.length;
					let parentEndOffset = parent.offset + parent.length;
					edit = { offset, length: parentEndOffset - 2 - offset, content: '' };
				} else {
					edit = { offset: toRemove.offset, length: parent.children[removalIndex + 1].offset - toRemove.offset, content: '' };
				}
				return withFormatting(text, edit, formattingOptions);
			} else {
//.........这里部分代码省略.........
开发者ID:ramesius,项目名称:vscode,代码行数:101,代码来源:jsonEdit.ts



注:本文中的vs/base/common/json.parseTree函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript jsonEdit.removeProperty函数代码示例发布时间:2022-05-25
下一篇:
TypeScript json.parse函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap