How to use the jsonc-parser.createScanner 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 Pure-D / code-d / src / json-contributions.ts View on Github external
if (overwriteRange) {
						suggestion.range = overwriteRange;
					}

					items.push(suggestion);
				}
			},
			error: (message: string) => console.error(message),
			log: (message: string) => console.log(message)
		};

		let collectPromise: Thenable = null;

		if (location.isAtPropertyKey) {
			let addValue = !location.previousNode || !location.previousNode.columnOffset && (offset == (location.previousNode.offset + location.previousNode.length));
			let scanner = createScanner(document.getText(), true);
			scanner.setPosition(offset);
			scanner.scan();
			let isLast = scanner.getToken() === SyntaxKind.CloseBraceToken || scanner.getToken() === SyntaxKind.EOF;
			collectPromise = this.jsonContribution.collectPropertySuggestions(document.fileName, location, currentWord, addValue, isLast, collector);
		} else if (location.path.length !== 0)
			collectPromise = this.jsonContribution.collectValueSuggestions(document.fileName, location, collector);

		if (collectPromise) {
			return collectPromise.then(() => {
				if (items.length > 0) {
					return new vscode.CompletionList(items);
				}
				return null;
			});
		}
		return null;
github OmniSharp / atom-json-schema / lib / vscode / plugin / jsonFormatter.ts View on Github external
range = new Range(new Point(0, 0), document.getBuffer().getEndPosition());
        initialIndentLevel = 0;
        rangeOffset = 0;
    }
    let eol = getEOL(document);

    let lineBreak = false;
    let indentLevel = 0;
    let indentValue: string;
    if (options.insertSpaces) {
        indentValue = repeat(' ', options.tabSize);
    } else {
        indentValue = '\t';
    }

    let scanner = Json.createScanner(value, false);

    function newLineAndIndent(): string {
        return eol + repeat(indentValue, initialIndentLevel + indentLevel);
    }
    function scanNext(): Json.SyntaxKind {
        let token = scanner.scan();
        lineBreak = false;
        while (token === Json.SyntaxKind.Trivia || token === Json.SyntaxKind.LineBreakTrivia) {
            lineBreak = lineBreak || (token === Json.SyntaxKind.LineBreakTrivia);
            token = scanner.scan();
        }
        return token;
    }
    let editOperations: { range: TextBuffer.Range, text: string }[] = [];
    function addEdit(text: string, startOffset: number, endOffset: number) {
        if (documentText.substring(startOffset, endOffset) !== text) {
github threadheap / serverless-ide-vscode / packages / language-server / src / language-service / services / completion / helpers.ts View on Github external
export const isInComment = (
	document: TextDocument,
	start: number,
	offset: number
) => {
	const scanner = JSONParser.createScanner(document.getText(), false)
	scanner.setPosition(start)
	let token = scanner.scan()
	while (
		token !== JSONParser.SyntaxKind.EOF &&
		scanner.getTokenOffset() + scanner.getTokenLength() < offset
	) {
		token = scanner.scan()
	}
	return (
		(token === JSONParser.SyntaxKind.LineCommentTrivia ||
			token === JSONParser.SyntaxKind.BlockCommentTrivia) &&
		scanner.getTokenOffset() <= offset
	)
}
github threadheap / serverless-ide-vscode / packages / language-server / src / language-service / services / completion / helpers.ts View on Github external
export const evaluateSeparatorAfter = (
	document: TextDocument,
	offset: number
) => {
	const scanner = JSONParser.createScanner(document.getText(), true)
	scanner.setPosition(offset)
	const token = scanner.scan()
	switch (token) {
		case JSONParser.SyntaxKind.CommaToken:
		case JSONParser.SyntaxKind.CloseBraceToken:
		case JSONParser.SyntaxKind.CloseBracketToken:
		case JSONParser.SyntaxKind.EOF:
			return ""
		default:
			return ""
	}
}
github DonJayamanne / pythonVSCode / src / client / debugger / extension / configuration / launch.json / updaterService.ts View on Github external
public getCursorPositionInConfigurationsArray(document: TextDocument, position: Position): PositionOfCursor | undefined {
        if (this.isConfigurationArrayEmpty(document)) {
            return 'InsideEmptyArray';
        }
        const scanner = createScanner(document.getText(), true);
        scanner.setPosition(document.offsetAt(position));
        const nextToken = scanner.scan();
        if (nextToken === SyntaxKind.CommaToken || nextToken === SyntaxKind.CloseBracketToken) {
            return 'AfterItem';
        }
        if (nextToken === SyntaxKind.OpenBraceToken) {
            return 'BeforeItem';
        }
    }
    public isConfigurationArrayEmpty(document: TextDocument): boolean {
github Huachao / vscode-restclient / src / utils / responseFormatUtility.ts View on Github external
private static jsonPrettify(text: string, indentSize = 2) {
        const scanner = createScanner(text, true);

        let indentLevel = 0;

        function newLineAndIndent() {
            return os.EOL + ' '.repeat(indentLevel * indentSize);
        }

        function scanNext(): [SyntaxKind, string] {
            const token = scanner.scan();
            const offset = scanner.getTokenOffset();
            const length = scanner.getTokenLength();
            const value = text.substr(offset, length);
            return [ token, value ];
        }

        let [firstToken, firstTokenValue] = scanNext();
github microsoft / vscode-json-languageservice / src / services / jsonFolding.ts View on Github external
export function getFoldingRanges(document: TextDocument, context?: FoldingRangesContext): FoldingRange[] {
	let ranges: FoldingRange[] = [];
	let nestingLevels: number[] = [];
	let stack: FoldingRange[] = [];
	let prevStart = -1;
	let scanner = createScanner(document.getText(), false);
	let token = scanner.scan();

	function addRange(range: FoldingRange) {
		ranges.push(range);
		nestingLevels.push(stack.length);
	}

	while (token !== SyntaxKind.EOF) {
		switch (token) {
			case SyntaxKind.OpenBraceToken:
			case SyntaxKind.OpenBracketToken: {
				let startLine = document.positionAt(scanner.getTokenOffset()).line;
				let range = { startLine, endLine: startLine, kind: token === SyntaxKind.OpenBraceToken ? 'object' : 'array' };
				stack.push(range);
				break;
			}
github microsoft / vscode-json-languageservice / src / services / jsonSelectionRanges.ts View on Github external
}
		let current: SelectionRange | undefined = undefined;
		for (let index = result.length - 1; index >= 0; index--) {
			current = SelectionRange.create(result[index], current);
		}
		if (!current) {
			current = SelectionRange.create(Range.create(position, position));
		}
		return current;
	}

	function newRange(start: number, end: number): Range {
		return Range.create(document.positionAt(start), document.positionAt(end));
	}

	const scanner = createScanner(document.getText(), true);

	function getOffsetAfterNextToken(offset: number, expectedToken: SyntaxKind): number {
		scanner.setPosition(offset);
		let token = scanner.scan();
		if (token === expectedToken) {
			return scanner.getTokenOffset() + scanner.getTokenLength();
		}
		return -1;
	}

	return positions.map(getSelectionRange);
}