Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin] support vscode.executeDocumentSymbol command. #6291

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { injectable, inject } from 'inversify';
import { ProtocolToMonacoConverter } from 'monaco-languageclient/lib';
import { Position, Location } from '@theia/languages/lib/browser';
import { Command, CommandContribution } from '@theia/core';
import { Command, CommandContribution, CommandRegistry } from '@theia/core';
import { CommonCommands } from '@theia/core/lib/browser';
import { QuickOpenService } from '@theia/core/lib/browser/quick-open/quick-open-service';
import { QuickOpenItem, QuickOpenMode } from '@theia/core/lib/browser/quick-open/quick-open-model';
Expand Down Expand Up @@ -91,7 +91,10 @@ export namespace MonacoCommands {
export class MonacoEditorCommandHandlers implements CommandContribution {

@inject(MonacoCommandRegistry)
protected readonly registry: MonacoCommandRegistry;
protected readonly monacoCommandRegistry: MonacoCommandRegistry;

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

@inject(ProtocolToMonacoConverter)
protected readonly p2m: ProtocolToMonacoConverter;
Expand All @@ -103,14 +106,36 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
this.registerCommonCommandHandlers();
this.registerEditorCommandHandlers();
this.registerMonacoActionCommands();
this.registerInternalLanguageServiceCommands();
}

protected registerInternalLanguageServiceCommands(): void {
const instantiationService = monaco.services.StaticServices.instantiationService.get();
const monacoCommands = monaco.commands.CommandsRegistry.getCommands();
for (const command of monacoCommands.keys()) {
if (command.startsWith('_execute')) {
this.commandRegistry.registerCommand(
{
id: MonacoCommandRegistry.MONACO_COMMAND_PREFIX + command
},
{
// tslint:disable-next-line:no-any
execute: (...args: any) => instantiationService.invokeFunction(
monacoCommands.get(command)!.handler,
...args
)
}
);
}
}
}

protected registerCommonCommandHandlers(): void {
// tslint:disable-next-line:forin
for (const action in MonacoCommands.COMMON_ACTIONS) {
const command = MonacoCommands.COMMON_ACTIONS[action];
const handler = this.newCommonActionHandler(action);
this.registry.registerHandler(command, handler);
this.monacoCommandRegistry.registerHandler(command, handler);
}
}
protected newCommonActionHandler(action: string): MonacoEditorCommandHandler {
Expand All @@ -121,11 +146,11 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
}

protected registerEditorCommandHandlers(): void {
this.registry.registerHandler(EditorCommands.SHOW_REFERENCES.id, this.newShowReferenceHandler());
this.registry.registerHandler(EditorCommands.CONFIG_INDENTATION.id, this.newConfigIndentationHandler());
this.registry.registerHandler(EditorCommands.CONFIG_EOL.id, this.newConfigEolHandler());
this.registry.registerHandler(EditorCommands.INDENT_USING_SPACES.id, this.newConfigTabSizeHandler(true));
this.registry.registerHandler(EditorCommands.INDENT_USING_TABS.id, this.newConfigTabSizeHandler(false));
this.monacoCommandRegistry.registerHandler(EditorCommands.SHOW_REFERENCES.id, this.newShowReferenceHandler());
this.monacoCommandRegistry.registerHandler(EditorCommands.CONFIG_INDENTATION.id, this.newConfigIndentationHandler());
this.monacoCommandRegistry.registerHandler(EditorCommands.CONFIG_EOL.id, this.newConfigEolHandler());
this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_SPACES.id, this.newConfigTabSizeHandler(true));
this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_TABS.id, this.newConfigTabSizeHandler(false));
}

protected newShowReferenceHandler(): MonacoEditorCommandHandler {
Expand Down Expand Up @@ -241,7 +266,7 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
protected registerMonacoActionCommands(): void {
for (const action of MonacoCommands.ACTIONS.values()) {
const handler = this.newMonacoActionHandler(action);
this.registry.registerCommand(action, handler);
this.monacoCommandRegistry.registerCommand(action, handler);
}
}
protected newMonacoActionHandler(action: MonacoCommand): MonacoEditorCommandHandler {
Expand Down
6 changes: 6 additions & 0 deletions packages/monaco/src/typings/monaco/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

declare module monaco.instantiation {
export interface IInstantiationService {
invokeFunction: (fn: any, ...args: any) => any
}
}

Expand Down Expand Up @@ -273,6 +274,10 @@ declare module monaco.editor {

declare module monaco.commands {

export const CommandsRegistry: {
getCommands(): Map<string, { id: string, handler: (...args: any) => any }>;
};

export interface ICommandEvent {
commandId: string;
}
Expand Down Expand Up @@ -514,6 +519,7 @@ declare module monaco.services {
export const codeEditorService: LazyStaticService<monaco.editor.ICodeEditorService>;
export const configurationService: LazyStaticService<IConfigurationService>;
export const resourcePropertiesService: LazyStaticService<ITextResourcePropertiesService>;
export const instantiationService: LazyStaticService<monaco.instantiation.IInstantiationService>;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable, inject } from 'inversify';
import { CommandContribution, CommandRegistry, Command } from '@theia/core';
import { Command, CommandContribution, CommandRegistry, ResourceProvider } from '@theia/core';
import { ApplicationShell, NavigatableWidget, open, OpenerService, Saveable } from '@theia/core/lib/browser';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
import { ApplicationShellMouseTracker } from '@theia/core/lib/browser/shell/application-shell-mouse-tracker';
import { CommandService } from '@theia/core/lib/common/command';
import TheiaURI from '@theia/core/lib/common/uri';
import URI from 'vscode-uri';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
import { DiffService } from '@theia/workspace/lib/browser/diff-service';
import { EditorManager } from '@theia/editor/lib/browser';
import { TextDocumentShowOptions } from '@theia/plugin-ext/lib/common/plugin-api-rpc-model';
import { DocumentsMainImpl } from '@theia/plugin-ext/lib/main/browser/documents-main';
import { createUntitledResource } from '@theia/plugin-ext/lib/main/browser/editor/untitled-resource';
import { WebviewWidget } from '@theia/plugin-ext/lib/main/browser/webview/webview';
import { ApplicationShell, NavigatableWidget, OpenerService, open, Saveable } from '@theia/core/lib/browser';
import { ResourceProvider } from '@theia/core';
import { fromViewColumn, toDocumentSymbol } from '@theia/plugin-ext/lib/plugin/type-converters';
import { ViewColumn } from '@theia/plugin-ext/lib/plugin/types-impl';
import { TextDocumentShowOptions } from '@theia/plugin-ext/lib/common/plugin-api-rpc-model';
import { fromViewColumn } from '@theia/plugin-ext/lib/plugin/type-converters';
import { WorkspaceCommands } from '@theia/workspace/lib/browser';
import { createUntitledResource } from '@theia/plugin-ext/lib/main/browser/editor/untitled-resource';
import { DocumentsMainImpl } from '@theia/plugin-ext/lib/main/browser/documents-main';
import { ApplicationShellMouseTracker } from '@theia/core/lib/browser/shell/application-shell-mouse-tracker';
import { DiffService } from '@theia/workspace/lib/browser/diff-service';
import { inject, injectable } from 'inversify';
import URI from 'vscode-uri';
benoitf marked this conversation as resolved.
Show resolved Hide resolved

export namespace VscodeCommands {
export const OPEN: Command = {
Expand Down Expand Up @@ -315,6 +314,27 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
* Show Opened File in New Window workbench.action.files.showOpenedFileInNewWindow
* Compare Opened File With workbench.files.action.compareFileWith
*/

// Register built-in language service commands
// see https://code.visualstudio.com/api/references/commands
// tslint:disable: no-any
commands.registerCommand(
{
id: 'vscode.executeDocumentSymbolProvider'
},
{
execute: (resource: URI) => commands.executeCommand('monaco._executeDocumentSymbolProvider',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a question: why not use async/await instead of the callbacks ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I don't understand what you mean. Could you clarify?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback :

 commands.executeCommand(...).then((foo) => {return foo.something() })

vs

async/await:

 const foo = await commands.executeCommand(...)
return foo.something()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. No specific reason but as this is a single expression I find it more readable and it has less code this way.

{ resource: monaco.Uri.parse(resource.toString()) }
).then((value: any) => {
if (!Array.isArray(value) || value === undefined) {
return undefined;
}
return value.map(loc => toDocumentSymbol(loc));
})
}
);
// TODO register other `vscode.execute...` commands.
// see https://github.com/microsoft/vscode/blob/master/src/vs/workbench/api/common/extHostApiCommands.ts
}

private getHtml(body: String): string {
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,17 @@ export function fromDocumentSymbol(info: theia.DocumentSymbol): model.DocumentSy
return result;
}

export function toDocumentSymbol(symbol: model.DocumentSymbol): theia.DocumentSymbol {
return {
name: symbol.name,
detail: symbol.detail,
range: toRange(symbol.range)!,
selectionRange: toRange(symbol.selectionRange)!,
children: symbol.children ? symbol.children.map(toDocumentSymbol) : [],
kind: SymbolKind.toSymbolKind(symbol.kind)
};
}

export function toWorkspaceFolder(folder: model.WorkspaceFolder): theia.WorkspaceFolder {
return {
uri: URI.revive(folder.uri),
Expand Down