🐛 fix(vscode): Rust 卡片照抄的类型、签名逐个名字带下标,返回类型也能悬停跳转
This commit is contained in:
1 parent
ecb6dc89c6
commit
55595e948c
17 files changed
+220
-122
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import type { FactBlock, Inline, Node, PropertyRow, SourceDocument, SourceSpan, TextBlock, TextTranslation, Translator } from '../contract'
|
||||
import { attributeRows, enumRows, functionRows, implRows, importRows, moduleRows, plainSay, structRows, traitRows } from '../rules/rustRows'
|
||||
import type { RustSay } from '../rules/rustRows'
|
||||
import { attributeRows, enumRows, functionRows, implRows, importRows, moduleRows, structRows, traitRows } from '../rules/rustRows'
|
||||
import { isCrateRoot } from '../rules/rustTree'
|
||||
import { createZhTemplateTranslator, KIND_ZH, ZH_ATTRIBUTE_NAMES } from './zhTemplate'
|
||||
|
||||
@@ -93,7 +94,7 @@ function translate(block: TextBlock, document: SourceDocument): TextTranslation
|
||||
}
|
||||
|
||||
function describe(block: FactBlock, document: SourceDocument): PropertyRow[] {
|
||||
const say = plainSay(node => template.doc(node, document))
|
||||
const say: RustSay = { verbatim: (node, role) => template.verbatim(node, document, role), doc: node => template.doc(node, document) }
|
||||
switch (block.kind) {
|
||||
case 'import':
|
||||
return importRows(block, say)
|
||||
|
||||
@@ -717,8 +717,12 @@ export function createZhTemplateTranslator(table: ZhTable): Translator {
|
||||
return out.tokens
|
||||
}
|
||||
|
||||
/** node 的源码原样照抄:叶子按源码着色(给了 role 就都按它),叶子之间的空白、标点照抄。 */
|
||||
function verbatim(node: Node, { source, language }: SourceDocument, role?: TokenRole): Segment[] {
|
||||
/**
|
||||
* `node` 的源码原样照抄:叶子按源码着色(给了 `role` 就都按它),名字带源码下标,叶子之间的空白、标点照抄;
|
||||
* `flat` 为真时压成一行,连着的空白(含换行)收成一个空格。
|
||||
*/
|
||||
function verbatim(node: Node, { source, language }: SourceDocument, role?: TokenRole, flat = false): Segment[] {
|
||||
const copy = (text: string): string => flat ? text.replace(/\s+/g, ' ') : text
|
||||
const segments: Segment[] = []
|
||||
let cursor = node.startIndex
|
||||
const visit = (current: Node): void => {
|
||||
@@ -731,12 +735,12 @@ export function createZhTemplateTranslator(table: ZhTable): Translator {
|
||||
}
|
||||
return
|
||||
}
|
||||
pushSegment(segments, source.slice(cursor, current.startIndex), 'plain')
|
||||
pushSegment(segments, current.text, role ?? language.tokenRole(current), offsetOf(current, language))
|
||||
pushSegment(segments, copy(source.slice(cursor, current.startIndex)), 'plain')
|
||||
pushSegment(segments, copy(current.text), role ?? language.tokenRole(current), offsetOf(current, language))
|
||||
cursor = current.endIndex
|
||||
}
|
||||
visit(node)
|
||||
pushSegment(segments, source.slice(cursor, node.endIndex), 'plain')
|
||||
pushSegment(segments, copy(source.slice(cursor, node.endIndex)), 'plain')
|
||||
return segments
|
||||
}
|
||||
|
||||
@@ -843,6 +847,7 @@ export function createZhTemplateTranslator(table: ZhTable): Translator {
|
||||
translate,
|
||||
describe,
|
||||
expression,
|
||||
verbatim: (node, document, role) => verbatim(node, document, role, true),
|
||||
doc,
|
||||
attributeNames: ZH_ATTRIBUTE_NAMES,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user