88import * as vscode from 'vscode' ;
99import { getLocation , visit } from 'jsonc-parser' ;
1010import * as path from 'path' ;
11+ import * as nls from 'vscode-nls' ;
12+
13+ const localize = nls . loadMessageBundle ( ) ;
1114
1215const decoration = vscode . window . createTextEditorDecorationType ( {
1316 color : '#b1b1b1'
1417} ) ;
1518
16- export function activate ( context ) {
19+ export function activate ( context ) : void {
1720
1821 //keybindings.json command-suggestions
1922 context . subscriptions . push ( registerKeybindingsCompletions ( ) ) ;
2023
24+ //settings.json suggestions
25+ context . subscriptions . push ( registerSettingsCompletions ( ) ) ;
26+
2127 // launch.json decorations
2228 context . subscriptions . push ( vscode . window . onDidChangeActiveTextEditor ( editor => updateLaunchJsonDecorations ( editor ) , null , context . subscriptions ) ) ;
2329 context . subscriptions . push ( vscode . workspace . onDidChangeTextDocument ( event => {
@@ -38,23 +44,50 @@ function registerKeybindingsCompletions(): vscode.Disposable {
3844 if ( location . path [ 1 ] === 'command' ) {
3945
4046 const range = document . getWordRangeAtPosition ( position ) || new vscode . Range ( position , position ) ;
41- return commands . then ( ids => ids . map ( id => newCompletionItem ( id , range ) ) ) ;
47+ return commands . then ( ids => ids . map ( id => newCompletionItem ( JSON . stringify ( id ) , range ) ) ) ;
4248 }
4349 }
4450 } ) ;
4551}
4652
47- function newCompletionItem ( text : string , range : vscode . Range ) {
48- const item = new vscode . CompletionItem ( JSON . stringify ( text ) ) ;
53+ function registerSettingsCompletions ( ) : vscode . Disposable {
54+ return vscode . languages . registerCompletionItemProvider ( { language : 'json' , pattern : '**/settings.json' } , {
55+
56+ provideCompletionItems ( document , position , token ) {
57+ const completions : vscode . CompletionItem [ ] = [ ] ;
58+ const location = getLocation ( document . getText ( ) , document . offsetAt ( position ) ) ;
59+
60+ // window.title
61+ if ( location . path [ 0 ] === 'window.title' ) {
62+ const range = document . getWordRangeAtPosition ( position ) || new vscode . Range ( position , position ) ;
63+
64+ completions . push ( newCompletionItem ( '${activeEditorName}' , range , localize ( 'activeEditorName' , "e.g. myFile.txt" ) ) ) ;
65+ completions . push ( newCompletionItem ( '${activeFilePath}' , range , localize ( 'activeFilePath' , "e.g. /Users/Development/myProject/myFile.txt" ) ) ) ;
66+ completions . push ( newCompletionItem ( '${rootName}' , range , localize ( 'rootName' , "e.g. myProject" ) ) ) ;
67+ completions . push ( newCompletionItem ( '${rootPath}' , range , localize ( 'rootPath' , "e.g. /Users/Development/myProject" ) ) ) ;
68+ completions . push ( newCompletionItem ( '${appName}' , range , localize ( 'appName' , "e.g. VS Code" ) ) ) ;
69+ completions . push ( newCompletionItem ( '${dirty}' , range , localize ( 'dirty' , "a dirty indicator if the active editor is dirty" ) ) ) ;
70+ completions . push ( newCompletionItem ( '${separator}' , range , localize ( 'separator' , "a conditional separator (' - ') that only shows when surrounded by variables with values" ) ) ) ;
71+ }
72+
73+ return Promise . resolve ( completions ) ;
74+ }
75+ } ) ;
76+ }
77+
78+ function newCompletionItem ( text : string , range : vscode . Range , description ?: string ) : vscode . CompletionItem {
79+ const item = new vscode . CompletionItem ( text ) ;
4980 item . kind = vscode . CompletionItemKind . Value ;
81+ item . detail = description ;
5082 item . textEdit = {
5183 range,
5284 newText : item . label
5385 } ;
86+
5487 return item ;
5588}
5689
57- function updateLaunchJsonDecorations ( editor : vscode . TextEditor | undefined ) {
90+ function updateLaunchJsonDecorations ( editor : vscode . TextEditor | undefined ) : void {
5891 if ( ! editor || path . basename ( editor . document . fileName ) !== 'launch.json' ) {
5992 return ;
6093 }
@@ -85,5 +118,4 @@ function updateLaunchJsonDecorations(editor: vscode.TextEditor | undefined) {
85118 } ) ;
86119
87120 editor . setDecorations ( decoration , ranges ) ;
88- }
89-
121+ }
0 commit comments