♻️ refactor(vscode): 代码阅读的翻译插件只经契约拿语言插件的词性和推导式结构
This commit is contained in:
1 parent
07eaf96a6b
commit
202fa954ba
12 files changed
+185
-76
No files matched your search
@@ -2,7 +2,7 @@ import type { IterationFacts, Node } from '../contract'
|
||||
import type { Outline } from '../testSupport'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { factsByLine, nodesToText, outline, readFixture, runtime, WASM_DIR, withBlocks } from '../testSupport'
|
||||
import { createPythonAdapter, pythonIteration } from './python'
|
||||
import { createPythonAdapter } from './python'
|
||||
|
||||
const adapter = createPythonAdapter(WASM_DIR)
|
||||
|
||||
@@ -335,7 +335,7 @@ describe('python 写在表达式里的遍历(推导式)', () => {
|
||||
try {
|
||||
const found: IterationFacts[] = []
|
||||
const visit = (node: Node): void => {
|
||||
const facts = pythonIteration(node)
|
||||
const facts = adapter.iteration?.(node)
|
||||
if (facts)
|
||||
found.push(facts)
|
||||
for (const child of node.children) {
|
||||
|
||||
@@ -269,7 +269,7 @@ const ITERATION_RESULTS: Record<string, IterationResult> = {
|
||||
* 推导式、生成器表达式的结构,别的节点没有。几种读不成遍历的写法也没有,照常翻:
|
||||
* `for x in a, b`(语法树认、Python 3 不认的多个来源),以及缺了变量、来源、条件或每一项的值(写到一半的代码)。
|
||||
*/
|
||||
export function pythonIteration(node: Node): IterationFacts | undefined {
|
||||
function pythonIteration(node: Node): IterationFacts | undefined {
|
||||
const result = ITERATION_RESULTS[node.type]
|
||||
const body = node.childForFieldName('body')
|
||||
if (!result || !body)
|
||||
@@ -360,7 +360,7 @@ function insideString(leaf: Node): boolean {
|
||||
}
|
||||
|
||||
/** 注解里的名字:typed_parameter / 返回值 / 带注解赋值的 type 节点之内。 */
|
||||
export function insideAnnotation(leaf: Node): boolean {
|
||||
function insideAnnotation(leaf: Node): boolean {
|
||||
for (let node = leaf.parent; node; node = node.parent) {
|
||||
if (node.type === 'type')
|
||||
return true
|
||||
@@ -395,7 +395,7 @@ function identifierRole(leaf: Node): TokenRole {
|
||||
return 'variable'
|
||||
}
|
||||
|
||||
export function pythonTokenRole(leaf: Node): TokenRole {
|
||||
function pythonTokenRole(leaf: Node): TokenRole {
|
||||
if (leaf.type === 'comment')
|
||||
return 'comment'
|
||||
if (insideString(leaf))
|
||||
@@ -430,5 +430,6 @@ export function createPythonAdapter(wasmDir: string): LanguageAdapter {
|
||||
grammarPath: () => path.join(wasmDir, 'tree-sitter-python.wasm'),
|
||||
toBlocks: root => statements(named(root), 'module', -1, true),
|
||||
tokenRole: pythonTokenRole,
|
||||
iteration: pythonIteration,
|
||||
}
|
||||
}
|
||||
@@ -479,7 +479,7 @@ function nameRole(leaf: Node): TokenRole {
|
||||
return 'variable'
|
||||
}
|
||||
|
||||
export function typescriptTokenRole(leaf: Node): TokenRole {
|
||||
function typescriptTokenRole(leaf: Node): TokenRole {
|
||||
if (leaf.type === 'comment' || leaf.type === 'hash_bang_line')
|
||||
return 'comment'
|
||||
if (insideString(leaf))
|
||||
|
||||
Reference in new issue
Block a user