move window.title config helper out to extension (for #20049)

This commit is contained in:
Benjamin Pasero
2017-02-07 12:23:58 +01:00
parent 7c8d8e822f
commit bcff7c60e5
4 changed files with 41 additions and 61 deletions

View File

@@ -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);
}
}