上一篇:使用Theia——建立語言支持html
Theia能夠經過多種不一樣的方式進行擴展。命令容許packages提供能夠被其它包調用的惟一命令,還能夠向這些命令添加快捷鍵和上下文,使得它們只能在某些特定的條件下被調用(如窗口獲取焦點、當前選項等)。java
要將命令添加到Theia,必須實現CommandContribution類,如:編程
java-commands.tsapi
@injectable() export class JavaCommandContribution implements CommandContribution { ... registerCommands(commands: CommandRegistry): void { commands.registerCommand(SHOW_JAVA_REFERENCES, { execute: (uri: string, position: Position, locations: Location[]) => commands.executeCommand(SHOW_REFERENCES.id, uri, position, locations) }); commands.registerCommand(APPLY_WORKSPACE_EDIT, { execute: (changes: WorkspaceEdit) => !!this.workspace.applyEdit && this.workspace.applyEdit(changes) }); } }
export default new ContainerModule(bind => { bind(CommandContribution).to(JavaCommandContribution).inSingletonScope(); ... });
負責註冊和執行命令的類是CommandRegistry,經過get commandIds() api能夠獲取命令列表。app
@injectable() export class EditorKeybindingContribution implements KeybindingContribution { constructor( @inject(EditorKeybindingContext) protected readonly editorKeybindingContext: EditorKeybindingContext ) { } registerKeybindings(registry: KeybindingRegistry): void { [ { command: 'editor.close', context: this.editorKeybindingContext, keybinding: "alt+w" }, { command: 'editor.close.all', context: this.editorKeybindingContext, keybinding: "alt+shift+w" } ].forEach(binding => { registry.registerKeybinding(binding); }); } }
@injectable() export class EditorKeybindingContext implements KeybindingContext { constructor( @inject(EditorManager) protected readonly editorService: EditorManager) { } id = 'editor.keybinding.context'; isEnabled(arg?: Keybinding) { return this.editorService && !!this.editorService.activeEditor; } }
export declare type Keystroke = { first: Key, modifiers?: Modifier[] };
Modifier是平臺無關的,因此Modifier.M1在OS X上是Command而在Windows/Linux上是CTRL。Key字符串常量定義在keys.ts中。frontend
export default new ContainerModule(bind => { ... bind(CommandContribution).to(EditorCommandHandlers); bind(EditorKeybindingContext).toSelf().inSingletonScope(); bind(KeybindingContext).toDynamicValue(context => context.container.get(EditorKeybindingContext)); bind(KeybindingContribution).to(EditorKeybindingContribution); });