mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-15 04:41:00 +01:00
commands - use CommandRegistry when it is just a command
This commit is contained in:
@@ -19,4 +19,30 @@ suite('Command Tests', function () {
|
||||
// assert.throws(() => CommandsRegistry.registerCommand('foo', () => { }));
|
||||
// });
|
||||
|
||||
});
|
||||
test('command with description', function() {
|
||||
|
||||
CommandsRegistry.registerCommand('test', function (accessor, args) {
|
||||
assert.ok(typeof args === 'string');
|
||||
});
|
||||
|
||||
CommandsRegistry.registerCommand('test2', function (accessor, args) {
|
||||
assert.ok(typeof args === 'string');
|
||||
});
|
||||
|
||||
CommandsRegistry.registerCommand('test3', {
|
||||
handler: function (accessor, args) {
|
||||
return true;
|
||||
},
|
||||
description: {
|
||||
description: 'a command',
|
||||
args: [{ name: 'value', constraint: Number }]
|
||||
}
|
||||
});
|
||||
|
||||
CommandsRegistry.getCommands()['test'].handler.apply(undefined, [undefined, 'string']);
|
||||
CommandsRegistry.getCommands()['test2'].handler.apply(undefined, [undefined, 'string']);
|
||||
assert.throws(() => CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 'string']));
|
||||
assert.equal(CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 1]), true);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
@@ -21,7 +21,6 @@ import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegi
|
||||
import {IStatusbarService} from 'vs/platform/statusbar/common/statusbar';
|
||||
import {IMessageService} from 'vs/platform/message/common/message';
|
||||
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
|
||||
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
|
||||
import Event, {Emitter, debounceEvent} from 'vs/base/common/event';
|
||||
|
||||
let KEYBINDING_CONTEXT_ATTR = 'data-keybinding-context';
|
||||
@@ -408,14 +407,8 @@ export abstract class KeybindingService extends AbstractKeybindingService implem
|
||||
}
|
||||
}
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: SET_CONTEXT_COMMAND_ID,
|
||||
handler: (accessor:ServicesAccessor, contextKey:any, contextValue:any) => {
|
||||
accessor.get(IKeybindingService).createKey(String(contextKey), contextValue);
|
||||
},
|
||||
weight: 0,
|
||||
primary: undefined,
|
||||
when: null
|
||||
CommandsRegistry.registerCommand(SET_CONTEXT_COMMAND_ID, function (accessor, contextKey: any, contextValue: any) {
|
||||
accessor.get(IKeybindingService).createKey(String(contextKey), contextValue);
|
||||
});
|
||||
|
||||
class ScopedKeybindingService extends AbstractKeybindingService {
|
||||
|
||||
@@ -1,55 +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 * as assert from 'assert';
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
|
||||
|
||||
suite('Keybinding Registry', () => {
|
||||
|
||||
test('command with description', function() {
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: 'test',
|
||||
when: undefined,
|
||||
primary: undefined,
|
||||
weight: 0,
|
||||
handler: function(accessor, args) {
|
||||
assert.ok(typeof args === 'string');
|
||||
}
|
||||
});
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: 'test2',
|
||||
when: undefined,
|
||||
primary: undefined,
|
||||
weight: 0,
|
||||
handler: function(accessor, args) {
|
||||
assert.ok(typeof args === 'string');
|
||||
}
|
||||
});
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: 'test3',
|
||||
when: undefined,
|
||||
primary: undefined,
|
||||
weight: 0,
|
||||
description: {
|
||||
description: 'a command',
|
||||
args: [{ name: 'value', constraint: Number }]
|
||||
},
|
||||
handler: function(accessor, args) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
CommandsRegistry.getCommands()['test'].handler.apply(undefined, [undefined, 'string']);
|
||||
CommandsRegistry.getCommands()['test2'].handler.apply(undefined, [undefined, 'string']);
|
||||
assert.throws(() => CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 'string']));
|
||||
assert.equal(CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 1]), true);
|
||||
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,6 @@
|
||||
'use strict';
|
||||
|
||||
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {ICommandService, CommandsRegistry, ICommandHandlerDescription} from 'vs/platform/commands/common/commands';
|
||||
import {TPromise} from 'vs/base/common/winjs.base';
|
||||
import {ExtHostContext, ExtHostCommandsShape} from './extHostProtocol';
|
||||
@@ -37,31 +36,25 @@ export class MainThreadCommands {
|
||||
|
||||
// --- command doc
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: '_generateCommandsDocumentation',
|
||||
handler: function(accessor) {
|
||||
return accessor.get(IThreadService).get(ExtHostContext.ExtHostCommands).$getContributedCommandHandlerDescriptions().then(result => {
|
||||
CommandsRegistry.registerCommand('_generateCommandsDocumentation', function (accessor) {
|
||||
return accessor.get(IThreadService).get(ExtHostContext.ExtHostCommands).$getContributedCommandHandlerDescriptions().then(result => {
|
||||
|
||||
// add local commands
|
||||
const commands = CommandsRegistry.getCommands();
|
||||
for (let id in commands) {
|
||||
let {description} = commands[id];
|
||||
if (description) {
|
||||
result[id] = description;
|
||||
}
|
||||
// add local commands
|
||||
const commands = CommandsRegistry.getCommands();
|
||||
for (let id in commands) {
|
||||
let {description} = commands[id];
|
||||
if (description) {
|
||||
result[id] = description;
|
||||
}
|
||||
}
|
||||
|
||||
// print all as markdown
|
||||
const all: string[] = [];
|
||||
for (let id in result) {
|
||||
all.push('`' + id + '` - ' + _generateMarkdown(result[id]));
|
||||
}
|
||||
console.log(all.join('\n'));
|
||||
});
|
||||
},
|
||||
when: undefined,
|
||||
weight: KeybindingsRegistry.WEIGHT.builtinExtension(0),
|
||||
primary: undefined
|
||||
// print all as markdown
|
||||
const all: string[] = [];
|
||||
for (let id in result) {
|
||||
all.push('`' + id + '` - ' + _generateMarkdown(result[id]));
|
||||
}
|
||||
console.log(all.join('\n'));
|
||||
});
|
||||
});
|
||||
|
||||
function _generateMarkdown(description: string | ICommandHandlerDescription): string {
|
||||
|
||||
@@ -22,7 +22,7 @@ import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
|
||||
import {IQuickOpenService, IPickOpenEntry} from 'vs/workbench/services/quickopen/common/quickOpenService';
|
||||
import {KeyMod} from 'vs/base/common/keyCodes';
|
||||
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
|
||||
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
|
||||
import * as browser from 'vs/base/browser/browser';
|
||||
|
||||
@@ -451,57 +451,39 @@ export class CloseMessagesAction extends Action {
|
||||
|
||||
// --- commands
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: '_workbench.ipc',
|
||||
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
|
||||
handler(accessor: ServicesAccessor, ipcMessage: string, ipcArgs: any[]) {
|
||||
if (ipcMessage && Array.isArray(ipcArgs)) {
|
||||
ipc.send(ipcMessage, ...ipcArgs);
|
||||
} else {
|
||||
ipc.send(ipcMessage);
|
||||
}
|
||||
},
|
||||
when: undefined,
|
||||
primary: undefined
|
||||
CommandsRegistry.registerCommand('_workbench.ipc', function (accessor: ServicesAccessor, ipcMessage: string, ipcArgs: any[]) {
|
||||
if (ipcMessage && Array.isArray(ipcArgs)) {
|
||||
ipc.send(ipcMessage, ...ipcArgs);
|
||||
} else {
|
||||
ipc.send(ipcMessage);
|
||||
}
|
||||
});
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: '_workbench.diff',
|
||||
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
|
||||
handler(accessor: ServicesAccessor, args: [URI, URI, string]) {
|
||||
CommandsRegistry.registerCommand('_workbench.diff', function (accessor: ServicesAccessor, args: [URI, URI, string]) {
|
||||
|
||||
const editorService = accessor.get(IWorkbenchEditorService);
|
||||
let [left, right, label] = args;
|
||||
const editorService = accessor.get(IWorkbenchEditorService);
|
||||
let [left, right, label] = args;
|
||||
|
||||
if (!label) {
|
||||
label = nls.localize('diffLeftRightLabel', "{0} ⟷ {1}", left.toString(true), right.toString(true));
|
||||
}
|
||||
if (!label) {
|
||||
label = nls.localize('diffLeftRightLabel', "{0} ⟷ {1}", left.toString(true), right.toString(true));
|
||||
}
|
||||
|
||||
return TPromise.join([editorService.createInput({ resource: left }), editorService.createInput({ resource: right })]).then(inputs => {
|
||||
const [left, right] = inputs;
|
||||
return TPromise.join([editorService.createInput({ resource: left }), editorService.createInput({ resource: right })]).then(inputs => {
|
||||
const [left, right] = inputs;
|
||||
|
||||
const diff = new DiffEditorInput(label, undefined, <EditorInput>left, <EditorInput>right);
|
||||
return editorService.openEditor(diff);
|
||||
}).then(() => {
|
||||
return void 0;
|
||||
});
|
||||
},
|
||||
when: undefined,
|
||||
primary: undefined
|
||||
const diff = new DiffEditorInput(label, undefined, <EditorInput>left, <EditorInput>right);
|
||||
return editorService.openEditor(diff);
|
||||
}).then(() => {
|
||||
return void 0;
|
||||
});
|
||||
});
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: '_workbench.open',
|
||||
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
|
||||
handler(accessor: ServicesAccessor, args: [URI, number]) {
|
||||
CommandsRegistry.registerCommand('_workbench.open', function (accessor: ServicesAccessor, args: [URI, number]) {
|
||||
|
||||
const editorService = accessor.get(IWorkbenchEditorService);
|
||||
let [resource, column] = args;
|
||||
const editorService = accessor.get(IWorkbenchEditorService);
|
||||
let [resource, column] = args;
|
||||
|
||||
return editorService.openEditor({ resource }, column).then(() => {
|
||||
return void 0;
|
||||
});
|
||||
},
|
||||
when: undefined,
|
||||
primary: undefined
|
||||
return editorService.openEditor({ resource }, column).then(() => {
|
||||
return void 0;
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@
|
||||
'use strict';
|
||||
|
||||
import {localize} from 'vs/nls';
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
|
||||
import {IInstantiationService, ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
|
||||
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
|
||||
import URI from 'vs/base/common/uri';
|
||||
@@ -26,19 +26,13 @@ import {SyncDescriptor} from 'vs/platform/instantiation/common/descriptors';
|
||||
|
||||
// --- Register Commands
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: '_workbench.previewHtml',
|
||||
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
|
||||
handler(accessor: ServicesAccessor, resource: URI | string, position?: EditorPosition, label?: string) {
|
||||
CommandsRegistry.registerCommand('_workbench.previewHtml', function (accessor: ServicesAccessor, resource: URI | string, position?: EditorPosition, label?: string) {
|
||||
|
||||
let uri = resource instanceof URI ? resource : URI.parse(resource);
|
||||
label = label || uri.fsPath;
|
||||
let input = accessor.get(IInstantiationService).createInstance(HtmlInput, label, '', uri);
|
||||
let uri = resource instanceof URI ? resource : URI.parse(resource);
|
||||
label = label || uri.fsPath;
|
||||
let input = accessor.get(IInstantiationService).createInstance(HtmlInput, label, '', uri);
|
||||
|
||||
return accessor.get(IWorkbenchEditorService)
|
||||
.openEditor(input, { pinned: true }, position)
|
||||
.then(editor => true);
|
||||
},
|
||||
when: undefined,
|
||||
primary: undefined
|
||||
return accessor.get(IWorkbenchEditorService)
|
||||
.openEditor(input, { pinned: true }, position)
|
||||
.then(editor => true);
|
||||
});
|
||||
|
||||
@@ -10,7 +10,7 @@ import {TPromise} from 'vs/base/common/winjs.base';
|
||||
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
|
||||
import {addDisposableListener, addClass} from 'vs/base/browser/dom';
|
||||
import {isLightTheme, isDarkTheme} from 'vs/platform/theme/common/themes';
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
|
||||
|
||||
declare interface WebviewElement extends HTMLElement {
|
||||
src: string;
|
||||
@@ -26,12 +26,8 @@ declare interface WebviewElement extends HTMLElement {
|
||||
closeDevTools(): any;
|
||||
}
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: '_webview.openDevTools',
|
||||
when: null,
|
||||
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
|
||||
primary: null,
|
||||
handler() {
|
||||
CommandsRegistry.registerCommand('_webview.openDevTools',
|
||||
function () {
|
||||
const elements = document.querySelectorAll('webview.ready');
|
||||
for (let i = 0; i < elements.length; i++) {
|
||||
try {
|
||||
@@ -40,8 +36,7 @@ KeybindingsRegistry.registerCommandDesc({
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
type ApiThemeClassName = 'vscode-light' | 'vscode-dark' | 'vscode-high-contrast';
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ import {OutputService} from 'vs/workbench/parts/output/browser/outputServices';
|
||||
import {ToggleOutputAction} from 'vs/workbench/parts/output/browser/outputActions';
|
||||
import {OUTPUT_MIME, OUTPUT_MODE_ID, OUTPUT_PANEL_ID, IOutputService} from 'vs/workbench/parts/output/common/output';
|
||||
import panel = require('vs/workbench/browser/panel');
|
||||
import {KEYBINDING_CONTEXT_EDITOR_FOCUS, KEYBINDING_CONTEXT_EDITOR_LANGUAGE_ID} from 'vs/editor/common/editorCommon';
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {KEYBINDING_CONTEXT_EDITOR_LANGUAGE_ID} from 'vs/editor/common/editorCommon';
|
||||
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
|
||||
import {KbExpr} from 'vs/platform/keybinding/common/keybinding';
|
||||
import {MenuRegistry} from 'vs/platform/actions/browser/menuService';
|
||||
|
||||
@@ -56,14 +56,8 @@ actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(ToggleOutputActi
|
||||
{
|
||||
const id = 'editor.action.clearoutput';
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id,
|
||||
primary: null,
|
||||
weight: KeybindingsRegistry.WEIGHT.editorContrib(),
|
||||
when: KbExpr.has(KEYBINDING_CONTEXT_EDITOR_FOCUS),
|
||||
handler(accessor) {
|
||||
accessor.get(IOutputService).getActiveChannel().clear();
|
||||
}
|
||||
CommandsRegistry.registerCommand(id, function handler(accessor) {
|
||||
accessor.get(IOutputService).getActiveChannel().clear();
|
||||
});
|
||||
|
||||
MenuRegistry.addCommand({
|
||||
|
||||
@@ -5,36 +5,24 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
import * as nls from 'vs/nls';
|
||||
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
|
||||
import {TPromise} from 'vs/base/common/winjs.base';
|
||||
import {Range} from 'vs/editor/common/core/range';
|
||||
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
|
||||
import {Action} from 'vs/base/common/actions';
|
||||
import WorkbenchContributions = require('vs/workbench/common/contributions');
|
||||
import {IWorkspace} from 'vs/platform/workspace/common/workspace';
|
||||
import paths = require('vs/base/common/paths');
|
||||
import URI from 'vs/base/common/uri';
|
||||
|
||||
import Platform = require('vs/platform/platform');
|
||||
import WorkbenchActionRegistry = require('vs/workbench/common/actionRegistry');
|
||||
import {IFileStat} from 'vs/platform/files/common/files';
|
||||
import {TextModelWithTokens} from 'vs/editor/common/model/textModelWithTokens';
|
||||
import {TextModel} from 'vs/editor/common/model/textModel';
|
||||
import {IModeService} from 'vs/editor/common/services/modeService';
|
||||
import pfs = require('vs/base/node/pfs');
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
|
||||
import {IInstantiationService, ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
|
||||
import {IThemeService} from 'vs/workbench/services/themes/common/themeService';
|
||||
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
|
||||
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
|
||||
import {asFileEditorInput} from 'vs/workbench/common/editor';
|
||||
|
||||
|
||||
interface Data {
|
||||
c: string; // content
|
||||
t: string; // token
|
||||
r: { [theme:string]:string}
|
||||
r: { [theme: string]: string };
|
||||
}
|
||||
|
||||
class Snapper {
|
||||
@@ -103,7 +91,7 @@ class Snapper {
|
||||
return id.substring(startIdx + part.length, id.length - 5);
|
||||
}
|
||||
return void 0;
|
||||
}
|
||||
};
|
||||
|
||||
return this.themeService.getThemes().then(themeDatas => {
|
||||
let defaultThemes = themeDatas.filter(themeData => !!getThemeName(themeData.id));
|
||||
@@ -114,7 +102,7 @@ class Snapper {
|
||||
let testNode = this.getTestNode(themeId);
|
||||
let themeName = getThemeName(themeId);
|
||||
data.forEach(entry => {
|
||||
entry.r[themeName] = this.getMatchedCSSRule(testNode, entry.t) + ' ' + this.getStyle(testNode, entry.t)
|
||||
entry.r[themeName] = this.getMatchedCSSRule(testNode, entry.t) + ' ' + this.getStyle(testNode, entry.t);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -147,37 +135,30 @@ class Snapper {
|
||||
}
|
||||
}
|
||||
|
||||
KeybindingsRegistry.registerCommandDesc({
|
||||
id: '_workbench.captureSyntaxTokens',
|
||||
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
|
||||
handler(accessor: ServicesAccessor, resource:URI) {
|
||||
CommandsRegistry.registerCommand('_workbench.captureSyntaxTokens', function (accessor: ServicesAccessor, resource: URI) {
|
||||
|
||||
let process = (resource: URI) => {
|
||||
let filePath = resource.fsPath;
|
||||
let fileName = paths.basename(filePath);
|
||||
let snapper = accessor.get(IInstantiationService).createInstance(Snapper);
|
||||
let process = (resource: URI) => {
|
||||
let filePath = resource.fsPath;
|
||||
let fileName = paths.basename(filePath);
|
||||
let snapper = accessor.get(IInstantiationService).createInstance(Snapper);
|
||||
|
||||
return pfs.readFile(filePath).then(content => {
|
||||
return snapper.captureSyntaxTokens(fileName, content.toString());
|
||||
return pfs.readFile(filePath).then(content => {
|
||||
return snapper.captureSyntaxTokens(fileName, content.toString());
|
||||
});
|
||||
};
|
||||
|
||||
if (!resource) {
|
||||
let editorService = accessor.get(IWorkbenchEditorService);
|
||||
let fileEditorInput = asFileEditorInput(editorService.getActiveEditorInput());
|
||||
if (fileEditorInput) {
|
||||
process(fileEditorInput.getResource()).then(result => {
|
||||
console.log(result);
|
||||
});
|
||||
};
|
||||
|
||||
if (!resource) {
|
||||
let editorService = accessor.get(IWorkbenchEditorService);
|
||||
let fileEditorInput = asFileEditorInput(editorService.getActiveEditorInput());
|
||||
if (fileEditorInput) {
|
||||
process(fileEditorInput.getResource()).then(result => {
|
||||
console.log(result);
|
||||
});
|
||||
} else {
|
||||
console.log('No file editor active');
|
||||
}
|
||||
} else {
|
||||
return process(resource);
|
||||
console.log('No file editor active');
|
||||
}
|
||||
|
||||
},
|
||||
when: undefined,
|
||||
primary: undefined
|
||||
} else {
|
||||
return process(resource);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user