當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript json.findNodeAtLocation函數代碼示例

本文整理匯總了TypeScript中vs/base/common/json.findNodeAtLocation函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript findNodeAtLocation函數的具體用法?TypeScript findNodeAtLocation怎麽用?TypeScript findNodeAtLocation使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了findNodeAtLocation函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: assertNodeAtLocation

function assertNodeAtLocation(input: Node, segments: Segment[], expected: any) {
	let actual = findNodeAtLocation(input, segments);
	assert.deepEqual(actual ? getNodeValue(actual) : void 0, expected);
}
開發者ID:m-khosravi,項目名稱:vscode,代碼行數:4,代碼來源:json.test.ts

示例3: 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.findNodeAtLocation函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。