✨ feat(vscode): 同名接口靠同一路由对象上的其他接口定挂载前缀

This commit is contained in:
cruldra committed 2026-10-12 12:38:54 +11:00
1 parent 00737ab1a0
commit e66b19b3b4
6 files changed
+129 -27

No files matched your search

+22 -8
View File
@@ -662,7 +662,8 @@ function requestItems(url: Inline[], method: string, placed: readonly Placed[],
}
}
function routeCard(block: FunctionBlock, route: Route, env: Env): RuleCard {
/** 接口卡片;siblings 是同一个路由对象上的全部接口,随卡片讲的 HTTP 接口一起交给宿主定挂载前缀。 */
function routeCard(block: FunctionBlock, route: Route, env: Env, siblings: readonly RouteOn[]): RuleCard {
const { context, words } = env
const { labels } = words
const { facts } = block
@@ -731,16 +732,29 @@ function routeCard(block: FunctionBlock, route: Route, env: Env): RuleCard {
...(facts.name && { defines: range(facts.name) }),
attributes: { [METHOD_ATTRIBUTE]: route.method },
absorbs: [],
...(facts.name && endpointOf(route, facts.name.text)),
...(facts.name && endpointOf(route, facts.name.text, siblings)),
}
}
/** 卡片讲的 HTTP 接口:前缀和装饰器里的路径都是字符串字面量(或不写)才算得出路径;FastAPI 默认的 operationId 是函数名接完整路径和方法。 */
function endpointOf(route: Route, fn: string): Pick<RuleCard, 'endpoint'> {
const parts = [route.router.prefix, route.path].map(part => part ? stringContent(part)?.text : '')
if (parts.includes(undefined))
/**
* 卡片讲的 HTTP 接口,连同同一个路由对象上的全部接口(算不出路径的不算):算不出这一个的路径时没有。
* FastAPI 默认的 operationId 是函数名接完整路径和方法。
*/
function endpointOf(route: Route, fn: string, siblings: readonly RouteOn[]): Pick<RuleCard, 'endpoint'> {
const path = localPath(route)
if (path === undefined)
return {}
return { endpoint: { method: route.method, path: parts.join(''), operationIdPrefix: `${fn}_` } }
const sameRouter = siblings.flatMap(({ route: other }) => {
const otherPath = localPath(other)
return otherPath === undefined ? [] : [{ method: other.method, path: otherPath }]
})
return { endpoint: { method: route.method, path, operationIdPrefix: `${fn}_`, sameRouter } }
}
/** 接口在这份文件里写出来的路径(路由对象的前缀接装饰器里的路径):两段都是字符串字面量(或不写)才算得出。 */
function localPath(route: Route): string | undefined {
const parts = [route.router.prefix, route.path].map(part => part ? stringContent(part)?.text : '')
return parts.includes(undefined) ? undefined : parts.join('')
}
/**
@@ -805,7 +819,7 @@ export const fastapiRule: LibraryRule = {
for (const decorator of block.facts.decorators) {
const route = routeOf(decorator, routers)
if (route)
return routeCard(block, route, env)
return routeCard(block, route, env, routesOn(root, routers, route.router))
}
}
// 路由对象只认模块顶层的赋值(routersOf),按赋值目标的位置认出是不是这一块