26 lines
1.3 KiB
TypeScript
26 lines
1.3 KiB
TypeScript
import type { LocalSymbol, SourceRange } from './contract'
|
|
|
|
/**
|
|
* 语言插件自己认得出的名字(LanguageAdapter.symbols)怎么用:VS Code 里没有这种语言的定义、高亮提供者时(shell),
|
|
* 宿主拿它跳到定义、高亮各处出现。这里不碰 VS Code。
|
|
*/
|
|
|
|
/** 盖住 offset 的那个名字。 */
|
|
export function symbolAt(symbols: readonly LocalSymbol[], offset: number): LocalSymbol | undefined {
|
|
return symbols.find(symbol => symbol.occurrences.some(range => range.startIndex <= offset && offset < range.endIndex))
|
|
}
|
|
|
|
/** offset 处用到的名字跳到哪个定义:它之前最近的一处(同名的变量前后赋过几次值),前面没有就是第一处。 */
|
|
export function definitionFor(symbol: LocalSymbol, offset: number): SourceRange | undefined {
|
|
return symbol.definitions.filter(range => range.startIndex <= offset).at(-1) ?? symbol.definitions[0]
|
|
}
|
|
|
|
/** 名字的各处出现:定义处算写,别处算读。 */
|
|
export function symbolOccurrences(symbol: LocalSymbol): { start: number, end: number, kind: 'read' | 'write' }[] {
|
|
return symbol.occurrences.map(({ startIndex, endIndex }) => ({
|
|
start: startIndex,
|
|
end: endIndex,
|
|
kind: symbol.definitions.some(range => range.startIndex === startIndex) ? 'write' : 'read',
|
|
}))
|
|
}
|