Skip to content

Commit

Permalink
expose context key info command, add first version of completion item…
Browse files Browse the repository at this point in the history
… provider for package.json and keybindings.json files, #9303
  • Loading branch information
jrieken committed Feb 17, 2021
1 parent 44dbd18 commit 1307001
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
40 changes: 40 additions & 0 deletions extensions/configuration-editing/src/configurationEditingMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function activate(context: vscode.ExtensionContext): void {

// task.json variable suggestions
context.subscriptions.push(registerVariableCompletions('**/tasks.json'));

// keybindings.json/package.json context key suggestions
context.subscriptions.push(registerContextKeyCompletions());
}

function registerSettingsCompletions(): vscode.Disposable {
Expand Down Expand Up @@ -136,3 +139,40 @@ vscode.languages.registerDocumentSymbolProvider({ pattern: '**/launch.json', lan
return result;
}
}, { label: 'Launch Targets' });

function registerContextKeyCompletions(): vscode.Disposable {
type ContextKeyInfo = { key: string, type?: string, description?: string };
return vscode.languages.registerCompletionItemProvider(
[{ language: 'json', pattern: '**/package.json' }, { language: 'jsonc', pattern: '**/keybindings.json' }],
{
async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {

const replacing = document.getWordRangeAtPosition(position, /[\w.-\d]/);
const inserting = replacing?.with(undefined, position);
if (!replacing || !inserting) {
return;
}

const location = getLocation(document.getText(), document.offsetAt(position));
if (!location.matches(['*', 'when']) && !location.matches(['contributes', 'menus', '*', '*', 'when'])) {
return;
}

const data = await vscode.commands.executeCommand<ContextKeyInfo[]>('getContextKeyInfo');
if (token.isCancellationRequested || !data) {
return;
}

const result = new vscode.CompletionList();
for (const item of data) {
const completion = new vscode.CompletionItem(item.key, vscode.CompletionItemKind.Constant);
completion.detail = item.type;
completion.range = { replacing, inserting };
completion.documentation = item.description;
result.items.push(completion);
}
return result;
}
}
);
}
12 changes: 12 additions & 0 deletions src/vs/platform/contextkey/browser/contextKeyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Iterable } from 'vs/base/common/iterator';
import { IDisposable, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
import { TernarySearchTree } from 'vs/base/common/map';
import { distinct } from 'vs/base/common/objects';
import { localize } from 'vs/nls';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContext, IContextKey, IContextKeyChangeEvent, IContextKeyService, IContextKeyServiceTarget, IReadableSet, SET_CONTEXT_COMMAND_ID, ContextKeyExpression, RawContextKey, ContextKeyInfo } from 'vs/platform/contextkey/common/contextkey';
Expand Down Expand Up @@ -578,6 +579,17 @@ CommandsRegistry.registerCommand(SET_CONTEXT_COMMAND_ID, function (accessor, con
accessor.get(IContextKeyService).createKey(String(contextKey), contextValue);
});

CommandsRegistry.registerCommand({
id: 'getContextKeyInfo',
handler() {
return [...RawContextKey.all()].sort((a, b) => a.key.localeCompare(b.key));
},
description: {
description: localize('getContextKeyInfo', "A command that returns information about context keys"),
args: []
}
});

CommandsRegistry.registerCommand('_generateContextKeyInfo', function () {
const result: ContextKeyInfo[] = [];
const seen = new Set<string>();
Expand Down
4 changes: 2 additions & 2 deletions src/vs/platform/contextkey/common/contextkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ export class ContextKeyOrExpr implements IContextKeyExpression {

export interface ContextKeyInfo {
readonly key: string;
readonly type: string;
readonly type?: string;
readonly description?: string;
}

Expand All @@ -1281,7 +1281,7 @@ export class RawContextKey<T> extends ContextKeyDefinedExpr {
if (typeof metaOrHide === 'object') {
RawContextKey._info.push({ ...metaOrHide, key });
} else if (metaOrHide !== true) {
RawContextKey._info.push({ key, description: metaOrHide, type: typeof defaultValue });
RawContextKey._info.push({ key, description: metaOrHide, type: defaultValue !== null && defaultValue !== undefined ? typeof defaultValue : undefined });
}
}

Expand Down

0 comments on commit 1307001

Please sign in to comment.