support when suggestions for keybindings and views as well, fyi @bpasero

This commit is contained in:
Johannes Rieken 2021-02-24 16:14:59 +01:00
parent 0f6eda5348
commit 48d47c8715

View file

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { getLocation, parse, visit } from 'jsonc-parser';
import { getLocation, JSONPath, parse, visit } from 'jsonc-parser';
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import { SettingsDocument } from './settingsDocumentHelper';
@ -142,8 +142,21 @@ vscode.languages.registerDocumentSymbolProvider({ pattern: '**/launch.json', lan
function registerContextKeyCompletions(): vscode.Disposable {
type ContextKeyInfo = { key: string, type?: string, description?: string };
const paths = new Map<vscode.DocumentFilter, JSONPath[]>([
[{ language: 'jsonc', pattern: '**/keybindings.json' }, [
['*', 'when']
]],
[{ language: 'json', pattern: '**/package.json' }, [
['contributes', 'menus', '*', '*', 'when'],
['contributes', 'views', '*', '*', 'when'],
['contributes', 'keybindings', '*', 'when'],
['contributes', 'keybindings', 'when'],
]]
]);
return vscode.languages.registerCompletionItemProvider(
[{ language: 'json', pattern: '**/package.json' }, { language: 'jsonc', pattern: '**/keybindings.json' }],
[...paths.keys()],
{
async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
@ -153,7 +166,17 @@ function registerContextKeyCompletions(): vscode.Disposable {
return;
}
if (!location.matches(['*', 'when']) && !location.matches(['contributes', 'menus', '*', '*', 'when'])) {
let isValidLocation = false;
for (const [key, value] of paths) {
if (vscode.languages.match(key, document)) {
if (value.some(location.matches.bind(location))) {
isValidLocation = true;
break;
}
}
}
if (!isValidLocation) {
return;
}