-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTestCodeLensProvider.ts
More file actions
184 lines (157 loc) · 4.47 KB
/
TestCodeLensProvider.ts
File metadata and controls
184 lines (157 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* CodeLens provider for FastAPI test client HTTP calls.
* Shows "Go to route" links above test client method calls.
*/
import {
CodeLens,
type CodeLensProvider,
EventEmitter,
Location,
Position,
Range,
type TextDocument,
Uri,
} from "vscode"
import type { Node } from "web-tree-sitter"
import { extractPathFromNode, findNodesByType } from "../core/extractors"
import { ROUTE_METHODS } from "../core/internal"
import type { Parser } from "../core/parser"
import {
pathMatchesEndpoint,
stripLeadingDynamicSegments,
} from "../core/pathUtils"
import type {
AppDefinition,
RouteDefinition,
RouterDefinition,
SourceLocation,
} from "../core/types"
interface TestClientCall {
method: string
path: string
line: number
column: number
}
export class TestCodeLensProvider implements CodeLensProvider {
private apps: AppDefinition[] = []
private parser: Parser
private _onDidChangeCodeLenses = new EventEmitter<void>()
readonly onDidChangeCodeLenses = this._onDidChangeCodeLenses.event
constructor(parser: Parser, apps: AppDefinition[]) {
this.parser = parser
this.apps = apps
}
setApps(apps: AppDefinition[]): void {
this.apps = apps
this._onDidChangeCodeLenses.fire()
}
provideCodeLenses(document: TextDocument): CodeLens[] {
const code = document.getText()
const tree = this.parser.parse(code)
if (!tree) {
return []
}
const testClientCalls = this.findTestClientCalls(tree.rootNode)
const codeLenses: CodeLens[] = []
for (const call of testClientCalls) {
const matchingRoutes = this.findMatchingRoutes(call.path, call.method)
if (matchingRoutes.length > 0) {
const range = new Range(
new Position(call.line, call.column),
new Position(call.line, call.column),
)
const methodUpper = call.method.toUpperCase()
const displayPath = stripLeadingDynamicSegments(call.path)
const locations = matchingRoutes.map(
(loc) =>
new Location(
Uri.file(loc.filePath),
new Position(loc.line - 1, loc.column),
),
)
codeLenses.push(
new CodeLens(range, {
title: `Go to route: ${methodUpper} ${displayPath}`,
command: "fastapi-vscode.goToDefinition",
arguments: [
locations,
document.uri,
new Position(call.line, call.column),
],
}),
)
}
}
return codeLenses
}
private findTestClientCalls(rootNode: Node): TestClientCall[] {
const calls: TestClientCall[] = []
const callNodes = findNodesByType(rootNode, "call")
for (const callNode of callNodes) {
const functionNode = callNode.childForFieldName("function")
if (!functionNode || functionNode.type !== "attribute") {
continue
}
const methodNode = functionNode.childForFieldName("attribute")
if (!methodNode) {
continue
}
const method = methodNode.text.toLowerCase()
if (!ROUTE_METHODS.has(method)) {
continue
}
// Get the path argument (first argument)
const argumentsNode = callNode.childForFieldName("arguments")
if (!argumentsNode) {
continue
}
const args = argumentsNode.namedChildren.filter(
(child) => child.type !== "comment",
)
if (args.length === 0) {
continue
}
const pathArg = args[0]
const path = extractPathFromNode(pathArg)
if (!path) {
continue
}
calls.push({
method,
path,
line: callNode.startPosition.row,
column: callNode.startPosition.column,
})
}
return calls
}
private findMatchingRoutes(
testPath: string,
testMethod: string,
): SourceLocation[] {
const matches: SourceLocation[] = []
const collectRoutes = (routes: RouteDefinition[]) => {
for (const route of routes) {
if (
route.method.toLowerCase() === testMethod.toLowerCase() &&
pathMatchesEndpoint(testPath, route.path)
) {
matches.push(route.location)
}
}
}
const walkRouters = (routers: RouterDefinition[]) => {
for (const router of routers) {
collectRoutes(router.routes)
if (router.children) {
walkRouters(router.children)
}
}
}
for (const app of this.apps) {
collectRoutes(app.routes)
walkRouters(app.routers)
}
return matches
}
}