Skip to content

Commit

Permalink
refactor: encapsulate tree localization
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 17, 2022
1 parent e4b6675 commit 3bfd8d4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export { getSymbolType, multilineTypeToString, pseudoBigIntToString, getNodeType
export { recursivelyExpandType } from "./merge"
export { generateTypeTree, getTypeInfoChildren } from "./tree"
export { TypeInfo, SymbolInfo, SignatureInfo, TypeId, IndexInfo, TypeInfoKind, TypeParameterInfo, SourceFileLocation } from "./types"
export { localizeTypeInfo, LocalizedTypeInfo, TypeInfoMap, generateTypeInfoMap, getLocalizedTypeInfoChildren } from "./localizedTree"
export { TypeInfoLocalizer, LocalizedTypeInfo, TypeInfoMap } from "./localizedTree"
53 changes: 31 additions & 22 deletions packages/api/src/localizedTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,41 @@ import { getTypeInfoChildren } from "./tree"
import { getEmptyTypeId, isEmpty, isNonEmpty, pseudoBigIntToString, wrapSafe } from "./util"
import { unwatchFile } from "fs"

export function localizeTypeInfo(info: TypeInfo, typeInfoMap: TypeInfoMap): LocalizedTypeInfo {
return _localizeTypeInfo(info, { typeInfoMap })
}
export class TypeInfoLocalizer {
typeInfoMap: TypeInfoMap

constructor(private typeInfo: TypeInfo) {
this.typeInfoMap = generateTypeInfoMap(typeInfo)
}

export function getLocalizedTypeInfoChildren(info: LocalizedTypeInfo, typeInfoMap: TypeInfoMap): LocalizedTypeInfo[] {
return info.children?.map(
({info, localizedInfo, opts}) => {
assert(info || localizedInfo, "Either info or localized info must be provided")
localize(info: TypeInfo) {
return _localizeTypeInfo(info, { typeInfoMap: this.typeInfoMap })
}

if(localizedInfo) {
return localizedInfo
localizeChildren(info: LocalizedTypeInfo): LocalizedTypeInfo[] {
return info.children?.map(
({info, localizedInfo, opts}) => {
assert(info || localizedInfo, "Either info or localized info must be provided")

if(localizedInfo) {
return localizedInfo
}

return _localizeTypeInfo(info!, { typeInfoMap: this.typeInfoMap }, opts)
}
) ?? []
}
}

return _localizeTypeInfo(info!, { typeInfoMap }, opts)
}
) ?? []

function generateTypeInfoMap(tree: TypeInfo, cache?: TypeInfoMap): TypeInfoMap {
cache ??= new Map()

if(tree.kind === 'reference') { return cache }
cache.set(tree.id, tree)
getTypeInfoChildren(tree).forEach(c => generateTypeInfoMap(c, cache))

return cache
}

type TypePurpose = 'return'|'index_type'|'index_value_type'|'conditional_check'|'conditional_extends'|'conditional_true'|'conditional_false'|'keyof'|'indexed_access_index'|'indexed_access_base'|'parameter_default'|'parameter_base_constraint'|'class_constructor'|'class_base_type'|'class_implementations'|'object_class'|'type_parameter_list'|'type_argument_list'|'parameter_value'
Expand Down Expand Up @@ -445,16 +464,6 @@ function localizePurpose(purpose: TypePurpose): string {
return nameByPurpose[purpose]
}

export function generateTypeInfoMap(tree: TypeInfo, cache?: TypeInfoMap): TypeInfoMap {
cache ??= new Map()

if(tree.kind === 'reference') { return cache }
cache.set(tree.id, tree)
getTypeInfoChildren(tree).forEach(c => generateTypeInfoMap(c, cache))

return cache
}

function getTypeLocations(info: TypeInfo): SourceFileLocation[]|undefined {
const baseLocations = wrapSafe(getLocations)(info.typeSymbolMeta ?? info.aliasSymbolMeta ?? info.symbolMeta)

Expand Down
2 changes: 0 additions & 2 deletions packages/typescript-explorer/src/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export class StateManager {
return
}

console.log("change selection", e)

getQuickInfoAtPosition(e.textEditor.document.fileName, e.selections[0].start)
.then((body) => {
const { __displayTree } = body ?? {}
Expand Down
15 changes: 8 additions & 7 deletions packages/typescript-explorer/src/view/typeTreeView.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypeInfo, TypeId, getTypeInfoChildren, SymbolInfo, SignatureInfo, IndexInfo, pseudoBigIntToString, LocalizedTypeInfo, localizeTypeInfo, TypeInfoMap, generateTypeInfoMap, getLocalizedTypeInfoChildren, SourceFileLocation } from '@ts-expand-type/api'
import { TypeInfo, TypeId, getTypeInfoChildren, SymbolInfo, SignatureInfo, IndexInfo, pseudoBigIntToString, LocalizedTypeInfo, TypeInfoMap, SourceFileLocation, TypeInfoLocalizer } from '@ts-expand-type/api'
import assert = require('assert');
import * as vscode from 'vscode'
import { StateManager } from '../state/stateManager';
Expand All @@ -9,14 +9,15 @@ const { None: NoChildren, Expanded, Collapsed } = vscode.TreeItemCollapsibleStat
export class TypeTreeProvider implements vscode.TreeDataProvider<TypeTreeItem> {
constructor(private stateManager: StateManager) { }

private typeInfoMap: TypeInfoMap = new Map()
// private typeInfoMap: TypeInfoMap = new Map()
private typeInfoLocalizer: TypeInfoLocalizer|undefined

private _onDidChangeTreeData: vscode.EventEmitter<TypeTreeItem | undefined | null | void> = new vscode.EventEmitter<TypeTreeItem | undefined | null | void>()
readonly onDidChangeTreeData: vscode.Event<TypeTreeItem | undefined | null | void> = this._onDidChangeTreeData.event

refresh(): void {
this.typeInfoMap.clear()
this._onDidChangeTreeData.fire();
this.typeInfoLocalizer = undefined
this._onDidChangeTreeData.fire()
}

getTreeItem(element: TypeTreeItem) {
Expand All @@ -28,12 +29,12 @@ export class TypeTreeProvider implements vscode.TreeDataProvider<TypeTreeItem> {
const typeInfo = this.stateManager.getTypeTree()
if(!typeInfo) { return [] }

this.typeInfoMap = generateTypeInfoMap(typeInfo)
const localizedTypeInfo = localizeTypeInfo(typeInfo, this.typeInfoMap)
this.typeInfoLocalizer = new TypeInfoLocalizer(typeInfo)
const localizedTypeInfo = this.typeInfoLocalizer.localize(typeInfo)

return [this.createTypeNode(localizedTypeInfo, /* root */ undefined)]
} else {
return getLocalizedTypeInfoChildren(element.typeInfo, this.typeInfoMap).map(info => this.createTypeNode(info, element))
return this.typeInfoLocalizer!.localizeChildren(element.typeInfo).map(info => this.createTypeNode(info, element))
}
}

Expand Down

0 comments on commit 3bfd8d4

Please sign in to comment.