Skip to content

Commit

Permalink
feat(vscode): go to definition button
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 17, 2022
1 parent b5340f9 commit 2564263
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
10 changes: 8 additions & 2 deletions packages/typescript-explorer/package.json
Expand Up @@ -42,7 +42,9 @@
{
"title": "Go To Type",
"command": "typescript-explorer.goToTypeInTypeTreeView",
"category": "Typescript Explorer"
"category": "Typescript Explorer",
"icon": "$(arrow-right)",
"enablement": "view == type-tree"
}
],
"viewsContainers": {
Expand Down Expand Up @@ -71,7 +73,11 @@
}
],
"view/item/context": [

{
"command": "typescript-explorer.goToTypeInTypeTreeView",
"when": "view == type-tree && viewItem == declared",
"group": "inline"
}
]
},
"typescriptServerPlugins": [
Expand Down
10 changes: 9 additions & 1 deletion packages/typescript-explorer/src/state/stateManager.ts
@@ -1,7 +1,7 @@
import { TypeInfo } from '@ts-expand-type/api'
import * as vscode from 'vscode'
import { getQuickInfoAtPosition } from '../util'
import { TypeTreeProvider } from '../view/typeTreeView'
import { TypeTreeItem, TypeTreeProvider } from '../view/typeTreeView'
import { ViewProviders } from '../view/views'

export class StateManager {
Expand Down Expand Up @@ -31,6 +31,14 @@ export class StateManager {
() => typeTreeProvider.refresh()
)
)

context.subscriptions.push(
vscode.commands.registerCommand(
"typescript-explorer.goToTypeInTypeTreeView",
(item: TypeTreeItem) => item.goToDefinition()

)
)
}

setTypeTree(typeTree: TypeInfo|undefined) {
Expand Down
44 changes: 36 additions & 8 deletions packages/typescript-explorer/src/view/typeTreeView.ts
Expand Up @@ -43,6 +43,7 @@ export class TypeTreeProvider implements vscode.TreeDataProvider<TypeTreeItem> {
}
}

type TypeTreeItemContextValue = "declared"
export class TypeTreeItem extends vscode.TreeItem {
protected depth: number

Expand All @@ -51,7 +52,7 @@ export class TypeTreeItem extends vscode.TreeItem {
private provider: TypeTreeProvider,
protected parent?: TypeTreeItem
) {
const { label, description } = getMeta(typeInfo)
const { label, description, contextValue } = getMeta(typeInfo)

const depth = (parent?.depth ?? 0) + 1
const collapsibleState = (typeInfo.children?.length ?? 0) === 0 ? NoChildren : depth === 1 ? Expanded : Collapsed
Expand All @@ -60,25 +61,48 @@ export class TypeTreeItem extends vscode.TreeItem {

this.depth = depth
this.description = description

if(typeInfo.locations && typeInfo.locations && typeInfo.locations.length > 0) {
this.command = getOpenCommand(typeInfo.locations[0])
}
this.contextValue = contextValue
}

createTypeNode(typeInfo: LocalizedTypeInfo) {
protected createTypeNode(typeInfo: LocalizedTypeInfo) {
return this.provider.createTypeNode(typeInfo, this)
}

goToDefinition() {
assert(this.typeInfo.locations && this.typeInfo.locations.length > 0, "Type has no locations!")

const location = this.typeInfo.locations[0]

const range = {
start: (location.range.start),
end: (location.range.end),
}

const args: [ vscode.Uri, vscode.TextDocumentShowOptions] = [
vscode.Uri.file(location.fileName),
{
selection: rangeFromLineAndCharacters(range.start, range.end)
}
]

vscode.commands.executeCommand("vscode.open", ...args)
}
}

function getMeta(info: LocalizedTypeInfo) {
type TypeTreeItemMeta = {
label: string,
description?: string,
contextValue?: TypeTreeItemContextValue,
}

function getMeta(info: LocalizedTypeInfo): TypeTreeItemMeta {
let nameOverridden = false

const label = getLabel()
const description = getDescription()

return {
label, description
label, description, contextValue: getContextValue()
}

function getLabel() {
Expand Down Expand Up @@ -120,6 +144,10 @@ function getMeta(info: LocalizedTypeInfo) {

return aliasDescription ? `${aliasDescription} (${baseDescription})` : baseDescription
}

function getContextValue(): TypeTreeItemContextValue|undefined {
return info.locations && info.locations.length > 0 ? "declared" : undefined
}
}

function addDecorations(text: string, decorations: { rest?: boolean, optional?: boolean, dimension?: number }) {
Expand Down

0 comments on commit 2564263

Please sign in to comment.