Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Oct 10, 2018
1 parent 1aa05b9 commit bd538a7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class TypeHierarchyTreeModel extends TreeModelImpl {
if (languageId && selection) {
const symbol = await this.symbol(languageId, type, selection);
if (symbol) {
this.tree.root = TypeHierarchyTree.Node.create(symbol, type);
this.tree.root = TypeHierarchyTree.RootNode.create(symbol, languageId, type);
}
}
}
Expand Down
101 changes: 85 additions & 16 deletions packages/typehierarchy/src/browser/tree/typehierarchy-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,68 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable } from 'inversify';
import { inject, injectable } from 'inversify';
import { v4 } from 'uuid';
import { TreeImpl, TreeNode, CompositeTreeNode } from '@theia/core/lib/browser/tree/tree';
import { Location } from '@theia/editor/lib/browser/editor';
import { TreeImpl, TreeNode, CompositeTreeNode, ExpandableTreeNode, SelectableTreeNode } from '@theia/core/lib/browser/tree';
import { DocumentSymbolExt } from '@theia/languages/lib/browser/typehierarchy/typehierarchy-protocol';
import { TypeHierarchyFeature } from '../../../../../node_modules/@theia/languages/src/browser/typehierarchy/typehierarchy-feature';
import { TypeHierarchyFeature } from '@theia/languages/lib/browser/typehierarchy/typehierarchy-feature';
import { TypeHierarchyService } from '@theia/typehierarchy/lib/browser/typehierarchy-service';

@injectable()
export class TypeHierarchyTree extends TreeImpl {

@inject(TypeHierarchyService)
protected readonly typeHierarchyService: TypeHierarchyService;

async resolveChildren(parent: CompositeTreeNode): Promise<TreeNode[]> {
if (TypeHierarchyTree.Node.is(parent)) {
if (parent.resolved) {
return parent.children.slice();
await this.ensureResolved(parent);
if (parent.children.length === 0) {
delete parent.children;
delete parent.expanded;
return [];
}
return parent.children.slice();
}
return [];
}

protected get languageId(): string | undefined {
if (TypeHierarchyTree.RootNode.is(this.root)) {
return this.root.languageId;
}
return undefined;
}

protected get type(): TypeHierarchyFeature.TypeHierarchyType | undefined {
if (TypeHierarchyTree.RootNode.is(this.root)) {
return this.root.type;
}
return undefined;
}

protected async ensureResolved(node: TypeHierarchyTree.Node): Promise<void> {
if (!node.resolved) {
const { languageId, type } = this;
if (languageId && type) {
const { location } = node;
const resolvedSymbol = await (TypeHierarchyFeature.TypeHierarchyType.SUBTYPE === type
? this.typeHierarchyService.subTypes(languageId, location)
: this.typeHierarchyService.superTypes(languageId, location));

if (resolvedSymbol) {
node.resolved = true;
if (resolvedSymbol.children) {
node.children = resolvedSymbol.children.filter(DocumentSymbolExt.is).map(child => TypeHierarchyTree.Node.create(child));
} else {
node.children = [];
}
}
}
}
}

}

export namespace TypeHierarchyTree {
Expand All @@ -43,39 +86,65 @@ export namespace TypeHierarchyTree {
readonly languageId: string | undefined;
}

export interface Node extends CompositeTreeNode {
export interface RootNode extends Node {
readonly type: TypeHierarchyFeature.TypeHierarchyType;
readonly languageId: string;
}

export namespace RootNode {

export function is(node: TreeNode | undefined): node is RootNode {
if (Node.is(node) && 'type' in node && 'languageId' in node) {
// tslint:disable-next-line:no-any
const { type, languageId } = (node as any);
return typeof languageId === 'string' && (type === TypeHierarchyFeature.TypeHierarchyType.SUBTYPE || type === TypeHierarchyFeature.TypeHierarchyType.SUPERTYPE);
}
return false;
}

export function create(symbol: DocumentSymbolExt, languageId: string, type: TypeHierarchyFeature.TypeHierarchyType): RootNode {
return {
...Node.create(symbol, true),
type,
languageId
};
}

}

export interface Node extends CompositeTreeNode, ExpandableTreeNode, SelectableTreeNode {
readonly location: Location;
resolved?: boolean;
resolved: boolean;
}

export namespace Node {

export function is(node: TreeNode | undefined): node is Node {
if (!!node && 'type' in node && 'location' in node) {
if (!!node && 'resolved' in node && 'location' in node) {
// tslint:disable-next-line:no-any
const { type, location } = (node as any);
return Location.is(location) && (type === 'subtype' || type === 'supertype');
const { resolved, location } = (node as any);
return Location.is(location) && typeof resolved === 'boolean';
}
return false;
}

export function create(symbol: DocumentSymbolExt, type: TypeHierarchyFeature.TypeHierarchyType, resolved?: boolean): Node {
export function create(symbol: DocumentSymbolExt, resolved: boolean = true): Node {
const id = v4();
const { name } = symbol;
const description = symbol.detail;
const parent = undefined;
const location = Location.create(symbol.uri, symbol.range);
const children = symbol.children ? symbol.children.filter(DocumentSymbolExt.is).map(child => create(child, type, false)) : [];
const children = symbol.children ? symbol.children.filter(DocumentSymbolExt.is).map(child => create(child, false)) : [];
return {
id,
name,
description,
parent,
parent: undefined,
location,
type,
resolved,
children
children,
expanded: false,
visible: true,
selected: false
};
}

Expand Down
16 changes: 8 additions & 8 deletions packages/typehierarchy/src/browser/typehierarchy-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { KeybindingRegistry } from '@theia/core/lib/browser/keybinding';
import { Command, CommandRegistry } from '@theia/core/lib/common/command';
import { EDITOR_CONTEXT_MENU } from '@theia/editor/lib/browser/editor-menu';
import { EditorAccess } from '@theia/editor/lib/browser/editor-manager';
import { TypeHierarchyFeature } from '@theia/languages/lib/browser/typehierarchy/typehierarchy-feature';
import { AbstractViewContribution, OpenViewArguments } from '@theia/core/lib/browser/shell/view-contribution';
import { TypeHierarchyTreeWidget } from './tree/typehierarchy-tree-widget';
import { TypeHierarchyTree } from './tree/typehierarchy-tree';
import { TypeHierarchyService } from './typehierarchy-service';

@injectable()
Expand Down Expand Up @@ -61,15 +61,15 @@ export class TypeHierarchyContribution extends AbstractViewContribution<TypeHier
execute: () => this.openView({
toggle: false,
activate: true,
type: 'subtype'
type: TypeHierarchyFeature.TypeHierarchyType.SUBTYPE
}),
isEnabled: this.isEnabled.bind(this)
});
commands.registerCommand(TypeHierarchyCommands.OPEN_SUPERTYPE, {
execute: () => this.openView({
toggle: false,
activate: true,
type: 'supertype'
type: TypeHierarchyFeature.TypeHierarchyType.SUPERTYPE
}),
isEnabled: this.isEnabled.bind(this)
});
Expand Down Expand Up @@ -98,14 +98,14 @@ export class TypeHierarchyContribution extends AbstractViewContribution<TypeHier
return this.typeHierarchyService.isEnabledFor(languageId);
}

protected getType(args?: Partial<TypeHierarchyOpenViewArguments>): TypeHierarchyTree.Type {
return !!args && !!args.type ? args.type : 'subtype';
protected getType(args?: Partial<TypeHierarchyOpenViewArguments>): TypeHierarchyFeature.TypeHierarchyType {
return !!args && !!args.type ? args.type : TypeHierarchyFeature.TypeHierarchyType.SUBTYPE;
}

}

export interface TypeHierarchyOpenViewArguments extends OpenViewArguments {
readonly type: TypeHierarchyTree.Type;
readonly type: TypeHierarchyFeature.TypeHierarchyType;
}

export namespace TypeHierarchyCommands {
Expand All @@ -116,12 +116,12 @@ export namespace TypeHierarchyCommands {

export const OPEN_SUBTYPE: Command = {
id: 'typehierarchy:open-subtype',
label: 'Open Subtype Hierarchy'
label: 'Subtype Hierarchy'
};

export const OPEN_SUPERTYPE: Command = {
id: 'typehierarchy:open-supertype',
label: 'Open Supertype Hierarchy'
label: 'Supertype Hierarchy'
};

}

0 comments on commit bd538a7

Please sign in to comment.