From 34ee2970158a6caa3beaeaa90bd83396797b3735 Mon Sep 17 00:00:00 2001 From: isidor Date: Fri, 14 Oct 2016 14:43:53 +0200 Subject: [PATCH] launch.json decorations: use json parser for better parsing --- .../configuration-editing/src/extension.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/extensions/configuration-editing/src/extension.ts b/extensions/configuration-editing/src/extension.ts index d9c15cdbb64..8e44525175b 100644 --- a/extensions/configuration-editing/src/extension.ts +++ b/extensions/configuration-editing/src/extension.ts @@ -6,12 +6,11 @@ 'use strict'; import * as vscode from 'vscode'; -import { getLocation } from 'jsonc-parser'; +import { getLocation, visit } from 'jsonc-parser'; import * as path from 'path'; const decoration = vscode.window.createTextEditorDecorationType({ - color: '#b1b1b1', - isWholeLine: true + color: '#b1b1b1' }); export function activate(context) { @@ -51,12 +50,20 @@ function updateLaunchJsonDecorations(editor: vscode.TextEditor) { } const ranges = []; - for (let i = 0; i < editor.document.lineCount; i++) { - const line = editor.document.lineAt(i); - if (line.text.indexOf('\"version\"') >= 0 || line.text.indexOf('\"type\"') >= 0 || line.text.indexOf('\"request\"') >= 0) { - ranges.push(new vscode.Range(line.range.start, line.range.start)); + let addPropertyAndValue = false; + visit(editor.document.getText(), { + onObjectProperty: (property, offset, length) => { + addPropertyAndValue = property === 'version' || property === 'type' || property === 'request'; + if (addPropertyAndValue) { + ranges.push(new vscode.Range(editor.document.positionAt(offset), editor.document.positionAt(offset + length))); + } + }, + onLiteralValue: (value, offset, length) => { + if (addPropertyAndValue) { + ranges.push(new vscode.Range(editor.document.positionAt(offset), editor.document.positionAt(offset + length))); + } } - } + }); editor.setDecorations(decoration, ranges); } @@ -70,4 +77,4 @@ function newCompletionItem(text: string, range: vscode.Range, documentation?: st newText: item.label }; return item; -} \ No newline at end of file +} -- GitLab