标签:log context oid container 有一个 register bin 可选参数 特定
上一篇:使用Theia——创建语言支持
Theia可以通过多种不同的方式进行扩展。命令允许packages提供可以被其它包调用的唯一命令,还可以向这些命令添加快捷键和上下文,使得它们只能在某些特定的条件下被调用(如窗口获取焦点、当前选项等)。
要将命令添加到Theia,必须实现CommandContribution类,如:
java-commands.ts
@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可以获取命令列表。
@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中。
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); });
标签:log context oid container 有一个 register bin 可选参数 特定
原文地址:https://www.cnblogs.com/jaxu/p/12165229.html