Files
superwork/vscode/src/codeReader/editor/definitionAction.ts
T

29 lines
1.3 KiB
TypeScript

/** 一处定义。uri 是 Uri.toString(),用来和当前文件比、去重。 */
export interface DefinitionTarget {
uri: string
line: number
character: number
}
/**
* ⌘ / Ctrl + 点击查到定义之后怎么做:
* none 不动;pick 让用户从几处里挑一处,挑好的那处再交给 definitionAction 决定;
* here 定义就在当前文件,预览原地滚过去;elsewhere 在另一个文件,用哪种编辑器打开见 openTarget.ts 的 openingFor。
*/
export type DefinitionAction<T extends DefinitionTarget>
= | { kind: 'none' }
| { kind: 'pick', targets: T[] }
| { kind: 'here' | 'elsewhere', target: T }
/** 定义提供者查到的几处(可能重复)→ 怎么跳。current 是当前文件的 Uri.toString()。 */
export function definitionAction<T extends DefinitionTarget>(targets: readonly T[], current: string): DefinitionAction<T> {
const unique = targets.filter((target, i) =>
targets.findIndex(other => other.uri === target.uri && other.line === target.line && other.character === target.character) === i)
if (unique.length === 0)
return { kind: 'none' }
if (unique.length > 1)
return { kind: 'pick', targets: unique }
const [target] = unique
return { kind: target.uri === current ? 'here' : 'elsewhere', target }
}