diff --git a/src/features/django/index.ts b/src/features/django/index.ts index 0a6bc595a..3b9969498 100644 --- a/src/features/django/index.ts +++ b/src/features/django/index.ts @@ -1,9 +1,14 @@ -/** Django: Run manage.py Task. */ +/** Django: Run manage.py Task, URL names in reverse() calls. */ +import { registerEditorContribution, registerNavigationProvider } from "@/editor/api"; +import { docOf } from "@/editor/lsp/lsp"; import { executeAction, registerActions } from "@/platform/actions"; -import type { RunConfig } from "@/platform/ipc"; +import { onIdeEvent, type RunConfig } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { notifications, registerRunAnythingProvider } from "@/workbench/contributions"; import { loadManage, manageRunAnything, resetManage, useManage } from "./manage"; +import { forgetUrlNames, urlCompletionExtension, urlNavigation } from "./urls"; + +const isUrls = (path: string) => path.endsWith("/urls.py"); const run = (config: RunConfig) => executeAction("run.runConfig", config); @@ -27,8 +32,20 @@ export function activate(): () => void { }, }, ]), + registerNavigationProvider(async (view, offset) => { + const doc = docOf(view); + if (!doc || doc.virtual || doc.language !== "python") return null; + const line = view.state.doc.lineAt(offset); + return urlNavigation(line.text, line.from, offset); + }), + registerEditorContribution({ id: "django.urlNames", extensions: urlCompletionExtension }), + onIdeEvent("fsChanged", (e) => { + if ([...e.created, ...e.modified, ...e.removed].some(isUrls)) forgetUrlNames(); + }), useProject.subscribe((s, prev) => { - if (s.project?.root !== prev.project?.root) resetManage(); + if (s.project?.root === prev.project?.root) return; + resetManage(); + forgetUrlNames(); }), ]; return () => disposers.reverse().forEach((d) => d()); diff --git a/src/features/django/urls.test.ts b/src/features/django/urls.test.ts new file mode 100644 index 000000000..e559dfa10 --- /dev/null +++ b/src/features/django/urls.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from "vitest"; +import { urlNameAt, urlNames } from "./urls"; + +const URLS = `from django.urls import path + +app_name = "shop" + +urlpatterns = [ + path("", views.product_list, name="products"), + path("products//", views.product_detail, name='product'), +] +`; + +describe("URL names", () => { + it("reads the names of a urls.py under its app_name", () => { + expect(urlNames("/p/shop/urls.py", URLS)).toEqual([ + { + name: "shop:products", + path: "/p/shop/urls.py", + range: { start: { line: 5, col: 39 }, end: { line: 5, col: 47 } }, + }, + { + name: "shop:product", + path: "/p/shop/urls.py", + range: { start: { line: 6, col: 62 }, end: { line: 6, col: 69 } }, + }, + ]); + expect(urlNames("/p/urls.py", `urlpatterns = [path("", home, name="home")]`).map((u) => u.name)).toEqual(["home"]); + }); + + it("finds the name in reverse() and the like", () => { + const line = ` return redirect("shop:product", slug=p.slug) or reverse_lazy('home')`; + const at = line.indexOf("shop:") + 3; + expect(urlNameAt(line, at)).toEqual({ name: "shop:product", from: 21, to: 33 }); + expect(urlNameAt(line, line.indexOf("home"))).toMatchObject({ name: "home" }); + expect(urlNameAt(`render(request, "shop/list.html")`, 20)).toBeNull(); + expect(urlNameAt(`reverse("a", args=["b"])`, 20)).toBeNull(); + }); +}); diff --git a/src/features/django/urls.ts b/src/features/django/urls.ts new file mode 100644 index 000000000..19d255d4e --- /dev/null +++ b/src/features/django/urls.ts @@ -0,0 +1,100 @@ +/** + * URL names in Python code: in `reverse("shop:product")` (and `reverse_lazy`, `redirect`, + * `resolve_url`) the name completes from the project's `urls.py` files and Go to Declaration + * opens its `path(..., name=...)` entry. + */ +import type { CompletionContext, CompletionResult } from "@codemirror/autocomplete"; +import { EditorState, type Extension } from "@codemirror/state"; +import type { EditorDocContext } from "@/editor/api"; +import type { NavigationMatch } from "@/editor/navigationProviders"; +import { call, commands, type Range } from "@/platform/ipc"; +import { useProject } from "@/platform/project"; + +export interface UrlName { + /** With the module's `app_name` namespace: `shop:product`. */ + name: string; + path: string; + /** The name inside its quotes (0-based line, UTF-16 column). */ + range: Range; +} + +const CALL = /\b(?:reverse|reverse_lazy|redirect|resolve_url)\(\s*$/; + +/** The URL names a urls.py defines: `name="…"` arguments, under the module's `app_name`. */ +export function urlNames(path: string, text: string): UrlName[] { + const app = /^app_name\s*=\s*["']([^"'\n]+)["']/m.exec(text)?.[1]; + return [...text.matchAll(/\bname\s*=\s*(["'])([^"'\n]+)\1/g)].map((m) => { + const from = m.index + m[0].length - m[2]!.length - 1; + const line = text.slice(0, from).split("\n").length - 1; + const col = from - text.lastIndexOf("\n", from - 1) - 1; + return { + name: app ? `${app}:${m[2]}` : m[2]!, + path, + range: { start: { line, col }, end: { line, col: col + m[2]!.length } }, + }; + }); +} + +/** The URL name string under `offset`, when it is the first argument of `reverse(` and the like. */ +export function urlNameAt(text: string, offset: number): { name: string; from: number; to: number } | null { + const start = text.lastIndexOf("\n", offset - 1) + 1; + const end = text.indexOf("\n", offset); + const line = text.slice(start, end < 0 ? text.length : end); + for (const m of line.matchAll(/(["'])([\w:.-]*)\1/g)) { + const from = start + m.index + 1; + const to = from + m[2]!.length; + if (offset >= from && offset <= to && CALL.test(line.slice(0, m.index))) return { name: m[2]!, from, to }; + } + return null; +} + +let cache: Promise | null = null; + +/** Every URL name of the project's own urls.py files (read once, until one changes). */ +export function projectUrlNames(): Promise { + cache ??= (async () => { + const paths = (await call(commands.findFilesNamed("urls.py"))) + .map(String) + .filter((p) => !/\/(?:site-packages|node_modules|\.venv|venv)\//.test(p)); + const texts = await Promise.all(paths.map((p) => call(commands.fsReadText(p)).catch(() => ""))); + return paths.flatMap((p, i) => urlNames(p, texts[i]!)); + })().catch(() => []); + return cache; +} + +export const forgetUrlNames = () => void (cache = null); + +/** `line`: the text of the line at `offset`, which starts at `lineFrom`. */ +export async function urlNavigation(line: string, lineFrom: number, offset: number): Promise { + const at = urlNameAt(line, offset - lineFrom); + if (!at?.name) return null; + const names = (await projectUrlNames()).filter((u) => u.name === at.name); + if (!names.length) return null; + const targets = names.map(({ path, range, name }) => ({ path, range, name })); + return { from: lineFrom + at.from, to: lineFrom + at.to, targets }; +} + +function where(path: string): string { + const root = useProject.getState().project?.root; + return root && path.startsWith(`${root}/`) ? path.slice(root.length + 1) : path; +} + +async function completion(ctx: CompletionContext): Promise { + const before = ctx.state.sliceDoc(Math.max(0, ctx.pos - 200), ctx.pos); + const m = /(["'])([\w:.-]*)$/.exec(before); + if (!m || !CALL.test(before.slice(0, m.index))) return null; + const names = await projectUrlNames(); + if (ctx.aborted || !names.length) return null; + return { + from: ctx.pos - m[2]!.length, + validFor: /^[\w:.-]*$/, + // Above what the language server offers in the string (parameter names). + options: names.map((u) => ({ label: u.name, type: "constant", typeText: where(u.path), boost: 50 })), + }; +} + +export function urlCompletionExtension(doc: EditorDocContext): Extension { + return doc.language === "python" && !doc.virtual && !doc.readOnly + ? EditorState.languageData.of(() => [{ openideaCompletion: completion }]) + : []; +}