How to use the jsonc-parser.getLocation function in jsonc-parser

To help you get started, we’ve selected a few jsonc-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / vscode-extension-samples / tree-view-sample / src / jsonOutline.ts View on Github external
this.editor.edit(editBuilder => {
						const path = json.getLocation(this.text, offset).path
						let propertyNode = json.findNodeAtLocation(this.tree, path);
						if (propertyNode.parent.type !== 'array') {
							propertyNode = propertyNode.parent.children[0];
						}
						const range = new vscode.Range(this.editor.document.positionAt(propertyNode.offset), this.editor.document.positionAt(propertyNode.offset + propertyNode.length));
						editBuilder.replace(range, `"${value}"`);
						setTimeout(() => {
							this.parseTree();
							this.refresh(offset);
						}, 100)
					});
				}
github microsoft / vscode-extension-samples / tree-view-sample / src / jsonOutline.ts View on Github external
getChildren(offset?: number): Thenable {
		if (offset) {
			const path = json.getLocation(this.text, offset).path
			const node = json.findNodeAtLocation(this.tree, path);
			return Promise.resolve(this.getChildrenOffsets(node));
		} else {
			return Promise.resolve(this.tree ? this.getChildrenOffsets(this.tree) : []);
		}
	}
github Pure-D / code-d / src / json-contributions.ts View on Github external
public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable {
		let currentWord = this.getCurrentWord(document, position);
		let overwriteRange = null;
		let items: vscode.CompletionItem[] = [];

		let offset = document.offsetAt(position);
		let location = getLocation(document.getText(), offset);

		let node = location.previousNode;
		if (node && node.offset <= offset && offset <= node.offset + node.length && (node.type === 'property' || node.type === 'string' || node.type === 'number' || node.type === 'boolean' || node.type === 'null')) {
			overwriteRange = new vscode.Range(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
		} else {
			overwriteRange = new vscode.Range(document.positionAt(offset - currentWord.length), position);
		}

		let proposed: { [key: string]: boolean } = {};
		let collector: ISuggestionsCollector = {
			add: (suggestion: vscode.CompletionItem) => {
				if (!proposed[suggestion.label]) {
					proposed[suggestion.label] = true;
					if (overwriteRange) {
						suggestion.range = overwriteRange;
					}
github OmniSharp / omnisharp-vscode / src / features / json / jsonContributions.ts View on Github external
public provideCompletionItems(document: TextDocument, position: Position, token: CancellationToken): Thenable {
        let currentWord = this.getCurrentWord(document, position);
        let overwriteRange = null;
        let items: CompletionItem[] = [];
        let isIncomplete = false;

        let offset = document.offsetAt(position);
        let location = getLocation(document.getText(), offset);

        let node = location.previousNode;
        if (node && node.offset <= offset && offset <= node.offset + node.length && (node.type === 'property' || node.type === 'string' || node.type === 'number' || node.type === 'boolean' || node.type === 'null')) {
            overwriteRange = new Range(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
        } else {
            overwriteRange = new Range(document.positionAt(offset - currentWord.length), position);
        }

        let proposed: { [key: string]: boolean } = {};
        let collector: ISuggestionsCollector = {
            add: (suggestion: CompletionItem) => {
                if (!proposed[suggestion.label]) {
                    proposed[suggestion.label] = true;
                    if (overwriteRange) {
                        suggestion.textEdit = TextEdit.replace(overwriteRange, suggestion.insertText);
                    }
github Pure-D / code-d / src / json-contributions.ts View on Github external
public provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable {
		let offset = document.offsetAt(position);
		let location = getLocation(document.getText(), offset);
		let node = location.previousNode;
		if (node && node.offset <= offset && offset <= node.offset + node.length) {
			let promise = this.jsonContribution.getInfoContribution(document.fileName, location);
			if (promise) {
				return promise.then(htmlContent => {
					let range = new vscode.Range(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
					let result: vscode.Hover = {
						contents: htmlContent,
						range: range
					};
					return result;
				});
			}
		}
		return null;
	}
github DaGhostman / vscode-tree-view / src / providers / json.ts View on Github external
public getChildren(offset?: string): Thenable {
        if (offset) {
            const p = json.getLocation(this.text, parseInt(offset, 10)).path;
            const node = json.findNodeAtLocation(this.tree, p);
            return Promise.resolve(this.getChildrenOffsets(node));
        } else {
            return Promise.resolve(this.tree ? this.getChildrenOffsets(this.tree) : []);
        }
    }
github microsoft / vscode-azure-iot-toolkit / src / jsonCompletionItemProvider.ts View on Github external
public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position): vscode.ProviderResult {
        const location: Location = getLocation(document.getText(), document.offsetAt(position));
        const range: vscode.Range = document.getWordRangeAtPosition(position) || new vscode.Range(position, position);
        if (location.path[0] === "moduleContent" && location.path[1] === "$edgeHub" && location.path[2] === "properties.desired" && location.path[3] === "routes") {
            const json = parse(document.getText());
            const modules: any = ((json.moduleContent.$edgeAgent || {})["properties.desired"] || {}).modules || {};
            const moduleIds: string[] = Object.keys(modules);

            const routeCompletionItem: vscode.CompletionItem = new vscode.CompletionItem("edgeRoute");
            routeCompletionItem.filterText = "\"edgeRoute\"";
            routeCompletionItem.kind = vscode.CompletionItemKind.Snippet;
            routeCompletionItem.detail = "Route for the Edge Hub. Route name is used as the key for the route. To delete a route, set the route name as null";
            routeCompletionItem.range = range;
            routeCompletionItem.insertText = new vscode.SnippetString(this.getRouteSnippetString(moduleIds));
            return [routeCompletionItem];
        }

        if (location.path[0] === "moduleContent" && location.path[1] === "$edgeAgent" && location.path[2] === "properties.desired" && location.path[3] === "modules") {
github microsoft / vscode-extension-samples / tree-view-sample / src / jsonOutline.ts View on Github external
getTreeItem(offset: number): vscode.TreeItem {
		const path = json.getLocation(this.text, offset).path
		const valueNode = json.findNodeAtLocation(this.tree, path);
		if (valueNode) {
			let hasChildren = valueNode.type === 'object' || valueNode.type === 'array';
			let treeItem: vscode.TreeItem = new vscode.TreeItem(this.getLabel(valueNode), hasChildren ? valueNode.type === 'object' ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None);
			treeItem.command = {
				command: 'extension.openJsonSelection',
				title: '',
				arguments: [new vscode.Range(this.editor.document.positionAt(valueNode.offset), this.editor.document.positionAt(valueNode.offset + valueNode.length))]
			};
			treeItem.iconPath = this.getIcon(valueNode);
			treeItem.contextValue = valueNode.type;
			return treeItem;
		}
		return null;
	}
github DaGhostman / vscode-tree-view / src / providers / json.ts View on Github external
public getTreeItem(offset: string): vscode.TreeItem {
        const p = json.getLocation(this.text, parseInt(offset, 10)).path;
        const valueNode = json.findNodeAtLocation(this.tree, p);
        if (valueNode) {
            const hasChildren = valueNode.type === "object" || valueNode.type === "array";
            let treeItem: vscode.TreeItem = new vscode.TreeItem(
                this.getLabel(valueNode),
                hasChildren ? vscode.TreeItemCollapsibleState.Collapsed :
                    vscode.TreeItemCollapsibleState.None,
            );

            treeItem.contextValue = valueNode.type;
            if (!hasChildren) {
                const start = vscode.window.activeTextEditor.document.positionAt(valueNode.offset);
                const end = new vscode.Position(start.line, start.character + valueNode.length);

                treeItem = Provider.addItemCommand(
                    treeItem,
github DaGhostman / vscode-tree-view / src / providers / json.ts View on Github external
private getChildrenOffsets(node: json.Node): string[] {
        const offsets = [];
        for (const child of node.children) {
            const childPath = json.getLocation(this.text, child.offset).path;
            const childNode = json.findNodeAtLocation(this.tree, childPath);
            if (childNode) {
                offsets.push(childNode.offset.toString());
            }
        }
        return offsets;
    }