diff --git a/extensions/configuration-editing/package.json b/extensions/configuration-editing/package.json index 3a3d08d6c68..365b9aae8f7 100644 --- a/extensions/configuration-editing/package.json +++ b/extensions/configuration-editing/package.json @@ -18,7 +18,8 @@ "watch": "gulp watch-extension:configuration-editing" }, "dependencies": { - "jsonc-parser": "^0.3.1" + "jsonc-parser": "^0.3.1", + "vscode-nls": "^2.0.1" }, "contributes": { "jsonValidation": [ diff --git a/extensions/configuration-editing/src/extension.ts b/extensions/configuration-editing/src/extension.ts index 5d636d95abc..8983c3fcbfb 100644 --- a/extensions/configuration-editing/src/extension.ts +++ b/extensions/configuration-editing/src/extension.ts @@ -8,16 +8,22 @@ import * as vscode from 'vscode'; import { getLocation, visit } from 'jsonc-parser'; import * as path from 'path'; +import * as nls from 'vscode-nls'; + +const localize = nls.loadMessageBundle(); const decoration = vscode.window.createTextEditorDecorationType({ color: '#b1b1b1' }); -export function activate(context) { +export function activate(context): void { //keybindings.json command-suggestions context.subscriptions.push(registerKeybindingsCompletions()); + //settings.json suggestions + context.subscriptions.push(registerSettingsCompletions()); + // launch.json decorations context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(editor => updateLaunchJsonDecorations(editor), null, context.subscriptions)); context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => { @@ -38,23 +44,50 @@ function registerKeybindingsCompletions(): vscode.Disposable { if (location.path[1] === 'command') { const range = document.getWordRangeAtPosition(position) || new vscode.Range(position, position); - return commands.then(ids => ids.map(id => newCompletionItem(id, range))); + return commands.then(ids => ids.map(id => newCompletionItem(JSON.stringify(id), range))); } } }); } -function newCompletionItem(text: string, range: vscode.Range) { - const item = new vscode.CompletionItem(JSON.stringify(text)); +function registerSettingsCompletions(): vscode.Disposable { + return vscode.languages.registerCompletionItemProvider({ language: 'json', pattern: '**/settings.json' }, { + + provideCompletionItems(document, position, token) { + const completions: vscode.CompletionItem[] = []; + const location = getLocation(document.getText(), document.offsetAt(position)); + + // window.title + if (location.path[0] === 'window.title') { + const range = document.getWordRangeAtPosition(position) || new vscode.Range(position, position); + + completions.push(newCompletionItem('${activeEditorName}', range, localize('activeEditorName', "e.g. myFile.txt"))); + completions.push(newCompletionItem('${activeFilePath}', range, localize('activeFilePath', "e.g. /Users/Development/myProject/myFile.txt"))); + completions.push(newCompletionItem('${rootName}', range, localize('rootName', "e.g. myProject"))); + completions.push(newCompletionItem('${rootPath}', range, localize('rootPath', "e.g. /Users/Development/myProject"))); + completions.push(newCompletionItem('${appName}', range, localize('appName', "e.g. VS Code"))); + completions.push(newCompletionItem('${dirty}', range, localize('dirty', "a dirty indicator if the active editor is dirty"))); + completions.push(newCompletionItem('${separator}', range, localize('separator', "a conditional separator (' - ') that only shows when surrounded by variables with values"))); + } + + return Promise.resolve(completions); + } + }); +} + +function newCompletionItem(text: string, range: vscode.Range, description?: string): vscode.CompletionItem { + const item = new vscode.CompletionItem(text); item.kind = vscode.CompletionItemKind.Value; + item.detail = description; item.textEdit = { range, newText: item.label }; + return item; } -function updateLaunchJsonDecorations(editor: vscode.TextEditor | undefined) { +function updateLaunchJsonDecorations(editor: vscode.TextEditor | undefined): void { if (!editor || path.basename(editor.document.fileName) !== 'launch.json') { return; } @@ -85,5 +118,4 @@ function updateLaunchJsonDecorations(editor: vscode.TextEditor | undefined) { }); editor.setDecorations(decoration, ranges); -} - +} \ No newline at end of file diff --git a/extensions/json/server/src/jsonServerMain.ts b/extensions/json/server/src/jsonServerMain.ts index bb541523362..3719674ec66 100644 --- a/extensions/json/server/src/jsonServerMain.ts +++ b/extensions/json/server/src/jsonServerMain.ts @@ -19,7 +19,6 @@ import Strings = require('./utils/strings'); import { JSONDocument, JSONSchema, LanguageSettings, getLanguageService } from 'vscode-json-languageservice'; import { ProjectJSONContribution } from './jsoncontributions/projectJSONContribution'; import { GlobPatternContribution } from './jsoncontributions/globPatternContribution'; -import { WindowTitleContribution } from './jsoncontributions/windowTitleContribution'; import { FileAssociationContribution } from './jsoncontributions/fileAssociationContribution'; import { getLanguageModelCache } from './languageModelCache'; @@ -129,7 +128,6 @@ let languageService = getLanguageService({ contributions: [ new ProjectJSONContribution(), new GlobPatternContribution(), - new WindowTitleContribution(), filesAssociationContribution ] }); diff --git a/extensions/json/server/src/jsoncontributions/windowTitleContribution.ts b/extensions/json/server/src/jsoncontributions/windowTitleContribution.ts deleted file mode 100644 index 4b091941192..00000000000 --- a/extensions/json/server/src/jsoncontributions/windowTitleContribution.ts +++ /dev/null @@ -1,51 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -'use strict'; - -import { MarkedString } from 'vscode-languageserver'; -import Strings = require('../utils/strings'); -import { JSONWorkerContribution, JSONPath, CompletionsCollector } from 'vscode-json-languageservice'; - -import * as nls from 'vscode-nls'; -const localize = nls.loadMessageBundle(); - -export class WindowTitleContribution implements JSONWorkerContribution { - - constructor() { - } - - private isSettingsFile(resource: string): boolean { - return Strings.endsWith(resource, '/settings.json'); - } - - public collectDefaultCompletions(resource: string, result: CompletionsCollector): Thenable { - return null; - } - - public collectPropertyCompletions(resource: string, location: JSONPath, currentWord: string, addValue: boolean, isLast: boolean, result: CompletionsCollector): Thenable { - return null; - } - - public collectValueCompletions(resource: string, location: JSONPath, currentKey: string, result: CompletionsCollector): Thenable { - return null; - } - - public getInfoContribution(resource: string, location: JSONPath): Thenable { - if (this.isSettingsFile(resource) && location.length === 1 && location[0] === 'window.title') { - return Promise.resolve([ - MarkedString.fromPlainText(localize('windowTitle.description', "Controls the window title based on the active editor. Variables are substituted based on the context:")), - MarkedString.fromPlainText(localize('windowTitle.activeEditorName', "${activeEditorName}: e.g. myFile.txt")), - MarkedString.fromPlainText(localize('windowTitle.activeFilePath', "${activeFilePath}: e.g. /Users/Development/myProject/myFile.txt")), - MarkedString.fromPlainText(localize('windowTitle.rootName', "${rootName}: e.g. myProject")), - MarkedString.fromPlainText(localize('windowTitle.rootPath', "${rootPath}: e.g. /Users/Development/myProject")), - MarkedString.fromPlainText(localize('windowTitle.appName', "${appName}: e.g. VS Code")), - MarkedString.fromPlainText(localize('windowTitle.dirty', "${dirty}: a dirty indicator if the active editor is dirty")), - MarkedString.fromPlainText(localize('windowTitle.separator', "${separator}: a conditional separator (\" - \") that only shows when surrounded by variables with values")) - ]); - } - - return null; - } -} \ No newline at end of file