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

Implement missing functionality for call hierarchy API #9681

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<a name="breaking_changes_1.16.0">[Breaking Changes:](#breaking_changes_1.16.0)</a>

- [monaco] upgraded to monaco 0.23.0 including replacement of `quickOpen` API (0.20.x) with `quickInput` API (0.23.x) [#9154](https://github.com/eclipse-theia/theia/pull/9154)
- [call-hierarchy] `CurrentEditorAccess` is deprecated. Use the version implemented in the `editor` package instead. The services in `call-hierarchy` that previously used the local `CurrentEditorAccess` no longer do. [#9681](https://github.com/eclipse-theia/theia/pull/9681)
- [workspace] `WorkspaceCommandContribution.addFolderToWorkspace` no longer accepts `undefined`. `WorkspaceService.addRoot` now accepts a URI or a URI[]. [#9684](https://github.com/eclipse-theia/theia/pull/9684)

## v1.15.0 - 6/30/2021
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import { injectable, inject } from '@theia/core/shared/inversify';
import { MenuModelRegistry, Command, CommandRegistry } from '@theia/core/lib/common';
import { AbstractViewContribution, OpenViewArguments, KeybindingRegistry } from '@theia/core/lib/browser';
import { EDITOR_CONTEXT_MENU } from '@theia/editor/lib/browser';
import { EDITOR_CONTEXT_MENU, CurrentEditorAccess } from '@theia/editor/lib/browser';
import { CallHierarchyTreeWidget } from './callhierarchy-tree/callhierarchy-tree-widget';
import { CALLHIERARCHY_ID } from './callhierarchy';
import { CurrentEditorAccess } from './current-editor-access';
import { CallHierarchyServiceProvider } from './callhierarchy-service';
import URI from '@theia/core/lib/common/uri';

Expand Down Expand Up @@ -52,15 +51,13 @@ export class CallHierarchyContribution extends AbstractViewContribution<CallHier
}

protected isCallHierarchyAvailable(): boolean {
const selection = this.editorAccess.getSelection();
const languageId = this.editorAccess.getLanguageId();
const { selection, languageId } = this.editorAccess;
return !!selection && !!languageId && !!this.callHierarchyServiceProvider.get(languageId, new URI(selection.uri));
}

async openView(args?: Partial<OpenViewArguments>): Promise<CallHierarchyTreeWidget> {
const widget = await super.openView(args);
const selection = this.editorAccess.getSelection();
const languageId = this.editorAccess.getLanguageId();
const { selection, languageId } = this.editorAccess;
widget.initializeModel(selection, languageId);
return widget;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { WidgetFactory, bindViewContribution } from '@theia/core/lib/browser';
import { CALLHIERARCHY_ID } from './callhierarchy';
import { createHierarchyTreeWidget } from './callhierarchy-tree';
import { CurrentEditorAccess } from './current-editor-access';

import { ContainerModule } from '@theia/core/shared/inversify';

import '../../src/browser/style/index.css';
Expand Down
3 changes: 3 additions & 0 deletions packages/callhierarchy/src/browser/current-editor-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { injectable, inject } from '@theia/core/shared/inversify';
import { EditorManager, TextEditor } from '@theia/editor/lib/browser';
import { Location } from '@theia/core/shared/vscode-languageserver-types';

/**
* @deprecated since 1.15.0. Import from `@theia/editor` instead.
*/
@injectable()
export class CurrentEditorAccess {

Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-ext-vscode/compile.tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
},
{
"path": "../terminal/compile.tsconfig.json"
},
{
"path": "../callhierarchy/compile.tsconfig.json"
}
]
}
1 change: 1 addition & 0 deletions packages/plugin-ext-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.15.0",
"description": "Theia - Plugin Extension for VsCode",
"dependencies": {
"@theia/callhierarchy": "1.15.0",
"@theia/core": "1.15.0",
"@theia/editor": "1.15.0",
"@theia/filesystem": "1.15.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Command, CommandContribution, CommandRegistry, environment, isOSX } from '@theia/core';
import { Command, CommandContribution, CommandRegistry, environment, isOSX, CancellationTokenSource } from '@theia/core';
import {
ApplicationShell,
CommonCommands,
Expand Down Expand Up @@ -45,7 +45,7 @@ import {
} from '@theia/plugin-ext/lib/common/plugin-api-rpc-model';
import { DocumentsMainImpl } from '@theia/plugin-ext/lib/main/browser/documents-main';
import { createUntitledURI } from '@theia/plugin-ext/lib/main/browser/editor/untitled-resource';
import { isUriComponents, toDocumentSymbol } from '@theia/plugin-ext/lib/plugin/type-converters';
import { isUriComponents, toDocumentSymbol, toPosition } from '@theia/plugin-ext/lib/plugin/type-converters';
import { ViewColumn } from '@theia/plugin-ext/lib/plugin/types-impl';
import { WorkspaceCommands } from '@theia/workspace/lib/browser';
import { WorkspaceService, WorkspaceInput } from '@theia/workspace/lib/browser/workspace-service';
Expand All @@ -65,6 +65,14 @@ import { FILE_NAVIGATOR_ID, FileNavigatorWidget } from '@theia/navigator/lib/bro
import { SelectableTreeNode } from '@theia/core/lib/browser/tree/tree-selection';
import { UriComponents } from '@theia/plugin-ext/lib/common/uri-components';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { CallHierarchyServiceProvider, CallHierarchyService } from '@theia/callhierarchy/lib/browser';
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
import {
fromCallHierarchyCalleeToModelCallHierarchyOutgoingCall,
fromCallHierarchyCallerToModelCallHierarchyIncomingCall,
fromDefinition,
toDefinition
} from '@theia/plugin-ext/lib/main/browser/callhierarchy/callhierarchy-type-converters';

export namespace VscodeCommands {
export const OPEN: Command = {
Expand Down Expand Up @@ -116,6 +124,10 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
protected readonly pluginServer: PluginServer;
@inject(FileService)
protected readonly fileService: FileService;
@inject(CallHierarchyServiceProvider)
protected readonly callHierarchyProvider: CallHierarchyServiceProvider;
@inject(MonacoTextModelService)
protected readonly textModelService: MonacoTextModelService;

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(VscodeCommands.OPEN, {
Expand Down Expand Up @@ -526,26 +538,57 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
id: 'vscode.prepareCallHierarchy'
},
{
execute: ((resource: URI, position: Position) =>
commands.executeCommand<CallHierarchyItem[]>('_executePrepareCallHierarchy', monaco.Uri.from(resource), position))
execute: async (resource: URI, position: Position): Promise<CallHierarchyItem[]> => {
const provider = await this.getCallHierarchyServiceForUri(resource);
const definition = await provider?.getRootDefinition(
resource.fsPath,
toPosition(position),
new CancellationTokenSource().token
);
if (definition) {
const item = fromDefinition(definition);
return [item];
};
return [];
}
}
);
commands.registerCommand(
{
id: 'vscode.provideIncomingCalls'
},
{
execute: ((item: CallHierarchyItem) =>
commands.executeCommand<CallHierarchyIncomingCall[]>('_executeProvideIncomingCalls', { item }))
}
execute: async (item: CallHierarchyItem): Promise<CallHierarchyIncomingCall[]> => {
const resource = URI.from(item.uri);
const provider = await this.getCallHierarchyServiceForUri(resource);
const incomingCalls = await provider?.getCallers(
toDefinition(item),
new CancellationTokenSource().token,
);
if (incomingCalls) {
return incomingCalls.map(fromCallHierarchyCallerToModelCallHierarchyIncomingCall);
}
return [];
},
},
);
commands.registerCommand(
{
id: 'vscode.provideOutgoingCalls'
},
{
execute: ((item: CallHierarchyItem) =>
commands.executeCommand<CallHierarchyOutgoingCall[]>('_executeProvideOutgoingCalls', { item }))
execute: async (item: CallHierarchyItem): Promise<CallHierarchyOutgoingCall[]> => {
const resource = URI.from(item.uri);
const provider = await this.getCallHierarchyServiceForUri(resource);
const outgoingCalls = await provider?.getCallees?.(
toDefinition(item),
new CancellationTokenSource().token,
);
if (outgoingCalls) {
return outgoingCalls.map(fromCallHierarchyCalleeToModelCallHierarchyOutgoingCall);
}
return [];
}
}
);

Expand Down Expand Up @@ -626,4 +669,12 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
}
});
}

protected async getCallHierarchyServiceForUri(resource: URI): Promise<CallHierarchyService | undefined> {
const reference = await this.textModelService.createModelReference(resource);
const uri = new TheiaURI(resource);
const languageId = reference.object.languageId;
reference.dispose();
return this.callHierarchyProvider.get(languageId, uri);
}
}
1 change: 1 addition & 0 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ export interface LanguagesExt {
$provideDocumentRangeSemanticTokens(handle: number, resource: UriComponents, range: Range, token: CancellationToken): Promise<BinaryBuffer | null>;
$provideRootDefinition(handle: number, resource: UriComponents, location: Position, token: CancellationToken): Promise<CallHierarchyDefinition | undefined>;
$provideCallers(handle: number, definition: CallHierarchyDefinition, token: CancellationToken): Promise<CallHierarchyReference[] | undefined>;
$provideCallees(handle: number, definition: CallHierarchyDefinition, token: CancellationToken): Promise<CallHierarchyReference[] | undefined>;
}

export const LanguagesMainFactory = Symbol('LanguagesMainFactory');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Definition as CallHierarchyDefinition, Caller as CallHierarchyCaller } from '@theia/callhierarchy/lib/browser';
import { Definition as CallHierarchyDefinition, Caller as CallHierarchyCaller, Callee as CallHierarchyCallee } from '@theia/callhierarchy/lib/browser';
import * as model from '../../../common/plugin-api-rpc-model';
import * as rpc from '../../../common/plugin-api-rpc';
import * as callhierarchy from '@theia/core/shared/vscode-languageserver-types';
import { URI } from '@theia/core/shared/vscode-uri';
import { UriComponents } from '../../../common/uri-components';
import { Location } from '@theia/core/shared/vscode-languageserver-types';

export function toUriComponents(uri: string): UriComponents {
return URI.parse(uri);
Expand All @@ -30,15 +29,15 @@ export function fromUriComponents(uri: UriComponents): string {
return URI.revive(uri).toString();
}

export function fromLocation(location: Location): model.Location {
export function fromLocation(location: callhierarchy.Location): model.Location {
return <model.Location>{
uri: URI.parse(location.uri),
range: fromRange(location.range)
};
}

export function toLocation(uri: UriComponents, range: model.Range): Location {
return <Location>{
export function toLocation(uri: UriComponents, range: model.Range): callhierarchy.Location {
return {
uri: URI.revive(uri).toString(),
range: toRange(range)
};
Expand All @@ -54,15 +53,20 @@ export function fromPosition(position: callhierarchy.Position): rpc.Position {
export function fromRange(range: callhierarchy.Range): model.Range {
const { start, end } = range;
return {
startLineNumber: start.line,
startColumn: start.character,
endLineNumber: end.line,
endColumn: end.character
startLineNumber: start.line + 1,
startColumn: start.character + 1,
endLineNumber: end.line + 1,
endColumn: end.character + 1,
};
}

export function toRange(range: model.Range): callhierarchy.Range {
return callhierarchy.Range.create(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn);
return callhierarchy.Range.create(
range.startLineNumber - 1,
range.startColumn - 1,
range.endLineNumber - 1,
range.endColumn - 1,
);
}

export namespace SymbolKindConverter {
Expand Down Expand Up @@ -169,8 +173,29 @@ export function toCaller(caller: model.CallHierarchyReference): CallHierarchyCal
}

export function fromCaller(caller: CallHierarchyCaller): model.CallHierarchyReference {
return <model.CallHierarchyReference>{
return {
callerDefinition: fromDefinition(caller.callerDefinition),
references: caller.references.map(fromRange)
};
}

export function toCallee(callee: model.CallHierarchyReference): CallHierarchyCallee {
return {
calleeDefinition: toDefinition(callee.callerDefinition),
references: callee.references.map(toRange),
};
}

export function fromCallHierarchyCallerToModelCallHierarchyIncomingCall(caller: CallHierarchyCaller): model.CallHierarchyIncomingCall {
return {
from: fromDefinition(caller.callerDefinition),
fromRanges: caller.references.map(fromRange),
};
}

export function fromCallHierarchyCalleeToModelCallHierarchyOutgoingCall(callee: CallHierarchyCallee): model.CallHierarchyOutgoingCall {
return {
to: fromDefinition(callee.calleeDefinition),
fromRanges: callee.references.map(fromRange),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class CommandRegistryMainImpl implements CommandRegistryMain, Disposable
throw new Error(`Command with id '${id}' is not registered.`);
}
try {
return await this.delegate.executeCommand(id, ...args);
return await this.delegate.executeCommand<T>(id, ...args);
} catch (e) {
// Command handler may be not active at the moment so the error must be caught. See https://github.com/eclipse-theia/theia/pull/6687#discussion_r354810079
if ('code' in e && e['code'] === 'NO_ACTIVE_HANDLER') {
Expand Down
24 changes: 16 additions & 8 deletions packages/plugin-ext/src/main/browser/languages-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import * as theia from '@theia/plugin';
import { UriComponents } from '../../common/uri-components';
import { CancellationToken } from '@theia/core/lib/common';
import { LanguageSelector, RelativePattern } from '@theia/callhierarchy/lib/common/language-selector';
import { CallHierarchyService, CallHierarchyServiceProvider, Caller, Definition } from '@theia/callhierarchy/lib/browser';
import { toDefinition, toUriComponents, fromDefinition, fromPosition, toCaller } from './callhierarchy/callhierarchy-type-converters';
import { CallHierarchyService, CallHierarchyServiceProvider, Definition } from '@theia/callhierarchy/lib/browser';
import { toDefinition, toUriComponents, fromDefinition, fromPosition, toCaller, toCallee } from './callhierarchy/callhierarchy-type-converters';
import { Position, DocumentUri } from '@theia/core/shared/vscode-languageserver-types';
import { ObjectIdentifier } from '../../common/object-identifier';
import { mixin } from '../../common/types';
Expand Down Expand Up @@ -782,15 +782,23 @@ export class LanguagesMainImpl implements LanguagesMain, Disposable {
}

if (Array.isArray(result)) {
const callers: Caller[] = [];
for (const item of result) {
callers.push(toCaller(item));
}
return callers;
return result.map(toCaller);
}

return undefined!;
})
}),

getCallees: (definition: Definition, cancellationToken: CancellationToken) => this.proxy.$provideCallees(handle, fromDefinition(definition), cancellationToken)
.then(result => {
if (!result) {
return undefined;
}
if (Array.isArray(result)) {
return result.map(toCallee);
}

return undefined;
}),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export class PluginTree extends TreeImpl {
themeIconId,
resourceUri,
tooltip: item.tooltip,
contextValue: item.contextValue
contextValue: item.contextValue,
command: item.command,
};
const node = this.getNode(item.id);
if (item.collapsibleState !== undefined && item.collapsibleState !== TreeViewItemCollapsibleState.None) {
Expand All @@ -179,7 +180,7 @@ export class PluginTree extends TreeImpl {
parent,
visible: true,
selected: false,
command: item.command
command: item.command,
}, update);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-ext/src/plugin/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ export class DocumentsExtImpl implements DocumentsExt {
if (options.selection) {
const { start, end } = options.selection;
selection = {
startLineNumber: start.line,
startColumn: start.character,
endLineNumber: end.line,
endColumn: end.character
startLineNumber: start.line + 1,
startColumn: start.character + 1,
endLineNumber: end.line + 1,
endColumn: end.character + 1
RomanNikitenko marked this conversation as resolved.
Show resolved Hide resolved
};
}
documentOptions = {
Expand Down
Loading