✨ 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,7 +1,7 @@
|
||||
import type { ClassBlock, CodeBlock, Inline, LibraryRule, Node, PropertyItem, PropertyRow, RuleCard, RuleContext, ThemeColor, TokenRole } from '../contract'
|
||||
import type { Call } from './pythonTree'
|
||||
import { withLeadingComments } from './children'
|
||||
import { aliasesOf, callOf, canonicalName, lastName, stringValue, syntaxChildren } from './pythonTree'
|
||||
import { aliasesOf, callOf, canonicalName, genericOf, lastName, stringValue, syntaxChildren, unwrapType } from './pythonTree'
|
||||
import { hint, joined, name, plain, range, word } from './segments'
|
||||
|
||||
/**
|
||||
@@ -164,22 +164,10 @@ function memberOf(block: CodeBlock): Member | undefined {
|
||||
}
|
||||
}
|
||||
|
||||
/** 注解里 `type` 节点只包着一个类型时取里面那个。 */
|
||||
function unwrapType(node: Node): Node {
|
||||
const inner = node.type === 'type' ? syntaxChildren(node) : []
|
||||
return inner.length === 1 ? inner[0] : node
|
||||
}
|
||||
|
||||
/** `Mapped[X]`(`so.Mapped[X]`)里的 X;注解不是 Mapped[...] 时没有。 */
|
||||
function mappedType(annotation: Node | undefined): Node | undefined {
|
||||
const node = annotation && unwrapType(annotation)
|
||||
if (node?.type === 'generic_type') {
|
||||
const [head, parameters] = syntaxChildren(node)
|
||||
return lastName(head) === 'Mapped' ? syntaxChildren(parameters)[0] : undefined
|
||||
}
|
||||
if (node?.type === 'subscript' && lastName(node.childForFieldName('value')) === 'Mapped')
|
||||
return node.childForFieldName('subscript') ?? undefined
|
||||
return undefined
|
||||
const generic = genericOf(annotation)
|
||||
return generic && lastName(generic.head) === 'Mapped' ? generic.args[0] : undefined
|
||||
}
|
||||
|
||||
function isNone(node: Node): boolean {
|
||||
|
||||
Reference in new issue
Block a user