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

32 lines
1.4 KiB
TypeScript

/** 一处定义。uri 是 Uri.toString(),用来和当前文件比、去重;supported:目标文件的语言有代码阅读。 */
export interface DefinitionTarget {
uri: string
line: number
character: number
supported: boolean
}
/**
* ⌘ / Ctrl + 点击查到定义之后怎么做:
* none 不动;pick 让用户从几处里挑一处,挑好的那处再交给 definitionAction 决定;
* here 定义就在当前文件,预览原地滚过去;preview 用代码阅读预览打开目标文件;text 目标语言没有代码阅读,用普通文本编辑器打开。
*/
export type DefinitionAction<T extends DefinitionTarget>
= | { kind: 'none' }
| { kind: 'pick', targets: T[] }
| { kind: 'here' | 'preview' | 'text', 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
if (target.uri === current)
return { kind: 'here', target }
return { kind: target.supported ? 'preview' : 'text', target }
}