✨ feat(vscode): 代码阅读认出 Python 数据类(@dataclass),整块换成「数据类」卡片,选项和字段的默认值、field 参数写成大白话
This commit is contained in:
1 parent
6f2c220908
commit
c5c5d2b517
10 files changed
+709
-78
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import type { CodeBlock } from '../contract'
|
||||
import type { CodeBlock, Node } from '../contract'
|
||||
import { syntaxChildren } from './pythonTree'
|
||||
|
||||
/** 库规则共用的挑子块的办法。 */
|
||||
|
||||
@@ -19,3 +20,27 @@ export function withLeadingComments(children: readonly CodeBlock[], members: rea
|
||||
return children.filter(child => members.includes(child))
|
||||
.flatMap(member => [...leadingComments(children, member), member])
|
||||
}
|
||||
|
||||
/** 注释块里的各条注释(几条行注释连成的一块是好几个节点);当注释用的字符串是那个字符串。 */
|
||||
function commentNodes(block: CodeBlock): Node[] {
|
||||
if (block.node.type !== 'comment')
|
||||
return syntaxChildren(block.node).slice(0, 1)
|
||||
const nodes: Node[] = []
|
||||
for (let node: Node | null = block.node; node?.type === 'comment' && node.endIndex <= block.header.endIndex; node = node.nextSibling)
|
||||
nodes.push(node)
|
||||
return nodes
|
||||
}
|
||||
|
||||
/**
|
||||
* Python 类主体里一个成员(赋值、只写注解的字段)的说明,照源码顺序:紧挨在它上面的注释块、它这一行行尾的注释、
|
||||
* 紧跟在它后面的属性 docstring。
|
||||
*/
|
||||
export function memberDocs(children: readonly CodeBlock[], block: CodeBlock): Node[] {
|
||||
const trailing = block.node.nextSibling
|
||||
const doc = block.kind === 'variable' ? block.facts.doc : block.kind === 'field' ? block.doc : undefined
|
||||
return [
|
||||
...leadingComments(children, block).flatMap(commentNodes),
|
||||
...(trailing?.type === 'comment' && trailing.startPosition.row === block.node.endPosition.row ? [trailing] : []),
|
||||
...(doc ? [doc] : []),
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user