дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-22 01:20:46 +02:00
Порівняти коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
83199bb549 | ||
|
|
abf96cf42a |
No files matched your search
@@ -18,6 +18,7 @@ import {
|
||||
showServiceView,
|
||||
} from "@/workbench/contributions";
|
||||
import { registerFileNavigation } from "./fileNavigation";
|
||||
import { registerUrlNavigation } from "./urlNavigation";
|
||||
import { registerVariableNavigation } from "./variableNavigation";
|
||||
import { HTTP_LANGUAGE, httpEditorExtension, refreshRequests, requestAtCaret } from "./editor";
|
||||
import { HttpClientHeaderActions, HttpClientView } from "./HttpClientView";
|
||||
@@ -143,6 +144,7 @@ export function activate(): () => void {
|
||||
registerSpringEndpointProvider(),
|
||||
registerFeignNavigation(),
|
||||
registerVariableNavigation(),
|
||||
registerUrlNavigation(),
|
||||
registerFileNavigation(),
|
||||
useProject.subscribe((s, prev) => {
|
||||
if (s.project?.root !== prev.project?.root) resetHttpClient();
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { Text } from "@codemirror/state";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { EndpointEntry } from "@/workbench/contributions";
|
||||
import { endpointsFor, requestPathAt } from "./urlNavigation";
|
||||
|
||||
const entry = (method: string, url: string, methodName = "handler"): EndpointEntry => ({
|
||||
method,
|
||||
url,
|
||||
controller: "C",
|
||||
methodName,
|
||||
path: "/p/C.java",
|
||||
rel: "C.java",
|
||||
line: 3,
|
||||
col: 4,
|
||||
});
|
||||
|
||||
describe("request URL navigation", () => {
|
||||
const doc = Text.of([
|
||||
"### 读取本区配置",
|
||||
"GET http://localhost:19210/api/cockpit/config?x=1",
|
||||
"Content-Type: application/json",
|
||||
"",
|
||||
"###",
|
||||
"POST {{host}}/api/orders/{{id}}/items",
|
||||
"http://localhost:8080/health",
|
||||
"GET /relative/path",
|
||||
]);
|
||||
const at = (line: number, col: number) => doc.line(line).from + col;
|
||||
|
||||
it("finds the method and the path of the URL under the caret", () => {
|
||||
const get = requestPathAt(doc, at(2, 30));
|
||||
expect(get).toMatchObject({ method: "GET", path: "/api/cockpit/config" });
|
||||
expect(doc.sliceString(get!.from, get!.to)).toBe("/api/cockpit/config");
|
||||
expect(requestPathAt(doc, at(6, 20))).toMatchObject({ method: "POST", path: "/api/orders/{{id}}/items" });
|
||||
expect(requestPathAt(doc, at(7, 5))).toMatchObject({ method: "GET", path: "/health" });
|
||||
expect(requestPathAt(doc, at(8, 8))).toMatchObject({ method: "GET", path: "/relative/path" });
|
||||
expect(requestPathAt(doc, at(1, 5))).toBeNull();
|
||||
expect(requestPathAt(doc, at(3, 5))).toBeNull();
|
||||
expect(requestPathAt(doc, at(2, 1))).toBeNull();
|
||||
});
|
||||
|
||||
it("matches the endpoint by method, path variables and a context path", () => {
|
||||
const endpoints = [
|
||||
entry("GET", "/api/cockpit/config", "getConfig"),
|
||||
entry("POST", "/api/cockpit/config", "saveConfig"),
|
||||
entry("POST", "/api/orders/{id}/items", "addItem"),
|
||||
entry("GET", "/files/**", "file"),
|
||||
entry("*", "/any", "any"),
|
||||
];
|
||||
const names = (method: string, path: string) => endpointsFor(endpoints, method, path).map((e) => e.methodName);
|
||||
expect(names("GET", "/api/cockpit/config")).toEqual(["getConfig"]);
|
||||
expect(names("POST", "/api/orders/{{id}}/items")).toEqual(["addItem"]);
|
||||
expect(names("POST", "/api/orders/42/items")).toEqual(["addItem"]);
|
||||
expect(names("GET", "/files/a/b.txt")).toEqual(["file"]);
|
||||
expect(names("DELETE", "/any")).toEqual(["any"]);
|
||||
expect(names("GET", "/app/api/cockpit/config")).toEqual(["getConfig"]);
|
||||
expect(names("PUT", "/api/cockpit/config")).toEqual([]);
|
||||
expect(names("GET", "/api/cockpit")).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Navigate › Declaration (⌘-click, ⌘B) on a request URL in a `.http` file: the
|
||||
* handler of the endpoint the request reaches (a Spring mapping, a plugin's
|
||||
* endpoint), matched by method and path.
|
||||
*/
|
||||
import type { Text } from "@codemirror/state";
|
||||
import { docOf } from "@/editor/lsp/lsp";
|
||||
import { registerNavigationProvider, type NavigationMatch } from "@/editor/navigationProviders";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { searchEndpoints, type EndpointEntry } from "@/workbench/contributions";
|
||||
import { HTTP_LANGUAGE } from "./editor";
|
||||
import { METHODS } from "./language";
|
||||
|
||||
/** The request line holding `offset`: its method and the path part of the URL, as document offsets. */
|
||||
export function requestPathAt(
|
||||
doc: Text,
|
||||
offset: number,
|
||||
): { method: string; path: string; from: number; to: number } | null {
|
||||
const line = doc.lineAt(offset);
|
||||
const m = new RegExp(`^(\\s*(?:(${METHODS.join("|")})\\s+)?)(\\S+)`).exec(line.text);
|
||||
if (!m) return null;
|
||||
const url = m[3]!;
|
||||
// The scheme and host, or a `{{host}}` variable standing for them.
|
||||
const origin = /^(?:https?:\/\/[^/\s?#]+|\{\{[^}]+\}\}(?=\/))/.exec(url)?.[0];
|
||||
if (origin === undefined && !(m[2] && url.startsWith("/"))) return null;
|
||||
const start = line.from + m[1]!.length;
|
||||
if (offset < start || offset > start + url.length) return null;
|
||||
const path = url.slice(origin?.length ?? 0).replace(/[?#].*$/, "");
|
||||
// No path (`http://host`): the whole URL is the link.
|
||||
const from = path ? start + (origin?.length ?? 0) : start;
|
||||
return { method: m[2] ?? "GET", path: path || "/", from, to: path ? from + path.length : start + url.length };
|
||||
}
|
||||
|
||||
const segments = (p: string) => p.split("/").filter(Boolean);
|
||||
|
||||
/** How `request` segments match mapping `pattern` segments: `{var}`, `*` and `**` in the mapping, `{{var}}` in the request. */
|
||||
function matches(request: string[], pattern: string[]): boolean {
|
||||
if (!pattern.length) return !request.length;
|
||||
const [p, ...rest] = pattern;
|
||||
if (p === "**") return request.some((_, i) => matches(request.slice(i), rest)) || matches([], rest);
|
||||
if (!request.length) return false;
|
||||
const [r, ...more] = request;
|
||||
const variable = /^\{[^}]*\}$/.test(p!) || p === "*" || /^\{\{[^}]+\}\}$/.test(r!);
|
||||
return (variable || decodeURIComponent(r!) === p) && matches(more, rest);
|
||||
}
|
||||
|
||||
/** Endpoints `method` `path` reaches: the exact path, else after a context path (`/app` + mapping). */
|
||||
export function endpointsFor(endpoints: readonly EndpointEntry[], method: string, path: string): EndpointEntry[] {
|
||||
const request = segments(path);
|
||||
const sameMethod = endpoints.filter((e) => e.method === method || e.method === "*");
|
||||
const exact = sameMethod.filter((e) => matches(request, segments(e.url)));
|
||||
if (exact.length) return exact;
|
||||
for (let skip = 1; skip < request.length; skip++) {
|
||||
const found = sameMethod.filter((e) => matches(request.slice(skip), segments(e.url)));
|
||||
if (found.length) return found;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export async function urlTargets(doc: Text, offset: number): Promise<NavigationMatch | null> {
|
||||
const at = requestPathAt(doc, offset);
|
||||
if (!at) return null;
|
||||
const endpoints = await searchEndpoints(useProject.getState().project?.root ?? null);
|
||||
const found = endpointsFor(endpoints, at.method, at.path);
|
||||
if (!found.length) return null;
|
||||
return {
|
||||
from: at.from,
|
||||
to: at.to,
|
||||
targets: found.map((e) => ({
|
||||
path: e.path,
|
||||
range: { start: { line: e.line, col: e.col }, end: { line: e.line, col: e.col + e.methodName.length } },
|
||||
name: e.handler ?? `${e.controller}.${e.methodName}`,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export function registerUrlNavigation(): () => void {
|
||||
return registerNavigationProvider(async (view, offset) => {
|
||||
const doc = docOf(view);
|
||||
if (!doc || doc.virtual || doc.language !== HTTP_LANGUAGE) return null;
|
||||
return urlTargets(view.state.doc, offset);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Заблокувати користувача