|
5 | 5 | 'use strict'; |
6 | 6 |
|
7 | 7 | import EditorCommon = require('vs/editor/common/editorCommon'); |
| 8 | +import {onUnexpectedError, illegalArgument} from 'vs/base/common/errors'; |
| 9 | +import URI from 'vs/base/common/uri'; |
| 10 | +import {Position} from 'vs/editor/common/core/position'; |
8 | 11 | import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation'; |
9 | 12 | import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry'; |
| 13 | +import {IModelService} from 'vs/editor/common/services/modelService'; |
10 | 14 | import {Registry} from 'vs/platform/platform'; |
11 | | -import Errors = require('vs/base/common/errors'); |
12 | 15 | import {KeybindingsRegistry,ICommandDescriptor} from 'vs/platform/keybinding/common/keybindingsRegistry'; |
13 | 16 | import config = require('vs/editor/common/config/config'); |
14 | 17 | import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; |
@@ -84,6 +87,38 @@ export module CommonEditorRegistry { |
84 | 87 |
|
85 | 88 | KeybindingsRegistry.registerCommandDesc(commandDesc); |
86 | 89 | } |
| 90 | + |
| 91 | + export function registerLanguageCommand(id: string, handler: (accessor: ServicesAccessor, args: { [n: string]: any }) => any) { |
| 92 | + KeybindingsRegistry.registerCommandDesc({ |
| 93 | + id, |
| 94 | + handler(accessor, args: any[]) { |
| 95 | + if (args && args.length > 1 || typeof args[0] !== 'object') { |
| 96 | + throw illegalArgument(); |
| 97 | + } |
| 98 | + return handler(accessor, args && args[0]); |
| 99 | + }, |
| 100 | + weight: KeybindingsRegistry.WEIGHT.editorContrib(), |
| 101 | + primary: undefined, |
| 102 | + context: undefined, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + export function registerDefaultLanguageCommand(id: string, handler: (model: EditorCommon.IModel, position: EditorCommon.IPosition) => any) { |
| 107 | + registerLanguageCommand(id, function(accessor, args) { |
| 108 | + |
| 109 | + const {resource, position} = args; |
| 110 | + if (!URI.isURI(resource) || !Position.isIPosition(position)) { |
| 111 | + throw illegalArgument(); |
| 112 | + } |
| 113 | + |
| 114 | + const model = accessor.get(IModelService).getModel(resource); |
| 115 | + if (!model) { |
| 116 | + throw illegalArgument(); |
| 117 | + } |
| 118 | + |
| 119 | + return handler(model, position); |
| 120 | + }); |
| 121 | + } |
87 | 122 | } |
88 | 123 |
|
89 | 124 | class SimpleEditorContributionDescriptor implements EditorCommon.ICommonEditorContributionDescriptor { |
@@ -193,7 +228,7 @@ function triggerEditorActionGlobal(actionId: string, accessor: ServicesAccessor, |
193 | 228 | var action = activeEditor.getAction(actionId); |
194 | 229 | if (action) { |
195 | 230 | accessor.get(ITelemetryService).publicLog('editorActionInvoked', {name: action.label} ); |
196 | | - action.run().done(null, Errors.onUnexpectedError); |
| 231 | + action.run().done(null, onUnexpectedError); |
197 | 232 | } |
198 | 233 | return; |
199 | 234 | } |
|
0 commit comments