Порівняти коміти

..
Автор SHA1 Повідомлення Дата
l0ng-ai bcb2728bcd docs(themes): Regenerate the token reference 2026-10-27 22:51:39 +08:00
l0ng-ai 5c50215d39 Merge remote-tracking branch 'origin/main' into feat/remote-development
# Conflicts:
#	crates/ide-jsonrpc/src/lib.rs
2026-10-27 22:47:56 +08:00
l0ng-ai 03ed6be82d fix(ui): Activating a tool window focuses its content, not its first button 2026-10-27 22:39:43 +08:00
l0ng-ai beee4cb633 fix(agents): Report starting the agent separately, and a refused sign-in with how to sign in 2026-10-27 22:36:55 +08:00
l0ng-ai 21c2934a36 fix(terminal): Open OSC 8 hyperlinks in the system browser 2026-10-27 22:22:44 +08:00
l0ng-ai d9ff048a14 feat(app): Closing the last project window shows the welcome screen 2026-10-27 22:20:25 +08:00
l0ng-ai a8ce8048f8 feat(remote): Show the connection progress while reopening a remote project at startup 2026-10-27 22:16:22 +08:00
l0ng-ai 8eb4834a48 fix(lsp): Make the language server tests deterministic (#107)
`ide-lsp` tests failed intermittently (about 1 run in 4, more under load).

The main cause, requests from the other side starting before earlier notifications, is fixed on main by b5bc150; this adds a regression test for it in `ide-jsonrpc`, documents that `on_notification` must not wait for the remote side, and drops the 200 ms polling workaround in `ide-agent` that papered over the same race for `session/request_permission`.

Test-only races fixed: mock replies are held with a gate instead of fixed sleeps and timing asserts; the indexing-retry test ends indexing after the first reply; the crash test counts Ready/Crashed transitions instead of reading the last status; the mock records a notification and applies its effect under one lock; `eventually` waits up to a deadline; `tcp_server` reserves its port with a bound socket and waits for the server's start marker.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 22:15:47 +08:00
l0ng-ai 831cd59ab0 feat(remote): Run agent tasks on the remote host; show why the agent list cannot load 2026-10-27 22:12:58 +08:00
l0ng-ai ec41d3c4a0 feat(remote): Show the stages and transfer progress of connecting; primary button hover follows the accent 2026-10-27 22:04:59 +08:00
l0ng-ai d7ff91760f fix(agents): Keep a deleted task from being saved again
A save that ran after delete (from the turn ending or the agent exiting)
wrote the task file back, so the task returned on the next load. Deleting
now marks the task under the lock that saving holds.
2026-10-27 22:03:24 +08:00
l0ng-ai 60f2f47547 ci: Cut redundant work from the CI run
- Save the Rust cache only on main; pull requests restore it.
- Check bindings against the file nextest's export_bindings already wrote
  instead of compiling the test again.
- Run e2e in its own job on the Playwright image, in parallel with the
  frontend checks, without installing browsers on each run.
- Cancel a run in progress when a newer push arrives, on main too.
2026-10-27 21:50:42 +08:00
l0ng-ai 6bcf99778a fix(remote): Reopen and list recent remote projects; missing rustup components and spelling of references
- Remote projects are remembered as ssh:// locations: the last session and
  a reloaded window reconnect, and the welcome screen lists them
- A rustup proxy without its component counts as a missing tool (install
  hint) rather than a server crashing on every restart
- Typo checks the names declarations introduce, not path segments and
  calls; more programming terms in the word list
- The query history records statements in execution order
2026-10-27 21:43:36 +08:00
l0ng-ai b5bc1501a0 fix(jsonrpc): Start incoming requests after the notifications that preceded them
Requests from the other side were handed to the blocking pool as soon as
they were read, while notifications run in order on their own thread, so a
request could be handled before an earlier didOpen. Requests now start from
the same ordered queue.
2026-10-27 21:38:42 +08:00
l0ng-ai 6d59cf377c chore: Update Cargo.lock for 0.1.18 2026-10-27 21:19:25 +08:00
l0ng-ai 0217586e2e Merge remote-tracking branch 'origin/main' into feat/remote-development 2026-10-27 21:18:06 +08:00
l0ng-ai db3b17d863 test(remote): Build the Unix path fixture only on Unix 2026-10-27 21:18:06 +08:00
l0ng-ai cf421e4fcb chore: v0.1.18 2026-10-27 21:15:46 +08:00
l0ng-ai f0261719b8 Merge remote-tracking branch 'origin/main' into feat/remote-development
# Conflicts:
#	src-tauri/src/state.rs
2026-10-27 21:13:02 +08:00
itningтаitning 8d4c8885ca fix(diff): Jump to Source from a commit's file, next to Next Difference (#103)
Jump to Source moves next to Next Difference in the diff toolbar, with the edit (pencil) icon. Diffs where neither side is a file on disk (Git Log commit files, Compare Branches, multi-commit diffs) now offer it when the path still exists in the working copy; files deleted by the commit don't.

Jumping from a pane that isn't the opened file used the other pane's caret. The caret line is now mapped through the diff to the target (the other pane's document, or the working copy read at click time including unsaved edits); the column is kept only when the line is unchanged.

Co-authored-by: itning <[email protected]>
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 21:10:53 +08:00
l0ng-ai b556a064e3 fix(remote): Map Windows project paths onto run targets and order query history by time 2026-10-27 21:06:46 +08:00
l0ng-ai 2fe6b7b87b feat(editor): Implemented / overridden gutter markers (#106)
Adds gutter markers for type hierarchies: ↓ "Is implemented / Has subclasses / Is overridden" on types and methods with implementations, ↑ "Implements / Overrides method in X" on methods that implement or override a supertype method. One target navigates, several open the Choose Implementation popup. Settings › Editor › General › Gutter Icons gets two toggles.

Markers come from the language server: a new batched `lsp_implementations` command (up to 4 concurrent, cancellable, sharing the goto query path) for ↓, and the Super Method lookup for ↑, which now prefers the overload with matching parameters (also fixes ⌘U on overloads). Only declarations near the viewport are queried, debounced, cached per document version and cancelled when superseded. Works for any server with implementation / type hierarchy support; servers without type hierarchy show ↓ only.

Fixes #99

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 21:02:54 +08:00
itningтаitning 6de6b33449 feat(vcs): Double click switches the log and Cmd/Ctrl+C copies branches in Git Log (#101)
In Git Log's Branches pane a single click filtered the log and a double click checked the branch out. Now a click only selects; double click or Enter shows the log of that ref (also the current branch and HEAD), and Enter on a multi-selection filters by all selected branches in tree order. Checkout stays in the context menu. The pane keeps its own selection and follows filter changes made elsewhere. Cmd/Ctrl+C copies the selected branches as full refs, one per line (HEAD as `HEAD`), leaving copy in the search field and elsewhere untouched.

Co-authored-by: itning <[email protected]>
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 21:02:10 +08:00
l0ng-ai c3b64304ac fix(agents): Offer project skills and commands in the composer (#105)
The new-task composer only offered the slash commands the agent sent in its last session, stored once globally, so a project that hadn't run a session (or whose last session ran elsewhere) showed none of its skills; matching was substring-only, so `/trig` didn't find `/triage` either.

Offered commands are now remembered per project root. For Claude Code, skills (`.claude/skills/*/SKILL.md`) and commands (`.claude/commands/**/*.md`) are also discovered on disk, from the working directory up to home plus `~/.claude`, and merged with the agent's list (agent entries win). New `agent_commands` IPC serves the home composer (project root) and session composer (the task's cwd). Matching ranks prefix (also after `plugin:`), then substring, then subsequence. Picking a completion inserts `/name ` and the prompt is sent unchanged.

Not covered: a new worktree of a project that gitignores `.claude/` doesn't contain the project's skills.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 21:01:11 +08:00
l0ng-ai 14f72fc307 feat(http-client): Generate requests from Spring Web mappings (#104)
Spring MVC/WebFlux mapping methods (Java and Kotlin) in @RestController / @Controller classes get a gutter icon with "Generate Request in HTTP Client", and the class line gets "Generate Requests for Controller"; both are also actions (`http.generateSpringRequest`, `http.generateSpringControllerRequests`) in Find Action, the editor menu and ⌥↵. Requests are appended to the `generated-requests.http` scratch and opened at the new request.

The URL combines class and method paths ({id} and {id:regex} → {{id}}), with host, port and context path from application.properties / .yml (default http://localhost:8080). @RequestParam, @RequestHeader, params/headers, consumes/produces and @RequestBody (JSON skeleton from the body type, nested one level) are mapped; same-file constants are resolved. Adds an `intentionProviders` editor facet so features can contribute ⌥↵ items.

Fixes #100

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 20:54:48 +08:00
l0ng-ai a065189516 fix(lsp): Launch jdtls with java and surface output outside the protocol (#102)
On Windows the auto-downloaded jdtls ran through `bin\jdtls.bat`, which starts the Python wrapper and ends with `pause`. When Python failed, `pause` wrote its prompt to stdout in the console code page; the transport treated that as a broken LSP header ("non-UTF-8 header"), dropped it and the server was killed, so the status only said "exited with code 1".

- jdtls is now launched with `java` directly on every platform (Equinox launcher, platform config dir, JDK 24+ flags, forwarded JVM args, `-data`), so Python is no longer needed; user wrappers and unusual layouts keep their command.
- Output outside the protocol on stdout (and non-UTF-8 stderr) is decoded (UTF-8, then the Windows OEM/ANSI code page, then lossy), logged and included in the crash message, for stdio and TCP language servers, DAP adapters and ACP agents.
- Crash messages carry the real exit code and the last lines of output.

未在 Windows 实机验证 (Windows paths covered by unit tests and a Windows-target clippy run).

Fixes #90

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 20:44:09 +08:00
l0ng-ai 9258185517 fix(ui): Keep focus rings off text fields regardless of role
Search Everywhere's query input carries role="combobox", so the global
focus-ring rule drew an outline inside its frame. Text inputs are now
excluded from that rule, and the selector list uses :where() so its
specificity matches a bare :focus-visible and component focus styles
always take precedence.
2026-10-27 20:43:51 +08:00
l0ng-ai feb882ed10 fix(remote): Harden deployment paths, server install and remote routing
- Deployment: reject `..` and `/` in remote names and sync paths, keep
  excluded paths root-relative, never delete a mapping's remote root,
  apply excludes to Sync steps
- SSH: connect per configuration so an unreachable host doesn't block
  others; `--` before the host in ssh arguments; read timeout on askpass
- Run targets: export the environment for shell command lines; map the
  project folder only as a whole path
- Remote development: use the server's resolved project root; give
  streaming channels connection-wide ids; per-install `.part` files;
  probe output between markers; require the server's SHA-256; unique
  download staging folders; stop runs, terminals and language servers
  when the server exits
- JSON-RPC: deliver a response after the notifications sent before it
- Release: build the aarch64 musl server with musl-gcc
2026-10-27 20:33:09 +08:00
l0ng-ai e82d766804 Merge remote-tracking branch 'origin/main' into feat/remote-development
# Conflicts:
#	src-tauri/src/commands/build.rs
#	src-tauri/src/commands/debug.rs
#	src-tauri/src/commands/lsp.rs
#	src-tauri/src/commands/run.rs
#	src-tauri/src/commands/tools.rs
#	src-tauri/src/commands/vcs.rs
#	src-tauri/src/state.rs
#	src/bindings.ts
#	src/features/database/SourceDialog.tsx
#	src/features/index.ts
#	src/platform/locales/text/zh.json
#	src/styles/tokens.css
2026-10-27 19:33:02 +08:00
l0ng-ai 8568ff001b fix(i18n): Translate interpolated UI text through a tx template tag (#98)
About 270 UI strings were JS template literals with interpolation passed to exact catalog lookup, so they could never be translated; about 25 more spliced lowercase English fragments into sentences.

`tx` now also works as a template tag (`` tx`Remove data source '${name}'?` ``): it looks up the entry by the literal parts (`{name}` placeholders), translates interpolated values that have entries, and remembers generated text so it re-translates after a language switch or when the catalog loads later. Call sites use the tag; spliced fragments are written as whole sentences per variant. Adds 280 Chinese translations and removes stale ones.

The catalog guard now also fails on untagged template literals reaching exact lookup (unless allowlisted with a reason), tagged text missing from the catalog, case-changed or ternary-suffix interpolations, and duplicate template patterns.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 18:56:54 +08:00
胡翀таzzxzz12345 2a8e5e791d fix(diff): Keep distant edits as separate chunks and render continuous change blocks (#95)
In long files several distant edits could collapse into one large replacement that highlighted unchanged lines, and multi-line changes showed per-line stripes.

Lines are now matched before character-level changes. The line diff is anchored on lines that occur once on both sides (patience-style), with a bounded diff only between anchors, so 50k-line files with distant edits keep separate chunks. Whole inserted or deleted lines render as continuous blocks; paired modified lines keep the line background plus word-level highlights, also inside multi-line chunks. Line decorations are built for the visible range only.

Co-authored-by: zzxzz12345 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 18:54:15 +08:00
Walter WangтаClaude 3b608355a3 feat(themes): Built-in Solarized and One light/dark themes (#85)
Adds Solarized Light, Solarized Dark, One Light and One Dark as built-in themes across the workbench, editor, terminal and Mermaid, keeping the original palettes (MIT notices in THIRD_PARTY_LICENSES.md, mapping in docs/themes.md).

- Native window colours are read from tokens.css at build time (`theme_frame.rs`) instead of a second colour table.
- Startup restores the built-in palette before first paint.
- Editor schemes, the terminal and Mermaid refresh on switches between themes of the same kind.
- The plugin packages of the same themes are removed.
- A test requires every built-in palette theme to define each token (only listed ones may inherit from the base theme), so new tokens can't silently miss them.

Co-authored-by: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 18:53:12 +08:00
l0ng-ai 50308a956d feat(maven): Dependency Analyzer with conflicts, usage paths and Exclude (#97)
Maven sync ran a plain `dependency:tree`, which drops the nodes Maven omits (conflict losers, duplicates), so there was no data to show where a conflict comes from.

Sync now runs `maven-dependency-plugin:3.8.1:tree -Dverbose` (the project's own plugin version when offline) and records omitted / conflictWith / managedFrom per node; omitted nodes stay out of the Maven tool window and External Libraries, and older caches are marked stale. The new Dependency Analyzer (Maven toolbar, module / Dependencies node menus, pom.xml menus, Find Action) lists a module's resolved artifacts or tree with conflict markers, search and scope filters, and every path to the selected artifact with its status. Exclude writes an `<exclusion>` into the declaring pom (module or in-project parent), keeping indentation and line endings, then reloads; Jump to Source and Copy Coordinates are on each path. Gradle is not covered yet.

Fixes #87

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 18:51:33 +08:00
l0ng-ai c666cee075 feat(project-tree): Expand directories with a single click (#96)
Adds "Expand Directories with Single Click" to the Project view options (Behavior group, off by default). When on, a plain left click anywhere on a directory row expands or collapses it (a compacted path opens its whole chain); a double-click toggles once, and modifier clicks, drag-and-drop, inline rename and the context menu are unaffected. Project view option toggles now show their checked state in Find Action, and the options menu is translated.

Fixes #94

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 18:43:39 +08:00
l0ng-ai a971483162 fix(ci): Drop a stale Chinese catalog entry and unix-only imports on Windows
The Solarized dark variant's description no longer appears in the source, so the
catalog check failed on its leftover translation. LaunchError and Launcher are
only used by the unix-only tcp_server tests and were unused imports on Windows.
2026-10-27 18:17:06 +08:00
l0ng-ai 2616e3fe6d fix(i18n): Translate missing Chinese UI text and guard the text catalog (#93)
The source-text catalog (locales/text/zh.json) is keyed by the English text, so reworded or new UI strings silently fell back to English (e.g. the language server JDK hint on the Java settings page). Adds the 321 missing Chinese translations, updates the 3 reworded keys, removes 45 stale keys, and makes the file-refactoring failure titles translatable.

New guard test (src/platform/locales/textCatalog.test.ts) parses src/ for literals that reach the translation layer and fails on missing or stale catalog entries, with an explicit allowlist for untranslated text.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 18:04:35 +08:00
l0ng-ai b5bd8e3935 chore: v0.1.17 2026-10-27 17:58:55 +08:00
l0ng-aiтаbghtyu 24330192c0 feat(themes): Solarized and One theme plugins (#92)
Solarized Light/Dark and One Light/Dark ship as theme plugins (plugins/theme-solarized, plugins/theme-one) with complete UI tokens, editor schemes and terminal colours, and the original palettes' MIT notices. Installing a plugin now also copies its LICENSE/NOTICE/COPYING.

Theme handling that benefits every plugin theme:
- The last plugin theme is restored before first paint (splash, workbench); the native window background follows the theme.
- HSL / color-mix() / var() colours are resolved to RGB for native windows, the Settings window and Mermaid.
- Mermaid, the terminal and editor schemes follow switches between two themes of the same kind.
- An explicitly chosen editor scheme wins over a plugin theme's editor colours.

Supersedes #85.

Co-authored-by: bghtyu <[email protected]>
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 17:53:07 +08:00
l0ng-ai 9d8280104f fix(agents): Run Codex through @agentclientprotocol/codex-acp
@zed-industries/codex-acp is archived; its last release (0.16.0) bundles an
older Codex core that doesn't know newer models, so a config.toml model such as
gpt-6.1-sol fell back to generic metadata with a warning. The maintained adapter,
@agentclientprotocol/codex-acp, runs the Codex App Server from a current
@openai/codex and lists the account's models.
2026-10-27 17:44:04 +08:00
l0ng-ai 5e099ecb5b ci: Build the remote development server for release and check its Linux build 2026-10-27 17:43:48 +08:00
l0ng-ai 7531834b3c feat(remote): Add SSH configurations, SFTP deployment, run targets and remote development
SSH (new crate ide-ssh)
- Shared SSH configurations (Settings › Tools › SSH Configurations) used by
  data source tunnels, deployment servers, run targets and terminals.
- Sessions with password, key pair or OpenSSH config + agent authentication,
  ProxyJump, known-hosts checks with trust prompts, saved secrets.
- Askpass: credential prompts of `ssh` / `git` child processes shown in the IDE.
- Tools › Start SSH Session opens a terminal tab for a configuration.
- The database SSH tunnel moves here and can use a shared configuration.

Deployment (new crate ide-remote)
- Settings › Build › Deployment: SFTP servers, path mappings, excludes,
  automatic upload on save.
- Upload / download / delete / sync with the server, Tools › Deployment
  menu, Remote Host tool window for browsing and editing remote files.

Run targets
- Settings › Build › Run Targets: run configurations can run on an SSH host
  ("Run on"), with project sync before the run and remote interpreters.

Remote development
- New crate ide-host: the backend (projects, documents, LSP, VCS, search,
  terminals, runs, debugger, tools, settings) shared by the desktop shell and
  `openidea-server`, a headless server speaking JSON-RPC over stdio.
- File › Remote Development… connects to an SSH host, installs the server
  (per-build folder, streamed gzip upload; the release build is downloaded
  when there is none locally) and opens a remote folder in the window.
- The window routes the commands the server answers to it; streaming
  channels and events are forwarded. The server takes the login shell's PATH.
2026-10-27 17:43:48 +08:00
l0ng-ai 0ab50e0dd4 fix(theme): Define the chart series colour in the high-contrast dark theme 2026-10-27 17:43:37 +08:00
l0ng-ai 8ec35c94ec fix(lsp): Find usages in unopened files outside the language server's scope (#89)
Reference-based features only asked the language server, which analyses its configured scope (e.g. Pyright's `include`) plus open files, so usages in unopened files outside that scope were silently missing.

Find Usages, Show Usages, usage counts, Safe Delete of a symbol, Rename and incoming calls now also search project files that contain the symbol's name (whole word, same language, .gitignore/excludes respected, unsaved edits included, up to 300 files, cancellable). Candidates the first answer didn't cover are opened in the server temporarily, queried and closed again. Files the user has open are never touched, and diagnostics for the temporary files are dropped. An empty result while the server is still indexing is retried once indexing ends. When results may still be incomplete (indexing, file cap), the usages UI says so. `lsp_references` takes a RequestId and is cancelled by newer searches. Not covered yet: the Safe Delete scan when deleting a file or directory.

Fixes #83

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 17:41:20 +08:00
l0ng-ai 482f391fd1 fix(splash): Draw the wordmark as outlines so it can't swap fonts
The splash set its wordmark in Inter, a web font with font-display: swap. When
the font arrived late (on a cold dev start the dev server took up to seconds to
serve it), the splash showed the system font first and switched to Inter just
before fading, so the word grew and shifted. It is now an inline SVG of the same
text in Inter 500, so it renders identically from the first frame. This replaces
the previous attempt, which only moved where the font faces load.
2026-10-27 17:35:37 +08:00
l0ng-ai 866af3d9cb fix(agents): Give child processes the login shell's environment
An app started from Finder or the Dock took only PATH from the login shell, so
variables exported in rc files never reached agents, language servers, run
configurations or debug targets. Claude Code configured for Bedrock through
CLAUDE_CODE_USE_BEDROCK / AWS_REGION in ~/.zshrc fell back to the Anthropic API
and failed with 403.

The shell's environment is read once at startup (in the background when PATH
comes from the cache) and kept in memory only, since it may hold secrets. Spawns
add the variables this process lacks before their own.
2026-10-27 17:16:47 +08:00
l0ng-ai 145f168525 fix(agents): Composer input-method Enter, autocorrect and task header layout
- Enter that commits an input-method candidate no longer sends the message.
- The composer turns off autocorrect, autocapitalisation and spellcheck, which
  popped up correction bubbles over prompts.
- The provider icon in the task header sits inline with the details instead of
  on its own line above them.
2026-10-27 17:16:46 +08:00
l0ng-ai f73c0a6b1d fix(keymap): Don't run the Enter that commits an input-method candidate
WebKit sends the Enter (or Space) that commits a candidate after compositionend,
with isComposing already false and keyCode 229. Handlers that checked isComposing
alone ran it: dialogs confirmed and shortcuts fired while typing Chinese or
Japanese. isImeKey() checks both, and every keydown handler that skipped
composition uses it.
2026-10-27 17:16:46 +08:00
l0ng-ai 2c576418cb fix(splash): Keep the wordmark still until the splash fades
The splash named Inter, but the font faces and antialiasing came with the app CSS,
which the dev server injects only once the bundle runs: the wordmark switched from
the system font to Inter, moving up and changing weight, just before the fade. The
font faces are now linked from index.html and the splash sets its own smoothing.
The splash also stays on one compositing layer, so the fade-out doesn't re-snap
the centred text.
2026-10-27 17:16:46 +08:00
l0ng-ai b70d487a92 fix(ui): Don't draw a second focus box inside framed text fields
The global focus ring listed input and textarea. :is() takes its most specific
argument, so the rule tied with the components' own :focus-visible resets and won
whenever it loaded later: the Settings search field and the agent composer showed
an inner square box inside their focus frame. Text fields show focus with their
frame; the ring stays on checkboxes, radios and other input controls.
2026-10-27 17:16:46 +08:00
胡翀таzzxzz12345 413564efcf feat(editor): Increase / Decrease Font Size, followed by the Markdown preview (#81)
Adds Increase Font Size / Decrease Font Size (⌥⇧. / ⌥⇧, in the IntelliJ keymaps) for the active editor, sharing the ±1px step and limits with ⌘/Ctrl + mouse wheel. The Markdown preview, including code blocks, follows its editor's zoom, and Reset Font Size in All Editors resets it too.

Editor zoom had no effect once a font size was configured in settings: the configured size was applied with higher precedence than the zoom. The zoom now wins.

Co-authored-by: zzxzz12345 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 16:52:25 +08:00
l0ng-ai f675dd2730 feat(vcs): Repository list in the Branches popup for multi-repository projects (#84)
In projects with several repositories the Branches popup began with a wrapping row of repository chips and listed only the selected repository's branches, and the widget always read "repo / branch".

The popup now lists one row per repository (colour, name, current branch) whose submenu holds that repository's branches. When all repositories are on the same branch the widget shows just the branch and the popup offers Common Local / Remote Branches whose actions apply to every repository; otherwise it shows the current repository's Recent / Local / Remote Branches and Tags. The widget and the popup use the same current repository: the one owning the active file, else the selected one. Search covers actions, repositories and branches across repositories. New Branch… creates the branch in all repositories. Single-repository projects are unchanged. `vcs_roots` now returns each repository's branch and HEAD.

Fixes #78

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 16:51:50 +08:00
l0ng-ai 4100ab3341 feat(markdown): Render Mermaid diagrams in the preview (#82)
Fenced ```mermaid blocks in the Markdown preview render as diagrams. mermaid (12.1.0) is loaded lazily, only when a preview contains a diagram, with securityLevel "strict"; colours come from the theme tokens and diagrams re-render on theme changes. Syntax errors show inline under the source. Rendered diagrams are cached by theme + source, so editing elsewhere in the file doesn't redraw them.

Fixes #79

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 16:23:35 +08:00
胡翀таzzxzz12345 fdd98fbfe4 feat(editor): Navigate inferred type inlay hints (#77)
Some language servers (basedpyright) send inferred type hints as plain text without a label-part location, so ⌘/Ctrl-clicking `: Series` did nothing. For a type hint right after an identifier, ⌘/Ctrl-click now goes to that identifier's type declaration; hints with locations keep direct navigation. Navigable hints show the link underline and pointer only while ⌘/Ctrl is held, including when it is pressed before hovering.

Co-authored-by: zzxzz12345 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 16:10:19 +08:00
l0ng-ai 5d2c89b0dd chore: v0.1.16 (#80)
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 15:36:39 +08:00
l0ng-ai d3f68ed180 fix(keymap): Navigate Back / Forward with the mouse side buttons (#76)
Keymaps only understood keystrokes, so the mouse back / forward side buttons did nothing. Keymaps now accept mouse shortcuts (Button4, Shift+Button5, …); every preset binds Button4 / Button5 to Navigate Back / Forward. The dispatcher handles side-button presses in the capture phase, so they work over the editor, tool windows and empty space, never change the editor selection and don't trigger the webview's own history navigation. Menus and tooltips keep showing the keyboard shortcut.

Fixes #75

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 15:34:54 +08:00
l0ng-ai e3dae02de7 feat(languages): Add Godot / GDScript support (#73)
Adds GDScript, Godot resource and shader file types with tree-sitter highlighting, the GDScript language server over TCP (a headless Godot editor started on demand), "Godot Project" / current-scene run configurations, and debugging through Godot's debug adapter.

Breakpoints set through symlinked paths (/tmp, /var on macOS) were reported verified but never hit; the adapter now sends resolved paths and maps stack frames back to the paths the IDE opened. The duplicate exit event Godot sends after disconnect is ignored.

Fixes #68

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 15:31:25 +08:00
l0ng-ai 2eaa6cb0c7 feat(agents): Dispatch and supervise coding agents over ACP (#72)
## What

An Agents feature: hand a task to a coding agent and follow it from the IDE. Agents speak the [Agent Client Protocol](https://agentclientprotocol.com). Built in: Claude Code, Codex, Gemini CLI, GitHub Copilot, Cursor, OpenCode, Qwen Code, Kimi CLI, Goose, Auggie, Kiro, Mistral Vibe. You can add your own in `<config>/agents/<id>.toml` (`name`, `command`, `args`, `[env]`). An agent whose command isn't on `PATH` is listed under "Not installed".

- **New Task page.** An editor page with a prompt box. You pick the agent, model, mode and thinking effort, and whether it runs in a new worktree (from a chosen branch) or in the checkout.
- **Agents tool window.** Tasks are grouped by what they need: Needs Input, Failed, Running, Ready to Review, Archived. It also shows a stripe badge, a status bar item and notifications.
- **Session tab.**
  - The conversation: Markdown, thinking, tool calls with diff stats, the plan.
  - Permission requests appear as an editor banner.
  - The changed files are compared against the task's base commit and open in the diff viewer.
  - A context-usage meter.
- **Composer.**
  - `@` references files from the project index. A referenced file's text is sent when the agent accepts embedded context, otherwise a `resource_link`.
  - `/` lists the agent's commands; ↑/↓ recall earlier messages.
  - Images can be pasted, dropped or picked; Shift+Tab cycles the mode.
  - While the agent works, Enter queues the message and ⌘↵ interrupts the turn and sends it now. Queued messages can be sent now, edited or removed.
- **Send to Agent.** In the editor and Project view context menus. It adds `@file:10-20` references to the message you typed last.
- Landing the work is left to the existing Git features: the worktree is a normal branch.

## How

- **`crates/ide-agent`**: one ACP agent process per task, running in the task's directory.
  - It folds `session/update` into a timeline and answers `session/request_permission`.
  - It tracks the session's settings: `configOptions`, falling back to the older `modes` / `models` fields. The user's choices are applied again on every new or reloaded session.
  - Tasks are saved as JSON per task. Sessions resume with `session/load` without duplicating history, and old task files are upgraded when read.
- **`ide-jsonrpc`**: adds newline-delimited framing (ACP) next to Content-Length (LSP).
- **`ide-vcs`**: adds `numstat_with_worktree` for the changes list.
- **Editor**:
  - The Switcher (⌃⇥) lists every open editor, as IDEA does, not just files.
  - `EditorKindContribution.restorable` lets a kind's tabs be saved with the editor session, so agent tabs survive reloads.
- **UI**: `Select` popups now size to fit short option details (they used to cut them off).

## Testing

- `cargo test -p ide-agent` (14 tests). They use an in-memory ACP agent and cover permissions, restart with `session/load`, settings, the queue with send-now and stop, and prompt blocks.
- There is also an ignored end-to-end test against the real Claude Code adapter: `OPENIDEA_AGENT_E2E=1 cargo test -p ide-agent real_claude -- --ignored`.
- `pnpm vitest run`: 2052 tests.
- `cargo clippy -p ide-agent -p openidea --all-targets -- -D warnings` and typecheck are clean.
- Driven in the running app with Claude Code:
  - A task in a worktree, with permission answers, changes listed and the diff opened.
  - A follow-up after restarting the IDE.
  - `@` reference with embedded content, `/` commands, Shift+Tab mode changes.
  - A queued message and send-now, an image prompt, Send to Agent from a selection.
  - The Switcher and restored tabs after a reload.

Not covered yet: only Claude Code was driven end to end. The other agents go through the same ACP path but weren't run here.


Merged on the maintainer's go-ahead. Merge with main: kept both sides in Cargo.toml (ide-agent + ide-clipboard), the feature registry (agents + vim) and zh.json (no duplicate keys). After the merge: typecheck clean, full vitest 268 files / 2247 tests passed, ide-agent/ide-jsonrpc/ide-vcs 144 passed, bindings unchanged after regeneration.
2026-10-27 14:44:28 +08:00
胡翀 b4742b730b Support custom editor font weights in steps of 50 (#74)
JetBrains Mono at 500 looks heavier than PyCharm's rendering, and 450 was rounded up to 500. Adds a Custom option with an input to the Main and Bold font weight rows; weights are clamped to 100–900 and rounded to the nearest 50 (the terminal, which follows the editor weights, gets the same steps).

Verified in the app: Custom → 450 shows the input and the preview renders at 450. Maintainer follow-up: formatted the dialog test.

Related: #32

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 14:38:42 +08:00
胡翀 566b4fdeb1 Make static symbol italics configurable (#70)
Python language servers mark class attributes `static`, so their declarations and `self.` references rendered italic, unlike PyCharm. Adds Color Scheme attributes for static symbols: Language Defaults › Identifiers › Static symbol (italic by default, as IDEA's static members) and Python › Static symbol (upright by default), applied to semantic tokens with the `static` modifier.

Verified in the app with basedpyright: a class attribute renders upright by default and italic when Python › Static symbol › Italic is on, applied live. Maintainer follow-up: formatted colorScheme.tsx.

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 14:27:24 +08:00
l0ng-ai 12418e0184 fix(editor): Show the ⌘/Ctrl-hover link without waiting for documentation (#71)
## Problem
When you hold ⌘ (Ctrl on Windows/Linux) and hover a symbol, the navigation link had three problems:
- **Slow to appear.** The underline and pointer cursor only showed after the hover (documentation) request came back. A slow or stuck documentation request held the link back.
- **Errors were remembered.** If the definition request was cancelled or the server returned an error, the word was cached as having no target. Hovering it again never retried.
- **Stuck requests blocked the word.** A definition request that never answered blocked any new request for that word.

## Changes (`src/editor/lsp/navigation.ts`)
- The link is underlined as soon as a definition is found. The short summary is filled in when the hover request returns.
- Cancellation and server errors count as temporary failures. They are no longer cached, so the next hover retries.
- A definition request expires after 3 seconds and can be retried. If its response arrives later, it is ignored.
- Responses for a word the pointer has already left, or that arrive after the editor plugin is destroyed, are dropped.

## Tests
- Four new tests in `src/editor/lsp/syntaxFallback.test.ts`: the link shows without waiting for documentation, a temporary error is retried, a stuck request is retried and its late answer ignored, and a late answer for the previous word is ignored. The first three fail without this change.
- `pnpm vitest run src/editor/lsp`: 179 passed. `pnpm typecheck` and eslint report no errors.
- Checked in the running app (`fixtures/ts-app`, `cart.ts`): ⌘-hover over `Cents` showed the underline and pointer cursor after 312 ms and the type summary after 356 ms. ⌘-click opened `money.ts` at the definition.
2026-10-27 14:23:23 +08:00
l0ng-ai 7f0fd870e8 fix(themes): Define the chart token in the high-contrast dark theme (#69)
contrast.test.ts requires every plain colour token in high-contrast dark; #63 added
--chart-series to the dark and light themes only.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 13:46:12 +08:00
l0ng-ai 575749f2ac feat(database): Add query plans, history, data tools and schema objects (#63)
New query, data and schema tools for the Database feature (SQLite, PostgreSQL, MySQL).

## Query console
- **Explain Plan** tab: SQLite `EXPLAIN QUERY PLAN`, PostgreSQL `EXPLAIN (FORMAT JSON)`, MySQL `EXPLAIN`, parsed in `ide-db::plan` into an operation tree (Full Scan / Index Scan / Group By / Order By …) under a statement root, with a properties panel.
- **Query history**: `ide-db::history` keeps executed statements per project and source in `db-history.json` (deduplicated, newest first, 500 max). The Browse Query History dialog has search, a highlighted preview, Paste and Delete.
- Output reports `(execution: X ms, fetching: Y ms)`.

## Results and table editor
- Local column filter (value list with counts) and local sort.
- Record / Value / Aggregates side panel.
- Chart view (bar / line) with X / Y / aggregation settings.
- Export Data dialog: extractor, Transpose, headers, preview, file or clipboard. An extractor drop-down sits on the data toolbars.
- Import Data from a CSV / TSV file.
- Table editor gets a WHERE | ORDER BY row. Column headers fit their names, with filter and sort indicators. Whole SQLite REALs display as `3108`, not `3108.0`.

## Database tree
- Triggers under tables. Routines, sequences and materialized views under schemas, with DDL, Quick Documentation and Drop.
- Data source options: object filter, show template databases, single database mode (PostgreSQL).
- New ▸ Import from Clipboard (connection URLs).

## IPC
New commands: `db_schema_objects`, `db_object_ddl`, `db_explain`, `db_history`, `db_remove_history`. Bindings are regenerated.

## Testing
- `cargo test -p ide-db`: plan parsing, history store, trigger listing and DDL, explain without executing.
- `cargo clippy -p ide-db -p openidea --all-targets -- -D warnings` is clean.
- `pnpm typecheck` and eslint pass. `pnpm vitest run src/features/database src/ui`: 226 passed.
- Checked in the running app against a SQLite database:
  - tree with triggers
  - result grid, local filter, side panel
  - chart
  - Explain Plan
  - history dialog
  - Export Data dialog
  - table editor ORDER BY
  - CSV import




Merge follow-up: grouped the new `--chart-series` token under a section comment and added it to the theme plugin template (theme.test.ts on main), regenerated docs/plugins/themes.md. Re-verified after merging main on macOS with a SQLite source: query + result grid with filter/sort headers, Explain Plan tab (Full Scan / Index Scan / Group By / Order By), Query History dialog; `cargo nextest run -p ide-db` 45 passed, bindings unchanged after regeneration.

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 13:31:29 +08:00
l0ng-ai 580f0efc28 fix(ui): Keep the Select chevron at the right edge with a custom value (#67)
Select only wrapped its default label in the flex-filling value span, so a
renderValue (the data source Color picker's swatch + name) left the chevron
right after the text, short of the field's edge.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 13:20:43 +08:00
l0ng-ai dcdb0e48e4 fix(ui): Draw focus rings on controls only, not on focusable containers (#66)
The global :focus-visible outline loads after the module CSS with the same
specificity, so every focusable container without its own override (the
Intentions list, other list/tree/menu popups) got a square blue frame. ee99b36
patched the Select popup only. Like IDEA, rings now mark controls (buttons,
links, fields, checkboxes, tabs…); lists, trees, tables and popups show focus
through their selected row, and the few rows that drew frames use the hover
background instead.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 13:09:29 +08:00
l0ng-ai 17400ade3d test(e2e): Reach later Settings groups in the virtualized page tree (#65)
The Settings page tree only renders rows near the viewport. Editor › Vim (#55) pushed
Version Control past the rendered rows on CI's Linux runner, so the e2e failed on main.
Collapse Editor before checking the later groups.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 13:00:37 +08:00
l0ng-ai b91402ef7d chore: v0.1.15
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 12:10:30 +08:00
l0ng-ai 56fdc005a8 feat(java): Download JDK, and one-click "Download JDK N" where a JDK is missing (#61)
Root cause: the IDE only detected installed JDKs and had no way to get one, so any launch needing a newer JDK than the machine has (latest jdtls needs JDK 25) dead-ended in an error.

Fix: IDEA-style Download JDK (Eclipse Temurin via the Adoptium API, sha256-verified, reusing ide-tools' download/verify code; Windows zip, Linux tar.gz, macOS Contents/Home) into <app-data>/tools/jdks, registered with JDK discovery and rescanned after each download. "Download JDK…" in the Project SDK and Language Server JDK pickers; a one-click `java.downloadJdk` "Download JDK N" on "needs JDK N" errors, which also repoints a too-old language server JDK and restarts jdtls.

Siblings: language server status popup, jdtls launch notification, run console classpath error, debug launch, run configurations without a JDK ≥ the module's Java version, Maven/Gradle sync failures, single-file Java run without `java` on PATH.

Verified in the app on macOS: vendor/version/location queries, the Download JDK dialog from the Language Server JDK picker, a real Temurin 25.0.5 download that runs. Not verified on Windows/Linux.

Fixes #49

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 12:09:51 +08:00
l0ng-ai beab7992f9 feat(editor): Rainbow brackets and semantic highlighting (rainbow variables) (#59)
Two editor toggles under Settings › Editor › Color Scheme › Language Defaults (off by default, also actions `editor.toggleRainbowBrackets` / `editor.toggleSemanticHighlighting`):
- Semantic highlighting (IDEA's rainbow variables): locals and parameters coloured by a name hash from LSP semantic tokens; fields, statics, constants, library symbols untouched.
- Rainbow brackets (VS Code's bracket pair colorization): (), [], {} by depth, unmatched in the error colour, strings/comments skipped, visible ranges only with 4 KB checkpoints.
Palettes are theme tokens (`--syn-rainbow-1..5`, `--syn-bracket-1..3`, light/dark/high-contrast), editable in the Color Scheme tree, and mapped from IntelliJ `.icls` RAINBOW_COLOR1..5.

App verification on macOS found the colours were overridden by the syntax highlighter's span nested inside the marks; the rules now cover descendants. Verified both toggles on/off live in a TS file.

Fixes #56

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 11:52:07 +08:00
l0ng-ai 2e0c74e31d docs(themes): Regenerate the theme token reference (#62)
Main's Frontend CI failed: docs/plugins/themes.md was stale after a token change.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 11:47:39 +08:00
l0ng-ai e16253a890 fix(editor): Ctrl/⌘-hover info replaces hover popups instead of stacking (#60)
Root cause: the quick-doc/problem hover and the Ctrl/⌘-hover navigation info were independent tooltips with no precedence, and the navigation modifier was only seen through the focused editor's key events, so holding Ctrl stacked both popups.

Fix: a window-wide platform navigation modifier (`src/platform/navigationModifier.ts`, ⌘ on macOS / Ctrl elsewhere, unchorded); with it held the plain hover doesn't open, late LSP results are dropped, an open hover closes (unless the pointer is inside it, so Ctrl/⌘+C still copies), release clears the link and info.

Siblings: problem/diagnostic hover, debugger value tooltip, inlay hint hovers (no doc or underline through hints, platform-correct Ctrl/⌘-click), consistent ⌘ vs ⌃ handling on macOS.

Verified in the app on macOS (⌘): hover then ⌘ leaves only the nav info, ⌘ before hover shows only nav info, release clears both and plain hover returns. Not verified on Windows.

Fixes #57

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 11:46:04 +08:00
l0ng-ai 7146779f6c feat(vim): Built-in Vim mode modelled on IdeaVim (#55)
Built-in Vim emulation on @replit/codemirror-vim (approved dependency), modelled on IdeaVim: `vim.toggle` (Tools › Vim, status widget, Settings › Editor › Vim), mode widget (NORMAL / INSERT / VISUAL / V-LINE / V-BLOCK / REPLACE), IdeaVim-style shortcut conflict handlers (Vim / IDE per key) through a generic `registerKeyClaim()` in the keymap dispatcher, Escape closing popups first, a safe subset of ~/.openideavimrc / ~/.ideavimrc (set, map variants, mapleader, :action / <Action>()), Ex commands :w :wa :q :qa :wq :x :action. Covers file editors, diff editable side and merge result via a new `embeddedEditorExtensions` extension point.

Verified in the app on macOS: motions/operators, i/Esc/V mode widget, :w saves, Ctrl+R redoes in Vim without running Run, toggling off restores plain editing. Not verified on Windows/Linux.

Fixes #27

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 11:43:37 +08:00
l0ng-ai cac95273d3 fix(java): Detect Lombok inherited from outside the project (#58)
Root cause: `uses_lombok` only scanned build files ~3 levels below the jdtls root, so Lombok inherited from outside that tree was judged `Unused` and no `-javaagent` was attached — even when Lombok was installed in Settings.

Covered: parent pom only in the local Maven repository (settings.xml localRepository honoured, multi-hop, cycle-safe), parent via relativePath outside the workspace, outer Gradle root build / version catalog, `lombok.config` in ancestor directories, and a bounded source fallback (`import lombok.` / `@lombok.`) for convention plugins and BOMs. A Lombok jar installed in Settings is now always attached (like IDEA's always-on Lombok support). Detection in `java_resolve_launch` runs in `spawn_blocking`.

Fixes #33

https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 11:19:45 +08:00
胡翀 55f42d2771 fix(editor): match IDEA font weights in the light scheme (#35)
Root cause: the editor had no font weight settings, and the WebView rendering of JetBrains Mono looks lighter than IDEA (#32).
Fix (IDEA's Settings › Editor › Font › Typography Settings): configurable Main font weight and Bold font weight (defaults Regular 400 / Bold 700) via --fw-code / --fw-code-bold; colour-scheme bold/regular overrides still win; settings preview updates live. Replaces the Light-only 450/550 hard-coding.
Siblings covered: sticky lines, diff/merge, run console (ANSI bold), Find in Files, completion/parameter info/docs, database editor, debugger value popup, matched braces, terminal with "Use editor font".
Verified: scoped vitest, ide-core tests, in-app on macOS (settings UI, editor weight follows the setting end to end).

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:41:56 +08:00
l0ng-ai 30310810e6 feat(plugins): Theme plugin template, theme docs and IntelliJ theme import (#54)
Per the maintainer's decision for #24, no third-party themes are bundled; instead a theme plugin template and docs let the community port themes (IntelliJ also ships few themes, the rest are plugins).
- templates/theme-plugin: manifest-only plugin with an original example (Harbor Night) covering every UI token and an editor scheme (.icls equivalent).
- docs/plugins/themes.md: format, IntelliJ / VS Code porting tables, generated token reference (pnpm gen:theme-docs); drift test fails when tokens.css and the template/reference disagree.
- pnpm theme:import-intellij: dependency-free converter for IntelliJ .theme.json (+ .icls).
- Plugin host: themes may name an editor-scheme merged over the theme; comments allowed in theme files.
- Fixes: a plugin theme's kind now switches the light/dark base; plugin themes go in their own stylesheet so the Appearance accent and editor scheme still layer on top.
Verified: ide-plugin tests, vitest (44), in-app on macOS: installed the template from disk, applied Harbor Night (dark base + teal accent over Islands Light), back to default restored the light theme.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:39:08 +08:00
l0ng-ai b2fec4ae30 feat(vcs): Show combined changes for multiple selected log commits (#52)
Root cause: the Git Log already supported multi-selection, but the Changes pane only read the last clicked commit and the backend had no "changes across several commits" query.
Fix (IDEA): ide-vcs Repo::commits_changes merges per-file changes across selected commits (before the first touching commit ↔ after the last, renames chained, add-then-delete dropped); the details pane shows "N files in M commits" with the commit list; exactly two commits get "Compare Versions" (version ↔ version dialog, also fixing added/deleted sides).
Siblings covered: Show History for file/folder (same log), Local History multi-select (oldest ↔ newest diff).
Verified: ide-vcs tests incl. temp-repo commits_changes, vcs vitest (143), in-app on macOS: 3 contiguous commits → "2 files in 3 commits"; Cmd-click 2 commits → Compare Versions lists the right files.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:39:01 +08:00
l0ng-ai aa5821bb46 fix(spelling): Rename identifiers on Typo fixes, add inspection controls, cut false positives (#51)
Root causes: Typo "Change to" replaced one occurrence even for declared names (breaking references); inspections had no Disable / Edit settings controls anywhere; en_US Hunspell does not derive agent nouns like "checkpointer" and compound identifier words were flagged; local inspections (Typo, file references, SQL) never reached Problems.
Fix (IDEA): "Typo: Rename to…" renames via LSP rename, falling back to syntax-based local rename, never a silent single replace, and lists identifier-safe words only; intention submenu (→) with Edit inspection profile setting / Disable inspection for every inspection; Problems and Inspection Results get Disable Inspection, Change Severity, Edit Inspection Profile Setting; Typo options Process code / literals / comments; -er/-ers derivation and identifier compounds accepted, common misspellings still flagged.
Siblings covered: all LSP and built-in inspections' intentions, file reference and SQL inspections in Problems, severity counter.
Verified: ide-spell tests (issue examples + ~50 real misspellings), vitest (286), in-app on macOS: only 'mesage' flagged, submenu, Rename to → 'message' updated both occurrences.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:38:53 +08:00
kevinh eda8cae0a1 feat(project-tree): Exchange files with the OS clipboard on Windows (#41)
Exchange files with the OS clipboard on Windows: CF_HDROP read/write in a new ide-clipboard crate (Win32 FFI behind cfg(windows), stub elsewhere), three thin commands, and project-view copy/paste hooks. Pasting from Explorer always copies; our own hand-off falls back to the internal clipboard so Copy/Move dialogs and refactoring stay intact.
Verified: ide-clipboard round-trip tests, project-tree vitest (93), CI on all three OSes. Not verified on Windows hardware here.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:38:45 +08:00
l0ng-ai e150101244 feat(debugger): Remote debug run configurations (Remote JVM, Go, Python, Node.js) (#50)
Root cause: run configurations could only launch a process; there was no "attach to a debugger that is already listening" type, and debugServer only connected to 127.0.0.1.
Fix (IDEA / GoLand / PyCharm): debug-only Remote JVM Debug (attach / listen, live JDWP agent args per JDK version), Go Remote (dlv --headless, substitutePath), Python Remote Debug (debugpy --listen, pathMappings) and Attach to Node.js (inspector, localRoot/remoteRoot); debugServer accepts host:port and [v6]:port; JVM listen mode via a cancellable relay so java-debug can attach.
Verified: ide-dap tests (host:port, relay handshake, cancel), run/debugger vitest, in-app on macOS: Python Remote Debug attached to a debugpy server, paused at app.py:3 with variables. Remote JVM/Go/Node not exercised in-app.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:38:38 +08:00
l0ng-ai 642b80cef0 fix(workbench): Keep a way out of Distraction Free Mode on Windows (#47)
Root cause: on Windows the window is frameless and the main menu lives in the custom title bar; Distraction Free Mode rendered a bare title bar without it, the toggle has no default shortcut (as in IDEA) and the mode persists across restarts, leaving no way out.
Fix (IDEA): the bare title bar keeps the main menu when there is no native menu bar; a lone Alt opens the main menu; entering the mode (or restoring it) shows "To exit Distraction Free Mode, use View | Appearance … or Find Action" with an Exit action, in its own notification group.
Siblings covered: Distraction Free + Full Screen on Windows; other chrome-hiding settings checked (unaffected).
Verified in-app on macOS (hint, Exit action, single Don't Show Again). Windows-only parts (☰ in bare title bar, lone Alt) not verified on Windows.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:38:26 +08:00
l0ng-ai b4a3e866c6 fix(terminal): render the terminal in the bundled JetBrains Mono (#44)
Root cause: the terminal built its own font-family ("JetBrains Mono", Nerd Fonts, monospace) without the bundled "JetBrains Mono Variable", so on Windows (no JetBrains Mono installed) xterm fell back to the system monospace (SimSun on Chinese Windows); xterm also measured cells before the webfont loaded.
Fix: one resolver (src/platform/fonts.ts monoFontFamily) maps JetBrains Mono to the bundled face; xterm re-measures once fonts load; "Use editor font" also takes the editor's fallback font (IDEA).
Siblings covered: embedded run terminal, editor default family + fallback, Settings › Editor › Font preview.
Verified in-app on macOS (terminal renders JetBrains Mono Variable). Not verified on Windows.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:38:18 +08:00
l0ng-ai 5670c2e8dd test: stub Range layout methods once in the test setup (#53)
Root cause: jsdom has no layout and CodeMirror measures text ranges in an animation frame, sometimes after a test finished; test files that did not stub Range.getClientRects (e.g. search/codePreview) threw an unhandled TypeError that failed whole CI runs at random (seen on #41).
Fix: stub Range.getClientRects / getBoundingClientRect once in src/test/setup.ts; drop the six per-file copies.
Verified: vitest on editor/lsp, editor/refactor, templates, search (55 files, 458 tests, no unhandled errors).

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:38:11 +08:00
l0ng-ai 843127f4a3 fix(app): Apply the saved IDE zoom before the page loads so the splash doesn't jump
The frontend set the webview zoom once settings loaded, while the splash was
still up, so a non-100% zoom visibly scaled the splash logo at the end.
2026-10-27 10:17:06 +08:00
l0ng-ai 1127263ea8 fix(run): Shorten java command lines too long for the OS (#28) (#45)
Root cause: every java launch inlined the whole classpath into one -cp argument with no "Shorten command line", so long classpaths exceeded CreateProcessW's 32767-char limit (os error 206) before the JVM started.
Fix (IDEA's auto "Shorten command line"): ide-run rewrites a too-long java command line at spawn time — @argfile on JDK 9+ with ASCII paths, otherwise a MANIFEST pathing jar (percent-encoded file: URLs, 72-byte lines); temp files are removed when the process exits. Debug passes the same choice to java-debug's shortenCommandLine.
Siblings covered: Application/Spring Boot, test classpath, Kotlin main, explicit java -cp configs, profiler, PTY launches, Debug.
Verified: unit tests, pathing jar loaded by real JDK 8/11/25, ProcessManager integration test with 3000 entries on JDK 8/17/25. Not verified on Windows.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 10:13:31 +08:00
l0ng-ai de84a643aa fix(ui): Make read-only content selectable with a Copy context menu (#48)
Root cause: body is user-select: none and each feature had to opt back in; the Markdown preview (and other read-only views) never did, and the app-wide contextmenu guard left no Copy item.
Fix: shared selectableText class (src/ui) for read-only content, and a fallback Copy context menu from browserGuards when right-clicking a selection or a selectable area (component menus still win).
Siblings covered: Quick Documentation / hover / completion docs, hover errors, Quick Definition, debugger value tooltip, inspection messages, message dialogs (no drag on body text), notification titles, About dialog; existing selectable consoles now get Copy too.
Verified in-app on macOS: select in Markdown preview, right-click Copy puts the text on the clipboard; tab context menu unchanged.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 08:56:13 +08:00
l0ng-ai 6dfd82de4c fix(vue): Run Vue Language Server 3 in hybrid mode with a usable TypeScript (#46)
Root cause: the tools dir installed typescript@latest, now TypeScript 7 (native port, no typescript.js/tsserver.js/ts.server), so @vue/language-server 3 crashed on ts.server.protocol and restarted forever; Volar 3 hybrid mode also needs tsserver/request forwarding, which we never answered.
Fix: pin TS-as-library servers to typescript@<7 and keep shared deps on uninstall; pick a usable tsdk (setting / bundled / project, --tsdk); bridge tsserver/request to vtsls with @vue/typescript-plugin (null when vtsls is absent); answer workspace/configuration for section ""; stop restarting after 3 consecutive early crashes (IDEA); crash summary starts at the exception line.
Siblings covered: typescript-language-server and svelte-language-server (same TS 7 break), vtsls config, install hints in language TOMLs.
Verified: scoped cargo tests, real-server tests (vue-language-server 3.3.13 + vtsls), in-app on macOS (server running, no crash loop). Not verified on Windows.

Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
2026-10-27 08:21:42 +08:00
kevinh b79522fd03 fix(java): Point the missing-JDK error at the Language Server JDK setting (#31)
Root cause: missing_runtime_message only suggested JAVA_HOME (process-wide) although a per-app Language Server JDK setting exists since #21.
Siblings covered: server_jdk_too_old_message now names the same Settings page via a shared SERVER_JDK_SETTING const; both asserted in tests.
2026-10-26 23:08:52 +08:00
l0ng-ai d7c27c1184 fix: configure git identity for merge and rebase test 2026-10-26 08:30:07 +08:00
l0ng-ai 6b9a2ec67e fix: sync inspection results with profiles and restore CSS validation 2026-10-25 23:41:15 +08:00
l0ng-ai d3feb056aa chore: v0.1.14
Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-25 21:34:56 +08:00
l0ng-ai 1e00ea5ee2 feat: Third pass aligning core experience with IntelliJ IDEA
Project & files
- Project view modes (Project Files / Open Files / All Changed Files /
  Scratches); External Libraries for Python, JDK (src.zip), Node, Cargo, Go,
  Maven/Gradle jars; read-only jar/zip browsing
- ⌘N New popup with type search and more file kinds; New Project, Rename
  Project, Reload All from Disk, Export/Import Settings, Power Save Mode
- File type associations, extra content roots and exclude patterns (indexed
  and watched without reopening the project)

Editor & navigation
- Find bar: search scope (comments/strings), Preserve Case, Exclude, multiline,
  history; Find in Files: Module scope, custom pattern scopes, multiline
- Search Everywhere filters, Assign Shortcut, settings results; named bookmark
  lists, file/folder bookmarks; Structure filters and Show Inherited; Recent
  Locations timestamps and breadcrumbs; Code Vision author
- Completion options, editor tab placement/layout, inline completion (LSP 3.18)
- Spell checking (Typo inspection, ide-spell) and Inspections settings with
  custom profiles; Code Style schemes, formatter markers, Do not format

Run, VCS, services, database
- Typed run configurations (Shell, Program, npm, Gradle, Maven, Docker,
  Compound), Modify options, Before launch, env dialog, Current File,
  Emulate terminal (PTY)
- Git log graph/view options, Go to Hash/Branch/Tag, date filter keeps the
  graph; Pull/Merge/Rebase dialogs, Manage Remotes, Worktrees
- Generic Services host (Docker connections/registries, run sessions); TODO
  scope/changes tabs with changelists; Coverage report import
- Data sources: wire-compatible vendors, URL import, SSL certs, Options,
  Schemas and Advanced tabs, color and comment

Workbench
- Run Anything, Customize Main Toolbar, named tool window layouts, menu
  completeness, Code Cleanup, macros, "Don't Show Again", editor banners
- Chinese translations for the new UI

Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-25 21:24:00 +08:00
l0ng-ai 294198a727 chore: v0.1.13 2026-10-25 09:45:20 +08:00
l0ng-ai 82b6980a59 feat: Second pass aligning interactions with IntelliJ IDEA (#23)
* feat: Second pass aligning interactions with IntelliJ IDEA

Result tool windows
- Next/Previous Occurrence (⌥⌘↓/↑) across Find, Usages, Problems, TODO,
  Inspection and Hierarchy, with an "N of M" status hint
- Find: reuse the unpinned tab, Pin Tab, source preview, group by file or
  directory, exclude moves to the next result, Rerun
- Usages: tabs with pinning, exclude and preview
- Problems, TODO, Inspection: no autoscroll by default, severity-first sorting,
  context menu with quick fixes and copy description
- Trees: F4/⌘↓ jump to source; speed search Home/End and arrow handling

Find in Files, bookmarks, navigation
- Replace mode: Enter replaces the selection, ⇧⌥Enter replaces all, multi-select
- Pin Window toggle, highlighted read-only preview, search/replace history (⌥↓),
  Alt mnemonics, search context filter (comments / string literals)
- Mnemonic bookmarks (⌥F3, ⌃⇧digit, ⌃digit), ⌘F3 bookmarks popup, descriptions,
  ⌘-click gutter toggles a bookmark
- Select In popup (⌥F1); Recent Locations double-click and Delete;
  Search Everywhere remembers the query per tab

Code insight and refactoring
- Extract/Introduce continue with in-place rename and name suggestions;
  expression and occurrence choosers with live highlighting
- Safe Delete confirms first; numbered Refactor This
- Quick Documentation history, in-popup links and a Documentation tool window;
  Quick Definition switches between targets and shows the whole declaration
- Goto chooser lists symbol names and can pin to the Find window
- Parameter Info Tab/⇧Tab, intention preview, Reformat dialog and summary,
  inlay hint ⌘-click and context menu
- Live templates: Enter moves to the next field, two-step Esc, ⌘J as a lookup,
  language surrounders with mnemonics
- File Structure popup as a filterable tree; Show Usages filter and preview;
  call hierarchy jumps to call sites

Editor
- Paste re-indents each line by default; Move Statement moves whole statements
  in and out of blocks and re-indents
- Type over closing quotes of existing strings; ⌘D/⌘⌫ cover folded regions;
  ⌥⌫ deletes the preceding word
- ⌃G / ⌃⌘G whole-word rules and wrap hint; double-modifier caret cloning
- Visual-column block selection, nested Collapse All, recursive folding,
  fold selection toggle, Go to Matching Brace, gutter drag selects lines,
  sticky line and error stripe clicks, Insert toggles overwrite mode

Run, VCS, workbench
- Gutter icons show the last test result; Modify Run Configuration; test tree
  hides passed tests, context menu and assertion diff
- Stack frames resolve by fully qualified class name; context run chooser
- Inline Smart Step Into
- Git Log multi-select filters, "me" and date range; favourite branches,
  Fetch and tags in the branch popup; stash Keep index and Unstash options
- Terminal tab titles follow the shell; Distraction Free persists; gear menu;
  sticky balloons move to the notifications tool window after a while

* fix(vcs): Label restored local changes as yours after Smart Checkout; name the branch checked out

Conflicts from restoring stashed changes show the user's uncommitted
changes on the left like other operations, and the checkout notification
names the new branch instead of the previous one.

* fix: Close the merge editor on Apply; confirm before terminating a running Run tab

Like IDEA, applying a merge result closes its tab, and closing a Run tab
whose process still runs asks before terminating it (with a Settings
toggle and "Do not ask again").

* fix(terminal): Don't start a new session in the background after closing the last tab

The hidden Terminal window stays mounted, so it reopened a session as soon
as the last tab closed. It now opens the Local tab only once the window is
shown again, as IDEA does.

* fix(vcs): Start the merge result from a three-way merge; close the editor on Apply

The result pane now begins with non-conflicting changes merged and each
conflict showing the base text, resolved from the side panes, instead of
raw conflict markers. Apply now reports success, so the tab closes.

fix(search): Match File Structure filter on word starts

* fix: Limit Smart Step Into to the paused line; drop usages hints on callbacks; show full intention titles

Adapters that list every remaining call in the function (debugpy) made Smart
Step Into fall back to a chooser that offered calls from later lines. Targets
are now narrowed to the calls on the paused line, so they are picked in place.

Anonymous functions and callbacks no longer get a 'no usages' hint, and
intention titles are no longer cut short when there is room for them.

* fix: Undo / Redo from the Edit menu and Find Action use the editor's history

They ran the webview's native undo, which misses programmatic edits such as
templates, completion and refactorings, so nothing happened in the editor.
2026-10-25 09:44:46 +08:00
l0ng-ai 81e0e8e2d0 feat: Align interactions with IntelliJ IDEA (#22)
* feat(nav): Complete file paths and flag unresolved web file references

Path completion in href/src/url()/@import, Markdown links and path-like strings, folders first. HTML and CSS paths that name no file get a 'Cannot resolve file' warning. File references resolve in the backend like IDEA's: relative to the file, /-rooted against the project and resource roots.

* feat: Align editor, workbench, run/debug and VCS interactions with IntelliJ IDEA

Keymap
- Let AltGr characters through instead of firing Ctrl+Alt shortcuts (Windows/Linux)
- Double Shift: strict press/release timing, ignored while typing or after Shift+click
- Chords wait 2s and swallow an unmatched second stroke
- Drop CodeMirror defaults that clash with IDEA keys; ⌘Enter is Split Line, add
  Scroll to Center and Code Block Start/End
- Add missing IDEA defaults (⌃J, ⌘Y, ⇧⌘C, ⌥⌘Y, ⇧⌘M, F9 Resume, ⌘R Rerun, …)

Editor
- Tab inserts at the caret; smart Backspace to the proper indent; Enter continues
  a `//` comment; per-language line comment placement
- Back/Forward records navigations only; ⌘F searches the selected text
- In-place rename; ⌃Space inserts a sole candidate; commit characters only for a
  focused lookup; usage highlighting survives edits; ⌥Enter offers fixes from the
  caret line; ⌘-hover shows declaration info only for navigable targets

Workbench
- Tool windows: primary and split area per side, six Move To anchors, layout migration
- ⌘W closes tool window content; Shift+Esc from the editor; closing a tab
  activates the left neighbour; preview tab and tab limit follow IDEA defaults
- Switcher lists open editors and tool windows; Recent Files Delete removes items
- Search Everywhere tab/scope behaviour; popups close when the window deactivates
- Project view: Copy/Move dialogs on paste, skippable usage search on delete,
  new-file input cancels on blur; Settings search Esc clears the filter
- Status bar breadcrumbs open a children popup; notification timeouts

Run / Debug / Build / Terminal
- Run asks to stop a running single-instance config; Rerun restarts silently;
  "Allow multiple instances" option
- Run chooser: Enter runs, Shift debugs, F4/Delete/digits
- Type into the console for stdin; Stop covers debug sessions
- Log (Shift+click) and temporary (Alt+click) breakpoints; independent suspend/log
  options; Evaluate prefill and history; changed-value highlighting; expandable hints
- Build window opens on failure and jumps to the first error
- Terminal confirms closing a busy tab and lets IDE shortcuts through

VCS
- Correct ours/theirs labels during rebase, cherry-pick and revert
- Smart/Force Checkout; remote branch checkout updates the local branch
- Partial commit by changed block; inline before-commit check results
- F7 continues into the next file; Rollback Lines; Commit and Push; Amend message swap
- Branch delete with Restore; force push confirmation; editable push target;
  update summary with the incoming commits

* chore: Regenerate bindings
2026-10-25 09:34:02 +08:00
l0ng-ai 87b463990c feat(nav): Follow links, file paths and CSS references with Go to Declaration
Go to Declaration and Cmd-click said "Cannot find declaration" on HTML href/src, CSS url() and @import, Markdown links and CSS selectors: language servers expose paths as document links, not definitions, and none resolves class/id to selectors.

- ide-lsp: textDocument/documentLink (lsp_document_links), resolving links that lack a target.
- Editor: when the server has no definition, follow its document links, then URLs and quoted / Markdown / url() paths in any language. Server file links are checked to exist; root-relative paths also resolve against resource roots. Cmd-hover underlines the whole link.
- ide-language: tree-sitter web references (class / id attributes, href="#id", var(--x)) and .class / #id / --var definitions from stylesheets and <style> elements, indexed as symbols. web_goto looks them up by exact name (ProjectIndex::symbols_named), using the open document's unsaved text.
- Detect public/ of package.json modules as a resource root (Vite, Next, CRA serve it at /).
2026-10-24 22:58:23 +08:00
l0ng-ai 046fd37064 fix(ui): Match PyCharm project view, matched tags and text weight
- Project widget initials follow IDEA's AvatarUtils.initials ("idea" → "I").
- "Show directories with changed descendants" is off by default, as in IDEA.
- FileEntry gains `excluded`: only excluded folders get the excluded icon and
  dimming; git-ignored ones keep the plain icon and the ignored colour.
- `.github` folders get IDEA's GitHub folder icon.
- Matched tag names are never bold and use their own background
  (MATCHED_TAG_NAME); matched braces are bold in dark schemes only.
- Revert Greyscale antialiasing to `antialiased` for UI and editor; `auto`
  rendered text heavier than PyCharm.

Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-24 22:56:48 +08:00
l0ng-ai cacddee19d feat(web): Open HTML in browsers with a built-in server; add Emmet
- ide-web: built-in static server on 127.0.0.1 that serves project files,
  plus detection and launching of installed browsers.
- Open In › Browser in the editor, project view and tab menus; browser bar
  and built-in preview beside HTML editors; ⌃⇧R opens the default browser.
- Emmet: Tab expansion in markup editors and Update Tag with Emmet.

Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-24 22:16:28 +08:00
l0ng-ai c8cea62eab fix(editor): Match PyCharm syntax colours and text weight
Greyscale antialiasing mapped to `-webkit-font-smoothing: antialiased`,
which drops macOS stem darkening, so editor and UI text rendered thinner
and syntax colours looked lighter than PyCharm's. Use `auto` instead.

Align lezer tag mappings with PyCharm's Light scheme:
- HTML/XML: `<!doctype>` is plain text (documentMeta is a sub-tag of meta).
- CSS/SCSS/Sass: property names use the attribute colour; values and units
  the attribute-value colour; class, id, pseudo, colour and url() the tag colour.
- YAML: keys are keywords.
- Markdown: quotes are strings with keyword markers, link destinations
  italic function colour, titles strings, horizontal rules keywords.

Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-24 22:16:16 +08:00
l0ng-ai 4a2d5c9599 chore: v0.1.12
Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-24 18:26:59 +08:00
l0ng-ai 059f25f13e feat(java): Separate the jdtls JDK and add Settings for New Projects (#21)
- Languages & Frameworks › Java › Language Server › JDK (languages.java.jdtlsJdk) runs jdtls on its own JDK, like IDEA's JDK for importer; the project keeps compiling and running with the Project JDK.
- Toolchain pages get the Global / This project switch (default This project); Global shows and edits the global layer.
- File › New Projects Setup › Settings for New Projects… edits only the global layer, which new projects inherit.
- Configuration errors (launch_error) show as "Not started" instead of "Crashed".

Fixes #18
Fixes #19

Claude-Session: https://claude.ai/code/session_01SaPPsVPuk6ZTNsoUCNCbMv
2026-10-24 18:26:31 +08:00
@鹿таl0ng-ai a765959d48 fix(process): Spawn child processes without a console window (#17)
- The IDE is a GUI-subsystem binary, so Windows allocated a fresh console
  window for every console-subsystem child it started: one black window per
  language server, per settings-driven restart, per debug adapter and per
  toolchain probe, titled with the child's own path. Closing it killed the
  child, which is how editing the language-server settings broke highlighting.
- Peer::spawn, the spawn point for every language server, wired stdio but never
  passed CREATE_NO_WINDOW. The rustup-proxy probe, the DAP adapter, debugpy
  probe, runInTerminal and cargo spawns, the SDK probes, the run-configuration
  probes, the coverage exporter and tool diagnostics/downloads (curl, tar) had
  the same gap, while git, cargo builds, gh, docker and run configurations
  passed the flag inline — five copies of the magic constant, six subsystems
  without it.
- Add ide_core::process::{CREATE_NO_WINDOW, hide_console_window} as the single
  definition, route every spawn through it (tokio commands via as_std_mut()) and
  fold the inline copies into it.
- Verified locally on Windows with a console-detached harness: a plain spawn
  reports a visible console window (GetConsoleWindow + IsWindowVisible), the
  patched Peer::spawn reports a console without a window. cargo check, cargo
  clippy --all-targets and the affected test suites (249 tests) are green.

Co-authored-by: l0ng-ai <[email protected]>
2026-10-24 16:57:34 +08:00
l0ng-ai efbb48c3cb chore: v0.1.11 2026-10-24 16:32:25 +08:00
l0ng-ai bb9486c758 fix(app): Stop console tools from opening cmd windows on Windows (#20)
Release builds use the GUI subsystem, so a console child spawned without
CREATE_NO_WINDOW gets its own console window. git, gh, docker, builds and
run configurations already set the flag; language servers, SDK and tool
probes, debug adapters, tool downloads, coverage export and perf did not.
Add ide_core::process::no_window and use it at every remaining spawn.

Fixes #16
2026-10-24 16:31:50 +08:00
l0ng-ai 5c0c3a009c docs: Keep the demo video as the README's only hero 2026-10-24 14:50:08 +08:00
l0ng-ai ef667f6e21 chore: v0.1.10 2026-10-24 14:48:53 +08:00
l0ng-ai 81b43a52b4 docs: Trim the README to a link row, screenshots and the benchmark 2026-10-24 14:48:31 +08:00
l0ng-ai 0bb730039d feat(search): Bring Search Everywhere closer to IDEA's popup
- All tab interleaves classes, files, symbols, actions and text by match
  degree, keeping each source's own order; Enter waits for every source
  except a text search the top row already beats.
- Preview pane under the list (remembered toggle), reusing CodePreview.
- "/" lists scope commands; "/files q" switches scope and searches q.
- Per-tab search history: magnifier dropdown and ⌥↑ / ⌥↓.
- Text hits: syntax colouring, IDEA's boxed-T icon, dimmed repeated file
  names, collapsed indentation.
- Flat scope chooser, IDEA header icons, wider popup with a stable list
  height; the list shrinks so the footer stays visible.
2026-10-24 14:47:45 +08:00
l0ng-ai 709e1eb855 docs: Drop the feature list from the README 2026-10-24 14:46:31 +08:00
l0ng-ai 4473324822 docs: Recapture the README editor screenshot with a focused window 2026-10-24 14:45:35 +08:00
l0ng-ai 5d5b3e4791 docs: Recapture README screenshots with the native window frame 2026-10-24 14:36:44 +08:00
l0ng-ai c5b4b71e30 docs: Lay out README screenshots as a hero plus an even pair 2026-10-24 14:34:29 +08:00
l0ng-ai 0e299ad936 docs: Add download links, screenshots and a feature list to the README 2026-10-24 14:31:17 +08:00
Richardтаl0ng-ai 5b8e0b9659 feat(i18n): 新增简体中文界面翻译 (#15)
* feat(i18n): 新增简体中文界面翻译

* test(i18n): 补充中文界面截图与端到端验证

* fix(i18n): Translate notifications and usages through catalog templates

Move the hard-coded Chinese notification patterns into source-text
templates (`{name} failed`) applied at render time, so text follows
locale switches and stays English under en. Use useTx in Search
Everywhere and add its missing zh strings.

* chore: Drop i18n screenshots

---------

Co-authored-by: l0ng-ai <[email protected]>
2026-10-24 14:14:43 +08:00
l0ng-ai 3d8a6f07b4 feat(app): Enable signed auto-updates from GitHub releases
Configure the updater public key and the releases/latest latest.json
endpoint, and build updater artifacts so tauri-action uploads signatures
and latest.json. Check for updates once per app start (release builds
only) and offer Install and Restart when one is available.
2026-10-24 14:13:12 +08:00
l0ng-ai 99b4f8bdf6 fix(terminal): Render with xterm's DOM renderer
The WebGL renderer rasterizes each glyph against its cell background, so
text over the selection came out at a visibly lighter weight than the
rest and looked like a different font. The DOM renderer draws text like
the editor does, selected or not.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 13:42:07 +08:00
l0ng-ai 86f1f666d7 chore: v0.1.9
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 13:15:41 +08:00
l0ng-ai be49490153 feat(terminal): Align the terminal with IDEA's tabs, menu and settings
- ⌘T (Ctrl+Shift+T on Windows) opens a terminal tab while the terminal
  has focus; elsewhere the key keeps its IDE action.
- The tab bar's ▾ menu lists installed shells under "New Predefined
  Session" (login shell, shell integration) and links to Settings.
- Settings › Tools › Terminal: pick the shell path from the installed
  shells or browse for one, "Move focus to the Editor with Escape",
  fallback font, column width and a link to the Terminal keymap.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 13:02:52 +08:00
l0ng-ai 1f5fcb477d fix(terminal): Default to the account's login shell
$SHELL is inherited from whatever launched the app, so starting it from a
terminal running bash opened bash even when the login shell is zsh. Read
the passwd entry first and fall back to $SHELL.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 12:36:40 +08:00
l0ng-ai 1bb454bd2f fix(terminal): Match the xterm scrollbar to the IDE's
Style xterm's own scrollbar slider with the scrollbar tokens, narrow it to
10px and blend the overview ruler border into the background.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 12:36:40 +08:00
l0ng-ai f38f63e397 test(e2e): Drive the Language Servers page through its list and details
The page is no longer a table: pick the tool in the list, then act in
its details pane. Fixes the Frontend CI failures since v0.1.7.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 12:11:21 +08:00
l0ng-ai 08d87c3903 chore: v0.1.8 2026-10-24 11:47:57 +08:00
l0ng-ai 1652866769 fix(workbench): Drag tool window stripe buttons with pointer events
With the window's native file drop enabled, the webview never receives
dragover / drop for HTML5 drags that start inside it, so stripe buttons
could not be moved. Track the drag with pointer events instead: a ghost
follows the pointer, the drop line shows the target, Escape cancels, and
an empty right stripe appears as a drop target while dragging.
2026-10-24 11:47:02 +08:00
l0ng-ai be5a6f62ad feat(editor): JetBrains-style line-number gutter context menu
Right-click on the gutter now shows Annotate with Git Blame, Add/Remove
Bookmark, Soft-Wrap, Configure Soft Wraps, an Appearance submenu and
Configure Gutter Icons, contributed through a new "editorGutter" menu
group. A breakpoint line still opens the breakpoint popover.
2026-10-24 11:38:08 +08:00
l0ng-ai f5e8d77dcf chore: v0.1.7
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 11:34:42 +08:00
l0ng-ai c3ea376899 feat(tools): Install 15 more language servers and debug adapters
Release downloads: buf, clojure-lsp, texlab, neocmakelsp, LemMinX, Perl
Navigator, kotlin-language-server, kotlin-debug-adapter and netcoredbg;
Open VSX: bash-debug and php-debug (run through node). Two new methods:
`gem` (ruby-lsp, solargraph), installed into the tool's folder and
launched with GEM_HOME, and `dotnet tool` (csharp-ls, fsautocomplete),
launched with DOTNET_ROOT so a Homebrew / user-local .NET is found.
Tools declare the runtime they need (Java, Node.js, Perl).

Each was installed from its real source and started on macOS arm64.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 11:26:31 +08:00
l0ng-ai fde075457b feat(editor): Tree-sitter highlighting for Zig, Nix, Make, GraphQL and HCL
Every grammar-only language now gets colours: Zig, Nix and Make from the
queries their crates ship, GraphQL and HCL (Terraform) from our own, as
those crates ship none.

Grammar queries follow two conventions, so the precedence is set per
grammar: nvim-style (the later pattern wins) or tree-sitter CLI style
(the first one wins, Nix). nvim's `#lua-match?` is evaluated; patterns
with predicates we cannot evaluate no longer match unconditionally,
which coloured every Zig identifier as a constant.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 11:26:31 +08:00
l0ng-ai 135723bca6 feat(tools): Install ElixirLS and its debugger, checking for Elixir first
ElixirLS ships one platform-independent zip of launcher scripts that
build the server with the project's Elixir / OTP on first start. Both
the language server and the debug adapter are installed from it, each
linked to its own launcher (language_server / debug_adapter).

Tools can now declare runtimes they need on PATH; Install stays
disabled with the reason ("Elixir is required") until they are found.

The Elixir debug adapter is renamed elixir-ls-debugger so it no longer
shares the server's tool name.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 11:26:31 +08:00
l0ng-ai 03a660354c feat(tools): Custom language server paths and a list/details Language Servers page
Servers and debug adapters could only be found on PATH or in the IDE's
tools folder; the only way to pick another executable (a version that
matches the project's Elixir/OTP, a local build) was an undocumented
languages/*.toml override. `tools.<command>.path` now names the
executable to launch: language servers (configure_server, restarted on
change), debug sessions and the tool list honour it, the latter
reporting a Custom status with its version or why the path is unusable.

The Language Servers page is laid out like Plugins: a compact list
grouped by availability on the left, the selected tool's command, path,
location, version, install actions and hints on the right.

A debugger named like its language server (ElixirLS's elixir-ls /
elixir-ls-debugger) was folded into the server's row and never listed;
it now gets a row of its own.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 11:26:31 +08:00
l0ng-ai 1572fc36ea feat(editor): Highlight languages without a CodeMirror mode from tree-sitter
Elixir files opened as plain text: highlighting only came from
@codemirror/language-data, which has no Elixir mode. Grammars now carry
their own highlights.scm; `doc_highlights` folds its captures into a small
kind vocabulary (inner nodes and later patterns win, interpolation clears
the string colour), memoised per document version on the incremental
tree. The editor maps kinds onto Lezer tags, so colours follow the active
scheme, and requests them only when no CodeMirror mode exists.

Enabled for Elixir; other grammar-only languages need their query wired.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-24 11:26:31 +08:00
l0ng-ai 4f74f22624 fix(ui): Size Select popups to the widest option and truncate the PR repo name
Virtualised list rows are absolutely positioned, so the popup only took the
trigger's width and cut short labels like "Merged". The Pull Requests header
now keeps the repository on one line with an ellipsis and a tooltip.
2026-10-24 10:02:13 +08:00
l0ng-ai 1309ce10e4 chore: v0.1.6 2026-10-24 09:21:16 +08:00
l0ng-ai 49be259232 fix(ui): Show the full SDK name in selects and shorten Windows home paths
A long detail (an installation path) was not cut, so it pushed the option label out
(e.g. "Eclipse Adopti…", the Maven name hidden) and scrolled the popup sideways. The
label now keeps its width and the detail is cut with an ellipsis (full path in the
tooltip, detail text translated). Paths under C:/Users/<name> are shown as ~/…
2026-10-24 09:11:58 +08:00
l0ng-ai 6a8016b888 fix(vcs): Keep submodule entries out of Rollback and refresh the log of another repository
- Rollback no longer offers a submodule's own entry: it cannot be reverted from the
  enclosing repository (it did nothing while reporting "Rolled back 1 file"). Rolling
  back only that entry explains how to revert it instead.
- A selected change is rolled back alone; only folders take the changes below them, so
  selecting a submodule's entry no longer pulls in the uncommitted files inside it.
- The Git log loads the branches of the newly selected repository, so it reloads when
  that repository's refs move (a new branch showed up only after a manual refresh).
2026-10-24 09:11:58 +08:00
l0ng-ai b8f422d451 fix(vcs): Route a nested repository's own entry to the enclosing one and harden multi-root
Review fixes for several repositories in one project:

- A path equal to a nested repository's root (a submodule's entry, an untracked
  clone) belongs to the enclosing repository, in the backend and the Commit view.
  It was routed to the nested repository as ".", so Rollback ran `git restore` and
  `git clean` over the whole submodule and Commit staged everything in it.
- Repository-scoped commands take the repository root exactly and fail when it is
  gone instead of running in an enclosing repository; path-based ones resolve the
  file's repository (Project::with_repo_of).
- The rescan on a repository appearing or going watches `.git/HEAD`: the watcher
  never reports a `.git` path itself, so the old check never fired.
- Status skips a repository that cannot be read; refresh falls back to the primary
  repository when the selected one is gone instead of failing every time.
- Commit and Compare-with-Local diffs resolve repository-relative paths against the
  selected repository's root, not the project root.
- Update Project and Fetch go on after a failing repository and report the failures
  together; Update skips repositories with a detached HEAD or no upstream.
- The Push dialog leaves out a repository that cannot be read.
- Shelve and patches fail clearly for changes of a nested repository instead of
  leaving them out silently (they still support the main repository only).
- Fix the repository discovery test on Windows (path separators); Chinese text for
  the new strings.
2026-10-23 23:15:09 +08:00
l0ng-ai c868ec8b68 fix(java): Apply the Maven settings to jdtls and Lombok, find more Maven installs
Review fixes for the Project JDK / Maven settings:

- jdtls gets the user settings.xml as java.configuration.maven.userSettings, so the
  editor resolves dependencies through the same mirrors and servers as sync and builds.
- The configured local repository (else <localRepository> of the chosen settings.xml)
  is where the static POM model looks for parent POMs / BOMs and where the Lombok
  agent is looked for.
- Maven discovery finds mvn.cmd on PATH on Windows and the homes Maven Wrapper 3.3+
  downloads (dists/<name>/<hash> without a nested folder).
- A Project JDK pointing at the JDK's bin directory is accepted.
- Detected run configurations reload when language settings change, so Maven and
  Gradle runs use the new JDK, Maven home and options.
- Chinese text for the new settings.
2026-10-23 23:15:09 +08:00
l0ng-ai 059a5193ff feat(vcs): Support several git repositories in one project (multi-root)
Only the repository containing the project root was known, so nested clones and
submodules (or a folder of side-by-side service repositories) showed no changes and
could not be committed or pushed. The project now has IDEA's VCS roots: the enclosing
repository plus those found below the project (scanned in the background, rescanned when
a .git appears or goes). Status covers all of them; path-based commands go to the file's
repository and repository-scoped ones take the selected repository.

The Commit view groups changes by repository and commits each one with included changes;
the branch widget names the selected repository and switches between them; Update Project
and Fetch run in every repository; Push lists every repository with outgoing commits; a
repository that stops in a merge or rebase is selected so Continue / Abort act on it.

Closes #12
2026-10-23 23:15:09 +08:00
l0ng-ai 0f7008391c feat(java): Add Project JDK and Maven home / settings.xml / local repository settings
The JDK was always picked automatically (JAVA_HOME, PATH, the usual install dirs, by the
module's Java version) and Maven was the project's mvnw or mvn on PATH, with no way to
override either. languages.java.jdk (Languages & Frameworks › Java › Project JDK) now sets
the default jdtls runtime, the JDK of Run/Debug and JAVA_HOME for Maven and Gradle;
languages.java.maven (Build Tools › Maven) sets the Maven home, -s and
-Dmaven.repo.local for sync, builds, tool-window goals and detected run configurations.
JDKs and Maven installations are detected for the pickers.

Closes #13
2026-10-23 23:15:09 +08:00
l0ng-ai c59f93988b feat(vcs): PyCharm-style Commit view and one shared message dialog
- workbench/messages: one promise-based ask/confirm/confirmChecked/showError
  host replaces the per-feature confirm dialogs (app, services, vcs,
  project tree, keymap, run, database, find, LSP choice popup).
- Commit tool window: Commit | Shelf | Stash tabs as the title, IDEA's
  toolbar (Update Project, View Options, Select Opened File), Unversioned
  collapsed, Commit / Commit and Push on one row, empty-message error.
- Commit Options popup: author, sign-off, before-commit checks, Git hooks;
  vcs_commit takes CommitOptions (--author, --no-verify).
- Remove from Recent Projects asks first; refused history rewrites show an
  error dialog.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 22:59:06 +08:00
l0ng-ai 76d33f0306 feat(ui): IDEA-style message dialogs, project widget popup and Chinese UI text
Message dialogs: `Dialog` gets an `icon` (question / info / warning / error)
that renders IDEA's alert layout: no title bar, a 32px icon, bold title,
text, checkbox under it. Every confirmation helper (prompt, VCS, services,
project tree, keymap, LSP choices, run, database, Replace All) uses it. Open
Project matches IDEA: "This Window" is the default and has focus. The Code
Analysis and TODO commit checks default to Review.

Project widget: the title bar project button opens a popup like IDEA's
ProjectWidget, with Open… / Clone Repository…, the projects open in other
windows (new `window_open_projects` command) and recent projects, filtered by
typing.

Chinese UI: UI text stays written in English and is translated through a
source-text catalog (`locales/text/zh.json`, about 2300 strings). The UI
primitives (buttons, checkboxes, selects, tabs, dialogs, menus, tooltips,
toasts, settings labels and hints) translate the strings they are given.
Action titles and categories are localised in the registry, and static JSX
text uses `<T>`, so text follows a locale switch without a reload. The
Settings window now syncs the locale too. Settings search matches both
languages.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 22:18:50 +08:00
l0ng-ai cd55bc8f7f chore: v0.1.5 2026-10-23 21:38:02 +08:00
l0ng-ai deec86fab9 refactor(perf): Remove the Memory Usage settings page
It was a live metrics view, not a setting, and duplicated the memory
indicator's popup, its status bar toggle and Clear Caches.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 21:35:02 +08:00
l0ng-ai 29e89f2525 fix(app): Give the Settings window a plain loader and make the splash bar progress
The Settings window loads the same page as project windows, so it showed the
launch splash with the logo. It now shows only the bar, after 400 ms.

The bar's sliding animation was switched off under reduced motion, leaving a
static 40% fill. It is now a fake progress that eases towards ~90% and fills
to 100% before the splash fades out.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 21:31:57 +08:00
l0ng-ai b1bb4a69cd fix(app): Keep the dark theme's background under a fast window resize
splash.css painted html light and only switched body to dark, so the page
canvas showed the light colour wherever a live resize outran relayout.
Leave html unpainted so the canvas takes body's themed background.

Also set the webview's background (not just the native window's) on a
theme change; it otherwise kept the colour the window was created with.
2026-10-23 21:29:45 +08:00
l0ng-ai ee99b3641f fix(ui): Hide the focus outline around dropdown popups
The global :focus-visible rule loads after the module CSS and has the same
specificity, so it overrode `outline: none` on the focused listbox wrapper
and drew a square blue frame inside the Select popup.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 21:23:06 +08:00
l0ng-ai 19aee5f262 fix(app): Never show the webview's native context menu (#11)
Right-clicks the app doesn't handle itself, such as on code-lens or inlay
widgets inside the editor or in text fields, fell through to WebKit's own
menu, whose Reload item reloads the whole app. Block it everywhere; the
app's own menus already call preventDefault before this guard runs.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 20:02:58 +08:00
l0ng-ai acf6931017 fix(java): Run jdtls with a Lombok agent and show build errors at launch (#9)
* fix(java): Run jdtls with a Lombok agent and show build errors at launch

jdtls compiles with ECJ, which only generates Lombok members when the
server JVM runs -javaagent:lombok.jar. Nothing put it there, so @Data /
@Setter classes lost their accessors and Run launched stale classes
with "Unresolved compilation problem" stubs and no explanation.

- Projects whose build files mention Lombok get the newest jar: the
  IDE-managed one (new "Lombok (jdtls agent)" tool, latest release from
  Maven Central), else ~/.m2 (incl. settings.xml localRepository) or the
  Gradle cache. Each agent gets its own jdtls workspace so everything is
  rebuilt with it.
- Settings › Languages & Frameworks › Java › JVM options
  (java.jdt.ls.vmargs); a Lombok agent there wins.
- Run (and Debug, as a notification) list the first compile errors when
  the build has errors, plus a hint when Lombok is missing or older than
  1.18.34.

Fixes #3

* fix(java): Base the Lombok hint on the agent jdtls actually runs

A Lombok agent in Settings › Java › JVM options wins over the managed
and cached jars, but the launch hint still looked only at those: with
no cached jar it said Lombok was missing, and with an old cached one it
blamed that version although jdtls ran the user's. The hint and the
startup warning now use the user's agent (version from its file name).
2026-10-23 18:46:13 +08:00
l0ng-ai 555ea8901f chore: v0.1.4
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 18:41:02 +08:00
l0ng-ai 769c1248d6 fix(app): Load the splash CSS from a file so release editors keep their styles (#10)
Release builds hash inline <style> blocks from index.html into the CSP.
With a hash present, style-src 'unsafe-inline' is ignored, so CodeMirror's
runtime-injected styles were blocked and the editor rendered blank (bare
line numbers, no text). Dev builds don't rewrite the CSP, so it only
showed in v0.1.3 release bundles.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 18:40:49 +08:00
l0ng-ai 61ab1851fa chore: Remove stale .ideal and .vscode folders 2026-10-23 17:48:52 +08:00
l0ng-ai c6a1a36676 chore: v0.1.3
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 17:30:21 +08:00
l0ng-ai 6165cb0174 perf: Add a black-box comparison with other IDEs (#6)
perf/compare/bench.py launches each IDE in apps.toml on the same project
and measures it from the outside, the same way for each: time to the
project window, to CPU settling and to log markers; memory and CPU of the
whole process tree; idle CPU, exit time, cache and install size. Runs on
Windows, macOS and Linux with psutil; --wipe-caches measures a cold index.

README: a Benchmark section with the first results against IntelliJ IDEA.
2026-10-23 16:54:39 +08:00
l0ng-ai 79f6caeaa8 test: Count ignored names in the excluded-folders glob test (#8)
a66f01e adds files.ignoredNames to exclude_globs(), so the expected
length is file_excludes() + 1, not files.exclude + 1. The test failed on
every platform since.
2026-10-23 16:41:18 +08:00
l0ng-ai dbfe66a119 chore: Add generated IDE-state .gitignore files 2026-10-23 15:40:41 +08:00
l0ng-ai a66f01ec87 feat(files): Add Ignored Files and Folders setting, honoured everywhere
Name patterns the IDE treats as nonexistent (.DS_Store, __pycache__, *.pyc, VCS dirs, ...) were hard-coded in five places, and Git changes ignored none of them, so .DS_Store showed up as unversioned. files.ignoredNames (IDEA's default list) now drives the project view, navigation bar, index, watcher, Find in Files, inspections and the Git status.
2026-10-23 15:40:41 +08:00
l0ng-ai bf06ca7832 feat(app): Show a themed splash until the launch project opens
Project windows are created in setup with the saved theme's frame colour, and the splash covers the first render while the launch project opens, so a window no longer flashes white or the Welcome screen. Single-instance is now release-only so a dev build can run beside an installed release.
2026-10-23 15:40:41 +08:00
l0ng-ai 63a03d1c08 fix: Status refresh loop, double project open at startup, jdtls Java requirement (#5)
* fix: Stop the status refresh loop and the double project open at startup

Every status rewrote .git/index, even with nothing to persist: gix's
`write_changes` writes an empty change set too. The watcher reports
.git/index writes as VCS changes, so each status triggered the next one,
together with an ahead/behind walk. On a repository with a long history
(freqtrade, 33k commits behind upstream) that kept a core busy for as long
as the project was open. Write back only when `has_changes()`.

A project passed on the command line was opened twice: the launch and the
"reopen last project" restore ran concurrently, and the restore checked
for an open project before the launch had finished. The second open found
the index cache locked and indexed in memory only; with a different last
project, the window ended up on whichever finished last. The restore now
waits for the startup launch.

* fix(lsp): Run jdtls on the Java version its bundles need

jdtls's launcher checks for Java 21, and so did JDTLS_MIN_JAVA, but the
Eclipse platform a release ships moves on: 1.63 bundles
org.eclipse.core.filesystem with JavaSE-25, so on a JDK 21 the server died
resolving its bundles ("jdtls is not ready (Crashed)") and the error told
users to install the 21 they already had.

Read the requirement from the installed jdtls instead: the highest osgi.ee
capability / Bundle-RequiredExecutionEnvironment among its plugins'
manifests (cached per install), never below 21. The runtime choice, the
rescan while no JDK fits and the "needs JDK N" message use it.

Fixes #4
2026-10-23 15:20:36 +08:00
l0ng-ai 4be1b152ef fix(ui): Drop the inner focus outline on text and search fields
The global :focus-visible outline outranked the module's `outline: none`
by load order, drawing a second ring inside the framed field's own focus
ring (visible on the Settings search box).

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 14:52:13 +08:00
l0ng-ai fe498c4b64 chore: v0.1.2 2026-10-23 11:40:03 +08:00
l0ng-ai 5e3e1f939d fix(lsp): Run jdtls on a JDK 21+ and offer Install in the server popup (#2)
* fix(lsp): Run jdtls on a JDK 21+ and offer Install in the server popup

jdtls refuses to start on Java < 21, and its launcher took whatever
JAVA_HOME / PATH pointed at, so it crash-looped with a Python traceback
when that was an older JDK (#1). Pick the first detected JDK 21+ and pass
it via JAVA_HOME; projects still compile against their own runtimes.
When none is found, report a clear message instead of launching, and
rescan on restart so a newly installed JDK is picked up. JDK detection
now also covers the java on PATH and Windows vendor directories.

The Language Servers status popup showed the manual install hint for
missing servers; it now has an Install button that runs tools.install,
which opens the Language Servers settings page for tools the IDE cannot
install itself.

Fixes #1

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu

* fix(lsp): Address review of the jdtls runtime check

- Cache the JDK scan and rescan at most every 10s while no JDK 21+ is
  found, refreshing the shared list instead of discarding it; skip
  duplicate Windows Program Files roots.
- Check `launch_error` in the manager (inside the blocking launch task)
  rather than in ProcessLauncher, and skip such servers in batch
  inspection.
- Keep the install hint visible next to the Install button; when the tool
  list failed to load, try the install so the error is reported.
- Don't let a server snapshot requested before a status event overwrite
  that newer status (left jdtls showing "Starting…" while missing).
2026-10-23 11:38:51 +08:00
l0ng-ai 3591b52034 docs: Add debugger screenshot 2026-10-23 09:01:29 +08:00
l0ng-ai dd9350e245 build(macos): Notarize the DMG and drop its license prompt; v0.1.1
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-23 08:12:17 +08:00
l0ng-ai ce9d5cf067 test: Make the Rust suite pass on Windows
Use the Windows wrapper name for Maven, compare walked paths with `/`,
relax the own-process footprint floor, skip tests that assert Unix paths
or shells on Windows, and stop Git converting fixture line endings in CI.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 23:34:35 +08:00
l0ng-ai fecdb974ff test: Fix Unix-only assumptions in ide-build tests; run all tests in CI
The Gradle and Maven tests expected the Unix wrapper and command names, and
the canonical-root test uses Unix absolute paths. CI now reports every
failing test instead of stopping at the first few.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 22:42:11 +08:00
l0ng-ai 909225858c test(e2e): Run specs with the macOS keymap on every host; update stale specs
CI runs Chromium on Linux, where the workbench picked the Linux keymap and
the ⌘-shortcut specs failed. Present a macOS user agent and platform.
Settings and workbench specs now match the current page tree and stripes.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 22:09:08 +08:00
l0ng-ai 3110de8803 fix: Satisfy clippy 1.99 lints
Box the unknown-host-key error variant (result_large_err), decode semantic
tokens with as_chunks, and use `?` for the warning-prefix fallbacks.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 22:01:38 +08:00
l0ng-ai b16096acf5 build(macos): Add hardened-runtime entitlements for the plugin JIT
Signed builds run under the hardened runtime, where wasmtime needs JIT
and unsigned executable memory to compile plugins. Also set the minimum
macOS version to 11.0.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 21:27:41 +08:00
l0ng-ai 4850cdff5e docs: Embed the product tour as a playable video
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 21:01:44 +08:00
l0ng-ai abbf5478e1 docs: Minimal README with a product tour video
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 20:59:36 +08:00
l0ng-ai 5f6e2a9c85 fix(workbench): Reset project state when a window switches projects
Opening another project in the same window left the previous project's state in
module-level stores: Amend and the commit message carried over, debug sessions kept
running, and Git Log, usages, hierarchy, inspection, HTTP client, recent locations,
navigation history and notifications still showed the old project.

Reset each of them on a project root change, stop the old debug sessions, ask about
unsaved changes before leaving a project, close the settings window (it would apply
to the new project) and end the indexing task when a scan is cancelled.
2026-10-22 20:26:04 +08:00
l0ng-ai 9a2025b76e Initial open-source release
Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
2026-10-22 18:55:10 +08:00
2587 змінених файлів з 411679 додано та 0 видалено

No files matched your search

+107
Переглянути файл
@@ -0,0 +1,107 @@
name: CI
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
concurrency:
group: ci-${{ github.ref }}
# A newer push supersedes the run in progress, on main too.
cancel-in-progress: true
env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: 0
jobs:
rust:
name: Rust (${{ matrix.os }})
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-22.04, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with: { version: 11 }
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- uses: dtolnay/rust-toolchain@stable
with: { components: "clippy, rustfmt" }
# Only main saves the cache (saving takes minutes on Windows); PRs restore main's.
- uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}
- uses: taiki-e/install-action@nextest
- name: Linux deps
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y libwebkit2gtk-4.1-dev libayatana-appindicator3-dev librsvg2-dev libxdo-dev libssl-dev patchelf
# tauri-build checks that frontendDist exists.
- run: pnpm install --frozen-lockfile && mkdir -p dist
shell: bash
- if: runner.os == 'Linux'
run: cargo fmt --all --check
- run: cargo clippy --workspace --all-targets -- -D warnings
# Test fixtures write LF files; keep Git from converting them on checkout.
- run: git config --global core.autocrlf false
- run: cargo nextest run --workspace --no-fail-fast
# nextest ran export_bindings, which rewrites src/bindings.ts.
- name: Bindings are up to date
if: runner.os == 'Linux'
run: git diff --exit-code src/bindings.ts
# The remote development server ships as a static Linux binary (release.yml):
# keep that build working.
server:
name: Remote server (linux musl)
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with: { targets: x86_64-unknown-linux-musl }
- uses: Swatinem/rust-cache@v2
with: { key: server-musl }
- run: sudo apt-get update && sudo apt-get install -y musl-tools
- run: cargo build --locked -p ide-host --bin openidea-server --target x86_64-unknown-linux-musl
- run: target/x86_64-unknown-linux-musl/debug/openidea-server --version
frontend:
name: Frontend
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with: { version: 11 }
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- run: pnpm typecheck
- run: pnpm lint
- run: pnpm test
- run: pnpm build
e2e:
name: E2E
runs-on: ubuntu-24.04
# Browsers and their system libraries are preinstalled; keep the tag in step with @playwright/test.
container: mcr.microsoft.com/playwright:v1.64.1-noble
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with: { version: 11 }
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- run: pnpm e2e
env:
HOME: /root
- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-report
path: playwright-report/
retention-days: 7
+186
Переглянути файл
@@ -0,0 +1,186 @@
name: Release
on:
push:
tags: ["v*"]
workflow_dispatch:
inputs:
tag:
description: "Tag to release (e.g. v0.1.0); must already exist"
required: true
permissions:
contents: write
concurrency:
group: release-${{ github.event.inputs.tag || github.ref_name }}
env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: 0
TAG: ${{ github.event.inputs.tag || github.ref_name }}
jobs:
# Creates (or reuses) one draft release that every platform build uploads into.
create-release:
runs-on: ubuntu-latest
outputs:
release_id: ${{ steps.release.outputs.id }}
steps:
- uses: actions/checkout@v4
with: { ref: "${{ env.TAG }}" }
- name: Tag matches app version
run: |
app="v$(jq -r .version src-tauri/tauri.conf.json)"
if [ "$app" != "$TAG" ]; then
echo "::error::tag $TAG does not match tauri.conf.json version $app"
exit 1
fi
- id: release
uses: actions/github-script@v7
with:
script: |
const tag = process.env.TAG;
const { owner, repo } = context.repo;
const releases = await github.paginate(github.rest.repos.listReleases, { owner, repo });
const existing = releases.find((r) => r.tag_name === tag);
if (existing) {
if (!existing.draft) core.setFailed(`release ${tag} is already published`);
core.setOutput("id", existing.id);
return;
}
const { data } = await github.rest.repos.createRelease({
owner, repo, tag_name: tag, name: `OpenIDEA ${tag}`,
draft: true, prerelease: tag.includes("-"), generate_release_notes: true,
});
core.setOutput("id", data.id);
build:
needs: create-release
name: Build (${{ matrix.name }})
strategy:
fail-fast: false
matrix:
include:
- name: macOS arm64
os: macos-latest
target: aarch64-apple-darwin
- name: macOS x64
os: macos-latest
target: x86_64-apple-darwin
- name: Linux x64
os: ubuntu-22.04
target: x86_64-unknown-linux-gnu
- name: Windows x64
os: windows-latest
target: x86_64-pc-windows-msvc
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with: { ref: "${{ env.TAG }}" }
- uses: pnpm/action-setup@v4
with: { version: 11 }
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- uses: dtolnay/rust-toolchain@stable
with: { targets: "${{ matrix.target }}" }
- uses: Swatinem/rust-cache@v2
with: { key: "${{ matrix.target }}" }
- name: Linux deps
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y libwebkit2gtk-4.1-dev libayatana-appindicator3-dev librsvg2-dev libxdo-dev libssl-dev patchelf
- run: pnpm install --frozen-lockfile
# Signing is optional: only secrets that are set get exported, so forks and
# unconfigured repos produce unsigned builds instead of failing.
- name: Signing env
shell: bash
env:
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY }}
APPLE_ID: ${{ secrets.APPLE_ID }}
APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
run: |
for v in APPLE_CERTIFICATE APPLE_CERTIFICATE_PASSWORD APPLE_SIGNING_IDENTITY APPLE_ID \
APPLE_PASSWORD APPLE_TEAM_ID TAURI_SIGNING_PRIVATE_KEY TAURI_SIGNING_PRIVATE_KEY_PASSWORD; do
[ -n "${!v}" ] && { echo "$v<<__EOF__"; echo "${!v}"; echo "__EOF__"; } >> "$GITHUB_ENV"
done
true
- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
releaseId: ${{ needs.create-release.outputs.release_id }}
tagName: ${{ env.TAG }}
args: --target ${{ matrix.target }}
# tauri-action notarizes the .app but not the DMG around it; notarize and
# staple the DMG too, then replace the uploaded asset.
- name: Notarize DMG
if: runner.os == 'macOS' && env.APPLE_ID != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
for dmg in target/${{ matrix.target }}/release/bundle/dmg/*.dmg; do
xcrun notarytool submit "$dmg" --apple-id "$APPLE_ID" --password "$APPLE_PASSWORD" \
--team-id "$APPLE_TEAM_ID" --wait
xcrun stapler staple "$dmg"
gh release upload "$TAG" "$dmg" --clobber
done
# The remote development server, per host platform it can run on. The desktop
# app downloads the one for the host (ide-tools `server`) and installs it over SSH.
# Linux builds are static (musl), so they run whatever the host's glibc.
server:
needs: create-release
name: Server (${{ matrix.os }}-${{ matrix.arch }})
strategy:
fail-fast: false
matrix:
include:
- { runner: ubuntu-22.04, target: x86_64-unknown-linux-musl, os: linux, arch: x86_64 }
- { runner: ubuntu-24.04-arm, target: aarch64-unknown-linux-musl, os: linux, arch: aarch64 }
- { runner: macos-latest, target: aarch64-apple-darwin, os: macos, arch: aarch64 }
- { runner: macos-latest, target: x86_64-apple-darwin, os: macos, arch: x86_64 }
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
with: { ref: "${{ env.TAG }}" }
- uses: dtolnay/rust-toolchain@stable
with: { targets: "${{ matrix.target }}" }
- uses: Swatinem/rust-cache@v2
with: { key: "server-${{ matrix.target }}" }
- name: musl toolchain
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y musl-tools
- name: Build
run: |
# musl-tools only ships `musl-gcc`; cc-rs would look for `<arch>-linux-musl-gcc`.
if [ "$RUNNER_OS" = Linux ]; then export CC_${TARGET//-/_}=musl-gcc; fi
cargo build --release --locked -p ide-host --bin openidea-server --target "$TARGET"
env:
TARGET: ${{ matrix.target }}
- name: Upload
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ASSET: openidea-server-${{ matrix.os }}-${{ matrix.arch }}.gz
run: |
gzip -9 -c target/${{ matrix.target }}/release/openidea-server > "$ASSET"
if command -v sha256sum >/dev/null; then sha256sum "$ASSET"; else shasum -a 256 "$ASSET"; fi > "$ASSET.sha256"
gh release upload "$TAG" "$ASSET" "$ASSET.sha256" --clobber
publish:
needs: [create-release, build, server]
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
RELEASE_ID: ${{ needs.create-release.outputs.release_id }}
with:
script: |
await github.rest.repos.updateRelease({
...context.repo, release_id: Number(process.env.RELEASE_ID), draft: false,
});
+35
Переглянути файл
@@ -0,0 +1,35 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
# Rust
target/
# Tauri generated schemas
/src-tauri/gen/schemas
*.tsbuildinfo
test-results/
playwright-report/
# Local AI agent files
CLAUDE.md
.claude/
+3
Переглянути файл
@@ -0,0 +1,3 @@
# Per-user IDE state
/shelf/
/changelists.json
+4
Переглянути файл
@@ -0,0 +1,4 @@
dist
target
src/bindings.ts
pnpm-lock.yaml
+1
Переглянути файл
@@ -0,0 +1 @@
{ "printWidth": 120, "semi": true, "singleQuote": false, "trailingComma": "all" }
Створено
+11341
File diff suppressed because it is too large. Load diff
+111
Переглянути файл
@@ -0,0 +1,111 @@
[workspace]
resolver = "3"
members = ["crates/*", "src-tauri"]
[workspace.package]
version = "0.1.18"
edition = "2024"
rust-version = "1.96"
license = "Apache-2.0"
authors = ["OpenIDEA contributors"]
[workspace.dependencies]
# internal
ide-core = { path = "crates/ide-core" }
ide-vfs = { path = "crates/ide-vfs" }
ide-text = { path = "crates/ide-text" }
ide-language = { path = "crates/ide-language" }
ide-index = { path = "crates/ide-index" }
ide-syntax = { path = "crates/ide-syntax" }
ide-search = { path = "crates/ide-search" }
ide-jsonrpc = { path = "crates/ide-jsonrpc" }
ide-lsp = { path = "crates/ide-lsp" }
ide-dap = { path = "crates/ide-dap" }
ide-vcs = { path = "crates/ide-vcs" }
ide-terminal = { path = "crates/ide-terminal" }
ide-run = { path = "crates/ide-run" }
ide-workspace = { path = "crates/ide-workspace" }
ide-sdk = { path = "crates/ide-sdk" }
ide-build = { path = "crates/ide-build" }
ide-analysis = { path = "crates/ide-analysis" }
ide-coverage = { path = "crates/ide-coverage" }
ide-profiler = { path = "crates/ide-profiler" }
ide-http = { path = "crates/ide-http" }
ide-db = { path = "crates/ide-db" }
ide-web = { path = "crates/ide-web" }
ide-spell = { path = "crates/ide-spell" }
ide-agent = { path = "crates/ide-agent" }
ide-clipboard = { path = "crates/ide-clipboard" }
ide-ssh = { path = "crates/ide-ssh" }
ide-remote = { path = "crates/ide-remote" }
ide-host = { path = "crates/ide-host" }
# serialization & IPC typing
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "0.9"
toml_edit = "0.25"
specta = { version = "=2.0.0-rc.25", features = ["derive"] }
# async & concurrency
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time", "process", "io-util", "fs"] }
tokio-util = { version = "0.7", features = ["rt"] }
parking_lot = "0.12"
rayon = "1"
dashmap = "6"
futures = "0.3"
# errors & diagnostics
thiserror = "2"
anyhow = "1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
# text & fs
ropey = "1.6"
encoding_rs = "0.8"
chardetng = "0.1"
ignore = "0.4"
globset = "0.4"
notify = "8"
notify-debouncer-full = "0.6"
trash = "5"
blake3 = "1"
dunce = "1"
dirs = "6"
# search, index, language
grep-searcher = "0.1"
grep-regex = "0.1"
grep-matcher = "0.1"
nucleo-matcher = "0.3"
redb = "4"
tree-sitter = "0.25"
# protocols & integrations
lsp-types = "0.97"
gix = { version = "0.89", default-features = false }
portable-pty = "0.9"
reqwest = { version = "0.13", default-features = false, features = ["rustls-no-provider", "http2", "charset", "gzip", "deflate", "brotli"] }
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12", "logging"] }
rustls-platform-verifier = "0.7"
sqlx = { version = "0.9", default-features = false, features = ["runtime-tokio", "tls-rustls-ring-webpki", "sqlite", "postgres", "mysql"] }
uuid = { version = "1", features = ["v4"] }
# testing
insta = { version = "1", features = ["yaml"] }
proptest = "1"
tempfile = "3"
[profile.dev]
# Keep debug builds snappy while giving dependencies decent speed.
opt-level = 0
[profile.dev.package."*"]
opt-level = 2
[profile.release]
codegen-units = 1
lto = true
opt-level = 3
panic = "abort"
strip = true
+202
Переглянути файл
@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+12
Переглянути файл
@@ -0,0 +1,12 @@
OpenIDEA
Copyright 2026 OpenIDEA contributors
This product includes software and icons developed by JetBrains s.r.o. and
contributors as part of the IntelliJ Platform (https://github.com/JetBrains/intellij-community,
https://github.com/JetBrains/intellij-plugins), licensed under the Apache License 2.0.
Copyright 2000-2026 JetBrains s.r.o. and contributors.
OpenIDEA is not affiliated with, endorsed by or sponsored by JetBrains s.r.o.
IntelliJ and IntelliJ IDEA are trademarks of JetBrains s.r.o.
See THIRD_PARTY_LICENSES.md for the full list of bundled third-party assets and their licences.
+61
Переглянути файл
@@ -0,0 +1,61 @@
<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="src-tauri/icons/source/openidea-wordmark-dark.svg">
<img src="src-tauri/icons/source/openidea-wordmark.svg" alt="OpenIDEA" height="56">
</picture>
</p>
<p align="center">
The IDE you know, rebuilt in Rust.
</p>
<p align="center">
<a href="https://github.com/l0ng-ai/openidea/releases/latest"><b>Download</b></a>
&nbsp;·&nbsp;
<a href="#benchmark">Benchmark</a>
&nbsp;·&nbsp;
<a href="#build">Build</a>
</p>
https://github.com/user-attachments/assets/234dd54c-1255-4b3a-b35c-533d78d737c0
OpenIDEA brings the IntelliJ IDEA way of working — its keymap, its tool windows,
its search-everything habits — to a native app with a Rust core.
Editor, code insight, debugger, Git, terminal, database and HTTP clients,
for 60+ languages. Builds for macOS, Windows and Linux.
## Benchmark
Opening [freqtrade](https://github.com/freqtrade/freqtrade) (Python, ~770 files)
with warm caches, median of 5 runs:
| | OpenIDEA | IntelliJ IDEA 2026.2.3 |
|---|---|---|
| Window with the project | **0.9 s** | 3.1 s |
| Ready (CPU settled) | **1.1 s** | 13.2 s |
| Memory (USS, all processes) | **169 MB** | 2011 MB |
| CPU time until ready | **2.4 s** | 47.9 s |
| Idle CPU | **4 %** of a core | 7 % of a core |
| Quit | **0.3 s** | 5.9 s |
A release build of OpenIDEA from `main` and IDEA with default settings and plugins, on
Windows 11, 32-core AMD, 62 GB RAM.
IDEA analyses Python more deeply, so "ready" doesn't mean the same work on
both sides. Both are measured from the outside, the same way: see
[`perf/compare`](perf/compare) to run it on your machine and projects.
## Build
```sh
pnpm install
pnpm tauri dev
```
Rust 1.96, Node 22, pnpm and the [Tauri prerequisites](https://v2.tauri.app/start/prerequisites/).
## License
[Apache 2.0](LICENSE). Bundled third-party assets are listed in [`THIRD_PARTY_LICENSES.md`](THIRD_PARTY_LICENSES.md).
<sub>Not affiliated with JetBrains. IntelliJ IDEA is a trademark of JetBrains s.r.o.</sub>
+242
Переглянути файл
@@ -0,0 +1,242 @@
# Third-party licenses
## JetBrains IntelliJ Platform icons
`src/ui/expui/**` contains SVG icons from the IntelliJ Platform "new UI" (expui)
icon set, copied unmodified (some renamed) from JetBrains' open-source repositories:
- [intellij-community](https://github.com/JetBrains/intellij-community) (Apache-2.0):
`platform/icons/src/expui/**` (incl. `fileTypes/` c, cpp, h, Csharp, typeScript,
sql, graphql, docker, gitignore, gradle, groovy, jenkins, jupyter, rst, swiftLang,
terraform, vue, bazel); `python/python-parser` (`fileTypes/python`);
`python/python-requirements` (`fileTypes/requirements`); `python/pipenv`
(`fileTypes/pipenv`); `python/python-pyproject` (`expui/toml.svg` →
`fileTypes/pyprojectToml`); `plugins/kotlin/base` (`fileTypes/kotlin`,
`kotlinScript`, `kotlinGradleScript`); `java/openapi` (`fileTypes/maven`);
`plugins/markdown` (`fileTypes/markdown`); `plugins/toml` (`fileTypes/toml`);
`plugins/terminal` (`toolwindows/terminal`); `plugins/maven`
(`images/expui/toolwindow/maven` → `toolwindows/maven`, `images/expui/build/maven*`
→ `maven/`); `plugins/gradle` (`icons/expui/gradle` → `toolwindows/gradle`,
`icons/expui/gradleLoadChanges` → `build/gradleLoadChanges`);
`platform/external-system-impl` (`icons/newui/task` → `externalSystem/task`).
Copyright 2000-2026 JetBrains s.r.o. and contributors. Apache License 2.0 (below).
- [intellij-plugins](https://github.com/JetBrains/intellij-plugins) (each plugin
carries its own licence): `protobuf/protoeditor-core` (`newui/protoFile` →
`fileTypes/protoFile`, Apache-2.0); `terraform` (`newui/hashiCorp` →
`fileTypes/hashiCorp`, Apache-2.0); `makefile` (`expui/makefile` →
`fileTypes/makefile`, MIT, Copyright (c) 2016 Victor Kropp — full text below).
At render time `src/ui/icons.tsx` may recolour monochrome icons to `currentColor`.
`src/ui/openidea/**` holds OpenIDEA's own file-type icons drawn in the expui style for
languages whose JetBrains icons are not open source (Go, Rust, Ruby, Dart, Lua, Zig,
Svelte, …). Those using the page-with-corner-badge layout (`goMod`, `goWork`,
`dotenv`, `readme`, `lock`, `license`) reuse the page outline of the Apache-2.0
expui `fileTypes/h.svg`; that outline is modified from the original by JetBrains.
`src/ui/openidea/runConfigurations/springBoot*.svg` (the Spring Boot run configuration
leaf) is OpenIDEA's own drawing.
The lettering in `src-tauri/icons/source/openidea-wordmark*.svg` is outlined from
Inter Tight (Copyright 2022 The Inter Project Authors, SIL Open Font License 1.1,
https://github.com/rsms/inter-tight).
### MIT License (makefile icon)
Copyright (c) 2016 Victor Kropp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
## Tools downloaded on request (not bundled)
The tool installer (`crates/ide-tools`, Settings › Languages & Frameworks ›
Language Servers) downloads third-party language servers and debuggers into the
user's app-data directory only when the user asks; none of them ships with OpenIDEA.
- **Microsoft java-debug** (Java debugger, loaded into jdtls as a bundle):
`com.microsoft.java.debug.plugin-*.jar` from <https://github.com/microsoft/java-debug>,
Copyright (c) Microsoft Corporation, licensed under the
[Eclipse Public License 1.0](https://github.com/microsoft/java-debug/blob/main/LICENSE.txt)
(EPL-1.0; it embeds `commons-io` and `rxjava`/`reactive-streams`, Apache-2.0). OpenIDEA
takes the jar from the `vscjava.vscode-java-debug` extension package on Open VSX
(<https://open-vsx.org/extension/vscjava/vscode-java-debug>, the extension itself is
MIT-licensed), verifies its published SHA-256 and extracts only the jar; it is used
unmodified, and its source is available from the repository above.
- **Eclipse JDT Language Server** (jdtls): EPL-2.0, from download.eclipse.org.
## en_US Hunspell dictionary
`crates/ide-spell/src/dict/en_US.{aff,dic}` (the spell checker's bundled dictionary) is the
SCOWL-derived en_US Hunspell dictionary (version 2020.12.07, from the `dictionary-en` npm
package 4.0.0), copied unmodified. Copyright Kevin Atkinson and the SCOWL / Hunspell
dictionary contributors; MIT- and BSD-style licences, full text in
`crates/ide-spell/src/dict/LICENSE-en_US`. It is checked with the
[spellbook](https://github.com/helix-editor/spellbook) crate (MPL-2.0).
## nvim-treesitter queries
`crates/ide-syntax/queries/<grammar>/locals.scm` are derived from the `locals.scm`
queries of [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter)
(`runtime/queries/`, commit `29212d82`, October 2026), Apache License 2.0 (the
same text as the Apache-2.0 licence below). Copyright the nvim-treesitter
contributors. Modifications by OpenIDEA: `inherits:` chains flattened (ecma,
c → cpp, php_only), node names adapted to the bundled grammar versions
(Kotlin for tree-sitter-kotlin-ng, Dart, Swift, Haskell, C#, Bash), member
definitions re-kinded as `field` / `method`, and missing scopes, definitions and
references added; each file's header notes its changes.
---
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy of this License; and
You must cause any modified files to carry prominent notices stating that You changed the files; and
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
## Solarized
The Solarized palettes in `src/styles/tokens.css` are adapted from
[Ethan Schoonover’s Solarized](https://github.com/altercation/solarized/tree/62f656a02f93c5190a8753159e34b385588d5ff3).
See [theme sources and mapping](docs/themes.md). MIT license:
```text
Copyright (c) 2011 Ethan Schoonover
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```
## Atom One Light and One Dark
The One palettes and semantic syntax mapping in `src/styles/tokens.css` are
adapted from GitHub’s Atom One syntax and UI themes.
Source revisions are listed in [theme sources and mapping](docs/themes.md).
Both syntax themes use the following MIT notice:
```text
Copyright (c) 2016 GitHub Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```
Both UI themes use the following MIT notice:
```text
Copyright (c) 2014 GitHub Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```
Бінарний файл не відображається.

До

Ширина:  |  Висота:  |  Розмір: 74 KiB

Бінарний файл не відображається.

До

Ширина:  |  Висота:  |  Розмір: 92 KiB

Бінарний файл не відображається.

До

Ширина:  |  Висота:  |  Розмір: 89 KiB

Бінарний файл не відображається.

До

Ширина:  |  Висота:  |  Розмір: 109 KiB

+22
Переглянути файл
@@ -0,0 +1,22 @@
[package]
name = "ide-agent"
description = "Coding agents over the Agent Client Protocol: tasks, sessions, worktrees and their timelines"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
ide-core.workspace = true
ide-jsonrpc.workspace = true
ide-vcs.workspace = true
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
tokio.workspace = true
toml.workspace = true
tracing.workspace = true
parking_lot.workspace = true
uuid.workspace = true
[dev-dependencies]
tempfile.workspace = true
+224
Переглянути файл
@@ -0,0 +1,224 @@
//! Session settings (model, mode, thinking effort…), slash commands and context use, as
//! the agent reports them over ACP.
use crate::model::{AgentCommand, AgentConfigOption, AgentConfigValue, AgentConfigVia, AgentUsage};
use serde_json::{Value, json};
/// The settings in a `session/new` or `session/load` result: its select `configOptions`,
/// plus the older `modes` / `models` fields for what those don't cover.
pub(crate) fn options_of(result: &Value) -> Vec<AgentConfigOption> {
let mut out = result.get("configOptions").map(parse_options).unwrap_or_default();
let has = |out: &[AgentConfigOption], category: &str| out.iter().any(|o| o.category.as_deref() == Some(category));
if !has(&out, "mode")
&& let Some(modes) = result.get("modes")
{
out.push(legacy(modes, "mode", "Mode", "availableModes", "id", "currentModeId", AgentConfigVia::Mode));
}
if !has(&out, "model")
&& let Some(models) = result.get("models")
{
out.push(legacy(
models,
"model",
"Model",
"availableModels",
"modelId",
"currentModelId",
AgentConfigVia::Model,
));
}
out.retain(|o| !o.values.is_empty());
out
}
/// A `configOptions` array; only select options are kept (the UI shows them as dropdowns).
pub(crate) fn parse_options(options: &Value) -> Vec<AgentConfigOption> {
let Some(list) = options.as_array() else { return Vec::new() };
list.iter()
.filter(|o| o.get("type").and_then(Value::as_str).is_none_or(|t| t == "select"))
.filter_map(|o| {
let id = o.get("id").or_else(|| o.get("configId")).and_then(Value::as_str)?.to_owned();
let mut values = Vec::new();
for v in o.get("options").and_then(Value::as_array).into_iter().flatten() {
// Grouped options: `{ group, name, options: [...] }`.
match v.get("options").and_then(Value::as_array) {
Some(group) => values.extend(group.iter().filter_map(value_of)),
None => values.extend(value_of(v)),
}
}
Some(AgentConfigOption {
name: str_of(o, "name").unwrap_or_else(|| id.clone()),
description: str_of(o, "description"),
category: str_of(o, "category").or_else(|| matches!(id.as_str(), "model" | "mode").then(|| id.clone())),
current: str_of(o, "currentValue"),
values,
via: AgentConfigVia::Option,
id,
})
})
.collect()
}
/// A `config_option_update`: its options replace the config-option ones; `modes` / `models`
/// stand-ins stay.
pub(crate) fn merge_update(config: &mut Vec<AgentConfigOption>, options: &Value) {
let fresh = parse_options(options);
let covered = |o: &AgentConfigOption| fresh.iter().any(|f| f.category.is_some() && f.category == o.category);
config.retain(|o| o.via != AgentConfigVia::Option && !covered(o));
let legacy = std::mem::take(config);
config.extend(fresh);
config.extend(legacy);
config.retain(|o| !o.values.is_empty());
}
/// `current_mode_update`.
pub(crate) fn set_mode(config: &mut [AgentConfigOption], mode: &str) {
if let Some(o) = config.iter_mut().find(|o| o.category.as_deref() == Some("mode")) {
o.current = Some(mode.to_owned());
}
}
/// The request (method, params without the session id) that sets `option` to `value`.
pub(crate) fn set_request(option: &AgentConfigOption, value: &str) -> (&'static str, Value) {
match option.via {
AgentConfigVia::Option => ("session/set_config_option", json!({ "configId": option.id, "value": value })),
AgentConfigVia::Mode => ("session/set_mode", json!({ "modeId": value })),
AgentConfigVia::Model => ("session/set_model", json!({ "modelId": value })),
}
}
/// `available_commands_update`.
pub(crate) fn commands_of(update: &Value) -> Vec<AgentCommand> {
update
.get("availableCommands")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|c| {
Some(AgentCommand {
name: c.get("name")?.as_str()?.trim_start_matches('/').to_owned(),
description: str_of(c, "description").unwrap_or_default(),
hint: c.pointer("/input/hint").and_then(Value::as_str).filter(|h| !h.is_empty()).map(str::to_owned),
})
})
.collect()
}
/// `usage_update`: `{ used, size, cost?: { amount, currency } }`.
pub(crate) fn usage_of(update: &Value) -> Option<AgentUsage> {
let tokens = |key: &str| update.get(key)?.as_f64().map(|n| n.clamp(0.0, f64::from(u32::MAX)) as u32);
let used = tokens("used")?;
let size = tokens("size").filter(|s| *s > 0)?;
Some(AgentUsage {
used,
size,
cost: update.pointer("/cost/amount").and_then(Value::as_f64),
currency: update.pointer("/cost/currency").and_then(Value::as_str).map(str::to_owned),
})
}
fn legacy(
v: &Value,
id: &str,
name: &str,
list: &str,
key: &str,
current: &str,
via: AgentConfigVia,
) -> AgentConfigOption {
let values = v
.get(list)
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|m| {
let value = m.get(key)?.as_str()?.to_owned();
Some(AgentConfigValue {
name: str_of(m, "name").unwrap_or_else(|| value.clone()),
description: str_of(m, "description"),
value,
})
})
.collect();
AgentConfigOption {
id: id.to_owned(),
name: name.to_owned(),
description: None,
category: Some(id.to_owned()),
current: str_of(v, current),
values,
via,
}
}
fn value_of(v: &Value) -> Option<AgentConfigValue> {
let value = v.get("value")?.as_str()?.to_owned();
Some(AgentConfigValue {
name: str_of(v, "name").unwrap_or_else(|| value.clone()),
description: str_of(v, "description"),
value,
})
}
fn str_of(v: &Value, key: &str) -> Option<String> {
v.get(key).and_then(Value::as_str).map(str::to_owned)
}
#[cfg(test)]
mod tests {
use super::*;
fn ids(c: &[AgentConfigOption]) -> Vec<(&str, Option<&str>)> {
c.iter().map(|o| (o.id.as_str(), o.current.as_deref())).collect()
}
#[test]
fn config_options_win_over_the_older_fields() {
let r = json!({
"modes": {"currentModeId": "default", "availableModes": [{"id": "default", "name": "Manual"}]},
"configOptions": [
{"id": "mode", "category": "mode", "type": "select", "currentValue": "plan",
"options": [{"value": "default", "name": "Manual"}, {"value": "plan", "name": "Plan"}]},
{"id": "effort", "category": "thought_level", "type": "select", "currentValue": "medium",
"options": [{"group": "g", "name": "G", "options": [{"value": "low"}, {"value": "medium", "name": "Medium"}]}]},
{"id": "fast", "type": "boolean", "currentValue": false}
],
"models": {"currentModelId": "m1", "availableModels": [{"modelId": "m1", "name": "One"}]}
});
let c = options_of(&r);
assert_eq!(ids(&c), [("mode", Some("plan")), ("effort", Some("medium")), ("model", Some("m1"))]);
assert_eq!(c[1].values.iter().map(|v| v.name.as_str()).collect::<Vec<_>>(), ["low", "Medium"]);
assert_eq!(set_request(&c[0], "default").0, "session/set_config_option");
assert_eq!(set_request(&c[2], "m1"), ("session/set_model", json!({"modelId": "m1"})));
}
#[test]
fn updates_replace_options_and_track_the_mode() {
let mut c = options_of(&json!({
"modes": {"currentModeId": "ask", "availableModes": [{"id": "ask"}, {"id": "code"}]},
"configOptions": [{"id": "model", "type": "select", "currentValue": "a", "options": [{"value": "a"}]}]
}));
assert_eq!(c[0].category.as_deref(), Some("model"), "the category defaults from the id");
set_mode(&mut c, "code");
assert_eq!(c[1].current.as_deref(), Some("code"));
assert_eq!(set_request(&c[1], "ask").0, "session/set_mode");
merge_update(
&mut c,
&json!([{"id": "model", "type": "select", "currentValue": "b", "options": [{"value": "b"}]}]),
);
assert_eq!(ids(&c), [("model", Some("b")), ("mode", Some("code"))]);
}
#[test]
fn commands_and_usage() {
let cmds = commands_of(&json!({"availableCommands": [
{"name": "review", "description": "Review changes", "input": {"hint": "what to review"}},
{"name": "/init", "description": "Write CLAUDE.md", "input": null}
]}));
assert_eq!(cmds[0].hint.as_deref(), Some("what to review"));
assert_eq!((cmds[1].name.as_str(), cmds[1].hint.as_ref()), ("init", None));
let u = usage_of(&json!({"used": 1200, "size": 200000, "cost": {"amount": 0.42, "currency": "USD"}})).unwrap();
assert_eq!((u.used, u.cost, u.currency.as_deref()), (1200, Some(0.42), Some("USD")));
assert!(usage_of(&json!({"used": 1, "size": 0})).is_none());
}
}
File diff suppressed because it is too large. Load diff
+285
Переглянути файл
@@ -0,0 +1,285 @@
//! IPC types: tasks, their timeline entries and the events streamed to the UI.
use ide_core::IpcPath;
use serde::{Deserialize, Serialize};
use specta::Type;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AgentStatus {
/// Creating the worktree / starting the agent.
Starting,
Running,
/// Waiting for a permission answer.
NeedsInput,
/// The turn ended; the result can be reviewed or the conversation continued.
Ready,
Failed,
Archived,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum Isolation {
/// A new git worktree on its own branch.
Worktree,
/// The project's own checkout.
Local,
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentTask {
pub id: String,
pub title: String,
/// [`AgentProvider::id`].
pub provider: String,
/// Model the task runs on (its `model` setting), when known.
pub model: Option<String>,
pub status: AgentStatus,
pub isolation: Isolation,
/// Directory the agent works in (the worktree, or the project root).
pub cwd: IpcPath,
/// Branch of the task's worktree.
pub branch: Option<String>,
/// Branch (or revision) the worktree was created from.
pub base: Option<String>,
/// Commit the task's changes are compared against.
pub base_commit: Option<String>,
/// Why the task failed, when it did.
pub error: Option<String>,
pub created_ms: f64,
pub updated_ms: f64,
/// The session's settings (model, mode, thinking effort…) as last reported, with the
/// user's choices applied.
pub config: Vec<AgentConfigOption>,
/// Slash commands the agent offers.
pub commands: Vec<AgentCommand>,
/// Context window use, when the agent reports it.
pub usage: Option<AgentUsage>,
/// Messages waiting for the current turn to end, oldest first.
pub queue: Vec<AgentQueued>,
}
/// One value of a select-type session setting.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentConfigValue {
pub value: String,
pub name: String,
pub description: Option<String>,
}
/// How a setting is changed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AgentConfigVia {
/// `session/set_config_option`.
#[default]
Option,
/// `session/set_mode` (agents that report `modes` instead of a mode config option).
Mode,
/// `session/set_model` (agents that report `models` instead of a model config option).
Model,
}
/// A session setting: an ACP select config option, or the older `modes` / `models`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentConfigOption {
pub id: String,
pub name: String,
pub description: Option<String>,
/// ACP category: `model`, `mode`, `thought_level`, or agent-specific.
pub category: Option<String>,
pub current: Option<String>,
pub values: Vec<AgentConfigValue>,
pub via: AgentConfigVia,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentCommand {
/// Without the leading `/`.
pub name: String,
pub description: String,
/// What to type after the command, when it takes input.
pub hint: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentUsage {
/// Tokens in the context window.
pub used: u32,
/// Size of the context window.
pub size: u32,
/// Session cost so far.
pub cost: Option<f64>,
pub currency: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentImage {
pub mime_type: String,
/// Base64, without a `data:` prefix.
pub data: String,
pub name: Option<String>,
}
/// A message to the agent: text, plus files to look at and images.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentPrompt {
pub text: String,
pub files: Vec<IpcPath>,
pub images: Vec<AgentImage>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentQueued {
pub id: String,
pub prompt: AgentPrompt,
}
/// A setting chosen for a new task, by [`AgentConfigOption::id`].
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentConfigChoice {
pub id: String,
pub value: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AgentToolStatus {
Pending,
Running,
Completed,
Failed,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentToolDiff {
pub path: IpcPath,
pub added: u32,
pub removed: u32,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentPlanItem {
pub content: String,
/// `pending`, `in_progress` or `completed`.
pub status: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentPermissionOption {
pub id: String,
pub name: String,
/// `allow_once`, `allow_always`, `reject_once` or `reject_always`.
pub kind: String,
}
/// One item of a task's conversation.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(tag = "type", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum AgentEntry {
User {
text: String,
/// Names of the files and images sent with it.
attachments: Vec<String>,
},
Agent {
text: String,
},
Thought {
text: String,
},
Tool {
id: String,
title: String,
/// ACP tool kind: `read`, `edit`, `execute`, `search`, `fetch`, `think`…
kind: String,
status: AgentToolStatus,
locations: Vec<IpcPath>,
/// Text the tool reported (command output…), truncated.
output: Option<String>,
diffs: Vec<AgentToolDiff>,
},
Plan {
items: Vec<AgentPlanItem>,
},
Permission {
id: u32,
title: String,
options: Vec<AgentPermissionOption>,
/// Name of the chosen option, or `None` while pending. Cancelled requests read "Cancelled".
chosen: Option<String>,
},
Error {
message: String,
},
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentProvider {
pub id: String,
pub name: String,
/// The command line that starts its ACP server.
pub command: String,
/// Whether the command was found (on `PATH`).
pub installed: bool,
/// Settings (models, modes…) the agent offered last time it ran; empty before its first session.
pub config: Vec<AgentConfigOption>,
/// Slash commands it offered last time.
pub commands: Vec<AgentCommand>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct NewAgentTask {
pub prompt: AgentPrompt,
pub provider: String,
/// Settings for the new session (model, mode…).
pub config: Vec<AgentConfigChoice>,
pub isolation: Isolation,
/// Branch to start the worktree from; the current branch when `None`.
pub base: Option<String>,
}
/// A file the task changed, relative to [`AgentTask::base_commit`].
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AgentChange {
pub path: IpcPath,
pub rel_path: String,
pub kind: ide_vcs::ChangeKind,
pub added: u32,
pub removed: u32,
}
/// Streamed to the window that subscribed (`AgentManager::subscribe`).
#[derive(Clone, Debug, Serialize, Type)]
#[serde(tag = "type", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum AgentEvent {
/// A task was created or its metadata changed.
Task {
task: AgentTask,
},
Removed {
id: String,
},
/// Entry `index` of `task` was added or replaced.
Entry {
task: String,
index: u32,
entry: AgentEntry,
},
}
+158
Переглянути файл
@@ -0,0 +1,158 @@
//! Agents and how to launch their ACP servers: the built-in ones, plus user-defined
//! agents from `<config>/agents/<id>.toml`.
use serde::Deserialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ProviderSpec {
pub id: String,
pub name: String,
pub command: String,
pub args: Vec<String>,
pub env: Vec<(String, String)>,
/// How to sign in when the agent reports it needs authentication.
pub login_hint: String,
}
/// `(id, name, command line, sign-in hint)`. npm adapters are pinned so a session's
/// protocol does not change under the user.
const BUILTIN: &[(&str, &str, &[&str], &str)] = &[
(
"claude",
"Claude Code",
&["npx", "-y", "@agentclientprotocol/[email protected]"],
"Run `claude` in a terminal and sign in.",
),
("codex", "Codex", &["npx", "-y", "@agentclientprotocol/[email protected]"], "Run `codex login` in a terminal."),
("gemini", "Gemini CLI", &["gemini", "--experimental-acp"], "Run `gemini` in a terminal and sign in."),
("copilot", "GitHub Copilot", &["copilot", "--acp"], "Run `copilot` in a terminal and sign in."),
("cursor", "Cursor", &["cursor-agent", "acp"], "Run `cursor-agent login` in a terminal."),
("opencode", "OpenCode", &["opencode", "acp"], "Run `opencode auth login` in a terminal."),
("qwen", "Qwen Code", &["qwen", "--experimental-acp"], "Run `qwen` in a terminal and sign in."),
("kimi", "Kimi CLI", &["kimi", "acp"], "Run `kimi` in a terminal and sign in."),
("goose", "Goose", &["goose", "acp"], "Run `goose configure` in a terminal."),
("auggie", "Auggie", &["auggie", "--acp"], "Run `auggie login` in a terminal."),
("kiro", "Kiro", &["kiro-cli", "acp"], "Run `kiro-cli login` in a terminal."),
("vibe", "Mistral Vibe", &["vibe-acp"], "Run `vibe` in a terminal and sign in."),
];
fn builtins() -> Vec<ProviderSpec> {
BUILTIN
.iter()
.map(|(id, name, cmd, hint)| ProviderSpec {
id: (*id).into(),
name: (*name).into(),
command: cmd[0].into(),
args: cmd[1..].iter().map(|a| (*a).to_owned()).collect(),
env: Vec::new(),
login_hint: (*hint).into(),
})
.collect()
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct CustomAgent {
name: Option<String>,
command: String,
#[serde(default)]
args: Vec<String>,
#[serde(default)]
env: HashMap<String, String>,
}
/// Built-in agents, then the `*.toml` agents of `custom_dir`; a custom agent with a
/// built-in id replaces it.
pub(crate) fn load(custom_dir: Option<&Path>) -> Vec<ProviderSpec> {
let mut specs = builtins();
let mut files: Vec<PathBuf> = custom_dir
.and_then(|d| std::fs::read_dir(d).ok())
.into_iter()
.flatten()
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|e| e == "toml"))
.collect();
files.sort();
for path in files {
let Some(id) = path.file_stem().and_then(|s| s.to_str()).map(str::to_owned) else { continue };
let parsed = std::fs::read_to_string(&path)
.map_err(|e| e.to_string())
.and_then(|t| toml::from_str::<CustomAgent>(&t).map_err(|e| e.to_string()));
let agent = match parsed {
Ok(a) => a,
Err(e) => {
tracing::warn!("skipping agent {}: {e}", path.display());
continue;
}
};
let mut env: Vec<(String, String)> = agent.env.into_iter().collect();
env.sort();
let spec = ProviderSpec {
name: agent.name.unwrap_or_else(|| id.clone()),
command: agent.command,
args: agent.args,
env,
login_hint: "Sign in with the agent's own command line first.".into(),
id,
};
match specs.iter_mut().find(|s| s.id == spec.id) {
Some(existing) => *existing = spec,
None => specs.push(spec),
}
}
specs
}
/// Whether `command` resolves to an executable (a path, or a name on `PATH`).
pub(crate) fn installed(command: &str) -> bool {
let p = Path::new(command);
if p.components().count() > 1 {
return is_executable(p);
}
let exts: &[&str] = if cfg!(windows) { &["exe", "cmd", "bat"] } else { &[] };
std::env::var_os("PATH").is_some_and(|path| {
std::env::split_paths(&path).any(|dir| {
let c = dir.join(command);
is_executable(&c) || exts.iter().any(|e| is_executable(&c.with_extension(e)))
})
})
}
fn is_executable(p: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
p.metadata().is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
}
#[cfg(not(unix))]
{
p.is_file()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn custom_agents_extend_and_override_the_builtins() {
let dir = tempfile::tempdir().unwrap();
let write = |name: &str, text: &str| std::fs::write(dir.path().join(name), text).unwrap();
write("mine.toml", "name = \"Mine\"\ncommand = \"/opt/mine\"\nargs = [\"acp\"]\n[env]\nK = \"v\"\n");
write("gemini.toml", "command = \"gemini\"\nargs = [\"--acp\"]\n");
write("broken.toml", "nope = 1\n");
let specs = load(Some(dir.path()));
let mine = specs.iter().find(|s| s.id == "mine").unwrap();
assert_eq!((mine.name.as_str(), mine.command.as_str()), ("Mine", "/opt/mine"));
assert_eq!(mine.env, vec![("K".to_owned(), "v".to_owned())]);
let gemini = specs.iter().find(|s| s.id == "gemini").unwrap();
assert_eq!((gemini.name.as_str(), gemini.args.as_slice()), ("gemini", &["--acp".to_owned()][..]));
assert!(!specs.iter().any(|s| s.id == "broken"));
assert_eq!(specs.iter().filter(|s| s.id == "claude").count(), 1);
assert!(!installed("/definitely/not/here"));
assert!(installed("sh"));
}
}
+281
Переглянути файл
@@ -0,0 +1,281 @@
//! Claude Code's slash commands on disk, for completion before (or without) a session:
//! skills (`.claude/skills/<dir>/SKILL.md`) and custom commands (`.claude/commands/**/*.md`)
//! of the working directory and every directory above it, then the user's `~/.claude`.
use crate::AgentCommand;
use std::collections::HashSet;
use std::path::Path;
const MAX_DESCRIPTION: usize = 200;
/// Command directories nest this deep at most (`a/b/c.md` → `a:b:c`).
const MAX_DEPTH: usize = 4;
/// The commands Claude Code finds from `cwd`: nearest directory first, the user's last;
/// the first of a name wins.
pub(crate) fn discover(cwd: &Path, home: Option<&Path>) -> Vec<AgentCommand> {
let mut out = Vec::new();
let mut seen = HashSet::new();
let mut add = |found: Vec<AgentCommand>| {
for c in found {
if seen.insert(c.name.clone()) {
out.push(c);
}
}
};
for dir in cwd.ancestors() {
if home.is_some_and(|h| h == dir) {
break;
}
add(in_dir(&dir.join(".claude")));
}
if let Some(home) = home {
add(in_dir(&home.join(".claude")));
}
out
}
/// `first` (the agent's own list) plus the commands of `more` it lacks.
pub(crate) fn merge(first: Vec<AgentCommand>, more: Vec<AgentCommand>) -> Vec<AgentCommand> {
let names: HashSet<String> = first.iter().map(|c| c.name.clone()).collect();
let mut out = first;
out.extend(more.into_iter().filter(|c| !names.contains(&c.name)));
out
}
fn in_dir(claude: &Path) -> Vec<AgentCommand> {
if !claude.is_dir() {
return Vec::new();
}
let mut out = skills(&claude.join("skills"));
commands(&claude.join("commands"), "", 0, &mut out);
out
}
fn skills(dir: &Path) -> Vec<AgentCommand> {
let mut out: Vec<AgentCommand> = sorted(dir)
.into_iter()
.filter_map(|path| {
let text = std::fs::read_to_string(path.join("SKILL.md")).ok()?;
let meta = Frontmatter::parse(&text);
if meta.get("user-invocable").is_some_and(|v| v.eq_ignore_ascii_case("false")) {
return None;
}
let name = meta.get("name").filter(|n| !n.is_empty()).map(str::to_owned).or_else(|| file_name(&path))?;
Some(meta.command(name, &text))
})
.collect();
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
fn commands(dir: &Path, prefix: &str, depth: usize, out: &mut Vec<AgentCommand>) {
if depth > MAX_DEPTH {
return;
}
for path in sorted(dir) {
let Some(name) = file_name(&path) else { continue };
if path.is_dir() {
commands(&path, &format!("{prefix}{name}:"), depth + 1, out);
} else if let Some(stem) = name.strip_suffix(".md")
&& let Ok(text) = std::fs::read_to_string(&path)
{
out.push(Frontmatter::parse(&text).command(format!("{prefix}{stem}"), &text));
}
}
}
fn sorted(dir: &Path) -> Vec<std::path::PathBuf> {
let mut paths: Vec<_> = std::fs::read_dir(dir)
.into_iter()
.flatten()
.flatten()
.map(|e| e.path())
.filter(|p| file_name(p).is_some_and(|n| !n.starts_with('.')))
.collect();
paths.sort();
paths
}
fn file_name(path: &Path) -> Option<String> {
path.file_name()?.to_str().map(str::to_owned)
}
/// The `key: value` lines between the leading `---` fences (folded `>` / `|` values joined).
struct Frontmatter {
fields: Vec<(String, String)>,
/// Where the body starts.
body: usize,
}
impl Frontmatter {
fn parse(text: &str) -> Self {
let text = text.strip_prefix('\u{feff}').unwrap_or(text);
let mut lines = text.split_inclusive('\n');
let mut offset = 0;
let mut fields: Vec<(String, String)> = Vec::new();
match lines.next() {
Some(first) if first.trim_end() == "---" => offset += first.len(),
_ => return Self { fields, body: 0 },
}
for line in lines {
offset += line.len();
let line = line.trim_end();
if line == "---" {
return Self { fields, body: offset };
}
if line.starts_with([' ', '\t']) {
if let Some((_, value)) = fields.last_mut() {
if !value.is_empty() {
value.push(' ');
}
value.push_str(line.trim());
}
continue;
}
if let Some((key, value)) = line.split_once(':') {
let value = value.trim();
let value = if matches!(value, ">" | "|" | ">-" | "|-") { "" } else { unquote(value) };
fields.push((key.trim().to_owned(), value.to_owned()));
}
}
// No closing fence: not frontmatter.
Self { fields: Vec::new(), body: 0 }
}
fn get(&self, key: &str) -> Option<&str> {
self.fields.iter().find(|(k, _)| k == key).map(|(_, v)| v.as_str())
}
/// `description`, else the body's first line.
fn command(&self, name: String, text: &str) -> AgentCommand {
let description = self.get("description").filter(|d| !d.is_empty()).map(str::to_owned).unwrap_or_else(|| {
let body = text.get(self.body..).unwrap_or_default();
body.lines()
.map(|l| l.trim().trim_start_matches('#').trim())
.find(|l| !l.is_empty())
.unwrap_or_default()
.to_owned()
});
AgentCommand {
name,
description: truncate(&description),
hint: self.get("argument-hint").filter(|h| !h.is_empty()).map(str::to_owned),
}
}
}
fn unquote(value: &str) -> &str {
for q in ['"', '\''] {
if let Some(inner) = value.strip_prefix(q).and_then(|v| v.strip_suffix(q)) {
return inner;
}
}
value
}
fn truncate(s: &str) -> String {
match s.char_indices().nth(MAX_DESCRIPTION) {
Some((i, _)) => format!("{}…", s[..i].trim_end()),
None => s.to_owned(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn write(path: &Path, text: &str) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}
fn names(cmds: &[AgentCommand]) -> Vec<&str> {
cmds.iter().map(|c| c.name.as_str()).collect()
}
fn cmd(name: &str, description: &str) -> AgentCommand {
AgentCommand { name: name.into(), description: description.into(), hint: None }
}
#[test]
fn project_and_user_skills_and_commands() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path().join("home");
let repo = tmp.path().join("work").join("idea");
let skills = repo.join(".claude/skills");
write(
&skills.join("triage/SKILL.md"),
"---\nname: triage\ndescription: One pass over open issues and PRs\n---\n# Triage\n",
);
write(&skills.join("issue-fix/SKILL.md"), "---\nname: issue-fix\ndescription: \"Fix an issue\"\n---\n");
// No frontmatter name: the directory's.
write(&skills.join("verify/SKILL.md"), "---\ndescription: >\n Build and\n drive the app\n---\n");
write(&skills.join("hidden/SKILL.md"), "---\nname: hidden\nuser-invocable: false\n---\n");
write(&skills.join("no-skill-file/README.md"), "nothing");
write(&repo.join(".claude/commands/review.md"), "---\nargument-hint: <pr>\n---\nReview a pull request\n");
write(&repo.join(".claude/commands/git/commit.md"), "# Commit the staged changes\n");
// A parent directory's commands apply too.
write(&tmp.path().join("work/.claude/commands/parent.md"), "---\ndescription: from above\n---\n");
// The user's: shadowed by the project's skill of the same name.
write(&home.join(".claude/skills/triage/SKILL.md"), "---\nname: triage\ndescription: user triage\n---\n");
write(&home.join(".claude/skills/lint/SKILL.md"), "---\nname: lint\ndescription: user lint\n---\n");
write(&home.join(".claude/commands/brooks-review.md"), "---\ndescription: Review\n---\n");
let found = discover(&repo, Some(&home));
assert_eq!(
names(&found),
["issue-fix", "triage", "verify", "git:commit", "review", "parent", "lint", "brooks-review"]
);
let by = |n: &str| found.iter().find(|c| c.name == n).unwrap();
assert_eq!(by("triage").description, "One pass over open issues and PRs");
assert_eq!(by("issue-fix").description, "Fix an issue");
assert_eq!(by("verify").description, "Build and drive the app");
assert_eq!(by("git:commit").description, "Commit the staged changes");
assert_eq!(
(by("review").description.as_str(), by("review").hint.as_deref()),
("Review a pull request", Some("<pr>"))
);
}
#[test]
fn nothing_without_claude_dirs() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path().join("home");
let repo = home.join("plain");
std::fs::create_dir_all(&repo).unwrap();
assert!(discover(&repo, Some(&home)).is_empty());
}
#[test]
fn walking_up_stops_at_home() {
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path().join("home");
// Above home: not the user's, and not this project's.
write(&tmp.path().join(".claude/commands/outside.md"), "x\n");
write(&home.join(".claude/commands/mine.md"), "x\n");
let repo = home.join("src/app");
std::fs::create_dir_all(&repo).unwrap();
assert_eq!(names(&discover(&repo, Some(&home))), ["mine"]);
}
#[test]
fn the_agents_entries_win() {
let merged = merge(
vec![cmd("triage", "from the agent"), cmd("compact", "Compact")],
vec![cmd("triage", "from disk"), cmd("verify", "Verify")],
);
assert_eq!(names(&merged), ["triage", "compact", "verify"]);
assert_eq!(merged[0].description, "from the agent");
}
#[test]
fn frontmatter_edge_cases() {
let f = Frontmatter::parse("no fence\n");
assert!(f.fields.is_empty());
let f = Frontmatter::parse("---\nname: x\nunterminated\n");
assert!(f.get("name").is_none());
let f = Frontmatter::parse("\u{feff}---\r\nname: 'quoted'\r\n---\r\nbody\r\n");
assert_eq!(f.get("name"), Some("quoted"));
assert_eq!(truncate(&"é".repeat(MAX_DESCRIPTION + 5)).chars().count(), MAX_DESCRIPTION + 1);
}
}
+471
Переглянути файл
@@ -0,0 +1,471 @@
//! End-to-end task flow against an in-memory ACP agent.
use super::*;
use std::sync::OnceLock;
use tokio::io::duplex;
/// Says "Hi", edits a file, asks to run a command and reports what it was allowed.
#[derive(Default)]
struct FakeAgent {
peer: OnceLock<Peer>,
calls: Mutex<Vec<String>>,
}
impl FakeAgent {
fn update(&self, session: &str, update: Value) {
let peer = self.peer.get().unwrap();
peer.notify("session/update", json!({ "sessionId": session, "update": update })).unwrap();
}
fn say(&self, session: &str, text: &str) {
self.update(
session,
json!({"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": text}}),
);
}
}
impl Handler for FakeAgent {
fn on_notification(&self, method: &str, _: Value) {
self.calls.lock().push(method.to_owned());
}
fn on_request(&self, method: &str, params: Value) -> Result<Value, (i64, String)> {
self.calls.lock().push(method.to_owned());
let session = params.get("sessionId").and_then(Value::as_str).unwrap_or("s1").to_owned();
match method {
"initialize" => Ok(json!({"protocolVersion": 1, "agentCapabilities": {"loadSession": true}})),
"session/new" => Ok(json!({"sessionId": "s1",
"modes": {"currentModeId": "ask", "availableModes": [{"id": "ask", "name": "Ask"}, {"id": "code", "name": "Code"}]},
"configOptions": [{"id": "model", "category": "model", "type": "select", "currentValue": "big",
"options": [{"value": "big", "name": "Big"}, {"value": "small", "name": "Small"}]}]})),
"session/load" => {
self.say(&session, "replayed history");
Ok(json!({}))
}
"session/set_config_option" | "session/set_mode" => {
self.calls.lock().push(format!("{method} {}", params.get("value").or(params.get("modeId")).unwrap()));
Ok(json!({}))
}
"session/prompt" => {
let text = params.pointer("/prompt/0/text").and_then(Value::as_str).unwrap_or_default().to_owned();
if text == "again" {
self.say(&session, "second turn");
return Ok(json!({"stopReason": "end_turn"}));
}
if text == "wait" {
// Works until cancelled.
while !self.calls.lock().iter().any(|c| c == "session/cancel") {
std::thread::sleep(Duration::from_millis(5));
}
return Ok(json!({"stopReason": "cancelled"}));
}
self.update(
&session,
json!({"sessionUpdate": "available_commands_update",
"availableCommands": [{"name": "review", "description": "Review", "input": {"hint": "focus"}}]}),
);
self.update(&session, json!({"sessionUpdate": "usage_update", "used": 50, "size": 1000}));
self.say(&session, "Hi");
self.update(
&session,
json!({"sessionUpdate": "tool_call", "toolCallId": "t1", "title": "Edit a.rs",
"kind": "edit", "status": "completed",
"content": [{"type": "diff", "path": "/p/a.rs", "oldText": "a\n", "newText": "b\n"}]}),
);
let peer = self.peer.get().unwrap().clone();
let answer: Value = tokio::runtime::Handle::current()
.block_on(peer.request(
"session/request_permission",
json!({"sessionId": session, "toolCall": {"toolCallId": "t1", "title": "Run cargo test"},
"options": [{"optionId": "yes", "name": "Allow", "kind": "allow_once"},
{"optionId": "no", "name": "Reject", "kind": "reject_once"}]}),
None,
))
.unwrap();
let chosen = answer.pointer("/outcome/optionId").and_then(Value::as_str).unwrap_or("none");
self.say(&session, &format!("chose {chosen}"));
Ok(json!({"stopReason": "end_turn"}))
}
_ => Err((codes::METHOD_NOT_FOUND, method.to_owned())),
}
}
}
fn manager(dir: &Path, agent: Arc<FakeAgent>) -> AgentManager {
manager_at(dir, &dir.join("models.json"), agent)
}
fn manager_at(dir: &Path, models_file: &Path, agent: Arc<FakeAgent>) -> AgentManager {
let connector: Connector = Arc::new(move |_spec, _cwd, client, _stderr| {
let (a, b) = duplex(1 << 20);
let (ar, aw) = tokio::io::split(a);
let (br, bw) = tokio::io::split(b);
let server = Peer::connect_framed(br, bw, agent.clone(), Framing::Lines);
// Keep the latest connection: a restarted manager connects again.
let _ = agent.peer.set(server);
Ok(Peer::connect_framed(ar, aw, client, Framing::Lines))
});
AgentManager::with_connector(
AgentPaths {
project_root: dir.to_path_buf(),
store_dir: dir.join("store"),
worktrees_dir: dir.join("worktrees"),
models_file: models_file.to_path_buf(),
custom_agents_dir: None,
home_dir: None,
},
tokio::runtime::Handle::current(),
connector,
)
}
#[tokio::test(flavor = "multi_thread")]
async fn commands_are_remembered_per_project_and_merged_with_skills_on_disk() {
let tmp = tempfile::tempdir().unwrap();
let (a, b, models) = (tmp.path().join("a"), tmp.path().join("b"), tmp.path().join("models.json"));
std::fs::create_dir_all(a.join(".claude/skills/triage")).unwrap();
std::fs::create_dir_all(&b).unwrap();
std::fs::write(a.join(".claude/skills/triage/SKILL.md"), "---\nname: triage\ndescription: Triage\n---\n").unwrap();
let names = |c: Vec<AgentCommand>| c.into_iter().map(|c| c.name).collect::<Vec<_>>();
let mgr_a = manager_at(&a, &models, Arc::new(FakeAgent::default()));
// Before any session: the skills on disk.
assert_eq!(names(mgr_a.commands("claude", None).await.unwrap()), ["triage"]);
let task = mgr_a
.create(NewAgentTask {
prompt: text("Fix the lexer"),
provider: "claude".into(),
config: Vec::new(),
isolation: Isolation::Local,
base: None,
})
.await
.unwrap();
wait_for(&mgr_a, &task.id, AgentStatus::NeedsInput).await;
assert_eq!(names(mgr_a.commands("claude", None).await.unwrap()), ["review", "triage"]);
assert_eq!(names(mgr_a.commands("claude", Some(&task.id)).await.unwrap()), ["review", "triage"]);
// Other agents get only what they offered.
assert!(mgr_a.commands("codex", None).await.unwrap().is_empty());
mgr_a.shutdown();
// Another project shares the settings but not the commands.
let mgr_b = manager_at(&b, &models, Arc::new(FakeAgent::default()));
let claude = mgr_b.providers().into_iter().find(|p| p.id == "claude").unwrap();
assert_eq!(claude.config.len(), 2);
assert!(claude.commands.is_empty());
assert!(mgr_b.commands("claude", None).await.unwrap().is_empty());
let mgr_a = manager_at(&a, &models, Arc::new(FakeAgent::default()));
assert_eq!(names(mgr_a.providers().into_iter().find(|p| p.id == "claude").unwrap().commands), ["review"]);
}
async fn wait_for(mgr: &AgentManager, id: &str, status: AgentStatus) {
for _ in 0..500 {
if mgr.task(id).unwrap().state.lock().task.status == status {
return;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
panic!("task never reached {status:?}");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_task_runs_asks_for_permission_and_survives_a_restart() {
let tmp = tempfile::tempdir().unwrap();
let agent = Arc::new(FakeAgent::default());
let mgr = manager(tmp.path(), agent.clone());
let events = Arc::new(Mutex::new(Vec::<AgentEvent>::new()));
let sink = events.clone();
mgr.subscribe(Arc::new(move |e| sink.lock().push(e)));
let task = mgr
.create(NewAgentTask {
prompt: text("Fix the lexer\nmore detail"),
provider: "claude".into(),
config: vec![AgentConfigChoice { id: "model".into(), value: "small".into() }],
isolation: Isolation::Local,
base: None,
})
.await
.unwrap();
assert_eq!(task.title, "Fix the lexer");
assert_ne!(mgr.task(&task.id).unwrap().state.lock().task.status, AgentStatus::Ready, "a new task is not settled");
wait_for(&mgr, &task.id, AgentStatus::NeedsInput).await;
let permission = mgr
.entries(&task.id)
.unwrap()
.iter()
.find_map(|e| match e {
AgentEntry::Permission { id, .. } => Some(*id),
_ => None,
})
.unwrap();
mgr.respond(&task.id, permission, Some("yes".into())).unwrap();
wait_for(&mgr, &task.id, AgentStatus::Ready).await;
let entries = mgr.entries(&task.id).unwrap();
let kinds: Vec<String> = entries
.iter()
.map(|e| match e {
AgentEntry::User { text, .. } | AgentEntry::Agent { text } => text.clone(),
AgentEntry::Tool { title, .. } => title.clone(),
AgentEntry::Permission { chosen, .. } => format!("permission: {}", chosen.clone().unwrap_or_default()),
other => format!("{other:?}"),
})
.collect();
assert_eq!(kinds, ["Fix the lexer\nmore detail", "Hi", "Edit a.rs", "permission: Allow", "chose yes"]);
assert!(
agent.calls.lock().contains(&"session/set_config_option \"small\"".to_owned()),
"switched to the chosen model"
);
{
let t = mgr.task(&task.id).unwrap().state.lock().task.clone();
assert_eq!(t.model.as_deref(), Some("small"));
let modes = t.config.iter().find(|o| o.id == "mode").unwrap();
assert_eq!((modes.via, modes.current.as_deref()), (AgentConfigVia::Mode, Some("ask")));
assert_eq!(t.commands.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(), ["review"]);
assert_eq!(t.usage.as_ref().map(|u| (u.used, u.size)), Some((50, 1000)));
}
mgr.set_config(&task.id, "mode", "code".into()).await.unwrap();
assert!(agent.calls.lock().contains(&"session/set_mode \"code\"".to_owned()));
assert!(mgr.set_config(&task.id, "mode", "nope".into()).await.is_err());
tokio::time::sleep(FLUSH_DELAY * 3).await;
{
let events = events.lock();
assert!(
events.iter().any(|e| matches!(e, AgentEvent::Task { task } if task.status == AgentStatus::NeedsInput))
);
let last_entry = events.iter().rev().find_map(|e| match e {
AgentEvent::Entry { index, entry, .. } => Some((*index, entry.clone())),
_ => None,
});
assert_eq!(last_entry, Some((4, AgentEntry::Agent { text: "chose yes".into() })));
}
let providers = mgr.providers();
let claude = providers.iter().find(|p| p.id == "claude").unwrap();
assert_eq!(claude.config.iter().map(|o| o.id.as_str()).collect::<Vec<_>>(), ["model", "mode"]);
assert_eq!(claude.commands.len(), 1);
// A new manager (IDE restart) finds the task and resumes its session without duplicating history.
mgr.shutdown();
let agent2 = Arc::new(FakeAgent::default());
let mgr2 = manager(tmp.path(), agent2.clone());
assert_eq!(mgr2.entries(&task.id).unwrap().len(), 5);
assert_eq!(mgr2.providers().iter().find(|p| p.id == "claude").unwrap().config.len(), 2);
mgr2.prompt(&task.id, text("again"), false).await.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
wait_for(&mgr2, &task.id, AgentStatus::Ready).await;
let entries = mgr2.entries(&task.id).unwrap();
assert_eq!(entries.len(), 7);
assert_eq!(entries[6], AgentEntry::Agent { text: "second turn".into() });
assert!(agent2.calls.lock().contains(&"session/load".to_owned()));
// The chosen settings are re-applied to the reloaded session.
let calls = agent2.calls.lock().clone();
assert!(calls.contains(&"session/set_mode \"code\"".to_owned()), "{calls:?}");
}
fn text(t: &str) -> AgentPrompt {
AgentPrompt { text: t.into(), ..AgentPrompt::default() }
}
#[tokio::test(flavor = "multi_thread")]
async fn messages_sent_while_busy_are_queued_and_send_now_interrupts() {
let tmp = tempfile::tempdir().unwrap();
let agent = Arc::new(FakeAgent::default());
let mgr = manager(tmp.path(), agent.clone());
let task = mgr
.create(NewAgentTask {
prompt: text("wait"),
provider: "claude".into(),
config: vec![],
isolation: Isolation::Local,
base: None,
})
.await
.unwrap();
wait_for(&mgr, &task.id, AgentStatus::Running).await;
mgr.prompt(&task.id, text("again"), false).await.unwrap();
let queue = mgr.task(&task.id).unwrap().state.lock().task.queue.clone();
assert_eq!(queue.iter().map(|q| q.prompt.text.as_str()).collect::<Vec<_>>(), ["again"]);
assert!(mgr.prompt(&task.id, text(" "), false).await.is_err());
mgr.send_queued_now(&task.id, &queue[0].id).unwrap();
for _ in 0..500 {
if mgr.entries(&task.id).unwrap().iter().any(|e| *e == AgentEntry::Agent { text: "second turn".into() }) {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
wait_for(&mgr, &task.id, AgentStatus::Ready).await;
let users: Vec<String> = mgr
.entries(&task.id)
.unwrap()
.into_iter()
.filter_map(|e| match e {
AgentEntry::User { text, .. } => Some(text),
_ => None,
})
.collect();
assert_eq!(users, ["wait", "again"]);
assert!(mgr.task(&task.id).unwrap().state.lock().task.queue.is_empty());
// Stopping a turn keeps the queue for later.
mgr.prompt(&task.id, text("wait"), false).await.unwrap();
wait_for(&mgr, &task.id, AgentStatus::Running).await;
agent.calls.lock().retain(|c| c != "session/cancel");
mgr.prompt(&task.id, text("later"), false).await.unwrap();
mgr.cancel(&task.id).unwrap();
wait_for(&mgr, &task.id, AgentStatus::Ready).await;
let queued = mgr.task(&task.id).unwrap().state.lock().task.queue.clone();
assert_eq!(queued.len(), 1);
assert_eq!(mgr.unqueue(&task.id, &queued[0].id).unwrap().prompt.text, "later");
assert!(mgr.task(&task.id).unwrap().state.lock().task.queue.is_empty());
}
#[test]
fn prompts_become_acp_content_blocks() {
let p = AgentPrompt {
text: "look".into(),
files: vec![IpcPath(PathBuf::from("/p/my file.rs"))],
images: vec![AgentImage { mime_type: "image/png".into(), data: "AAA".into(), name: None }],
};
assert_eq!(
blocks(&p, &[]),
[
json!({"type": "text", "text": "look"}),
json!({"type": "resource_link", "uri": "file:///p/my%20file.rs", "name": "my file.rs"}),
json!({"type": "image", "mimeType": "image/png", "data": "AAA"}),
]
);
assert_eq!(
blocks(&p, &[Some("fn a() {}".into())])[1],
json!({"type": "resource", "resource": {"uri": "file:///p/my%20file.rs", "text": "fn a() {}"}})
);
// A slash command goes through as typed: the agent runs it.
let skill = clean(text(" /triage dry-run\n")).unwrap();
assert_eq!(blocks(&skill, &[]), [json!({"type": "text", "text": "/triage dry-run"})]);
let old = upgrade(
json!({"task": {"id": "t"}, "entries": [{"type": "user", "text": "hi"}, {"type": "agent", "text": "a"}]}),
);
assert_eq!(old["task"]["queue"], json!([]));
assert_eq!(old["entries"][0]["attachments"], json!([]));
assert!(old["entries"][1].get("attachments").is_none());
assert_eq!(title_of(&AgentPrompt { files: p.files.clone(), ..AgentPrompt::default() }), "my file.rs");
assert!(clean(text(" ")).is_none());
}
#[tokio::test(flavor = "multi_thread")]
async fn cancelling_resolves_the_open_permission_and_delete_forgets_the_task() {
let tmp = tempfile::tempdir().unwrap();
let mgr = manager(tmp.path(), Arc::new(FakeAgent::default()));
let task = mgr
.create(NewAgentTask {
prompt: text("do it"),
provider: "codex".into(),
config: vec![],
isolation: Isolation::Local,
base: None,
})
.await
.unwrap();
wait_for(&mgr, &task.id, AgentStatus::NeedsInput).await;
mgr.cancel(&task.id).unwrap();
wait_for(&mgr, &task.id, AgentStatus::Ready).await;
let entries = mgr.entries(&task.id).unwrap();
assert!(entries.iter().any(|e| matches!(e, AgentEntry::Permission { chosen: Some(c), .. } if c == "Cancelled")));
assert_eq!(entries.last(), Some(&AgentEntry::Agent { text: "chose none".into() }));
assert_eq!(mgr.task(&task.id).unwrap().state.lock().task.model.as_deref(), Some("big"), "the agent's default");
mgr.delete(&task.id, false).await.unwrap();
assert!(mgr.entries(&task.id).is_err());
assert!(!tmp.path().join("store").join(format!("{}.json", task.id)).exists());
assert!(
mgr.create(NewAgentTask {
prompt: text(" "),
provider: "codex".into(),
config: vec![],
isolation: Isolation::Local,
base: None
})
.await
.is_err()
);
}
/// Drives the real Claude Code adapter (needs `npx`, network and a signed-in `claude`):
/// `OPENIDEA_AGENT_E2E=1 cargo test -p ide-agent real_claude -- --ignored --nocapture`.
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn real_claude_task_in_a_worktree() {
if std::env::var_os("OPENIDEA_AGENT_E2E").is_none() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir(&repo).unwrap();
for args in [&["init", "-q", "-b", "main"][..], &["commit", "-q", "--allow-empty", "-m", "init"]] {
let ok = std::process::Command::new("git")
.args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"])
.args(args)
.current_dir(&repo)
.status()
.unwrap()
.success();
assert!(ok);
}
let mgr = AgentManager::new(
AgentPaths {
project_root: repo.clone(),
store_dir: tmp.path().join("store"),
worktrees_dir: tmp.path().join("wt"),
models_file: tmp.path().join("models.json"),
custom_agents_dir: None,
home_dir: None,
},
tokio::runtime::Handle::current(),
);
let task = mgr
.create(NewAgentTask {
prompt: text("Create a file named hello.txt containing the single word hi. Do nothing else."),
provider: "claude".into(),
config: vec![AgentConfigChoice { id: "model".into(), value: "haiku".into() }],
isolation: Isolation::Worktree,
base: None,
})
.await
.unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(240);
loop {
assert!(std::time::Instant::now() < deadline, "timed out");
let status = mgr.task(&task.id).unwrap().state.lock().task.status;
match status {
AgentStatus::NeedsInput => {
let pending = mgr.entries(&task.id).unwrap().into_iter().rev().find_map(|e| match e {
AgentEntry::Permission { id, options, chosen: None, .. } => Some((id, options)),
_ => None,
});
if let Some((id, options)) = pending {
let allow = options.iter().find(|o| o.kind == "allow_once").or(options.first()).unwrap();
eprintln!("allowing: {}", allow.name);
let _ = mgr.respond(&task.id, id, Some(allow.id.clone()));
}
}
AgentStatus::Ready | AgentStatus::Failed => break,
_ => {}
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
for e in mgr.entries(&task.id).unwrap() {
eprintln!("{e:?}");
}
let t = mgr.task(&task.id).unwrap().state.lock().task.clone();
assert_eq!(t.status, AgentStatus::Ready, "{:?}", t.error);
assert_eq!(t.model.as_deref(), Some("haiku"));
let changes = mgr.changes(&task.id).await.unwrap();
assert!(changes.iter().any(|c| c.rel_path == "hello.txt"), "{changes:?}");
assert!(!repo.join("hello.txt").exists(), "the project checkout is untouched");
assert!(!mgr.providers().iter().find(|p| p.id == "claude").unwrap().config.is_empty());
mgr.shutdown();
}
+300
Переглянути файл
@@ -0,0 +1,300 @@
//! Folds ACP `session/update` notifications into a task's entries.
use crate::model::{AgentEntry, AgentPlanItem, AgentToolDiff, AgentToolStatus};
use ide_core::IpcPath;
use serde_json::Value;
use std::collections::HashMap;
/// Tool output kept per call; longer output keeps its tail.
const MAX_OUTPUT: usize = 64 * 1024;
#[derive(Default)]
pub(crate) struct Timeline {
pub entries: Vec<AgentEntry>,
tools: HashMap<String, usize>,
plan: Option<usize>,
}
/// What a `session/update` changed in the timeline.
#[derive(Debug, Default, PartialEq)]
pub(crate) struct Applied {
/// Indices of the entries added or changed.
pub changed: Vec<usize>,
}
impl Timeline {
pub fn from_entries(entries: Vec<AgentEntry>) -> Self {
let mut t = Self { entries, ..Self::default() };
for (i, e) in t.entries.iter().enumerate() {
match e {
AgentEntry::Tool { id, .. } => {
t.tools.insert(id.clone(), i);
}
AgentEntry::Plan { .. } => t.plan = Some(i),
_ => {}
}
}
t
}
pub fn push(&mut self, entry: AgentEntry) -> usize {
self.entries.push(entry);
self.entries.len() - 1
}
/// Applies one `session/update` `update` object.
pub fn apply(&mut self, update: &Value) -> Applied {
let mut out = Applied::default();
let kind = update.get("sessionUpdate").and_then(Value::as_str).unwrap_or_default();
match kind {
"agent_message_chunk" | "agent_thought_chunk" => {
let Some(text) = content_text(update.get("content")) else { return out };
out.changed.push(self.append_text(kind == "agent_thought_chunk", &text));
}
"tool_call" => {
let id = str_field(update, "toolCallId");
let entry = AgentEntry::Tool {
id: id.clone(),
title: str_field(update, "title"),
kind: update.get("kind").and_then(Value::as_str).unwrap_or("other").to_owned(),
status: tool_status(update.get("status")).unwrap_or(AgentToolStatus::Pending),
locations: locations(update.get("locations")),
output: None,
diffs: Vec::new(),
};
let index = match self.tools.get(&id) {
Some(&i) => {
self.entries[i] = entry;
i
}
None => {
let i = self.push(entry);
self.tools.insert(id, i);
i
}
};
self.merge_tool(index, update);
out.changed.push(index);
}
"tool_call_update" => {
let Some(&index) = self.tools.get(&str_field(update, "toolCallId")) else { return out };
self.merge_tool(index, update);
out.changed.push(index);
}
"plan" => {
let items = update
.get("entries")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.map(|e| AgentPlanItem { content: str_field(e, "content"), status: str_field(e, "status") })
.collect()
})
.unwrap_or_default();
let entry = AgentEntry::Plan { items };
let index = match self.plan {
Some(i) => {
self.entries[i] = entry;
i
}
None => self.push(entry),
};
self.plan = Some(index);
out.changed.push(index);
}
_ => {}
}
out
}
fn append_text(&mut self, thought: bool, text: &str) -> usize {
match self.entries.last_mut() {
Some(AgentEntry::Agent { text: t }) if !thought => t.push_str(text),
Some(AgentEntry::Thought { text: t }) if thought => t.push_str(text),
_ => {
let text = text.to_owned();
return self.push(if thought { AgentEntry::Thought { text } } else { AgentEntry::Agent { text } });
}
}
self.entries.len() - 1
}
fn merge_tool(&mut self, index: usize, update: &Value) {
let AgentEntry::Tool { title, kind, status, locations: locs, output, diffs, .. } = &mut self.entries[index]
else {
return;
};
if let Some(t) = update.get("title").and_then(Value::as_str) {
*title = t.to_owned();
}
if let Some(k) = update.get("kind").and_then(Value::as_str) {
*kind = k.to_owned();
}
if let Some(s) = tool_status(update.get("status")) {
*status = s;
}
if update.get("locations").is_some_and(Value::is_array) {
*locs = locations(update.get("locations"));
}
let Some(content) = update.get("content").and_then(Value::as_array) else { return };
let mut text = String::new();
let mut new_diffs = Vec::new();
for item in content {
match item.get("type").and_then(Value::as_str) {
Some("content") => {
if let Some(t) = content_text(item.get("content")) {
if !text.is_empty() {
text.push('\n');
}
text.push_str(&t);
}
}
Some("diff") => {
let old = item.get("oldText").and_then(Value::as_str).unwrap_or_default();
let new = item.get("newText").and_then(Value::as_str).unwrap_or_default();
let (added, removed) = line_stats(old, new);
new_diffs.push(AgentToolDiff { path: IpcPath(str_field(item, "path").into()), added, removed });
}
_ => {}
}
}
if !text.is_empty() {
*output = Some(tail(text));
}
if !new_diffs.is_empty() {
*diffs = new_diffs;
}
}
/// Settles what an ended turn left open: running tools and pending permissions.
pub fn end_turn(&mut self) -> Vec<usize> {
let mut changed = Vec::new();
for (i, e) in self.entries.iter_mut().enumerate() {
match e {
AgentEntry::Tool { status, .. }
if matches!(status, AgentToolStatus::Pending | AgentToolStatus::Running) =>
{
*status = AgentToolStatus::Failed;
changed.push(i);
}
AgentEntry::Permission { chosen, .. } if chosen.is_none() => {
*chosen = Some("Cancelled".into());
changed.push(i);
}
_ => {}
}
}
changed
}
}
fn str_field(v: &Value, key: &str) -> String {
v.get(key).and_then(Value::as_str).unwrap_or_default().to_owned()
}
fn content_text(content: Option<&Value>) -> Option<String> {
let c = content?;
match c.get("type").and_then(Value::as_str) {
Some("text") => c.get("text").and_then(Value::as_str).map(str::to_owned),
Some("resource_link") => c.get("uri").and_then(Value::as_str).map(str::to_owned),
Some("resource") => c.pointer("/resource/text").and_then(Value::as_str).map(str::to_owned),
_ => None,
}
}
fn tool_status(v: Option<&Value>) -> Option<AgentToolStatus> {
Some(match v?.as_str()? {
"pending" => AgentToolStatus::Pending,
"in_progress" => AgentToolStatus::Running,
"completed" => AgentToolStatus::Completed,
"failed" => AgentToolStatus::Failed,
_ => return None,
})
}
fn locations(v: Option<&Value>) -> Vec<IpcPath> {
v.and_then(Value::as_array)
.map(|a| a.iter().filter_map(|l| l.get("path")?.as_str()).map(|p| IpcPath(p.into())).collect())
.unwrap_or_default()
}
fn line_stats(old: &str, new: &str) -> (u32, u32) {
ide_vcs::diff_hunks(old, new).iter().fold((0, 0), |(a, r), h| (a + h.lines, r + h.old_lines))
}
fn tail(mut text: String) -> String {
if text.len() > MAX_OUTPUT {
let mut cut = text.len() - MAX_OUTPUT;
while !text.is_char_boundary(cut) {
cut += 1;
}
text.replace_range(..cut, "…");
}
text
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn chunks_merge_into_one_message_until_another_entry_comes() {
let mut t = Timeline::default();
let chunk = |s: &str| json!({"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": s}});
assert_eq!(t.apply(&chunk("Hel")).changed, vec![0]);
assert_eq!(t.apply(&chunk("lo")).changed, vec![0]);
t.apply(&json!({"sessionUpdate": "agent_thought_chunk", "content": {"type": "text", "text": "hmm"}}));
t.apply(&chunk("again"));
assert_eq!(
t.entries,
vec![
AgentEntry::Agent { text: "Hello".into() },
AgentEntry::Thought { text: "hmm".into() },
AgentEntry::Agent { text: "again".into() },
]
);
}
#[test]
fn tool_calls_update_in_place() {
let mut t = Timeline::default();
t.apply(&json!({"sessionUpdate": "tool_call", "toolCallId": "c1", "title": "Edit a.rs", "kind": "edit",
"status": "pending", "locations": [{"path": "/p/a.rs", "line": 3}]}));
let applied = t.apply(&json!({"sessionUpdate": "tool_call_update", "toolCallId": "c1", "status": "completed",
"content": [
{"type": "diff", "path": "/p/a.rs", "oldText": "a\nb\n", "newText": "a\nB\nc\n"},
{"type": "content", "content": {"type": "text", "text": "done"}}
]}));
assert_eq!(applied.changed, vec![0]);
let AgentEntry::Tool { status, diffs, output, locations, .. } = &t.entries[0] else { panic!() };
assert_eq!(*status, AgentToolStatus::Completed);
assert_eq!(diffs, &vec![AgentToolDiff { path: IpcPath("/p/a.rs".into()), added: 2, removed: 1 }]);
assert_eq!(output.as_deref(), Some("done"));
assert_eq!(locations, &vec![IpcPath("/p/a.rs".into())]);
// Unknown calls are ignored.
assert!(t.apply(&json!({"sessionUpdate": "tool_call_update", "toolCallId": "zz"})).changed.is_empty());
}
#[test]
fn plan_is_one_entry_and_end_turn_settles_open_items() {
let mut t = Timeline::default();
let plan =
|s: &str| json!({"sessionUpdate": "plan", "entries": [{"content": "x", "priority": "high", "status": s}]});
t.apply(&plan("pending"));
t.push(AgentEntry::Agent { text: "between".into() });
assert_eq!(t.apply(&plan("completed")).changed, vec![0]);
t.apply(&json!({"sessionUpdate": "tool_call", "toolCallId": "c", "title": "Run", "status": "in_progress"}));
t.push(AgentEntry::Permission { id: 1, title: "Run?".into(), options: vec![], chosen: None });
assert_eq!(t.end_turn(), vec![2, 3]);
let again = Timeline::from_entries(t.entries.clone());
assert_eq!(again.plan, Some(0));
assert_eq!(again.tools.get("c"), Some(&2));
}
#[test]
fn long_output_keeps_its_tail() {
let s = tail("é".repeat(MAX_OUTPUT));
assert!(s.starts_with('…') && s.len() <= MAX_OUTPUT + 3);
}
}
+156
Переглянути файл
@@ -0,0 +1,156 @@
//! Task worktrees and the changes made in them.
use crate::model::AgentChange;
use ide_core::{IdeError, IdeResult, IpcPath};
use ide_vcs::{ChangeKind, Repo};
use std::path::{Path, PathBuf};
pub(crate) struct Created {
pub path: PathBuf,
pub branch: String,
pub base: String,
pub base_commit: String,
}
fn repo_at(path: &Path) -> IdeResult<Repo> {
Repo::discover(path)?.ok_or_else(|| IdeError::unavailable(format!("{} is not in a git repository", path.display())))
}
/// The current branch (or commit when detached) and its commit.
pub(crate) fn head_of(path: &Path) -> IdeResult<(String, String)> {
let info = repo_at(path)?.branch_info()?;
let commit = info.head_commit.ok_or_else(|| IdeError::unavailable("the repository has no commits yet"))?;
Ok((info.head.unwrap_or_else(|| commit.clone()), commit))
}
/// `git worktree add -b agent/<slug> <dir>/<slug> <base>`.
pub(crate) fn create(project_root: &Path, worktrees_dir: &Path, title: &str, base: Option<&str>) -> IdeResult<Created> {
let repo = repo_at(project_root)?;
let (base, base_commit) = match base.map(str::trim).filter(|b| !b.is_empty()) {
Some(b) => {
let commit =
repo.merge_base(b, b)?.ok_or_else(|| IdeError::not_found(format!("no branch or revision {b}")))?;
(b.to_owned(), commit)
}
None => head_of(project_root)?,
};
std::fs::create_dir_all(worktrees_dir).map_err(|e| IdeError::internal(e.to_string()))?;
let branch_exists = |s: &str| {
let r = format!("refs/heads/agent/{s}");
repo.merge_base(&r, &r).ok().flatten().is_some()
};
let slug = unique_slug(&slugify(title), |s| worktrees_dir.join(s).exists() || branch_exists(s));
let path = worktrees_dir.join(&slug);
let branch = format!("agent/{slug}");
repo.add_worktree(&path, &base_commit, Some(&branch))?;
Ok(Created { path, branch, base, base_commit })
}
pub(crate) fn remove(project_root: &Path, path: &Path) -> IdeResult<()> {
repo_at(project_root)?.remove_worktree(path, true)
}
/// Files under `cwd` that differ from `base` (untracked files included), with line counts.
pub(crate) fn changes(cwd: &Path, base: &str) -> IdeResult<Vec<AgentChange>> {
let repo = repo_at(cwd)?;
let root = repo.root().to_path_buf();
let stats = repo.numstat_with_worktree(base)?;
let stat = |rel: &str| stats.iter().find(|(p, ..)| p == rel).map_or((0, 0), |&(_, a, r)| (a, r));
let mut out: Vec<AgentChange> = repo
.diff_with_worktree(base)?
.into_iter()
.map(|f| {
let (added, removed) = stat(&f.path);
AgentChange { path: IpcPath(root.join(&f.path)), rel_path: f.path, kind: f.kind, added, removed }
})
.collect();
for s in repo.status()? {
if s.unstaged == Some(ChangeKind::Untracked) && !out.iter().any(|c| c.rel_path == s.rel_path) {
let added = std::fs::read_to_string(&*s.path).map_or(0, |t| t.lines().count() as u32);
out.push(AgentChange { path: s.path, rel_path: s.rel_path, kind: ChangeKind::Added, added, removed: 0 });
}
}
out.sort_by(|a, b| a.rel_path.cmp(&b.rel_path));
Ok(out)
}
pub(crate) fn file_at(cwd: &Path, path: &Path, rev: &str) -> IdeResult<Option<String>> {
repo_at(cwd)?.file_at(path, Some(rev))
}
/// A short ASCII branch-safe name from the task title.
pub(crate) fn slugify(title: &str) -> String {
let mut out = String::new();
for c in title.chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
} else if !out.is_empty() && !out.ends_with('-') {
out.push('-');
}
if out.len() >= 32 {
break;
}
}
let out = out.trim_matches('-');
if out.is_empty() { "task".to_owned() } else { out.to_owned() }
}
fn unique_slug(base: &str, taken: impl Fn(&str) -> bool) -> String {
if !taken(base) {
return base.to_owned();
}
(2..).map(|n| format!("{base}-{n}")).find(|s| !taken(s)).expect("unbounded")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slugs_are_branch_safe() {
assert_eq!(slugify("Fix lexer UTF-16 columns!"), "fix-lexer-utf-16-columns");
assert_eq!(slugify("修复 lexer 越界"), "lexer");
assert_eq!(slugify("修复越界"), "task");
assert_eq!(slugify(&"a".repeat(50)).len(), 32);
assert_eq!(unique_slug("x", |s| s == "x" || s == "x-2"), "x-3");
}
fn git(dir: &Path, args: &[&str]) {
let ok = std::process::Command::new("git")
.args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"])
.args(args)
.current_dir(dir)
.status()
.unwrap()
.success();
assert!(ok, "git {args:?}");
}
#[test]
fn worktree_changes_are_listed_against_the_base() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir(&repo).unwrap();
git(&repo, &["init", "-q", "-b", "main"]);
std::fs::write(repo.join("a.txt"), "one\ntwo\n").unwrap();
git(&repo, &["add", "."]);
git(&repo, &["commit", "-q", "-m", "init"]);
let created = create(&repo, &tmp.path().join("wt"), "Edit A", None).unwrap();
assert_eq!(created.branch, "agent/edit-a");
assert_eq!(created.base, "main");
std::fs::write(created.path.join("a.txt"), "one\nTWO\nthree\n").unwrap();
std::fs::write(created.path.join("new.txt"), "x\ny\n").unwrap();
let got = changes(&created.path, &created.base_commit).unwrap();
let summary: Vec<_> = got.iter().map(|c| (c.rel_path.as_str(), c.kind, c.added, c.removed)).collect();
assert_eq!(summary, [("a.txt", ChangeKind::Modified, 2, 1), ("new.txt", ChangeKind::Added, 2, 0)]);
let base_text = file_at(&created.path, &created.path.join("a.txt"), &created.base_commit).unwrap();
assert_eq!(base_text.as_deref(), Some("one\ntwo\n"));
// A second task with the same title gets its own worktree.
assert_eq!(create(&repo, &tmp.path().join("wt"), "Edit A", None).unwrap().branch, "agent/edit-a-2");
remove(&repo, &created.path).unwrap();
assert!(!created.path.exists());
}
}
+17
Переглянути файл
@@ -0,0 +1,17 @@
[package]
name = "ide-analysis"
description = "Project-wide code analysis over syntax trees: duplicate detection (Locate Duplicates) and structural search & replace"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
ide-core.workspace = true
ide-language.workspace = true
serde.workspace = true
specta.workspace = true
tree-sitter.workspace = true
rayon.workspace = true
tracing.workspace = true
# Pinned: 0.40.2+ moves to tree-sitter 0.26, the workspace is on 0.25.
ast-grep-core = "=0.40.1"
+453
Переглянути файл
@@ -0,0 +1,453 @@
//! Locate Duplicates: token-based clone detection (CPD-style).
//!
//! 1. Every file is tokenised with its tree-sitter grammar: the leaves of the
//! tree, comments dropped, literals kept as one token each. Identifiers and
//! literals can be anonymised so renamed copies still match.
//! 2. A rolling hash over each window of `min_tokens` tokens indexes every
//! position; windows sharing a hash are verified token by token.
//! 3. Each verified pair that cannot be extended backwards is extended
//! forwards to its maximal length; equal token runs of equal length form one
//! group of fragments.
use crate::lines::Lines;
use crate::{Loader, SourceFile};
use ide_core::{CancellationToken, IpcPath, LanguageId, Range};
use ide_language::Syntax;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::HashMap;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::time::Instant;
/// Fragments of one group whose text is sent with the report (each capped).
const MAX_FRAGMENT_TEXT: usize = 32 * 1024;
/// Window positions compared per hash bucket (pathologically repetitive code).
const MAX_BUCKET: usize = 64;
/// Groups reported at most.
const MAX_GROUPS: usize = 500;
/// Base of the polynomial rolling hash.
const BASE: u64 = 0x100_0000_01b3;
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DuplicateOptions {
/// Shortest duplicate reported, in tokens.
pub min_tokens: u32,
/// Treat all identifiers as equal (finds copies with renamed variables).
pub anonymize_identifiers: bool,
/// Treat all literals as equal.
pub anonymize_literals: bool,
}
impl Default for DuplicateOptions {
fn default() -> Self {
Self { min_tokens: 60, anonymize_identifiers: true, anonymize_literals: true }
}
}
/// One occurrence of a duplicated code fragment.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct Fragment {
pub path: IpcPath,
pub rel_path: String,
pub range: Range,
/// The fragment's source, from the start of its first line (capped).
pub text: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DuplicateGroup {
pub tokens: u32,
/// Lines of the first fragment.
pub lines: u32,
pub language: LanguageId,
pub fragments: Vec<Fragment>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DuplicatesReport {
pub groups: Vec<DuplicateGroup>,
/// Files tokenised (files without a grammar are skipped).
pub files: u32,
pub tokens: u32,
/// More groups were found than reported.
pub truncated: bool,
pub cancelled: bool,
pub elapsed_ms: u32,
}
#[derive(Clone, Copy, Debug)]
struct Token {
hash: u64,
start: u32,
end: u32,
}
struct Tokenized {
file: SourceFile,
language: LanguageId,
text: String,
tokens: Vec<Token>,
/// `prefix[i]` = rolling hash of `tokens[..i]`.
prefix: Vec<u64>,
}
impl Tokenized {
fn window(&self, at: usize, len: usize, pow: &Pow) -> u64 {
self.prefix[at + len].wrapping_sub(self.prefix[at].wrapping_mul(pow.get(len)))
}
}
/// Powers of [`BASE`], computed lazily up to the longest run needed.
struct Pow(Vec<u64>);
impl Pow {
fn new(max: usize) -> Self {
let mut v = Vec::with_capacity(max + 1);
let mut p = 1u64;
for _ in 0..=max {
v.push(p);
p = p.wrapping_mul(BASE);
}
Self(v)
}
fn get(&self, n: usize) -> u64 {
self.0[n]
}
}
/// Finds duplicated code among `files`. Files of different languages never
/// match each other.
pub fn find_duplicates(
syntax: &Syntax,
files: &[SourceFile],
load: Loader<'_>,
options: &DuplicateOptions,
cancel: &CancellationToken,
) -> DuplicatesReport {
let started = Instant::now();
let k = options.min_tokens.max(5) as usize;
let docs: Vec<Tokenized> = files
.par_iter()
.filter_map(|file| {
if cancel.is_cancelled() {
return None;
}
let (language, text) = load(&file.path)?;
let tokens = tokenize(syntax, &language, &text, options, cancel)?;
(tokens.len() >= k).then(|| {
let mut prefix = Vec::with_capacity(tokens.len() + 1);
let mut h = 0u64;
prefix.push(h);
for t in &tokens {
h = h.wrapping_mul(BASE).wrapping_add(t.hash);
prefix.push(h);
}
Tokenized { file: file.clone(), language, text, tokens, prefix }
})
})
.collect();
let mut report = DuplicatesReport {
files: docs.len() as u32,
tokens: docs.iter().map(|d| d.tokens.len() as u32).sum(),
..Default::default()
};
if cancel.is_cancelled() {
report.cancelled = true;
return report;
}
let pow = Pow::new(docs.iter().map(|d| d.tokens.len()).max().unwrap_or(0));
// Window hash -> positions, per language (mixed in so languages never collide).
let mut index: HashMap<u64, Vec<(u32, u32)>> = HashMap::new();
for (fi, d) in docs.iter().enumerate() {
let salt = hash_str(d.language.as_str());
for at in 0..=d.tokens.len() - k {
index.entry(d.window(at, k, &pow) ^ salt).or_default().push((fi as u32, at as u32));
}
if cancel.is_cancelled() {
report.cancelled = true;
return report;
}
}
// (length, content hash) -> occurrences.
let mut groups: HashMap<(usize, u64), Vec<(u32, u32)>> = HashMap::new();
for bucket in index.values().filter(|b| b.len() > 1) {
if cancel.is_cancelled() {
report.cancelled = true;
return report;
}
let bucket = &bucket[..bucket.len().min(MAX_BUCKET)];
for (i, &(fa, pa)) in bucket.iter().enumerate() {
for &(fb, pb) in &bucket[i + 1..] {
let (a, b) = (&docs[fa as usize], &docs[fb as usize]);
let (pa, pb) = (pa as usize, pb as usize);
if a.language != b.language || !same(a, pa, b, pb, k) {
continue;
}
// Not maximal: the pair also matches one token earlier.
if pa > 0 && pb > 0 && a.tokens[pa - 1].hash == b.tokens[pb - 1].hash {
continue;
}
let mut len = k;
while pa + len < a.tokens.len()
&& pb + len < b.tokens.len()
&& a.tokens[pa + len].hash == b.tokens[pb + len].hash
{
len += 1;
}
if fa == fb {
// Overlapping copies within one file (repetitive code): keep the disjoint part.
let (lo, hi) = (pa.min(pb), pa.max(pb));
len = len.min(hi - lo);
if len < k {
continue;
}
}
let key = (len, a.window(pa, len, &pow));
let occ = groups.entry(key).or_default();
for o in [(fa, pa as u32), (fb, pb as u32)] {
if !occ.contains(&o) {
occ.push(o);
}
}
}
}
}
// Largest duplication (length × copies) first; ties in source order, so runs are stable.
let mut ranked: Vec<(usize, Vec<(u32, u32)>)> = groups
.into_iter()
.map(|((len, _), mut occ)| {
occ.sort_unstable();
(len, occ)
})
.collect();
ranked.sort_by(|a, b| (b.0 * b.1.len()).cmp(&(a.0 * a.1.len())).then_with(|| a.1.cmp(&b.1)));
report.truncated = ranked.len() > MAX_GROUPS;
report.groups = ranked
.into_iter()
.take(MAX_GROUPS)
.map(|(len, occ)| {
let fragments: Vec<Fragment> =
occ.iter().map(|&(f, p)| fragment(&docs[f as usize], p as usize, len)).collect();
let lines = fragments.first().map_or(0, |f| f.range.end.line - f.range.start.line + 1);
DuplicateGroup { tokens: len as u32, lines, language: docs[occ[0].0 as usize].language.clone(), fragments }
})
.collect();
report.elapsed_ms = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
report
}
fn same(a: &Tokenized, pa: usize, b: &Tokenized, pb: usize, k: usize) -> bool {
a.tokens[pa..pa + k].iter().zip(&b.tokens[pb..pb + k]).all(|(x, y)| x.hash == y.hash)
}
fn fragment(d: &Tokenized, at: usize, len: usize) -> Fragment {
let start = d.tokens[at].start as usize;
let end = d.tokens[at + len - 1].end as usize;
let lines = Lines::new(&d.text);
let from = d.text[..start].rfind('\n').map_or(0, |i| i + 1);
let mut to = end.min(from + MAX_FRAGMENT_TEXT);
while !d.text.is_char_boundary(to) {
to -= 1;
}
Fragment {
path: IpcPath(d.file.path.clone()),
rel_path: d.file.rel_path.clone(),
range: lines.range(start, end),
text: d.text[from..to].to_owned(),
}
}
fn hash_str(s: &str) -> u64 {
let mut h = DefaultHasher::new();
s.hash(&mut h);
h.finish()
}
fn is_comment(kind: &str) -> bool {
kind.contains("comment")
}
/// Literal nodes are one token each (their inner structure — quotes, escapes,
/// string fragments — does not matter for duplication).
fn is_literal(kind: &str) -> bool {
!kind.contains("content")
&& ["string", "char", "number", "integer", "float", "literal", "decimal", "rune"]
.iter()
.any(|w| kind.contains(w))
}
fn is_identifier(kind: &str) -> bool {
kind.contains("identifier") || matches!(kind, "name" | "constant" | "variable_name" | "word")
}
/// The token stream of `text`, or `None` without a grammar.
fn tokenize(
syntax: &Syntax,
language: &LanguageId,
text: &str,
options: &DuplicateOptions,
cancel: &CancellationToken,
) -> Option<Vec<Token>> {
let anon_id = hash_str("\0id");
let anon_lit = hash_str("\0lit");
syntax
.with_parsed(language, text, &|| cancel.is_cancelled(), |parsed| {
let mut out = Vec::new();
let mut cursor = parsed.tree.walk();
'walk: loop {
let node = cursor.node();
let kind = node.kind();
let literal = node.is_named() && is_literal(kind);
if is_comment(kind) {
// Skipped with its children.
} else if literal || node.child_count() == 0 {
let (start, end) = (node.start_byte(), node.end_byte());
if end > start {
let hash = if literal && options.anonymize_literals {
anon_lit
} else if !literal && options.anonymize_identifiers && is_identifier(kind) {
anon_id
} else {
let mut h = DefaultHasher::new();
kind.hash(&mut h);
text[start..end].hash(&mut h);
h.finish()
};
out.push(Token { hash, start: start as u32, end: end as u32 });
}
} else if cursor.goto_first_child() {
continue;
}
while !cursor.goto_next_sibling() {
if !cursor.goto_parent() {
break 'walk;
}
}
}
out
})
.ok()
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::{Path, PathBuf};
const A: &str = r#"
fn total(items: &[Item]) -> u64 {
let mut sum = 0;
for item in items {
if item.enabled && item.price > 10 {
sum += item.price * item.count;
} else {
sum += 1;
}
}
println!("total {}", sum);
sum
}
"#;
/// The same body with other names and literals.
const B: &str = r#"
// a copy
fn grand_total(entries: &[Entry]) -> u64 {
let mut acc = 0;
for e in entries {
if e.enabled && e.price > 99 {
acc += e.price * e.count;
} else {
acc += 2;
}
}
println!("sum {}", acc);
acc
}
"#;
const C: &str = "fn other() { let x = vec![1, 2, 3]; x.len() }\n";
fn run(files: &[(&str, &str)], options: DuplicateOptions) -> DuplicatesReport {
let syntax = Syntax::new();
let sources: Vec<SourceFile> =
files.iter().map(|(name, _)| SourceFile { path: PathBuf::from(name), rel_path: (*name).into() }).collect();
let map: HashMap<PathBuf, String> = files.iter().map(|(n, t)| (PathBuf::from(n), (*t).to_owned())).collect();
let load = |p: &Path| map.get(p).map(|t| (LanguageId::new("rust"), t.clone()));
find_duplicates(&syntax, &sources, &load, &options, &CancellationToken::new())
}
#[test]
fn finds_renamed_copies_when_anonymised() {
let r =
run(&[("a.rs", A), ("b.rs", B), ("c.rs", C)], DuplicateOptions { min_tokens: 20, ..Default::default() });
assert_eq!(r.files, 3);
assert_eq!(r.groups.len(), 1, "{:#?}", r.groups);
let g = &r.groups[0];
assert_eq!(g.fragments.len(), 2);
assert_eq!(g.fragments[0].rel_path, "a.rs");
assert_eq!(g.fragments[1].rel_path, "b.rs");
// The whole function matches, from `fn` to the closing brace.
assert_eq!(g.fragments[0].range.start.line, 1);
assert_eq!(g.fragments[0].range.end.line, 12);
assert_eq!(g.fragments[1].range.start.line, 2, "the comment is not part of the clone");
assert!(g.fragments[1].text.starts_with("fn grand_total"));
assert_eq!(g.lines, 12);
}
#[test]
fn exact_mode_needs_equal_names() {
let r = run(
&[("a.rs", A), ("b.rs", B)],
DuplicateOptions { min_tokens: 20, anonymize_identifiers: false, anonymize_literals: false },
);
assert!(r.groups.is_empty(), "{:#?}", r.groups);
let r = run(
&[("a.rs", A), ("a2.rs", A)],
DuplicateOptions { min_tokens: 20, anonymize_identifiers: false, anonymize_literals: false },
);
assert_eq!(r.groups.len(), 1);
assert_eq!(r.groups[0].fragments.len(), 2);
}
#[test]
fn three_copies_form_one_group_and_min_size_applies() {
let r =
run(&[("a.rs", A), ("b.rs", B), ("c.rs", A)], DuplicateOptions { min_tokens: 20, ..Default::default() });
assert_eq!(r.groups.len(), 1);
assert_eq!(r.groups[0].fragments.len(), 3);
let r = run(&[("a.rs", A), ("b.rs", B)], DuplicateOptions { min_tokens: 500, ..Default::default() });
assert!(r.groups.is_empty());
}
#[test]
fn repeats_within_one_file_do_not_overlap() {
let twice = format!("{A}\n{A}");
let r = run(&[("a.rs", &twice)], DuplicateOptions { min_tokens: 20, ..Default::default() });
assert_eq!(r.groups.len(), 1);
let f = &r.groups[0].fragments;
assert_eq!(f.len(), 2);
assert!(f[0].range.end.line < f[1].range.start.line);
}
#[test]
fn cancelled_runs_report_it() {
let syntax = Syntax::new();
let cancel = CancellationToken::new();
cancel.cancel();
let files = vec![SourceFile { path: PathBuf::from("a.rs"), rel_path: "a.rs".into() }];
let load = |_: &Path| Some((LanguageId::new("rust"), A.to_owned()));
let r = find_duplicates(&syntax, &files, &load, &DuplicateOptions::default(), &cancel);
assert!(r.cancelled);
assert!(r.groups.is_empty());
}
}
+36
Переглянути файл
@@ -0,0 +1,36 @@
//! Project-wide analyses over syntax trees.
//!
//! * [`duplicates`]: Locate Duplicates — token-based clone detection in the
//! style of PMD's CPD (tree-sitter leaves as tokens, optional anonymisation
//! of identifiers and literals, rolling hashes over token windows).
//! * [`structural`]: Search / Replace Structurally — code patterns with
//! `$VAR$` placeholders matched against syntax trees (ast-grep's matcher on
//! the bundled grammars).
//!
//! Both work on whole files supplied by the caller (unsaved buffers first),
//! run on rayon and honour a [`CancellationToken`]: call them from a blocking
//! thread. Positions are 0-based lines and UTF-16 columns.
pub mod duplicates;
mod lines;
pub mod structural;
pub use duplicates::{DuplicateGroup, DuplicateOptions, DuplicatesReport, Fragment, find_duplicates};
pub use structural::{
StructuralFile, StructuralMatch, StructuralQuery, StructuralSummary, normalize_placeholders, search_structurally,
validate_structural,
};
use ide_core::LanguageId;
use std::path::{Path, PathBuf};
/// A file to analyse: its project-relative path and a way to read it.
#[derive(Clone, Debug)]
pub struct SourceFile {
pub path: PathBuf,
pub rel_path: String,
}
/// Reads a file for analysis: its language and current text (the unsaved
/// buffer when open). `None` skips the file (binary, unreadable, too large).
pub type Loader<'a> = &'a (dyn Fn(&Path) -> Option<(LanguageId, String)> + Sync);
+57
Переглянути файл
@@ -0,0 +1,57 @@
//! Byte offset -> 0-based line / UTF-16 column for one text.
use ide_core::{Pos, Range};
pub(crate) struct Lines<'a> {
text: &'a str,
starts: Vec<usize>,
}
impl<'a> Lines<'a> {
pub(crate) fn new(text: &'a str) -> Self {
let mut starts = vec![0];
starts.extend(text.bytes().enumerate().filter(|&(_, b)| b == b'\n').map(|(i, _)| i + 1));
Self { text, starts }
}
pub(crate) fn line_of(&self, byte: usize) -> usize {
self.starts.partition_point(|&s| s <= byte.min(self.text.len())) - 1
}
pub(crate) fn pos(&self, byte: usize) -> Pos {
let mut byte = byte.min(self.text.len());
while !self.text.is_char_boundary(byte) {
byte -= 1;
}
let line = self.line_of(byte);
let col: usize = self.text[self.starts[line]..byte].chars().map(char::len_utf16).sum();
Pos::new(line as u32, col as u32)
}
pub(crate) fn range(&self, start: usize, end: usize) -> Range {
Range::new(self.pos(start), self.pos(end))
}
/// Text of the line containing `byte`, without the line break.
pub(crate) fn line_text(&self, byte: usize) -> &'a str {
let line = self.line_of(byte);
let start = self.starts[line];
let end = self.starts.get(line + 1).map_or(self.text.len(), |&s| s - 1);
self.text[start..end].trim_end_matches('\r')
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn utf16_columns_and_lines() {
let l = Lines::new("ab\nx😀y\r\nz");
assert_eq!(l.pos(0), Pos::new(0, 0));
assert_eq!(l.pos(3), Pos::new(1, 0));
assert_eq!(l.pos(8), Pos::new(1, 3));
assert_eq!(l.line_text(4), "x😀y");
assert_eq!(l.pos(100), Pos::new(2, 1));
}
}
+354
Переглянути файл
@@ -0,0 +1,354 @@
//! Search / Replace Structurally: a code pattern with placeholders, matched
//! against syntax trees rather than text, so formatting, comments and
//! nesting do not matter.
//!
//! Placeholders: IDEA's `$name$` (one node), ast-grep's `$NAME` (one node),
//! `$$$NAME` (any number of nodes, e.g. arguments) and `$_` (one node, not
//! captured). A placeholder used twice must match equal code. The replacement
//! template refers to the same placeholders.
use crate::lines::Lines;
use crate::{Loader, SourceFile};
use ast_grep_core::matcher::PatternBuilder;
use ast_grep_core::tree_sitter::{LanguageExt, StrDoc, TSLanguage};
use ast_grep_core::{Language, Pattern, PatternError};
use ide_core::{CancellationToken, IdeError, IdeResult, IpcPath, LanguageId, Range};
use ide_language::Syntax;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::borrow::Cow;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering::Relaxed};
use std::time::Instant;
/// Matches reported at most (0 in the query).
const DEFAULT_MAX_MATCHES: u32 = 5_000;
/// Matched text sent per match (the editor shows the rest).
const MAX_MATCH_TEXT: usize = 4 * 1024;
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct StructuralQuery {
pub pattern: String,
/// Only files of this language are searched.
pub language: LanguageId,
/// Replacement template; matches then carry their replacement text.
pub replacement: Option<String>,
/// Stop after this many matches (0 = 5 000).
pub max_matches: u32,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct StructuralMatch {
pub range: Range,
/// The matched code (capped).
pub text: String,
/// The first line of the match, for result rows.
pub line_text: String,
pub replacement: Option<String>,
}
/// All matches in one file; streamed as files complete.
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct StructuralFile {
pub path: IpcPath,
pub rel_path: String,
pub matches: Vec<StructuralMatch>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct StructuralSummary {
pub files: u32,
pub matches: u32,
pub limit_hit: bool,
pub cancelled: bool,
pub elapsed_ms: u32,
}
/// A bundled grammar as an ast-grep language.
#[derive(Clone)]
struct Lang {
grammar: &'static str,
ts: TSLanguage,
}
impl Lang {
fn new(syntax: &Syntax, language: &LanguageId) -> IdeResult<Self> {
let (grammar, ts) = syntax
.ts_language(language)
.ok_or_else(|| IdeError::invalid(format!("no syntax tree support for {language}")))?;
Ok(Self { grammar, ts })
}
}
impl Language for Lang {
fn kind_to_id(&self, kind: &str) -> u16 {
self.ts.id_for_node_kind(kind, true)
}
fn field_to_id(&self, field: &str) -> Option<u16> {
self.ts.field_id_for_name(field).map(|f| f.get())
}
/// Placeholders are spelled with a character the grammar accepts in
/// identifiers, as ast-grep does for its own languages.
fn expando_char(&self) -> char {
match self.grammar {
"javascript" | "typescript" | "tsx" | "java" | "bash" | "json" | "lua" | "scala" | "yaml" => '$',
"c" | "cpp" => '\u{10000}',
"css" | "nix" => '_',
_ => 'µ',
}
}
fn pre_process_pattern<'q>(&self, query: &'q str) -> Cow<'q, str> {
let expando = self.expando_char();
if expando == '$' {
return Cow::Borrowed(query);
}
// `$A`, `$$$A`, `$_` and `$$$` become expando-spelled; other `$` stay.
let mut out = String::with_capacity(query.len());
let mut dollars = 0;
for c in query.chars() {
if c == '$' {
dollars += 1;
continue;
}
let sigil = if c.is_ascii_uppercase() || c == '_' || dollars == 3 { expando } else { '$' };
out.extend(std::iter::repeat_n(sigil, dollars));
dollars = 0;
out.push(c);
}
out.extend(std::iter::repeat_n(if dollars == 3 { expando } else { '$' }, dollars));
Cow::Owned(out)
}
fn build_pattern(&self, builder: &PatternBuilder) -> Result<Pattern, PatternError> {
builder.build(|src| StrDoc::try_new(src, self.clone()))
}
}
impl LanguageExt for Lang {
fn get_ts_language(&self) -> TSLanguage {
self.ts.clone()
}
}
/// Rewrites IDEA-style `$name$` placeholders to ast-grep's `$NAME`.
pub fn normalize_placeholders(src: &str) -> String {
let chars: Vec<char> = src.chars().collect();
let mut out = String::with_capacity(src.len());
let mut i = 0;
while i < chars.len() {
if chars[i] == '$' && chars.get(i + 1).is_some_and(|c| c.is_ascii_alphabetic() || *c == '_') {
let mut j = i + 1;
while j < chars.len() && (chars[j].is_ascii_alphanumeric() || chars[j] == '_') {
j += 1;
}
if chars.get(j) == Some(&'$') {
out.push('$');
out.extend(chars[i + 1..j].iter().map(char::to_ascii_uppercase));
i = j + 1;
continue;
}
}
out.push(chars[i]);
i += 1;
}
out
}
fn compile(syntax: &Syntax, query: &StructuralQuery) -> IdeResult<(Lang, Pattern)> {
let lang = Lang::new(syntax, &query.language)?;
let source = normalize_placeholders(query.pattern.trim());
if source.is_empty() {
return Err(IdeError::invalid("the search template is empty"));
}
let pattern = Pattern::try_new(&source, lang.clone()).map_err(|e| IdeError::invalid(pattern_error(&e)))?;
Ok((lang, pattern))
}
fn pattern_error(e: &PatternError) -> String {
match e {
PatternError::Parse(_) => "the search template is not valid code in this language".into(),
PatternError::NoContent(_) => "the search template contains no code".into(),
PatternError::MultipleNode(_) => {
"the search template must be a single statement or expression (use $$$ for sequences)".into()
}
other => other.to_string(),
}
}
/// Checks the template while the user types.
pub fn validate_structural(syntax: &Syntax, query: &StructuralQuery) -> IdeResult<()> {
compile(syntax, query).map(|_| ())
}
/// Matches of `query` in one text.
fn search_text(lang: &Lang, pattern: &Pattern, replacement: Option<&str>, text: &str) -> Vec<StructuralMatch> {
let root = lang.ast_grep(text);
let lines = Lines::new(text);
root.root()
.find_all(pattern)
.map(|m| {
let range = m.range();
let mut cap = range.end.min(range.start + MAX_MATCH_TEXT);
while !text.is_char_boundary(cap) {
cap -= 1;
}
let replacement = replacement.map(|r| {
let edit = m.replace_by(r);
String::from_utf8_lossy(&edit.inserted_text).into_owned()
});
StructuralMatch {
range: lines.range(range.start, range.end),
text: text[range.start..cap].to_owned(),
line_text: lines.line_text(range.start).to_owned(),
replacement,
}
})
.collect()
}
/// Searches `files` (those of the query's language) in parallel, calling
/// `on_file` for each file with matches.
pub fn search_structurally(
syntax: &Syntax,
files: &[SourceFile],
load: Loader<'_>,
query: &StructuralQuery,
cancel: &CancellationToken,
on_file: &(dyn Fn(StructuralFile) + Sync),
) -> IdeResult<StructuralSummary> {
let started = Instant::now();
let (lang, pattern) = compile(syntax, query)?;
let replacement = query.replacement.as_deref().map(normalize_placeholders);
let max = if query.max_matches == 0 { DEFAULT_MAX_MATCHES } else { query.max_matches };
let found = AtomicU32::new(0);
let file_count = AtomicU32::new(0);
let limit_hit = AtomicBool::new(false);
files.par_iter().for_each(|file| {
if cancel.is_cancelled() || limit_hit.load(Relaxed) {
return;
}
let Some((language, text)) = load(&file.path) else { return };
if syntax.grammar_name(&language) != Some(lang.grammar) {
return;
}
let mut matches = search_text(&lang, &pattern, replacement.as_deref(), &text);
if matches.is_empty() {
return;
}
let before = found.fetch_add(matches.len() as u32, Relaxed);
if before >= max {
limit_hit.store(true, Relaxed);
return;
}
if before + matches.len() as u32 > max {
matches.truncate((max - before) as usize);
limit_hit.store(true, Relaxed);
}
file_count.fetch_add(1, Relaxed);
on_file(StructuralFile { path: IpcPath(file.path.clone()), rel_path: file.rel_path.clone(), matches });
});
Ok(StructuralSummary {
files: file_count.load(Relaxed),
matches: found.load(Relaxed).min(max),
limit_hit: limit_hit.load(Relaxed),
cancelled: cancel.is_cancelled(),
elapsed_ms: u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX),
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
fn query(lang: &str, pattern: &str, replacement: Option<&str>) -> StructuralQuery {
StructuralQuery {
pattern: pattern.into(),
language: LanguageId::new(lang),
replacement: replacement.map(Into::into),
max_matches: 0,
}
}
fn matches(lang: &str, text: &str, pattern: &str, replacement: Option<&str>) -> Vec<StructuralMatch> {
let syntax = Syntax::new();
let (l, p) = compile(&syntax, &query(lang, pattern, replacement)).unwrap();
let replacement = replacement.map(normalize_placeholders);
search_text(&l, &p, replacement.as_deref(), text)
}
#[test]
fn idea_placeholders_become_metavariables() {
assert_eq!(normalize_placeholders("$a$.equals($b$)"), "$A.equals($B)");
assert_eq!(normalize_placeholders("foo($$$ARGS)"), "foo($$$ARGS)");
assert_eq!(normalize_placeholders("let $x = 1; \"$5\""), "let $x = 1; \"$5\"");
assert_eq!(normalize_placeholders("$Expr$ + $_"), "$EXPR + $_");
}
#[test]
fn matches_ignore_formatting_and_respect_repeated_placeholders() {
let ts = "if (a == a) {}\nif (a ==\n b) {}\nconst x = f(1, 2, 3);\n";
let m = matches("typescript", ts, "$x$ == $x$", None);
assert_eq!(m.len(), 1);
assert_eq!(m[0].text, "a == a");
assert_eq!(m[0].range.start.line, 0);
let m = matches("typescript", ts, "$a$ == $b$", None);
assert_eq!(m.len(), 2);
assert_eq!(m[1].range.end.line, 2);
assert_eq!(m[1].line_text, "if (a ==");
let m = matches("typescript", ts, "f($$$ARGS)", None);
assert_eq!(m.len(), 1);
}
#[test]
fn languages_without_dollar_identifiers_work() {
let rust = "fn main() {\n let v = x.unwrap();\n let w = y.unwrap();\n}\n";
let m = matches("rust", rust, "$e$.unwrap()", Some("$e$.expect(\"todo\")"));
assert_eq!(m.len(), 2);
assert_eq!(m[0].replacement.as_deref(), Some("x.expect(\"todo\")"));
let py = "print(a)\nprint(b, c)\n";
assert_eq!(matches("python", py, "print($A)", None).len(), 1);
}
#[test]
fn invalid_templates_and_languages_are_reported() {
let syntax = Syntax::new();
assert!(validate_structural(&syntax, &query("typescript", "", None)).is_err());
assert!(validate_structural(&syntax, &query("no-such-language", "a", None)).is_err());
assert!(validate_structural(&syntax, &query("typescript", "foo($a$)", None)).is_ok());
}
#[test]
fn searches_files_of_the_language_with_a_limit() {
let syntax = Syntax::new();
let files: Vec<SourceFile> = ["a.ts", "b.ts", "c.py"]
.iter()
.map(|n| SourceFile { path: PathBuf::from(n), rel_path: (*n).into() })
.collect();
let load = |p: &Path| {
Some(match p.to_str().unwrap() {
"c.py" => (LanguageId::new("python"), "log(1)\n".to_owned()),
_ => (LanguageId::new("typescript"), "log(1);\nlog(2);\n".to_owned()),
})
};
let out = Mutex::new(Vec::new());
let q = StructuralQuery { max_matches: 3, ..query("typescript", "log($v$)", None) };
let s = search_structurally(&syntax, &files, &load, &q, &CancellationToken::new(), &|f| {
out.lock().unwrap().push(f)
})
.unwrap();
assert_eq!(s.matches, 3);
assert!(s.limit_hit);
let out = out.into_inner().unwrap();
assert!(out.iter().all(|f| f.rel_path.ends_with(".ts")));
assert_eq!(out.iter().map(|f| f.matches.len()).sum::<usize>(), 3);
}
}
+21
Переглянути файл
@@ -0,0 +1,21 @@
[package]
name = "ide-build"
description = "Build tool models for the Maven and Gradle tool windows: POM parsing, Gradle task lists, background sync"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
dunce.workspace = true
ide-core.workspace = true
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
tracing.workspace = true
dirs.workspace = true
[target.'cfg(unix)'.dependencies]
libc = "0.2"
[dev-dependencies]
tempfile.workspace = true
+25
Переглянути файл
@@ -0,0 +1,25 @@
//! Prints the build model of a folder, optionally after a sync:
//! `cargo run -p ide-build --example model -- <dir> [cache-dir] [maven|gradle]`.
fn main() {
let mut args = std::env::args().skip(1);
let root = std::path::PathBuf::from(args.next().unwrap_or_else(|| ".".into()));
let cache =
args.next().map(std::path::PathBuf::from).unwrap_or_else(|| std::env::temp_dir().join("ide-build-model"));
let cancel = ide_core::CancellationToken::new();
let progress = |p: ide_build::SyncProgress| eprintln!(" {} {:?}", p.message, p.fraction);
let options = ide_build::SyncOptions::default();
let started = std::time::Instant::now();
let synced = match args.next().as_deref() {
Some("maven") => Some(ide_build::sync_maven(&root, &cache, &options, &cancel, &progress)),
Some("gradle") => Some(ide_build::sync_gradle(&root, &cache, &options, &cancel, &progress)),
_ => None,
};
if let Some(r) = synced {
eprintln!("synced in {:?}: {r:?}", started.elapsed());
}
let started = std::time::Instant::now();
let model = ide_build::load(&root, &cache, None);
println!("{}", serde_json::to_string_pretty(&model).unwrap());
eprintln!("loaded in {:?}", started.elapsed());
}
+843
Переглянути файл
@@ -0,0 +1,843 @@
//! Build › Build Project / Rebuild / Build Module / Build Artifacts: picks the
//! build system of the project (or of the file's module), runs it in the
//! background with cancellation and parses the compiler output into messages
//! for the Build tool window, the Problems view and the editor.
mod parse;
use crate::process::{self, Stream};
use crate::{BuildModel, maven};
use ide_core::{CancellationToken, Diagnostic, IdeError, IdeResult, Pos, Range, Severity};
use parse::{ColUnit, Format, OutputParser, RawMessage};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::Instant;
/// Diagnostics source of build messages in the Problems view and the editor.
pub const DIAGNOSTICS_SOURCE: &str = "build";
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum BuildKind {
/// Incremental compile of sources and tests.
Build,
/// Clean, then build.
Rebuild,
/// Package the outputs (`package`, `assemble`, `cargo build --release`, `npm run build`).
Artifacts,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum BuildSystem {
Maven,
Gradle,
Cargo,
Go,
#[serde(rename = "typescript")]
TypeScript,
}
/// One command of a build.
#[derive(Clone, Debug, PartialEq)]
pub struct BuildStep {
pub program: String,
pub args: Vec<String>,
pub cwd: PathBuf,
pub env: BTreeMap<String, String>,
format: Format,
}
impl BuildStep {
fn new(program: impl Into<String>, args: &[&str], cwd: &Path, format: Format) -> Self {
BuildStep {
program: program.into(),
args: args.iter().map(|a| (*a).to_owned()).collect(),
cwd: cwd.to_path_buf(),
env: BTreeMap::new(),
format,
}
}
/// The command line as echoed in the console.
pub fn display(&self) -> String {
let program = Path::new(&self.program).file_name().map_or(self.program.clone(), |n| n.to_string_lossy().into());
std::iter::once(program)
.chain(self.args.iter().map(|a| if a.contains(' ') { format!("\"{a}\"") } else { a.clone() }))
.collect::<Vec<_>>()
.join(" ")
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct BuildPlan {
pub system: BuildSystem,
/// Tree root title: `demo` / `Build module 'core'`.
pub title: String,
pub steps: Vec<BuildStep>,
}
/// A compiler message in IDE coordinates (0-based line, UTF-16 column).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct BuildMessage {
pub severity: Severity,
pub text: String,
/// Absolute path; `None` for messages about the build itself.
pub path: Option<ide_core::IpcPath>,
pub range: Option<Range>,
pub code: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum BuildEvent {
/// The build starts: its tree root.
Started {
system: BuildSystem,
title: String,
},
/// A command starts (echoed in the console).
Step {
command: String,
cwd: ide_core::IpcPath,
},
/// Console output (one or more lines, each ending in `\n`).
Output {
text: String,
stderr: bool,
},
Message {
message: BuildMessage,
},
/// Status bar progress; `fraction` 0..1 or `None` when indeterminate.
Progress {
message: String,
fraction: Option<f32>,
},
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct BuildResult {
pub system: BuildSystem,
pub title: String,
pub success: bool,
pub errors: u32,
pub warnings: u32,
pub duration_ms: f64,
pub messages: Vec<BuildMessage>,
}
// ---------------------------------------------------------------- planning
fn has(dir: &Path, names: &[&str]) -> bool {
names.iter().any(|n| dir.join(n).is_file())
}
fn detect_dir(dir: &Path, model: &BuildModel, root: &Path) -> Option<BuildSystem> {
let rel = rel_str(root, dir);
if model.maven.as_ref().is_some_and(|m| m.projects.iter().any(|p| p.dir == rel)) || has(dir, &["pom.xml"]) {
Some(BuildSystem::Maven)
} else if has(dir, &["build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts"]) {
Some(BuildSystem::Gradle)
} else if has(dir, &["Cargo.toml"]) {
Some(BuildSystem::Cargo)
} else if has(dir, &["go.mod"]) {
Some(BuildSystem::Go)
} else if has(dir, &["tsconfig.json", "package.json"]) {
Some(BuildSystem::TypeScript)
} else {
None
}
}
/// `root/rel`, without a trailing separator for the root itself.
fn subdir(root: &Path, rel: &str) -> PathBuf {
if rel.is_empty() { root.to_path_buf() } else { root.join(rel) }
}
fn rel_str(root: &Path, dir: &Path) -> String {
dir.strip_prefix(root).map(|r| r.to_string_lossy().replace('\\', "/")).unwrap_or_default()
}
/// Directories from `file`'s folder up to `root` (inclusive).
fn ancestors_within<'a>(root: &'a Path, file: &'a Path) -> impl Iterator<Item = &'a Path> {
let start = if file.is_dir() { Some(file) } else { file.parent() };
start.into_iter().flat_map(Path::ancestors).take_while(move |d| d.starts_with(root))
}
/// The build of `root`, or of the module containing `file` (Build Module / Recompile).
pub fn plan(root: &Path, model: &BuildModel, kind: BuildKind, file: Option<&Path>) -> IdeResult<BuildPlan> {
let project_name = root.file_name().map_or_else(|| "project".to_owned(), |n| n.to_string_lossy().into_owned());
let module_dir = match file {
Some(f) => {
let dir = ancestors_within(root, f)
.find(|d| detect_dir(d, model, root).is_some())
.ok_or_else(|| IdeError::not_found(format!("{} is not in a module", f.display())))?;
Some(dir.to_path_buf())
}
None => None,
};
let dir = module_dir.clone().unwrap_or_else(|| root.to_path_buf());
let system = detect_dir(&dir, model, root)
.or_else(|| if model.gradle.is_some() { Some(BuildSystem::Gradle) } else { None })
.ok_or_else(|| IdeError::not_found("no supported build system (Maven, Gradle, Cargo, Go, TypeScript) found"))?;
let module = module_dir.as_deref().filter(|d| *d != root);
let title = match module {
Some(d) => format!(
"{}: build module '{}'",
project_name,
d.file_name().map_or_else(String::new, |n| n.to_string_lossy().into_owned())
),
None => project_name,
};
let steps = match system {
BuildSystem::Maven => maven_steps(root, model, kind, module_dir.as_deref())?,
BuildSystem::Gradle => gradle_steps(root, model, kind, module_dir.as_deref())?,
BuildSystem::Cargo => cargo_steps(&dir, kind),
// Go builds the file's package (a module holds many).
BuildSystem::Go => go_steps(file.and_then(Path::parent).unwrap_or(&dir), kind),
BuildSystem::TypeScript => ts_steps(&dir, kind),
};
Ok(BuildPlan { system, title, steps })
}
fn maven_steps(root: &Path, model: &BuildModel, kind: BuildKind, module: Option<&Path>) -> IdeResult<Vec<BuildStep>> {
let fallback;
let m = match &model.maven {
Some(m) => m,
None => {
fallback = maven::load(root);
&fallback
}
};
let goals: &[&str] = match kind {
BuildKind::Build => &["test-compile"],
BuildKind::Rebuild => &["clean", "test-compile"],
BuildKind::Artifacts => &["package", "-DskipTests"],
};
let tops: Vec<&maven::MavenProject> = m.projects.iter().filter(|p| !p.aggregated).collect();
if tops.is_empty() {
return Err(IdeError::not_found("no Maven project found"));
}
let step = |p: &maven::MavenProject, extra: &[String]| {
let mut s = BuildStep::new(&p.command, &["-B", "-Dstyle.color=never"], &subdir(root, &p.dir), Format::Maven);
s.args.extend(p.options.iter().cloned());
s.args.extend(extra.iter().cloned());
s.args.extend(goals.iter().map(|g| (*g).to_owned()));
s.env = p.env.clone();
s
};
let Some(module) = module else { return Ok(tops.iter().map(|p| step(p, &[])).collect()) };
// The reactor root containing the module builds it with what it depends on (`-pl … -am`).
let rel = rel_str(root, module);
let top = tops
.iter()
.filter(|t| t.dir.is_empty() || rel == t.dir || rel.starts_with(&format!("{}/", t.dir)))
.max_by_key(|t| t.dir.len())
.copied()
.unwrap_or(tops[0]);
let top_dir = subdir(root, &top.dir);
if module == top_dir {
return Ok(vec![step(top, &[])]);
}
let pl = module.strip_prefix(&top_dir).map(|r| r.to_string_lossy().replace('\\', "/")).unwrap_or(rel);
Ok(vec![step(top, &["-pl".into(), pl, "-am".into()])])
}
fn gradle_steps(root: &Path, model: &BuildModel, kind: BuildKind, module: Option<&Path>) -> IdeResult<Vec<BuildStep>> {
let fallback;
let g = match &model.gradle {
Some(g) => g,
None => {
fallback = crate::gradle::load(root).ok_or_else(|| IdeError::not_found("no Gradle build found"))?;
&fallback
}
};
let prefix = module
.and_then(|m| {
let rel = rel_str(root, m);
g.projects.iter().filter(|p| p.dir == rel).map(|p| p.path.clone()).next()
})
.filter(|p| p != ":")
.map(|p| format!("{p}:"))
.unwrap_or_default();
let tasks: Vec<String> = match kind {
BuildKind::Build => vec![format!("{prefix}classes"), format!("{prefix}testClasses")],
BuildKind::Rebuild => {
vec![format!("{prefix}clean"), format!("{prefix}classes"), format!("{prefix}testClasses")]
}
BuildKind::Artifacts => vec![format!("{prefix}assemble")],
};
let mut s = BuildStep::new(&g.command, &["--console=plain"], &subdir(root, &g.dir), Format::Gradle);
s.args.extend(tasks);
s.env = g.env.clone();
Ok(vec![s])
}
fn cargo_steps(dir: &Path, kind: BuildKind) -> Vec<BuildStep> {
let build = |release: bool| {
let mut args = vec!["build", "--all-targets", "--message-format=json"];
if release {
args = vec!["build", "--release", "--message-format=json"];
}
args.extend(["--manifest-path", "Cargo.toml"]);
BuildStep::new("cargo", &args, dir, Format::Cargo)
};
match kind {
BuildKind::Build => vec![build(false)],
BuildKind::Rebuild => {
vec![BuildStep::new("cargo", &["clean", "--manifest-path", "Cargo.toml"], dir, Format::Cargo), build(false)]
}
BuildKind::Artifacts => vec![build(true)],
}
}
fn go_steps(dir: &Path, kind: BuildKind) -> Vec<BuildStep> {
let module_root = dir.ancestors().find(|d| d.join("go.mod").is_file()).unwrap_or(dir);
let target = if module_root == dir { "./...".to_owned() } else { format!("./{}/...", rel_str(module_root, dir)) };
let mut args = vec!["build"];
if kind == BuildKind::Rebuild {
args.push("-a");
}
let mut s = BuildStep::new("go", &args, module_root, Format::Go);
s.args.push(target);
vec![s]
}
/// `pnpm` / `yarn` / `bun` / `npm`, from the lock file next to `package.json` or above it.
fn package_manager(dir: &Path) -> &'static str {
for d in dir.ancestors() {
if d.join("pnpm-lock.yaml").is_file() {
return "pnpm";
} else if d.join("yarn.lock").is_file() {
return "yarn";
} else if d.join("bun.lockb").is_file() || d.join("bun.lock").is_file() {
return "bun";
} else if d.join("package-lock.json").is_file() {
return "npm";
}
}
"npm"
}
fn has_build_script(dir: &Path) -> bool {
std::fs::read(dir.join("package.json"))
.ok()
.and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok())
.is_some_and(|v| v.pointer("/scripts/build").is_some_and(serde_json::Value::is_string))
}
fn ts_steps(dir: &Path, kind: BuildKind) -> Vec<BuildStep> {
let script = has_build_script(dir);
if script && (kind == BuildKind::Artifacts || !dir.join("tsconfig.json").is_file()) {
return vec![BuildStep::new(package_manager(dir), &["run", "build"], dir, Format::Tsc)];
}
let local = dir.ancestors().map(|d| d.join("node_modules/.bin/tsc")).find(|p| p.is_file());
let program = local.map_or_else(|| "tsc".to_owned(), |p| p.to_string_lossy().into_owned());
vec![BuildStep::new(program, &["--noEmit", "--pretty", "false", "-p", "."], dir, Format::Tsc)]
}
// ---------------------------------------------------------------- running
/// Project paths as the UI knows them: compilers may print the canonical path
/// (`/private/tmp/…` for `/tmp/…`).
struct PathMap {
root: PathBuf,
canonical: Option<PathBuf>,
}
impl PathMap {
fn new(root: &Path) -> Self {
let canonical = dunce::canonicalize(root).ok().filter(|c| c != root);
PathMap { root: root.to_path_buf(), canonical }
}
fn resolve(&self, printed: &str, cwd: &Path, base: Option<&str>) -> PathBuf {
let p = Path::new(printed);
let abs = if p.is_absolute() {
p.to_path_buf()
} else {
let in_base = base.map(|b| Path::new(b).join(p)).filter(|c| c.exists());
in_base.unwrap_or_else(|| cwd.join(p))
};
let abs = normalize(&abs);
match &self.canonical {
Some(c) => abs.strip_prefix(c).map(|r| self.root.join(r)).unwrap_or(abs),
None => abs,
}
}
}
/// Removes `.` and resolvable `..` components without touching the file system.
fn normalize(p: &Path) -> PathBuf {
use std::path::Component;
let mut out = PathBuf::new();
for c in p.components() {
match c {
Component::CurDir => {}
Component::ParentDir if out.file_name().is_some() => {
out.pop();
}
other => out.push(other),
}
}
out
}
/// Line texts of files, for converting printed columns to UTF-16.
#[derive(Default)]
struct Lines {
files: HashMap<PathBuf, Option<Vec<String>>>,
}
impl Lines {
fn line(&mut self, path: &Path, line: u32) -> Option<&str> {
let lines = self.files.entry(path.to_path_buf()).or_insert_with(|| {
std::fs::read(path).ok().map(|b| String::from_utf8_lossy(&b).lines().map(str::to_owned).collect())
});
lines.as_ref()?.get(line as usize).map(String::as_str)
}
}
/// 1-based column in `unit` → 0-based UTF-16 column of `text`.
fn utf16_col(text: Option<&str>, col: u32, unit: ColUnit) -> u32 {
let n = col.saturating_sub(1);
let Some(text) = text else { return n };
match unit {
ColUnit::Utf16 => n,
ColUnit::Char => {
text.chars().take(n as usize).map(|c| c.len_utf16() as u32).sum::<u32>()
+ n.saturating_sub(text.chars().count() as u32)
}
ColUnit::Byte => {
let mut b = n as usize;
while b > 0 && b < text.len() && !text.is_char_boundary(b) {
b -= 1;
}
let b = b.min(text.len());
text[..b].encode_utf16().count() as u32 + (n as usize).saturating_sub(text.len()) as u32
}
}
}
/// UTF-16 length of the identifier starting at UTF-16 column `col` of `text`.
fn identifier_len(text: &str, col: u32) -> u32 {
let mut at = 0;
let mut len = 0;
for c in text.chars() {
if at >= col {
if !(c.is_alphanumeric() || c == '_' || c == '$') {
break;
}
len += c.len_utf16() as u32;
}
at += c.len_utf16() as u32;
}
len
}
fn resolve(raw: RawMessage, cwd: &Path, paths: &PathMap, lines: &mut Lines) -> BuildMessage {
let path = raw.path.as_deref().map(|p| paths.resolve(p, cwd, raw.base.as_deref()));
let range = match (&path, raw.line) {
(Some(p), Some(line)) => {
let l = line - 1;
let start_col = raw.col.map_or(0, |c| utf16_col(lines.line(p, l), c, raw.unit));
let end = match (raw.end_line, raw.end_col) {
(Some(el), Some(ec)) if el >= line => {
let el = el - 1;
Pos::new(el, utf16_col(lines.line(p, el), ec, raw.unit))
}
// A point: underline the identifier there, like the compilers' carets.
_ => {
let word = lines.line(p, l).map_or(0, |t| identifier_len(t, start_col));
Pos::new(l, start_col + word)
}
};
Some(Range::new(Pos::new(l, start_col), end))
}
_ => None,
};
BuildMessage { severity: raw.severity, text: raw.text, path: path.map(Into::into), range, code: raw.code }
}
fn progress_of(line: &str) -> Option<(String, Option<f32>)> {
let line = line.trim();
if let Some((name, i, total)) = crate::sync::maven_building(line) {
return Some((format!("Building {name}"), Some((i.saturating_sub(1) as f32 / total.max(1) as f32).min(1.0))));
}
if let Some(task) = line.strip_prefix("> Task ") {
return Some((format!("Running {}", task.split_whitespace().next().unwrap_or(task)), None));
}
if let Some(krate) = line.strip_prefix("Compiling ") {
return Some((format!("Compiling {}", krate.split_whitespace().next().unwrap_or(krate)), None));
}
None
}
/// Runs `plan`, streaming console output, messages and progress to `on_event`.
/// Returns `cancelled` when `cancel` fires (the processes are killed).
pub fn run(
root: &Path,
plan: &BuildPlan,
cancel: &CancellationToken,
on_event: &(dyn Fn(BuildEvent) + Sync),
) -> IdeResult<BuildResult> {
let started = Instant::now();
on_event(BuildEvent::Started { system: plan.system, title: plan.title.clone() });
let paths = PathMap::new(root);
let mut lines = Lines::default();
let mut messages: Vec<BuildMessage> = Vec::new();
let mut seen: HashSet<(Option<PathBuf>, Option<Range>, String)> = HashSet::new();
let mut success = true;
let mut last_error_line = String::new();
for step in &plan.steps {
let command = step.display();
on_event(BuildEvent::Step { command: command.clone(), cwd: step.cwd.clone().into() });
on_event(BuildEvent::Progress { message: command.clone(), fraction: None });
let mut parser = OutputParser::new(step.format);
let mut emit = |raws: Vec<RawMessage>, messages: &mut Vec<BuildMessage>| {
for raw in raws {
let m = resolve(raw, &step.cwd, &paths, &mut lines);
if seen.insert((m.path.clone().map(Into::into), m.range, m.text.clone())) {
on_event(BuildEvent::Message { message: m.clone() });
messages.push(m);
}
}
};
let out = process::run(&step.program, &step.args, &step.cwd, &step.env, cancel, |stream, line| {
let parsed = parser.feed(line);
match parsed.console {
Some(text) if text.is_empty() => {}
Some(text) => on_event(BuildEvent::Output { text, stderr: stream == Stream::Stderr }),
None => {
on_event(BuildEvent::Output { text: format!("{line}\n"), stderr: stream == Stream::Stderr });
if let Some((message, fraction)) = progress_of(line) {
on_event(BuildEvent::Progress { message, fraction });
}
}
}
let stripped = parse::strip_ansi(line);
if !stripped.trim().is_empty() && !stripped.starts_with('{') {
last_error_line = stripped;
}
emit(parsed.messages, &mut messages);
});
let out = match out {
Ok(out) => out,
Err(e) if e.kind == ide_core::error::ErrorKind::Cancelled => return Err(e),
Err(e) => {
let m = BuildMessage {
severity: Severity::Error,
text: e.message.clone(),
path: None,
range: None,
code: None,
};
on_event(BuildEvent::Output { text: format!("{}\n", e.message), stderr: true });
on_event(BuildEvent::Message { message: m.clone() });
messages.push(m);
success = false;
break;
}
};
emit(parser.finish(), &mut messages);
let code = if out.success { "0" } else { "non-zero" };
on_event(BuildEvent::Output { text: format!("\nProcess finished with exit code {code}\n"), stderr: false });
if !out.success {
success = false;
if !messages.iter().any(|m| m.severity == Severity::Error) {
let summary = crate::sync::error_summary(&out.text);
let text = if summary.is_empty() { last_error_line.clone() } else { summary };
let m = BuildMessage { severity: Severity::Error, text, path: None, range: None, code: None };
on_event(BuildEvent::Message { message: m.clone() });
messages.push(m);
}
break;
}
}
let count = |s: Severity| messages.iter().filter(|m| m.severity == s).count() as u32;
Ok(BuildResult {
system: plan.system,
title: plan.title.clone(),
success,
errors: count(Severity::Error),
warnings: count(Severity::Warning),
duration_ms: started.elapsed().as_secs_f64() * 1000.0,
messages,
})
}
/// Per-file editor / Problems diagnostics of the messages that have a position.
pub fn diagnostics(messages: &[BuildMessage]) -> BTreeMap<PathBuf, Vec<Diagnostic>> {
let mut out: BTreeMap<PathBuf, Vec<Diagnostic>> = BTreeMap::new();
for m in messages {
let (Some(path), Some(range)) = (&m.path, m.range) else { continue };
out.entry(path.to_path_buf()).or_default().push(Diagnostic {
range,
severity: m.severity,
message: m.text.clone(),
source: Some(DIAGNOSTICS_SOURCE.to_owned()),
code: m.code.clone(),
unnecessary: false,
deprecated: false,
server: Some(DIAGNOSTICS_SOURCE.to_owned()),
});
}
out
}
// ---------------------------------------------------------------- JVM launches of non-Java modules
/// Whether the JVM module at `module` has Kotlin, Groovy or Scala sources, which jdtls does not compile.
pub fn has_non_java_sources(module: &Path) -> bool {
fn walk(dir: &Path, depth: u32) -> bool {
let Ok(entries) = std::fs::read_dir(dir) else { return false };
entries.flatten().any(|e| {
let p = e.path();
if p.is_dir() {
depth > 0 && walk(&p, depth - 1)
} else {
p.extension().is_some_and(|x| x == "kt" || x == "groovy" || x == "scala")
}
})
}
walk(&module.join("src"), 12)
}
/// Compiles the module at `module` with its build system before a JVM launch and
/// returns the class directories it wrote, to put ahead of the jdtls classpath.
pub fn build_for_launch(
root: &Path,
model: &BuildModel,
module: &Path,
test: bool,
cancel: &CancellationToken,
) -> IdeResult<Vec<PathBuf>> {
let plan = plan(root, model, BuildKind::Build, Some(module))?;
let result = run(root, &plan, cancel, &|_| {})?;
if !result.success {
let errors: Vec<String> = result
.messages
.iter()
.filter(|m| m.severity == Severity::Error)
.take(5)
.map(|m| match (&m.path, m.range) {
(Some(p), Some(r)) => format!("{}:{}: {}", p.display(), r.start.line + 1, m.text),
_ => m.text.clone(),
})
.collect();
return Err(IdeError::internal(format!("Build failed:\n{}", errors.join("\n"))));
}
Ok(launch_class_dirs(plan.system, module, test))
}
fn launch_class_dirs(system: BuildSystem, module: &Path, test: bool) -> Vec<PathBuf> {
let sets: &[&str] = if test { &["test", "main"] } else { &["main"] };
let dirs: Vec<PathBuf> = match system {
BuildSystem::Gradle => sets
.iter()
.flat_map(|s| {
["classes/kotlin", "classes/groovy", "classes/scala", "classes/java", "resources"]
.map(|d| module.join("build").join(d).join(s))
})
.collect(),
BuildSystem::Maven if test => vec![module.join("target/test-classes"), module.join("target/classes")],
BuildSystem::Maven => vec![module.join("target/classes")],
_ => Vec::new(),
};
dirs.into_iter().filter(|d| d.is_dir()).collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn write(root: &Path, rel: &str, text: &str) {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, text).unwrap();
}
#[test]
fn plans_per_build_system() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("demo");
write(
&root,
"pom.xml",
"<project><artifactId>demo</artifactId><modules><module>core</module></modules></project>",
);
write(&root, "core/pom.xml", "<project><artifactId>core</artifactId></project>");
write(&root, "core/src/main/java/A.java", "class A {}");
let model = crate::load(&root, &tmp.path().join("cache"), None);
let p = plan(&root, &model, BuildKind::Build, None).unwrap();
assert_eq!(p.system, BuildSystem::Maven);
assert_eq!(p.title, "demo");
assert_eq!(p.steps.len(), 1);
assert_eq!(p.steps[0].args, ["-B", "-Dstyle.color=never", "test-compile"]);
let p = plan(&root, &model, BuildKind::Rebuild, Some(&root.join("core/src/main/java/A.java"))).unwrap();
assert_eq!(p.steps[0].args, ["-B", "-Dstyle.color=never", "-pl", "core", "-am", "clean", "test-compile"]);
assert_eq!(p.steps[0].cwd, root);
assert_eq!(p.title, "demo: build module 'core'");
let g = tmp.path().join("g");
write(&g, "settings.gradle", "include 'app'");
write(&g, "app/build.gradle", "apply plugin: 'java'");
let model = crate::load(&g, &tmp.path().join("cache2"), None);
let p = plan(&g, &model, BuildKind::Build, Some(&g.join("app/src/A.java"))).unwrap();
assert_eq!(p.system, BuildSystem::Gradle);
assert_eq!(p.steps[0].args, ["--console=plain", ":app:classes", ":app:testClasses"]);
assert_eq!(
plan(&g, &model, BuildKind::Artifacts, None).unwrap().steps[0].args,
["--console=plain", "assemble"]
);
let c = tmp.path().join("c");
write(&c, "Cargo.toml", "[package]\nname='c'");
let p = plan(&c, &BuildModel::default(), BuildKind::Rebuild, None).unwrap();
assert_eq!(p.system, BuildSystem::Cargo);
assert_eq!(p.steps.len(), 2);
assert_eq!(p.steps[0].args[0], "clean");
let go = tmp.path().join("go");
write(&go, "go.mod", "module x");
let p = plan(&go, &BuildModel::default(), BuildKind::Build, Some(&go.join("pkg/a/a.go"))).unwrap();
assert_eq!(p.steps[0].args, ["build", "./pkg/a/..."]);
assert_eq!(p.steps[0].cwd, go);
let ts = tmp.path().join("ts");
write(&ts, "package.json", r#"{"scripts":{"build":"vite build"}}"#);
write(&ts, "tsconfig.json", "{}");
write(&ts, "pnpm-lock.yaml", "");
let p = plan(&ts, &BuildModel::default(), BuildKind::Build, None).unwrap();
assert_eq!(p.steps[0].args, ["--noEmit", "--pretty", "false", "-p", "."]);
let p = plan(&ts, &BuildModel::default(), BuildKind::Artifacts, None).unwrap();
assert_eq!(
(p.steps[0].program.as_str(), p.steps[0].args.as_slice()),
("pnpm", &["run".to_owned(), "build".to_owned()][..])
);
let none = tmp.path().join("none");
std::fs::create_dir_all(&none).unwrap();
assert!(plan(&none, &BuildModel::default(), BuildKind::Build, None).is_err());
}
#[test]
fn columns_become_utf16() {
assert_eq!(utf16_col(Some("a😀b"), 3, ColUnit::Char), 3);
assert_eq!(utf16_col(Some("a😀b"), 6, ColUnit::Byte), 3);
assert_eq!(utf16_col(Some("a😀b"), 4, ColUnit::Utf16), 3);
assert_eq!(utf16_col(None, 5, ColUnit::Char), 4);
assert_eq!(utf16_col(Some("ab"), 5, ColUnit::Char), 4);
assert_eq!(identifier_len("x 😀ab_1 c", 2), 0);
assert_eq!(identifier_len("x é_1 c", 2), 3);
}
// Uses Unix absolute paths for the canonical root.
#[cfg(unix)]
#[test]
fn resolves_paths_against_cwd_and_canonical_root() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("p");
write(&root, "src/a.ts", "let é = 1;\nlet x: number = 'a';\n");
let map = PathMap { root: root.clone(), canonical: Some(PathBuf::from("/canon/p")) };
assert_eq!(map.resolve("/canon/p/src/a.ts", &root, None), root.join("src/a.ts"));
assert_eq!(map.resolve("./src/../src/a.ts", &root, None), root.join("src/a.ts"));
let mut lines = Lines::default();
let raw = RawMessage {
severity: Severity::Error,
text: "bad".into(),
path: Some("src/a.ts".into()),
line: Some(2),
col: Some(5),
end_line: None,
end_col: None,
unit: ColUnit::Utf16,
code: Some("TS2322".into()),
base: None,
};
let m = resolve(raw, &root, &map, &mut lines);
assert_eq!(m.path.as_deref(), Some(root.join("src/a.ts").as_path()));
assert_eq!(m.range, Some(Range::new(Pos::new(1, 4), Pos::new(1, 5))), "the identifier at the column");
let d = diagnostics(&[m]);
assert_eq!(d[&root.join("src/a.ts")][0].source.as_deref(), Some("build"));
}
#[test]
fn runs_a_failing_build_and_reports_messages() {
if cfg!(windows) {
return;
}
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("p");
write(&root, "main.go", "package main\n");
let script = "echo '# demo'; echo './main.go:1:9: undefined: x' >&2; exit 1";
let mut step = BuildStep::new("sh", &["-c", script], &root, Format::Go);
step.env.insert("A".into(), "1".into());
let plan = BuildPlan { system: BuildSystem::Go, title: "p".into(), steps: vec![step] };
let events = parking_lot_free::Events::default();
let result = run(&root, &plan, &CancellationToken::new(), &|e| events.push(e)).unwrap();
assert!(!result.success);
assert_eq!((result.errors, result.warnings), (1, 0));
let m = &result.messages[0];
assert_eq!(m.path.as_deref(), Some(root.join("main.go").as_path()));
assert_eq!(m.range, Some(Range::new(Pos::new(0, 8), Pos::new(0, 12))));
let kinds = events.kinds();
assert_eq!(kinds.first().map(String::as_str), Some("started"));
assert!(kinds.iter().any(|k| k == "message") && kinds.iter().any(|k| k == "output"));
let cancel = CancellationToken::new();
cancel.cancel();
let slow = BuildPlan {
system: BuildSystem::Go,
title: "p".into(),
steps: vec![BuildStep::new("sh", &["-c", "sleep 5"], &root, Format::Go)],
};
assert_eq!(run(&root, &slow, &cancel, &|_| {}).err().unwrap().kind, ide_core::error::ErrorKind::Cancelled);
}
mod parking_lot_free {
use super::super::BuildEvent;
use std::sync::Mutex;
#[derive(Default)]
pub struct Events(Mutex<Vec<String>>);
impl Events {
pub fn push(&self, e: BuildEvent) {
let kind = serde_json::to_value(&e).unwrap()["kind"].as_str().unwrap().to_owned();
self.0.lock().unwrap().push(kind);
}
pub fn kinds(&self) -> Vec<String> {
self.0.lock().unwrap().clone()
}
}
}
#[test]
fn non_java_launch_class_dirs() {
let tmp = tempfile::tempdir().unwrap();
let m = tmp.path();
assert!(!has_non_java_sources(m));
write(m, "src/main/java/A.java", "class A {}");
assert!(!has_non_java_sources(m));
write(m, "src/main/kotlin/a/App.kt", "fun main() {}");
assert!(has_non_java_sources(m));
let g = tmp.path().join("g");
write(&g, "src/test/groovy/ASpec.groovy", "class ASpec {}");
assert!(has_non_java_sources(&g));
for d in ["build/classes/kotlin/main", "build/classes/kotlin/test", "build/resources/main", "target/classes"] {
std::fs::create_dir_all(m.join(d)).unwrap();
}
assert_eq!(
launch_class_dirs(BuildSystem::Gradle, m, false),
[m.join("build/classes/kotlin/main"), m.join("build/resources/main")]
);
assert_eq!(launch_class_dirs(BuildSystem::Gradle, m, true)[0], m.join("build/classes/kotlin/test"));
assert_eq!(launch_class_dirs(BuildSystem::Maven, m, true), [m.join("target/classes")]);
}
}
+590
Переглянути файл
@@ -0,0 +1,590 @@
//! Compiler output → messages. One [`OutputParser`] per build step consumes the
//! output line by line; multi-line messages (javac's source / caret lines,
//! `symbol:` / `location:` details, tsc message chains) are completed by the
//! following lines, so callers must call [`OutputParser::finish`] at the end.
//!
//! Positions are kept as printed (1-based, in the unit of the producing tool);
//! [`super::resolve`] turns them into IDE coordinates.
use ide_core::Severity;
use serde_json::Value;
/// Output dialect of a build step.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Format {
/// Maven: `[ERROR] /p/A.java:[12,5] msg`, plus Kotlin and javac forms.
Maven,
/// Gradle: javac's `A.java:12: error: msg` and Kotlin's `e: file:///p/A.kt:12:5 msg`.
Gradle,
/// `cargo --message-format=json`.
Cargo,
/// `tsc`: `a.ts(12,5): error TS2322: msg` or the pretty `a.ts:12:5 - error TS2322: msg`.
Tsc,
/// `go build`: `./a.go:12:5: msg`.
Go,
}
/// What a printed column counts.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ColUnit {
/// Unicode scalar values (javac, kotlinc, rustc).
Char,
/// UTF-16 code units (tsc).
Utf16,
/// Bytes (go).
Byte,
}
/// A message as printed: path as written, 1-based line / column.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct RawMessage {
pub severity: Severity,
pub text: String,
pub path: Option<String>,
pub line: Option<u32>,
pub col: Option<u32>,
pub end_line: Option<u32>,
pub end_col: Option<u32>,
pub unit: ColUnit,
pub code: Option<String>,
/// Directory relative paths resolve against first (cargo: the package of the message).
pub base: Option<String>,
}
impl RawMessage {
fn new(severity: Severity, text: impl Into<String>, unit: ColUnit) -> Self {
RawMessage {
severity,
text: text.into(),
path: None,
line: None,
col: None,
end_line: None,
end_col: None,
unit,
code: None,
base: None,
}
}
fn at(mut self, path: &str, line: u32, col: Option<u32>) -> Self {
self.path = Some(path.to_owned());
self.line = Some(line);
self.col = col;
self
}
}
/// What a line contributes: messages completed by it and the console text to
/// show instead of the raw line (cargo's JSON becomes the rendered diagnostic).
#[derive(Debug, Default)]
pub(crate) struct Parsed {
pub messages: Vec<RawMessage>,
/// `None` = show the line as is; `Some("")` = hide it.
pub console: Option<String>,
}
/// A message waiting for its detail lines.
struct Pending {
msg: RawMessage,
/// javac (Gradle) prints the source line and a caret line after the header: lines still expected.
source_lines: u8,
}
pub(crate) struct OutputParser {
format: Format,
pending: Option<Pending>,
}
/// Removes ANSI escape sequences (colour, cursor movement).
pub(crate) fn strip_ansi(s: &str) -> String {
if !s.contains('\u{1b}') {
return s.to_owned();
}
let mut out = String::with_capacity(s.len());
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c != '\u{1b}' {
out.push(c);
continue;
}
if chars.peek() == Some(&'[') {
chars.next();
for c in chars.by_ref() {
if c.is_ascii_alphabetic() || c == '~' {
break;
}
}
} else {
chars.next();
}
}
out
}
fn num(s: &str) -> Option<u32> {
s.trim().parse().ok().filter(|n| *n > 0)
}
/// `path:12:5` / `path:12` at the start of `s` (path may contain `:` on Windows) → (path, line, col, rest).
fn path_line_col(s: &str) -> Option<(&str, u32, Option<u32>, &str)> {
// Scan `:` positions; the first one followed by digits and `:` / end is the line.
let bytes = s.as_bytes();
let mut i = 0;
while let Some(off) = s[i..].find(':') {
let colon = i + off;
let digits = s[colon + 1..].bytes().take_while(u8::is_ascii_digit).count();
let after = colon + 1 + digits;
if colon > 0 && digits > 0 && (after == s.len() || bytes[after] == b':' || bytes[after] == b' ') {
let path = &s[..colon];
let line = num(&s[colon + 1..after])?;
if after < s.len() && bytes[after] == b':' {
let cdigits = s[after + 1..].bytes().take_while(u8::is_ascii_digit).count();
if cdigits > 0 {
let end = after + 1 + cdigits;
return Some((path, line, num(&s[after + 1..end]), &s[end..]));
}
}
return Some((path, line, None, &s[after..]));
}
i = colon + 1;
}
None
}
fn looks_like_source_path(p: &str) -> bool {
!p.is_empty() && !p.contains(' ') && p.rsplit(['/', '\\']).next().is_some_and(|name| name.contains('.'))
}
/// `[12,5] msg` (Maven) → (12, 5, msg).
fn bracket_pos(s: &str) -> Option<(u32, Option<u32>, &str)> {
let inner = s.strip_prefix('[')?;
let (pos, rest) = inner.split_once(']')?;
let (line, col) = match pos.split_once(',') {
Some((l, c)) => (num(l)?, num(c)),
None => (num(pos)?, None),
};
Some((line, col, rest.trim_start()))
}
/// `file:///p/A.kt` → `/p/A.kt` (percent-decoded).
fn from_file_uri(s: &str) -> String {
let Some(rest) = s.strip_prefix("file://") else { return s.to_owned() };
let hex = |b: u8| (b as char).to_digit(16).and_then(|d| u8::try_from(d).ok());
let bytes = rest.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%'
&& let (Some(h), Some(l)) = (bytes.get(i + 1).and_then(|b| hex(*b)), bytes.get(i + 2).and_then(|b| hex(*b)))
{
out.push(h * 16 + l);
i += 3;
continue;
}
out.push(bytes[i]);
i += 1;
}
let path = String::from_utf8_lossy(&out).into_owned();
// `file:///C:/x` on Windows.
if path.len() > 2 && path.as_bytes()[0] == b'/' && path.as_bytes()[2] == b':' { path[1..].to_owned() } else { path }
}
/// Kotlin: `file:///p/A.kt:12:5 msg` or `/p/A.kt: (12, 5): msg`, after the `e: ` / `w: ` prefix.
fn kotlin_location(s: &str, severity: Severity) -> Option<RawMessage> {
if let Some((path, rest)) = s.split_once(": (") {
let (pos, msg) = rest.split_once(')')?;
let (l, c) = pos.split_once(',')?;
let msg = msg.trim_start_matches(':').trim();
return Some(RawMessage::new(severity, msg, ColUnit::Char).at(&from_file_uri(path), num(l)?, num(c)));
}
let (path, line, col, rest) = path_line_col(s)?;
let msg = rest.trim_start_matches(':').trim();
if !looks_like_source_path(path) || msg.is_empty() {
return None;
}
Some(RawMessage::new(severity, msg, ColUnit::Char).at(&from_file_uri(path), line, col))
}
fn kotlin_line(line: &str) -> Option<RawMessage> {
let (severity, rest) = if let Some(r) = line.strip_prefix("e: ") {
(Severity::Error, r)
} else {
let r = line.strip_prefix("w: ")?;
(Severity::Warning, r)
};
kotlin_location(rest, severity)
}
/// javac: `A.java:12: error: msg` / `A.java:12: warning: [deprecation] msg`.
fn javac_header(line: &str) -> Option<RawMessage> {
let (path, ln, col, rest) = path_line_col(line)?;
if !looks_like_source_path(path) {
return None;
}
let rest = rest.trim_start_matches(':').trim_start();
let (severity, msg) = if let Some(m) = rest.strip_prefix("error:") {
(Severity::Error, m)
} else {
let m = rest.strip_prefix("warning:")?;
(Severity::Warning, m)
};
Some(RawMessage::new(severity, msg.trim(), ColUnit::Char).at(path, ln, col))
}
/// Maven: `[ERROR] /p/A.java:[12,5] msg`, `[WARNING] …`, and Kotlin / javac forms after the level.
fn maven_line(line: &str) -> Option<RawMessage> {
let (severity, rest) = if let Some(r) = line.strip_prefix("[ERROR] ") {
(Severity::Error, r)
} else {
let r = line.strip_prefix("[WARNING] ")?;
(Severity::Warning, r)
};
let rest = rest.trim_start();
if let Some((path, pos)) = rest.split_once(":[")
&& looks_like_source_path(path)
&& let Some((ln, col, msg)) = bracket_pos(&format!("[{pos}"))
{
return Some(RawMessage::new(severity, msg, ColUnit::Char).at(path, ln, col));
}
if let Some(m) = kotlin_line(rest) {
return Some(m);
}
if rest.starts_with("file://") || rest.contains(": (") {
return kotlin_location(rest, severity);
}
None
}
/// tsc: `a.ts(12,5): error TS2322: msg` or `a.ts:12:5 - error TS2322: msg`.
fn tsc_line(line: &str) -> Option<RawMessage> {
let (path, ln, col, rest) = if let Some(open) = line.find('(')
&& let Some(close) = line[open..].find("):")
{
let (l, c) = line[open + 1..open + close].split_once(',')?;
(&line[..open], num(l)?, num(c), &line[open + close + 2..])
} else {
let (path, ln, col, rest) = path_line_col(line)?;
(path, ln, col, rest.strip_prefix(" -")?)
};
if !looks_like_source_path(path) {
return None;
}
let rest = rest.trim_start();
let (severity, rest) = if let Some(r) = rest.strip_prefix("error") {
(Severity::Error, r)
} else {
let r = rest.strip_prefix("warning")?;
(Severity::Warning, r)
};
let (code, msg) = match rest.trim_start().split_once(':') {
Some((code, msg)) if code.starts_with("TS") => (Some(code.to_owned()), msg.trim()),
_ => (None, rest.trim_start_matches(':').trim()),
};
let mut m = RawMessage::new(severity, msg, ColUnit::Utf16).at(path.trim(), ln, col);
m.code = code;
Some(m)
}
/// go: `./a.go:12:5: msg` (errors only; `go vet` style `a.go:12: msg` too).
fn go_line(line: &str) -> Option<RawMessage> {
let (path, ln, col, rest) = path_line_col(line)?;
if !path.ends_with(".go") && !path.ends_with(".s") {
return None;
}
let msg = rest.strip_prefix(':')?.trim();
(!msg.is_empty()).then(|| RawMessage::new(Severity::Error, msg, ColUnit::Byte).at(path, ln, col))
}
/// rustc's JSON diagnostic (`reason: compiler-message`) → message and its rendered text.
fn cargo_json(v: &Value) -> Option<(Option<RawMessage>, String)> {
if v.get("reason")?.as_str()? != "compiler-message" {
return Some((None, String::new()));
}
let msg = v.get("message")?;
let rendered = msg.get("rendered").and_then(Value::as_str).unwrap_or_default().trim_end().to_owned();
let severity = match msg.get("level")?.as_str()? {
"error" | "error: internal compiler error" => Severity::Error,
"warning" => Severity::Warning,
"note" | "help" => Severity::Info,
_ => return Some((None, rendered)),
};
let text = msg.get("message")?.as_str()?.to_owned();
let span = msg
.get("spans")
.and_then(Value::as_array)
.and_then(|s| s.iter().find(|s| s.get("is_primary").and_then(Value::as_bool) == Some(true)));
let Some(span) = span else {
// Span-less summaries ("2 warnings emitted", "aborting due to…") repeat the real messages.
let summary = text.starts_with("aborting due to")
|| text.ends_with("emitted")
|| text.contains("generated") && text.contains("warning");
let keep = severity == Severity::Error && !summary;
return Some((keep.then(|| RawMessage::new(severity, text, ColUnit::Char)), rendered));
};
let u = |k: &str| span.get(k).and_then(Value::as_u64).and_then(|n| u32::try_from(n).ok());
let mut m = RawMessage::new(severity, text, ColUnit::Char).at(
span.get("file_name")?.as_str()?,
u("line_start")?,
u("column_start"),
);
m.end_line = u("line_end");
m.end_col = u("column_end");
m.code = msg.get("code").and_then(|c| c.get("code")).and_then(Value::as_str).map(str::to_owned);
m.base = v
.get("manifest_path")
.and_then(Value::as_str)
.and_then(|p| std::path::Path::new(p).parent())
.map(|p| p.to_string_lossy().into_owned());
Some((Some(m), rendered))
}
impl OutputParser {
pub(crate) fn new(format: Format) -> Self {
OutputParser { format, pending: None }
}
fn flush(&mut self, out: &mut Vec<RawMessage>) {
if let Some(p) = self.pending.take() {
out.push(p.msg);
}
}
/// Source, caret and detail lines that belong to the pending message; `true` when consumed.
fn continue_pending(&mut self, line: &str) -> bool {
let format = self.format;
let Some(p) = &mut self.pending else { return false };
if p.source_lines > 0 {
let t = line.trim_end();
if t.trim_start() == "^" {
if p.msg.col.is_none() {
p.msg.col = u32::try_from(t.chars().count()).ok();
}
p.source_lines = 0;
return true;
}
if p.source_lines == 2 {
p.source_lines = 1;
return true;
}
p.source_lines = 0;
}
// Maven repeats compiler messages in its failure summary with every line prefixed.
let body = match format {
Format::Maven => line
.strip_prefix("[ERROR]")
.or_else(|| line.strip_prefix("[WARNING]"))
.map_or(line, |r| r.strip_prefix(' ').unwrap_or(r)),
_ => line,
};
if matches!(format, Format::Maven | Format::Gradle | Format::Tsc)
&& body.starts_with(" ")
&& !body.trim().is_empty()
{
p.msg.text.push('\n');
p.msg.text.push_str(body.trim());
return true;
}
false
}
pub(crate) fn feed(&mut self, raw: &str) -> Parsed {
let line = strip_ansi(raw);
let mut out = Parsed::default();
if self.format == Format::Cargo {
if line.starts_with('{')
&& let Ok(v) = serde_json::from_str::<Value>(&line)
{
let (msg, rendered) = cargo_json(&v).unwrap_or_default();
out.messages.extend(msg);
out.console = Some(if rendered.is_empty() { String::new() } else { format!("{rendered}\n") });
}
return out;
}
if self.continue_pending(&line) {
return out;
}
self.flush(&mut out.messages);
let parsed = match self.format {
Format::Maven => maven_line(&line),
Format::Gradle => javac_header(&line).or_else(|| kotlin_line(&line)),
Format::Tsc => tsc_line(&line),
Format::Go => go_line(&line),
Format::Cargo => None,
};
if let Some(msg) = parsed {
let javac = self.format == Format::Gradle
&& msg.unit == ColUnit::Char
&& !line.starts_with("e: ")
&& !line.starts_with("w: ");
self.pending = Some(Pending { msg, source_lines: if javac { 2 } else { 0 } });
}
out
}
pub(crate) fn finish(&mut self) -> Vec<RawMessage> {
let mut out = Vec::new();
self.flush(&mut out);
out
}
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(format: Format, text: &str) -> Vec<RawMessage> {
let mut p = OutputParser::new(format);
let mut out = Vec::new();
for line in text.lines() {
out.extend(p.feed(line).messages);
}
out.extend(p.finish());
out
}
fn pos(m: &RawMessage) -> (Severity, &str, u32, Option<u32>) {
(m.severity, m.path.as_deref().unwrap(), m.line.unwrap(), m.col)
}
#[test]
fn maven_javac() {
let out = parse(
Format::Maven,
"[INFO] --- compiler:3.13.0:compile (default-compile) @ demo ---\n\
[ERROR] COMPILATION ERROR : \n\
[ERROR] /p/src/main/java/a/A.java:[12,5] cannot find symbol\n\
\x20 symbol: class Foo\n\
\x20 location: class a.A\n\
[WARNING] /p/src/main/java/a/B.java:[3,8] [deprecation] old() in a.C has been deprecated\n\
[INFO] 1 error\n\
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile\n",
);
assert_eq!(out.len(), 2, "{out:?}");
assert_eq!(pos(&out[0]), (Severity::Error, "/p/src/main/java/a/A.java", 12, Some(5)));
assert_eq!(out[0].text, "cannot find symbol\nsymbol: class Foo\nlocation: class a.A");
assert_eq!(pos(&out[1]), (Severity::Warning, "/p/src/main/java/a/B.java", 3, Some(8)));
assert_eq!(out[1].unit, ColUnit::Char);
}
#[test]
fn maven_detail_lines_with_level_prefix_and_kotlin() {
let out = parse(
Format::Maven,
"[ERROR] /p/A.java:[7,20] incompatible types: String cannot be converted to int\n\
[ERROR] -> [Help 1]\n\
[ERROR] file:///p/src/K%20t.kt:4:9 Unresolved reference 'x'.\n\
[WARNING] /p/src/K.kt: (2, 3): Variable 'y' is never used\n",
);
assert_eq!(out.len(), 3, "{out:?}");
assert_eq!(out[0].text, "incompatible types: String cannot be converted to int");
assert_eq!(pos(&out[1]), (Severity::Error, "/p/src/K t.kt", 4, Some(9)));
assert_eq!(pos(&out[2]), (Severity::Warning, "/p/src/K.kt", 2, Some(3)));
assert_eq!(out[2].text, "Variable 'y' is never used");
}
#[test]
fn gradle_javac_with_caret_column() {
let out = parse(
Format::Gradle,
"> Task :compileJava\n\
/p/src/main/java/a/A.java:12: error: cannot find symbol\n\
\x20 Foo foo = new Foo();\n\
\x20 ^\n\
\x20 symbol: class Foo\n\
\x20 location: class A\n\
/p/src/main/java/a/A.java:20: warning: [removal] finalize() has been deprecated\n\
\x20 protected void finalize() {}\n\
\x20 ^\n\
1 error\n\
1 warning\n\
> Task :compileJava FAILED\n",
);
assert_eq!(out.len(), 2, "{out:?}");
assert_eq!(pos(&out[0]), (Severity::Error, "/p/src/main/java/a/A.java", 12, Some(9)));
assert_eq!(out[0].text, "cannot find symbol\nsymbol: class Foo\nlocation: class A");
assert_eq!(pos(&out[1]), (Severity::Warning, "/p/src/main/java/a/A.java", 20, Some(20)));
assert_eq!(out[1].text, "[removal] finalize() has been deprecated");
}
#[test]
fn gradle_kotlin() {
let out = parse(
Format::Gradle,
"> Task :app:compileKotlin FAILED\n\
e: file:///p/app/src/main/kotlin/Main.kt:12:5 Unresolved reference: foo\n\
w: file:///p/app/src/main/kotlin/Main.kt:3:9 Parameter 'args' is never used\n\
e: Compilation error. See log for more details\n",
);
assert_eq!(out.len(), 2, "{out:?}");
assert_eq!(pos(&out[0]), (Severity::Error, "/p/app/src/main/kotlin/Main.kt", 12, Some(5)));
assert_eq!(out[0].text, "Unresolved reference: foo");
assert_eq!(pos(&out[1]), (Severity::Warning, "/p/app/src/main/kotlin/Main.kt", 3, Some(9)));
}
#[test]
fn cargo_json_messages() {
let err = r#"{"reason":"compiler-message","package_id":"p 0.1.0","manifest_path":"/w/crates/p/Cargo.toml","target":{},"message":{"rendered":"error[E0425]: cannot find value `x`\n --> src/main.rs:2:5\n","$message_type":"diagnostic","children":[],"code":{"code":"E0425","explanation":null},"level":"error","message":"cannot find value `x` in this scope","spans":[{"byte_end":20,"byte_start":19,"column_end":6,"column_start":5,"expansion":null,"file_name":"src/main.rs","is_primary":true,"label":null,"line_end":2,"line_start":2,"suggested_replacement":null,"suggestion_applicability":null,"text":[]}]}}"#;
let summary = r#"{"reason":"compiler-message","manifest_path":"/w/Cargo.toml","message":{"rendered":"error: aborting due to 1 previous error\n","children":[],"code":null,"level":"error","message":"aborting due to 1 previous error","spans":[]}}"#;
let artifact = r#"{"reason":"compiler-artifact","package_id":"p"}"#;
let mut p = OutputParser::new(Format::Cargo);
let a = p.feed(err);
assert_eq!(a.console.as_deref(), Some("error[E0425]: cannot find value `x`\n --> src/main.rs:2:5\n"));
let m = &a.messages[0];
assert_eq!(pos(m), (Severity::Error, "src/main.rs", 2, Some(5)));
assert_eq!((m.end_line, m.end_col, m.code.as_deref()), (Some(2), Some(6), Some("E0425")));
assert_eq!(m.base.as_deref(), Some("/w/crates/p"));
let s = p.feed(summary);
assert!(s.messages.is_empty());
assert_eq!(s.console.as_deref(), Some("error: aborting due to 1 previous error\n"));
let a = p.feed(artifact);
assert!(a.messages.is_empty() && a.console.as_deref() == Some(""));
let plain = p.feed(" Compiling p v0.1.0 (/w/crates/p)");
assert!(plain.messages.is_empty() && plain.console.is_none());
}
#[test]
fn tsc_plain_and_pretty() {
let out = parse(
Format::Tsc,
"src/a.ts(12,5): error TS2322: Type 'string' is not assignable to type 'number'.\n\
src/b.ts(3,1): error TS2345: Argument of type 'A' is not assignable to parameter of type 'B'.\n\
\x20 Property 'x' is missing in type 'A'.\n\
\x1b[96msrc/c.tsx\x1b[0m:\x1b[93m7\x1b[0m:\x1b[93m10\x1b[0m - \x1b[91merror\x1b[0m\x1b[90m TS2304: \x1b[0mCannot find name 'foo'.\n\
\n\
Found 3 errors in 3 files.\n",
);
assert_eq!(out.len(), 3, "{out:?}");
assert_eq!(pos(&out[0]), (Severity::Error, "src/a.ts", 12, Some(5)));
assert_eq!(out[0].code.as_deref(), Some("TS2322"));
assert_eq!(out[0].unit, ColUnit::Utf16);
assert!(out[1].text.ends_with("\nProperty 'x' is missing in type 'A'."));
assert_eq!(pos(&out[2]), (Severity::Error, "src/c.tsx", 7, Some(10)));
assert_eq!(out[2].text, "Cannot find name 'foo'.");
}
#[test]
fn go_errors() {
let out = parse(
Format::Go,
"# example.com/demo\n\
./main.go:12:5: undefined: foo\n\
pkg/util.go:3:2: \"fmt\" imported and not used\n\
note: module requires Go 1.22\n",
);
assert_eq!(out.len(), 2, "{out:?}");
assert_eq!(pos(&out[0]), (Severity::Error, "./main.go", 12, Some(5)));
assert_eq!(out[0].text, "undefined: foo");
assert_eq!(out[1].unit, ColUnit::Byte);
assert_eq!(pos(&out[1]), (Severity::Error, "pkg/util.go", 3, Some(2)));
}
#[test]
fn helpers() {
assert_eq!(strip_ansi("\x1b[1;31merror\x1b[0m: x"), "error: x");
assert_eq!(from_file_uri("file:///a/b%20c.kt"), "/a/b c.kt");
assert_eq!(from_file_uri("file:///C:/a.kt"), "C:/a.kt");
assert_eq!(path_line_col("C:\\a\\b.go:3:4: x"), Some(("C:\\a\\b.go", 3, Some(4), ": x")));
assert_eq!(path_line_col("no position here"), None);
}
}
+536
Переглянути файл
@@ -0,0 +1,536 @@
//! Gradle projects for the Gradle tool window: the project structure from
//! `settings.gradle(.kts)` (read statically) and the task list from
//! `gradle tasks --all` (after a sync; until then the usual tasks of the
//! applied plugins are shown).
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
pub const SETTINGS_FILES: &[&str] = &["settings.gradle.kts", "settings.gradle"];
pub const BUILD_FILES: &[&str] = &["build.gradle.kts", "build.gradle"];
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct GradleModel {
/// Project-relative directory of the build (where `settings.gradle` is); `""` = project root.
pub dir: String,
/// The root project first, then subprojects in `include` order.
pub projects: Vec<GradleProject>,
pub java_version: Option<u32>,
/// `gradlew` of the build (absolute), else `gradle`.
pub command: String,
/// Environment for runs (`JAVA_HOME`); filled in by the shell.
pub env: BTreeMap<String, String>,
/// When the task list was last read from Gradle (Unix ms); `None` = static defaults.
pub synced_at: Option<f64>,
pub stale: bool,
pub sync_error: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct GradleProject {
/// Gradle path: `:` for the root project, `:app`, `:libs:core`.
pub path: String,
pub name: String,
/// Project-relative directory.
pub dir: String,
/// Project-relative build script, if any.
pub build_file: Option<String>,
pub tasks: Vec<GradleTask>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct GradleTask {
pub name: String,
/// Lower-case group (`build`, `build setup`, `other`).
pub group: String,
pub description: Option<String>,
}
fn task(group: &str, name: &str) -> GradleTask {
GradleTask { name: name.to_owned(), group: group.to_owned(), description: None }
}
/// A Groovy / Kotlin script with `//` and `/* */` comments removed.
fn strip_comments(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut chars = text.chars().peekable();
let mut quote: Option<char> = None;
while let Some(c) = chars.next() {
if let Some(q) = quote {
out.push(c);
if c == '\\' {
if let Some(n) = chars.next() {
out.push(n);
}
} else if c == q {
quote = None;
}
continue;
}
match c {
'"' | '\'' => {
quote = Some(c);
out.push(c);
}
'/' if chars.peek() == Some(&'/') => {
for n in chars.by_ref() {
if n == '\n' {
out.push('\n');
break;
}
}
}
'/' if chars.peek() == Some(&'*') => {
chars.next();
let mut prev = ' ';
for n in chars.by_ref() {
if prev == '*' && n == '/' {
break;
}
prev = n;
}
}
_ => out.push(c),
}
}
out
}
/// Quoted string literals in `s`.
fn literals(s: &str) -> Vec<String> {
let mut out = Vec::new();
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '"' || c == '\'' {
out.push(chars.by_ref().take_while(|&n| n != c).collect());
}
}
out
}
/// Arguments of `include(...)` (Kotlin) or `include 'a', 'b'` (Groovy, continued
/// over lines that end with a comma).
fn include_args(after: &str) -> Vec<String> {
if let Some(inner) = after.trim_start().strip_prefix('(') {
return literals(&inner[..inner.find(')').unwrap_or(inner.len())]);
}
let mut out = Vec::new();
for line in after.lines() {
out.extend(literals(line));
if !line.trim_end().ends_with(',') {
break;
}
}
out
}
/// `(root project name, included project paths)` from a settings script.
pub fn parse_settings(text: &str) -> (Option<String>, Vec<String>) {
let text = strip_comments(text);
let mut name = None;
let mut includes: Vec<String> = Vec::new();
for (i, _) in text.match_indices("rootProject.name") {
let line = text[i + "rootProject.name".len()..].lines().next().unwrap_or_default();
if let Some(n) = literals(line).into_iter().next() {
name = Some(n);
}
}
let mut from = 0;
while let Some(i) = text[from..].find("include") {
let at = from + i;
from = at + "include".len();
let before_ok = at == 0 || !text[..at].ends_with(|c: char| c.is_alphanumeric() || c == '_' || c == '.');
let after = &text[from..];
let after_ok = after.starts_with(|c: char| c == '(' || c.is_whitespace());
if !before_ok || !after_ok {
continue;
}
for p in include_args(after) {
let path = if p.starts_with(':') { p } else { format!(":{p}") };
if path.len() > 1 && !includes.contains(&path) {
includes.push(path);
}
}
}
(name, includes)
}
/// The directory of the Gradle build under `root`: the root itself, or a direct
/// subfolder, holding a settings or build script.
pub fn build_dir(root: &Path) -> Option<PathBuf> {
let has = |d: &Path| SETTINGS_FILES.iter().chain(BUILD_FILES).any(|f| d.join(f).is_file());
if has(root) {
return Some(root.to_path_buf());
}
let mut dirs: Vec<PathBuf> = std::fs::read_dir(root)
.ok()?
.flatten()
.filter(|e| e.file_type().is_ok_and(|t| t.is_dir()) && !e.file_name().to_string_lossy().starts_with('.'))
.map(|e| e.path())
.collect();
dirs.sort();
dirs.into_iter().find(|d| SETTINGS_FILES.iter().any(|f| d.join(f).is_file()))
}
fn wrapper(dir: &Path) -> String {
let name = if cfg!(windows) { "gradlew.bat" } else { "gradlew" };
if dir.join(name).is_file() {
return dir.join(name).to_string_lossy().into_owned();
}
if cfg!(windows) { "gradle.bat".into() } else { "gradle".into() }
}
fn rel(root: &Path, p: &Path) -> String {
p.strip_prefix(root).unwrap_or(p).to_string_lossy().replace('\\', "/")
}
/// The tasks Gradle's own plugins add, for display before the first sync.
pub fn default_tasks(is_root: bool, script: &str) -> Vec<GradleTask> {
let mut out = Vec::new();
if is_root {
out.extend([task("build setup", "init"), task("build setup", "wrapper")]);
}
for t in [
"buildEnvironment",
"dependencies",
"dependencyInsight",
"help",
"javaToolchains",
"projects",
"properties",
"tasks",
] {
out.push(task("help", t));
}
let jvm = [
"java",
"application",
"kotlin(",
"org.jetbrains.kotlin",
"groovy",
"scala",
"org.springframework.boot",
"war",
]
.iter()
.any(|k| script.contains(k));
if jvm {
for t in ["assemble", "build", "buildDependents", "buildNeeded", "classes", "clean", "jar", "testClasses"] {
out.push(task("build", t));
}
out.extend([task("verification", "check"), task("verification", "test"), task("documentation", "javadoc")]);
}
if script.contains("application") {
out.push(task("application", "run"));
}
if script.contains("org.springframework.boot") {
out.extend([task("application", "bootRun"), task("build", "bootJar")]);
}
out
}
/// Reads the Gradle build under `root` (no Gradle run), or `None` when there is none.
pub fn load(root: &Path) -> Option<GradleModel> {
let dir = build_dir(root)?;
let read = |d: &Path, names: &[&str]| {
names.iter().find_map(|n| std::fs::read_to_string(d.join(n)).ok().map(|t| (n.to_string(), t)))
};
let (name, includes) = read(&dir, SETTINGS_FILES).map(|(_, t)| parse_settings(&t)).unwrap_or_default();
let root_name =
name.unwrap_or_else(|| dir.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default());
let mut projects = Vec::new();
let mut add = |path: String, name: String, pdir: PathBuf| {
let script = read(&pdir, BUILD_FILES);
let tasks = default_tasks(path == ":", script.as_ref().map(|(_, t)| t.as_str()).unwrap_or_default());
projects.push(GradleProject {
build_file: script.map(|(n, _)| rel(root, &pdir.join(n))),
dir: rel(root, &pdir),
path,
name,
tasks,
});
};
add(":".into(), root_name, dir.clone());
for path in includes {
let pdir = dir.join(path.trim_start_matches(':').replace(':', "/"));
let name = path.rsplit(':').next().unwrap_or_default().to_owned();
add(path, name, pdir);
}
let java_version = projects
.iter()
.filter_map(|p| p.build_file.as_ref())
.filter_map(|f| std::fs::read_to_string(root.join(f)).ok())
.find_map(|t| script_java_version(&strip_comments(&t)));
Some(GradleModel { dir: rel(root, &dir), projects, java_version, command: wrapper(&dir), ..GradleModel::default() })
}
/// `JavaLanguageVersion.of(21)` / `sourceCompatibility = JavaVersion.VERSION_17` in a build script.
pub fn script_java_version(text: &str) -> Option<u32> {
let number = |s: &str| -> Option<u32> {
let s = s.trim_start_matches([' ', '=', '\t', '(', '"', '\'']);
let s = s.strip_prefix("JavaVersion.VERSION_").unwrap_or(s);
let s = s.strip_prefix("1_").or_else(|| s.strip_prefix("1.")).unwrap_or(s);
s.chars().take_while(char::is_ascii_digit).collect::<String>().parse().ok()
};
for key in ["JavaLanguageVersion.of(", "sourceCompatibility", "jvmToolchain("] {
if let Some(v) = text.find(key).and_then(|i| number(&text[i + key.len()..])) {
return Some(v);
}
}
None
}
/// Configurations whose dependencies are libraries (`testFixturesImplementation` ends in one).
const DEPENDENCY_CONFIGURATIONS: &[&str] = &[
"implementation",
"api",
"compileOnly",
"runtimeOnly",
"compileOnlyApi",
"annotationProcessor",
"kapt",
"ksp",
"compile",
"runtime",
"testCompile",
"testRuntime",
"developmentOnly",
];
fn is_dependency_configuration(name: &str) -> bool {
DEPENDENCY_CONFIGURATIONS.iter().any(|c| {
name == *c || {
let mut cap = c.chars();
let first = cap.next().map(|f| f.to_ascii_uppercase()).into_iter();
let capitalised: String = first.chain(cap).collect();
name.len() > c.len() && name.ends_with(&capitalised)
}
})
}
/// `(group, artifact, version)` of the external dependencies a build script declares:
/// `implementation 'g:a:v'`, `api("g:a:v:classifier@jar")`, and the map notation
/// `implementation group: 'g', name: 'a', version: 'v'` (Groovy) / `group = "g", …` (Kotlin).
/// Versions with interpolations, ranges and catalog aliases (`libs.x`) are skipped.
pub fn script_dependencies(text: &str) -> Vec<(String, String, String)> {
let text = strip_comments(text);
let mut out = Vec::new();
for line in text.lines().flat_map(|l| l.split(['{', ';'])) {
let line = line.trim_start();
let name: String = line.chars().take_while(|c| c.is_ascii_alphanumeric() || *c == '_').collect();
if name.is_empty() || !is_dependency_configuration(&name) {
continue;
}
let rest = &line[name.len()..];
let lits = literals(rest);
let map_value = |key: &str| -> Option<String> {
let i = rest.find(key)?;
let after = rest[i + key.len()..].trim_start().strip_prefix([':', '='])?;
literals(after).into_iter().next()
};
let coordinate = match (map_value("group"), map_value("name"), map_value("version")) {
(Some(g), Some(a), Some(v)) => Some((g, a, v)),
_ => lits.first().and_then(|l| {
let l = l.split('@').next().unwrap_or(l);
let mut parts = l.split(':');
Some((parts.next()?.to_owned(), parts.next()?.to_owned(), parts.next()?.to_owned()))
}),
};
if let Some((g, a, v)) = coordinate {
let plain = |s: &str| {
!s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_' | '+'))
};
if plain(&g) && plain(&a) && plain(&v) && !v.contains('+') {
out.push((g, a, v));
}
}
}
out
}
/// Every settings and build script of the model (for change detection).
pub fn build_files(root: &Path, model: &GradleModel) -> Vec<PathBuf> {
let dir = root.join(&model.dir);
let mut out: Vec<PathBuf> = SETTINGS_FILES.iter().map(|f| dir.join(f)).filter(|p| p.is_file()).collect();
out.extend(model.projects.iter().filter_map(|p| p.build_file.as_ref().map(|f| root.join(f))));
out
}
/// `(project path, task)` from `gradle tasks --all` console output; tasks listed as
/// `app:run` belong to `:app`, unqualified ones to the root project `:`.
pub fn parse_tasks(output: &str) -> Vec<(String, GradleTask)> {
let lines: Vec<&str> = output.lines().collect();
let mut out = Vec::new();
let mut group: Option<String> = None;
let mut i = 0;
while i < lines.len() {
let line = lines[i].trim_end();
let underline = lines.get(i + 1).map(|l| l.trim_end()).unwrap_or_default();
if !line.is_empty() && underline.len() >= 3 && underline.chars().all(|c| c == '-') && !line.starts_with('-') {
group = line.strip_suffix(" tasks").map(str::to_lowercase);
i += 2;
continue;
}
if line.is_empty() {
group = None;
i += 1;
continue;
}
if let Some(g) = &group {
let (name, description) = match line.split_once(" - ") {
Some((n, d)) => (n.trim(), Some(d.trim().to_owned())),
None => (line.trim(), None),
};
if !name.is_empty() && !name.contains(char::is_whitespace) {
let (project, task_name) = match name.rsplit_once(':') {
Some((p, t)) => (format!(":{}", p.trim_start_matches(':')), t),
None => (":".to_owned(), name),
};
out.push((project, GradleTask { name: task_name.to_owned(), group: g.clone(), description }));
}
}
i += 1;
}
out
}
#[cfg(test)]
mod tests {
#[test]
fn script_dependencies_read_string_and_map_notations() {
let script = r#"
plugins { id 'java' }
dependencies {
implementation 'com.google.guava:guava:33.0.0-jre'
api("org.slf4j:slf4j-api:2.0.9")
testImplementation "junit:junit:4.13.2@jar"
// implementation 'commented:out:1.0'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.30'
runtimeOnly(group = "com.h2database", name = "h2", version = "2.2.224")
implementation "org.x:dyn:${ver}"
implementation 'org.x:range:1.+'
implementation libs.okhttp
implementation project(':core')
classpath 'not:a:library'
}
"#;
let deps = script_dependencies(script);
let names: Vec<String> = deps.iter().map(|(g, a, v)| format!("{g}:{a}:{v}")).collect();
assert_eq!(
names,
[
"com.google.guava:guava:33.0.0-jre",
"org.slf4j:slf4j-api:2.0.9",
"junit:junit:4.13.2",
"org.projectlombok:lombok:1.18.30",
"com.h2database:h2:2.2.224",
]
);
}
use super::*;
#[test]
fn settings_scripts() {
let kts = r#"
// include("commented")
rootProject.name = "gradle-demo"
include("app", "lib")
include(":libs:core")
/* include("block") */
includeBuild("plugins")
"#;
let (name, includes) = parse_settings(kts);
assert_eq!(name.as_deref(), Some("gradle-demo"));
assert_eq!(includes, [":app", ":lib", ":libs:core"]);
let groovy = "rootProject.name = 'demo'\ninclude 'a',\n 'b'\ninclude ':c'\nprintln 'x'\n";
let (name, includes) = parse_settings(groovy);
assert_eq!(name.as_deref(), Some("demo"));
assert_eq!(includes, [":a", ":b", ":c"]);
}
#[test]
fn task_list_output() {
let out = "\
------------------------------------------------------------
Tasks runnable from root project 'gradle-demo'
------------------------------------------------------------
Application tasks
-----------------
app:run - Runs this project as a JVM application
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Other tasks
-----------
app:compileJava - Compiles main Java source.
prepareKotlinBuildScriptModel
libs:core:jar
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
";
let tasks = parse_tasks(out);
let flat: Vec<(&str, &str, &str, Option<&str>)> = tasks
.iter()
.map(|(p, t)| (p.as_str(), t.group.as_str(), t.name.as_str(), t.description.as_deref()))
.collect();
assert_eq!(
flat,
[
(":app", "application", "run", Some("Runs this project as a JVM application")),
(":", "build setup", "init", Some("Initializes a new Gradle build.")),
(":", "build setup", "wrapper", Some("Generates Gradle wrapper files.")),
(":app", "other", "compileJava", Some("Compiles main Java source.")),
(":", "other", "prepareKotlinBuildScriptModel", None),
(":libs:core", "other", "jar", None),
]
);
}
#[test]
fn loads_projects_and_default_tasks() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::write(root.join("settings.gradle.kts"), "rootProject.name = \"demo\"\ninclude(\"app\")").unwrap();
std::fs::create_dir_all(root.join("app")).unwrap();
std::fs::write(root.join("app/build.gradle.kts"), "plugins { application }").unwrap();
let wrapper = if cfg!(windows) { "gradlew.bat" } else { "gradlew" };
std::fs::write(root.join(wrapper), "#!/bin/sh").unwrap();
let m = load(root).unwrap();
assert_eq!(m.dir, "");
let ps: Vec<(&str, &str, &str, Option<&str>)> = m
.projects
.iter()
.map(|p| (p.path.as_str(), p.name.as_str(), p.dir.as_str(), p.build_file.as_deref()))
.collect();
assert_eq!(ps, [(":", "demo", "", None), (":app", "app", "app", Some("app/build.gradle.kts"))]);
assert!(m.projects[0].tasks.iter().any(|t| t.name == "wrapper"));
assert!(!m.projects[0].tasks.iter().any(|t| t.name == "build"));
assert!(m.projects[1].tasks.iter().any(|t| t.name == "run" && t.group == "application"));
assert!(m.command.ends_with(wrapper));
assert_eq!(build_files(root, &m).len(), 2);
assert_eq!(m.java_version, None);
assert_eq!(
script_java_version("java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }"),
Some(21)
);
assert_eq!(script_java_version("sourceCompatibility = JavaVersion.VERSION_1_8"), Some(8));
assert_eq!(script_java_version("kotlin { jvmToolchain(17) }"), Some(17));
assert_eq!(script_java_version("sourceCompatibility = '11'"), Some(11));
let none = tempfile::tempdir().unwrap();
assert!(load(none.path()).is_none());
}
}
+187
Переглянути файл
@@ -0,0 +1,187 @@
//! Build tool models for the Maven and Gradle tool windows, and Build › Build
//! Project ([`compile`]) for Maven, Gradle, Cargo, Go and TypeScript projects.
//!
//! [`load`] reads the project's build files (instant, no build tool involved)
//! and merges what the last sync ([`sync_maven`] / [`sync_gradle`], which run
//! Maven / Gradle in the background) cached for the project.
pub mod compile;
pub mod gradle;
pub mod maven;
pub mod pom_edit;
mod process;
mod sync;
mod xml;
pub use gradle::{GradleModel, GradleProject, GradleTask};
pub use maven::{MavenDependency, MavenModel, MavenPlugin, MavenProfile, MavenProject, Omission};
pub use sync::{SyncOptions, SyncProgress, sync_gradle, sync_maven};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::path::{Path, PathBuf};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum BuildTool {
Maven,
Gradle,
}
/// The build systems of a project; `None` when the project does not use one.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct BuildModel {
pub maven: Option<MavenModel>,
pub gradle: Option<GradleModel>,
}
/// Static model of the builds under `root`, merged with the sync results cached in `cache_dir`.
/// Parent POMs are looked up in `maven_repo` (the configured local repository), else the default one.
pub fn load(root: &Path, cache_dir: &Path, maven_repo: Option<PathBuf>) -> BuildModel {
let maven = maven::is_maven_project(root).then(|| {
let mut m = maven::load_with_repo(root, maven_repo.or_else(maven::default_local_repo));
sync::apply_maven_cache(root, cache_dir, &mut m);
m
});
let gradle = gradle::load(root).map(|mut g| {
sync::apply_gradle_cache(root, cache_dir, &mut g);
g
});
BuildModel { maven, gradle }
}
/// A library the project depends on, as `groupId:artifactId:version`.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DependencyCoordinate {
/// Gradle builds resolve into the Gradle cache, Maven ones into the local repository.
pub gradle: bool,
pub group_id: String,
pub artifact_id: String,
pub version: String,
}
/// The external libraries of `model`, sorted and deduplicated: Maven dependencies
/// (with the transitive ones the last sync read), except the build's own modules,
/// `pom` imports and `system` scope; dependencies declared in the Gradle build scripts.
pub fn dependency_coordinates(root: &Path, model: &BuildModel) -> Vec<DependencyCoordinate> {
let mut out = Vec::new();
if let Some(m) = &model.maven {
let modules: std::collections::HashSet<String> = m.projects.iter().map(MavenProject::key).collect();
let mut stack: Vec<&MavenDependency> = m.projects.iter().flat_map(|p| &p.dependencies).collect();
while let Some(d) = stack.pop() {
// Omitted occurrences (conflict losers, duplicates) are not on any classpath.
if !d.resolved() {
continue;
}
stack.extend(&d.children);
let Some(version) = d.version.as_deref().filter(|v| !v.is_empty() && !v.contains("${")) else {
continue;
};
if d.kind == "pom" || matches!(d.scope.as_str(), "import" | "system") {
continue;
}
if modules.contains(&format!("{}:{}", d.group_id, d.artifact_id)) {
continue;
}
out.push(DependencyCoordinate {
gradle: false,
group_id: d.group_id.clone(),
artifact_id: d.artifact_id.clone(),
version: version.to_owned(),
});
}
}
if let Some(g) = &model.gradle {
for script in g.projects.iter().filter_map(|p| p.build_file.as_ref()) {
let Ok(text) = std::fs::read_to_string(root.join(script)) else { continue };
out.extend(gradle::script_dependencies(&text).into_iter().map(|(group_id, artifact_id, version)| {
DependencyCoordinate { gradle: true, group_id, artifact_id, version }
}));
}
}
out.sort();
out.dedup();
out
}
/// Whether a created / changed / removed project-relative path can change the build model.
pub fn affects_build(rel: &Path) -> bool {
let Some(name) = rel.file_name().and_then(|n| n.to_str()) else { return false };
name == "pom.xml"
|| gradle::SETTINGS_FILES.contains(&name)
|| gradle::BUILD_FILES.contains(&name)
|| matches!(name, "mvnw" | "mvnw.cmd" | "gradlew" | "gradlew.bat")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_build_systems_and_build_files() {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().join("cache");
let root = tmp.path().join("p");
std::fs::create_dir_all(root.join("svc")).unwrap();
assert_eq!(load(&root, &cache, None), BuildModel::default());
std::fs::write(root.join("svc/pom.xml"), "<project><artifactId>svc</artifactId></project>").unwrap();
std::fs::write(root.join("build.gradle"), "").unwrap();
let m = load(&root, &cache, None);
assert_eq!(m.maven.unwrap().projects[0].pom, "svc/pom.xml");
assert_eq!(m.gradle.unwrap().projects[0].path, ":");
assert!(affects_build(Path::new("a/pom.xml")) && affects_build(Path::new("settings.gradle.kts")));
assert!(!affects_build(Path::new("src/Main.java")));
}
#[test]
fn dependency_coordinates_flatten_maven_trees_and_read_gradle_scripts() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::write(root.join("build.gradle"), "dependencies { implementation 'org.a:x:1.0' }\n").unwrap();
let dep = |g: &str, a: &str, v: Option<&str>, children: Vec<MavenDependency>| MavenDependency {
group_id: g.into(),
artifact_id: a.into(),
version: v.map(Into::into),
kind: "jar".into(),
scope: "compile".into(),
children,
..Default::default()
};
let maven = MavenModel {
projects: vec![
MavenProject {
group_id: "me".into(),
artifact_id: "app".into(),
dependencies: vec![
dep("me", "core", Some("1"), vec![]),
dep(
"org.b",
"y",
Some("2"),
vec![
dep("org.c", "z", Some("3"), vec![]),
MavenDependency {
omitted: Some(Omission::Conflict),
conflict_with: Some("3".into()),
..dep("org.c", "z", Some("1"), vec![])
},
],
),
dep("org.d", "unresolved", Some("${v}"), vec![]),
],
..Default::default()
},
MavenProject { group_id: "me".into(), artifact_id: "core".into(), ..Default::default() },
],
..Default::default()
};
let gradle = gradle::load(root);
let model = BuildModel { maven: Some(maven), gradle };
let names: Vec<String> = dependency_coordinates(root, &model)
.into_iter()
.map(|c| format!("{}{}:{}:{}", if c.gradle { "G " } else { "M " }, c.group_id, c.artifact_id, c.version))
.collect();
assert_eq!(names, ["M org.b:y:2", "M org.c:z:3", "G org.a:x:1.0"]);
}
}
File diff suppressed because it is too large. Load diff
+336
Переглянути файл
@@ -0,0 +1,336 @@
//! Edits of `pom.xml` text for the dependency analyzer: finding where a direct
//! dependency is declared and adding an `<exclusion>` to it, in the style
//! (indentation, line endings, one-line elements) of the surrounding POM.
use crate::maven::{self, MavenModel};
use crate::xml::{self, Element};
use ide_core::{Pos, Range, TextEdit};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::{HashMap, HashSet};
/// `groupId:artifactId` of a dependency.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct ArtifactKey {
pub group_id: String,
pub artifact_id: String,
}
impl ArtifactKey {
pub fn new(group_id: impl Into<String>, artifact_id: impl Into<String>) -> Self {
Self { group_id: group_id.into(), artifact_id: artifact_id.into() }
}
}
impl std::fmt::Display for ArtifactKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.group_id, self.artifact_id)
}
}
/// What adding an exclusion does to a POM.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Exclusion {
/// The edit that adds it.
Edit(TextEdit),
/// The dependency already excludes it (or everything, `*:*`).
Present,
}
/// Properties the coordinates of a POM's dependencies may refer to.
fn properties(project: &Element) -> HashMap<String, String> {
let mut props: HashMap<String, String> = project
.child("properties")
.map(|p| p.children.iter().map(|c| (c.name.clone(), c.text.trim().to_owned())).collect())
.unwrap_or_default();
let own_or_parent = |name: &str| project.text_of(name).or_else(|| project.child("parent")?.text_of(name));
for (key, value) in [
("project.groupId", own_or_parent("groupId")),
("project.version", own_or_parent("version")),
("project.artifactId", project.text_of("artifactId")),
] {
if let Some(v) = value {
props.insert(key.into(), v);
}
}
props
}
fn matches(e: &Element, key: &ArtifactKey, props: &HashMap<String, String>) -> bool {
let text = |name: &str| e.text_of(name).map(|t| maven::interpolate(&t, props));
text("groupId").as_deref() == Some(key.group_id.as_str())
&& text("artifactId").as_deref() == Some(key.artifact_id.as_str())
}
/// The `<dependency>` declaring `key` in the project's `<dependencies>`, else in a
/// profile's (never `<dependencyManagement>`, which declares no dependency).
fn find_dependency<'a>(project: &'a Element, key: &ArtifactKey) -> Option<&'a Element> {
let props = properties(project);
let own = project.list("dependencies", "dependency");
let profiles = project.list("profiles", "profile").into_iter().flat_map(|p| p.list("dependencies", "dependency"));
own.into_iter().chain(profiles).find(|d| matches(d, key, &props))
}
/// 0-based line / UTF-16 column of byte offset `at`.
fn pos_at(text: &str, at: usize) -> Pos {
let before = &text[..at];
let line_start = before.rfind('\n').map_or(0, |i| i + 1);
Pos::new(before.matches('\n').count() as u32, before[line_start..].encode_utf16().count() as u32)
}
fn line_start(text: &str, at: usize) -> usize {
text[..at].rfind('\n').map_or(0, |i| i + 1)
}
/// The whitespace before `at` on its line, or `None` when other text precedes it there.
fn indent_before(text: &str, at: usize) -> Option<&str> {
let ws = &text[line_start(text, at)..at];
ws.chars().all(|c| c == ' ' || c == '\t').then_some(ws)
}
/// One level of indentation: what the first child of `e` adds to `e`'s, else two spaces.
fn indent_unit(text: &str, e: &Element) -> String {
let outer = indent_before(text, e.start);
let inner = e.children.first().and_then(|c| indent_before(text, c.start));
match (outer, inner) {
(Some(o), Some(i)) if i.len() > o.len() && i.starts_with(o) => i[o.len()..].to_owned(),
(_, Some(i)) if i.starts_with('\t') => "\t".into(),
_ => " ".into(),
}
}
fn escape(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;")
}
/// Range of the `<artifactId>` of the dependency declaring `key` in `text` (a POM), or of
/// the whole `<dependency>` when it has none; `None` when the POM does not declare it.
pub fn declaration(text: &str, key: &ArtifactKey) -> Option<Range> {
let project = xml::parse(text)?;
let dep = find_dependency(&project, key)?;
let target = dep.child("artifactId").unwrap_or(dep);
Some(Range::new(pos_at(text, target.start), pos_at(text, target.end)))
}
/// Adds `exclude` to the `<exclusions>` of the dependency declaring `dependency` in `text`
/// (a POM); `None` when the POM does not declare it.
pub fn add_exclusion(text: &str, dependency: &ArtifactKey, exclude: &ArtifactKey) -> Option<Exclusion> {
let project = xml::parse(text)?;
let dep = find_dependency(&project, dependency)?;
let nl = if text.contains("\r\n") { "\r\n" } else { "\n" };
let unit = indent_unit(text, dep);
let (g, a) = (escape(&exclude.group_id), escape(&exclude.artifact_id));
// Insert `block` (lines relative to `parent`'s indentation + one level) before `parent`'s
// end tag; on one line (`compact`) when `parent` is not laid out one element per line.
let insert = |parent: &Element, lines: &[(usize, String)], compact: String| {
let close_on_own_line = indent_before(text, parent.inner_end).is_some() && parent.inner_end < parent.end;
let (at, new_text) = match indent_before(text, parent.start) {
Some(base) if close_on_own_line => {
let mut out = String::new();
for (depth, line) in lines {
out.push_str(base);
out.push_str(&unit.repeat(depth + 1));
out.push_str(line);
out.push_str(nl);
}
(line_start(text, parent.inner_end), out)
}
_ => (parent.inner_end, compact),
};
let p = pos_at(text, at);
Exclusion::Edit(TextEdit { range: Range::point(p), new_text })
};
let exclusion_lines = |depth: usize| {
vec![
(depth, "<exclusion>".to_owned()),
(depth + 1, format!("<groupId>{g}</groupId>")),
(depth + 1, format!("<artifactId>{a}</artifactId>")),
(depth, "</exclusion>".to_owned()),
]
};
let compact = format!("<exclusion><groupId>{g}</groupId><artifactId>{a}</artifactId></exclusion>");
if let Some(exclusions) = dep.child("exclusions") {
let props = properties(&project);
let wildcard = ArtifactKey::new("*", "*");
let any_artifact = ArtifactKey::new(exclude.group_id.clone(), "*");
if exclusions
.children_named("exclusion")
.any(|e| [exclude, &wildcard, &any_artifact].iter().any(|k| matches(e, k, &props)))
{
return Some(Exclusion::Present);
}
return Some(insert(exclusions, &exclusion_lines(0), compact));
}
let mut lines = vec![(0, "<exclusions>".to_owned())];
lines.extend(exclusion_lines(1));
lines.push((0, "</exclusions>".to_owned()));
Some(insert(dep, &lines, format!("<exclusions>{compact}</exclusions>")))
}
/// The POMs that may declare a dependency of the module `pom` (project-relative), nearest
/// first: the module's own, then its parents that are part of the project.
pub fn declaring_poms(model: &MavenModel, pom: &str) -> Vec<String> {
let mut out: Vec<String> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
let mut current = model.projects.iter().find(|p| p.pom == pom);
while let Some(p) = current {
if !seen.insert(p.pom.clone()) {
break;
}
out.push(p.pom.clone());
current = p.parent.as_deref().and_then(|coords| {
model.projects.iter().find(|q| format!("{}:{}:{}", q.group_id, q.artifact_id, q.version) == coords)
});
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn apply(text: &str, edit: &TextEdit) -> String {
let offset = |p: Pos| {
let start: usize = text.split_inclusive('\n').take(p.line as usize).map(str::len).sum();
let line = &text[start..];
start + line.char_indices().nth(p.col as usize).map_or(line.len(), |(i, _)| i)
};
let (from, to) = (offset(edit.range.start), offset(edit.range.end));
format!("{}{}{}", &text[..from], edit.new_text, &text[to..])
}
fn exclude(text: &str, dep: (&str, &str), excl: (&str, &str)) -> Option<String> {
match add_exclusion(text, &ArtifactKey::new(dep.0, dep.1), &ArtifactKey::new(excl.0, excl.1))? {
Exclusion::Edit(e) => Some(apply(text, &e)),
Exclusion::Present => Some(text.to_owned()),
}
}
const POM: &str = "<project>
<groupId>demo</groupId>
<properties>
<guice.group>com.google.inject</guice.group>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>${guice.group}</groupId>
<artifactId>guice</artifactId>
</dependency>
</dependencies>
</project>
";
#[test]
fn adds_exclusions_to_the_declaration_not_the_managed_entry() {
let out = exclude(POM, ("com.google.inject", "guice"), ("com.google.guava", "guava")).unwrap();
let expected = POM.replace(
" <artifactId>guice</artifactId>\n </dependency>\n </dependencies>",
" <artifactId>guice</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>",
);
assert_eq!(out, expected);
// A second exclusion joins the first; excluding it again changes nothing.
let twice = exclude(&out, ("com.google.inject", "guice"), ("aopalliance", "aopalliance")).unwrap();
assert!(twice.contains(
" </exclusion>
<exclusion>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</exclusion>
</exclusions>"
));
assert_eq!(exclude(&twice, ("com.google.inject", "guice"), ("aopalliance", "aopalliance")).unwrap(), twice);
}
#[test]
fn keeps_tabs_crlf_and_one_line_dependencies() {
let tabs = "<project>\r\n\t<dependencies>\r\n\t\t<dependency>\r\n\t\t\t<groupId>g</groupId>\r\n\t\t\t<artifactId>a</artifactId>\r\n\t\t</dependency>\r\n\t</dependencies>\r\n</project>\r\n";
let out = exclude(tabs, ("g", "a"), ("x", "y")).unwrap();
assert!(out.contains("<artifactId>a</artifactId>\r\n\t\t\t<exclusions>\r\n\t\t\t\t<exclusion>\r\n\t\t\t\t\t<groupId>x</groupId>\r\n"));
assert!(out.contains("\t\t\t</exclusions>\r\n\t\t</dependency>"));
let inline = "<project><dependencies><dependency><groupId>g</groupId><artifactId>a</artifactId></dependency></dependencies></project>";
assert_eq!(
exclude(inline, ("g", "a"), ("x", "y")).unwrap(),
"<project><dependencies><dependency><groupId>g</groupId><artifactId>a</artifactId><exclusions><exclusion><groupId>x</groupId><artifactId>y</artifactId></exclusion></exclusions></dependency></dependencies></project>"
);
}
#[test]
fn wildcards_profiles_and_missing_declarations() {
let pom = "<project>
<profiles>
<profile>
<dependencies>
<dependency>
<groupId>g</groupId><artifactId>a</artifactId>
<exclusions><exclusion><groupId>x</groupId><artifactId>*</artifactId></exclusion></exclusions>
</dependency>
</dependencies>
</profile>
</profiles>
</project>";
let key = ArtifactKey::new("g", "a");
assert_eq!(add_exclusion(pom, &key, &ArtifactKey::new("x", "y")), Some(Exclusion::Present));
let out = exclude(pom, ("g", "a"), ("z", "w")).unwrap();
assert!(out.contains(
"</exclusion><exclusion><groupId>z</groupId><artifactId>w</artifactId></exclusion></exclusions>"
));
assert_eq!(add_exclusion(pom, &ArtifactKey::new("g", "other"), &key), None);
assert_eq!(add_exclusion("not xml", &key, &key), None);
}
#[test]
fn declaration_ranges_use_utf16_columns() {
let pom = "<project><!-- é😀 --><dependencies>\n <dependency><groupId>g</groupId><artifactId>a</artifactId></dependency>\n</dependencies></project>";
let r = declaration(pom, &ArtifactKey::new("g", "a")).unwrap();
assert_eq!(r, Range::new(Pos::new(1, 34), Pos::new(1, 60)));
let first = "<project><!-- é😀 --><dependencies><dependency><groupId>g</groupId><artifactId>a</artifactId>";
let r = declaration(&format!("{first}</dependency></dependencies></project>"), &ArtifactKey::new("g", "a"))
.unwrap();
let start = first.find("<artifactId>").unwrap();
assert_eq!(r.start.col as usize, first[..start].encode_utf16().count());
}
#[test]
fn declaring_poms_follow_parents_inside_the_project() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let write = |rel: &str, text: &str| {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, text).unwrap();
};
write(
"pom.xml",
"<project><groupId>d</groupId><artifactId>parent</artifactId><version>1</version><packaging>pom</packaging><modules><module>app</module></modules></project>",
);
write(
"app/pom.xml",
"<project><parent><groupId>d</groupId><artifactId>parent</artifactId><version>1</version></parent><artifactId>app</artifactId></project>",
);
let model = maven::load_with_repo(root, None);
assert_eq!(declaring_poms(&model, "app/pom.xml"), ["app/pom.xml", "pom.xml"]);
assert_eq!(declaring_poms(&model, "pom.xml"), ["pom.xml"]);
assert!(declaring_poms(&model, "missing/pom.xml").is_empty());
}
}
+155
Переглянути файл
@@ -0,0 +1,155 @@
//! Line-oriented child processes for syncs and builds: output is fed line by
//! line to a callback, and cancellation kills the whole process group (the
//! `mvnw` / `gradlew` wrapper scripts start the JVM as a child).
use ide_core::{CancellationToken, IdeError, IdeResult};
use std::collections::BTreeMap;
use std::io::{BufRead, BufReader, Read};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::sync::mpsc;
use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Stream {
Stdout,
Stderr,
}
pub(crate) struct Output {
pub success: bool,
/// Every line, stdout and stderr interleaved as received.
pub text: String,
}
fn kill_tree(child: &mut Child) {
#[cfg(unix)]
if let Ok(pid) = i32::try_from(child.id()) {
// SAFETY: plain syscall; the group was created by `process_group(0)` at spawn.
unsafe {
libc::kill(-pid, libc::SIGKILL);
}
}
let _ = child.kill();
let _ = child.wait();
}
/// Runs `program args` in `cwd`, feeding each output line to `on_line`; kills
/// the process group and returns `cancelled` when `cancel` fires.
pub(crate) fn run(
program: &str,
args: &[String],
cwd: &Path,
env: &BTreeMap<String, String>,
cancel: &CancellationToken,
mut on_line: impl FnMut(Stream, &str),
) -> IdeResult<Output> {
tracing::info!("build: {program} {} (in {})", args.join(" "), cwd.display());
let mut command = Command::new(program);
command.args(args).current_dir(cwd).envs(env).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped());
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
command.process_group(0);
}
ide_core::process::no_window(&mut command);
let mut child = command.spawn().map_err(|e| IdeError::unavailable(format!("cannot start {program}: {e}")))?;
let (tx, rx) = mpsc::channel::<(Stream, String)>();
let pump = |stream: Box<dyn Read + Send>, kind: Stream, tx: mpsc::Sender<(Stream, String)>| {
std::thread::spawn(move || {
let mut reader = BufReader::new(stream);
let mut buf = Vec::new();
loop {
buf.clear();
match reader.read_until(b'\n', &mut buf) {
Ok(0) | Err(_) => break,
Ok(_) => {
let line = String::from_utf8_lossy(&buf);
let line = line.trim_end_matches(['\n', '\r']).to_owned();
if tx.send((kind, line)).is_err() {
break;
}
}
}
}
})
};
let readers = [
child.stdout.take().map(|s| pump(Box::new(s), Stream::Stdout, tx.clone())),
child.stderr.take().map(|s| pump(Box::new(s), Stream::Stderr, tx.clone())),
];
drop(tx);
let mut text = String::new();
loop {
if cancel.is_cancelled() {
kill_tree(&mut child);
return Err(IdeError::cancelled());
}
match rx.recv_timeout(Duration::from_millis(100)) {
Ok((stream, line)) => {
on_line(stream, &line);
text.push_str(&line);
text.push('\n');
}
Err(mpsc::RecvTimeoutError::Timeout) => {}
Err(mpsc::RecvTimeoutError::Disconnected) => break,
}
}
for r in readers.into_iter().flatten() {
let _ = r.join();
}
let status = child.wait()?;
Ok(Output { success: status.success(), text })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn runs_and_cancels_processes() {
if cfg!(windows) {
return;
}
let tmp = tempfile::tempdir().unwrap();
let cancel = CancellationToken::new();
let mut lines = Vec::new();
let out = run(
"sh",
&["-c".into(), "echo one; echo two >&2; exit 3".into()],
tmp.path(),
&BTreeMap::new(),
&cancel,
|s, l| lines.push((s, l.to_owned())),
)
.unwrap();
assert!(!out.success);
lines.sort_by(|a, b| a.1.cmp(&b.1));
assert_eq!(lines, [(Stream::Stdout, "one".to_owned()), (Stream::Stderr, "two".to_owned())]);
cancel.cancel();
let err = run("sh", &["-c".into(), "sleep 5".into()], tmp.path(), &BTreeMap::new(), &cancel, |_, _| {})
.err()
.unwrap();
assert_eq!(err.kind, ide_core::error::ErrorKind::Cancelled);
}
#[test]
fn cancel_kills_grandchildren() {
if cfg!(windows) {
return;
}
let tmp = tempfile::tempdir().unwrap();
let cancel = CancellationToken::new();
let marker = tmp.path().join("survived");
let script = format!("(sleep 1; touch '{}') & wait", marker.display());
let c = cancel.clone();
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(200));
c.cancel();
});
let err = run("sh", &["-c".into(), script], tmp.path(), &BTreeMap::new(), &cancel, |_, _| {}).err().unwrap();
assert_eq!(err.kind, ide_core::error::ErrorKind::Cancelled);
std::thread::sleep(Duration::from_millis(1300));
assert!(!marker.exists(), "the background child must be killed with the group");
}
}
+378
Переглянути файл
@@ -0,0 +1,378 @@
//! Reading the effective build from Maven / Gradle itself ("Reload" in the tool
//! windows): `mvn help:effective-pom dependency:tree -Dverbose` and `gradle tasks --all`,
//! run in the background with progress and cancellation. Results are cached
//! per project (`maven.json`, `gradle.json` in the cache directory) and merged
//! into the static model by [`crate::load`]; a changed build file marks them stale.
use crate::gradle::{self, GradleModel, GradleTask};
use crate::maven::{self, MavenDependency, MavenModel, MavenPlugin};
use crate::process::{self, Output};
use ide_core::{CancellationToken, IdeError, IdeResult};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use specta::Type;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct SyncProgress {
pub message: String,
/// 0..1, or `None` when indeterminate.
pub fraction: Option<f32>,
}
#[derive(Clone, Debug, Default)]
pub struct SyncOptions {
/// `-o` / `--offline`.
pub offline: bool,
/// Maven profiles (`-P`), `!id` deactivates.
pub profiles: Vec<String>,
/// Extra environment (`JAVA_HOME`).
pub env: BTreeMap<String, String>,
/// Maven launcher replacing the project's `mvnw` / `mvn` (Settings › Maven home).
pub maven_command: Option<String>,
/// Maven options before the goals (`-s`, `-Dmaven.repo.local=`).
pub maven_options: Vec<String>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
struct CachedMavenProject {
key: String,
plugins: Vec<MavenPlugin>,
/// Effective direct dependencies, with transitive ones when `dependency:tree` ran.
dependencies: Vec<MavenDependency>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
struct MavenCache {
hash: String,
at_ms: f64,
error: Option<String>,
projects: Vec<CachedMavenProject>,
/// The trees list omitted occurrences (`-Dverbose`); older caches are reloaded.
#[serde(default)]
verbose: bool,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
struct GradleCache {
hash: String,
at_ms: f64,
error: Option<String>,
tasks: Vec<(String, GradleTask)>,
}
const MAVEN_CACHE: &str = "maven.json";
/// `dependency:tree` of a plugin version whose `-Dverbose` lists the occurrences Maven
/// omitted (conflicts, duplicates): verbose output was dropped in 3.0 and is back since 3.2.
/// Offline, the project's own plugin version is used instead, since this one may not be cached.
const DEPENDENCY_TREE: &str = "org.apache.maven.plugins:maven-dependency-plugin:3.8.1:tree";
/// The goals that read the effective POM into `effective` and the verbose dependency trees.
fn maven_sync_goals(effective: &Path, offline: bool) -> Vec<String> {
vec![
"help:effective-pom".into(),
format!("-Doutput={}", effective.to_string_lossy()),
if offline { "dependency:tree".into() } else { DEPENDENCY_TREE.into() },
"-Dverbose".into(),
]
}
const GRADLE_CACHE: &str = "gradle.json";
fn read_cache<T: DeserializeOwned>(dir: &Path, name: &str) -> Option<T> {
serde_json::from_slice(&std::fs::read(dir.join(name)).ok()?).ok()
}
fn write_cache<T: Serialize>(dir: &Path, name: &str, value: &T) -> IdeResult<()> {
std::fs::create_dir_all(dir)?;
let json = serde_json::to_vec(value).map_err(|e| IdeError::internal(e.to_string()))?;
let tmp = dir.join(format!("{name}.tmp"));
std::fs::write(&tmp, json)?;
std::fs::rename(&tmp, dir.join(name))?;
Ok(())
}
/// FNV-1a over the paths and contents of `files` (missing files count as empty).
fn hash_files(files: &[PathBuf]) -> String {
let mut h: u64 = 0xcbf29ce484222325;
let mut feed = |bytes: &[u8]| {
for b in bytes {
h ^= u64::from(*b);
h = h.wrapping_mul(0x100000001b3);
}
};
for f in files {
feed(f.to_string_lossy().as_bytes());
feed(&[0]);
feed(&std::fs::read(f).unwrap_or_default());
feed(&[0]);
}
format!("{h:016x}")
}
fn now_ms() -> f64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |d| d.as_millis() as f64)
}
/// Merges the cached effective build into `model`.
pub fn apply_maven_cache(root: &Path, cache_dir: &Path, model: &mut MavenModel) {
let Some(cache) = read_cache::<MavenCache>(cache_dir, MAVEN_CACHE) else { return };
for p in &mut model.projects {
if let Some(c) = cache.projects.iter().find(|c| c.key == p.key()) {
if !c.plugins.is_empty() {
p.plugins = c.plugins.clone();
}
p.dependencies = c.dependencies.clone();
}
}
model.synced_at = Some(cache.at_ms);
model.sync_error = cache.error;
model.stale = !cache.verbose || cache.hash != hash_files(&maven::build_files(root, model));
}
/// Merges the cached task list into `model`.
pub fn apply_gradle_cache(root: &Path, cache_dir: &Path, model: &mut GradleModel) {
let Some(cache) = read_cache::<GradleCache>(cache_dir, GRADLE_CACHE) else { return };
if !cache.tasks.is_empty() {
for p in &mut model.projects {
let tasks: Vec<GradleTask> =
cache.tasks.iter().filter(|(path, _)| *path == p.path).map(|(_, t)| t.clone()).collect();
if !tasks.is_empty() {
p.tasks = tasks;
}
}
}
model.synced_at = Some(cache.at_ms);
model.sync_error = cache.error;
model.stale = cache.hash != hash_files(&gradle::build_files(root, model));
}
// ---------------------------------------------------------------- processes
fn run(
program: &str,
args: &[String],
cwd: &Path,
env: &BTreeMap<String, String>,
cancel: &CancellationToken,
mut on_line: impl FnMut(&str),
) -> IdeResult<Output> {
process::run(program, args, cwd, env, cancel, |_, line| on_line(line))
}
/// The first error lines of a failed build, for the tool window.
pub(crate) fn error_summary(text: &str) -> String {
let lines: Vec<&str> = text
.lines()
.filter(|l| {
l.starts_with("[ERROR]")
|| l.starts_with("FAILURE:")
|| l.starts_with("* What went wrong")
|| l.starts_with("> ")
})
.map(|l| l.trim_start_matches("[ERROR]").trim())
.filter(|l| !l.is_empty() && !l.starts_with("Re-run Maven") && !l.starts_with("To see the full stack trace"))
.take(4)
.collect();
if lines.is_empty() {
text.lines().rev().find(|l| !l.trim().is_empty()).unwrap_or("the build failed").trim().to_owned()
} else {
lines.join("\n")
}
}
/// `[INFO] Building core 1.0-SNAPSHOT [2/3]` → ("core 1.0-SNAPSHOT", 2, 3).
pub(crate) fn maven_building(line: &str) -> Option<(String, u32, u32)> {
let body = line.strip_prefix("[INFO] Building ")?;
let open = body.rfind('[')?;
let (n, m) = body[open + 1..].trim_end_matches(']').split_once('/')?;
Some((body[..open].trim().to_owned(), n.trim().parse().ok()?, m.trim().parse().ok()?))
}
/// Runs Maven on every top-level project and caches effective plugins and dependency trees.
pub fn sync_maven(
root: &Path,
cache_dir: &Path,
options: &SyncOptions,
cancel: &CancellationToken,
progress: &dyn Fn(SyncProgress),
) -> IdeResult<()> {
let model = maven::load(root);
std::fs::create_dir_all(cache_dir)?;
let mut cache =
MavenCache { hash: hash_files(&maven::build_files(root, &model)), verbose: true, ..MavenCache::default() };
let mut errors: Vec<String> = Vec::new();
for (n, top) in model.projects.iter().filter(|p| !p.aggregated).enumerate() {
let effective = cache_dir.join(format!("effective-pom-{n}.xml"));
let _ = std::fs::remove_file(&effective);
let mut args: Vec<String> = vec!["-B".into(), "-fae".into(), "-Dstyle.color=never".into()];
args.extend(options.maven_options.iter().cloned());
if options.offline {
args.push("-o".into());
}
if !options.profiles.is_empty() {
args.push(format!("-P{}", options.profiles.join(",")));
}
args.extend(maven_sync_goals(&effective, options.offline));
progress(SyncProgress { message: format!("Reading {}", top.display_name), fraction: None });
let command = options.maven_command.as_deref().unwrap_or(&top.command);
let out = run(command, &args, &root.join(&top.dir), &options.env, cancel, |line| {
if let Some((name, i, total)) = maven_building(line) {
let fraction = (i.saturating_sub(1) as f32 / total.max(1) as f32).min(1.0);
progress(SyncProgress { message: format!("Building model of {name}"), fraction: Some(fraction) });
} else if let Some(url) =
line.strip_prefix("Downloading from ").and_then(|l| l.split_once(": ")).map(|(_, u)| u)
{
progress(SyncProgress {
message: format!("Downloading {}", url.rsplit('/').next().unwrap_or(url)),
fraction: None,
});
}
})?;
let effective_projects =
std::fs::read_to_string(&effective).map(|t| maven::parse_effective_pom(&t)).unwrap_or_default();
let trees = maven::parse_dependency_tree(&out.text);
for (key, plugins, deps) in effective_projects {
let dependencies = trees.iter().find(|(k, _)| *k == key).map(|(_, t)| t.clone()).unwrap_or(deps);
cache.projects.push(CachedMavenProject { key, plugins, dependencies });
}
// Modules whose effective POM could not be written still get their tree.
for (key, tree) in trees {
if !cache.projects.iter().any(|c| c.key == key) {
cache.projects.push(CachedMavenProject { key, plugins: Vec::new(), dependencies: tree });
}
}
if !out.success {
errors.push(error_summary(&out.text));
}
}
cache.at_ms = now_ms();
cache.error = (!errors.is_empty()).then(|| errors.join("\n"));
write_cache(cache_dir, MAVEN_CACHE, &cache)
}
/// Runs `gradle tasks --all` and caches the task list.
pub fn sync_gradle(
root: &Path,
cache_dir: &Path,
options: &SyncOptions,
cancel: &CancellationToken,
progress: &dyn Fn(SyncProgress),
) -> IdeResult<()> {
let model = gradle::load(root).ok_or_else(|| IdeError::not_found("no Gradle build in this project"))?;
let mut args: Vec<String> = vec!["--console=plain".into(), "-q".into(), "tasks".into(), "--all".into()];
if options.offline {
args.push("--offline".into());
}
progress(SyncProgress { message: "Loading Gradle tasks".into(), fraction: None });
let out = run(&model.command, &args, &root.join(&model.dir), &options.env, cancel, |_| {})?;
let cache = GradleCache {
hash: hash_files(&gradle::build_files(root, &model)),
at_ms: now_ms(),
error: (!out.success).then(|| error_summary(&out.text)),
tasks: if out.success { gradle::parse_tasks(&out.text) } else { Vec::new() },
};
write_cache(cache_dir, GRADLE_CACHE, &cache)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn caches_merge_and_go_stale() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("p");
let cache_dir = tmp.path().join("cache");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(root.join("pom.xml"), "<project><groupId>g</groupId><artifactId>a</artifactId></project>")
.unwrap();
let mut model = maven::load_with_repo(&root, None);
assert!(model.synced_at.is_none());
let cache = MavenCache {
hash: hash_files(&maven::build_files(&root, &model)),
at_ms: 5.0,
error: None,
verbose: true,
projects: vec![CachedMavenProject {
key: "g:a".into(),
plugins: vec![maven::plugin(
"org.apache.maven.plugins",
"maven-clean-plugin",
Some("3.2.0".into()),
&[],
)],
dependencies: vec![MavenDependency { artifact_id: "dep".into(), ..MavenDependency::default() }],
}],
};
write_cache(&cache_dir, MAVEN_CACHE, &cache).unwrap();
apply_maven_cache(&root, &cache_dir, &mut model);
assert_eq!(model.synced_at, Some(5.0));
assert!(!model.stale);
assert_eq!(model.projects[0].plugins.len(), 1);
assert_eq!(model.projects[0].dependencies[0].artifact_id, "dep");
std::fs::write(
root.join("pom.xml"),
"<project><groupId>g</groupId><artifactId>a</artifactId><version>2</version></project>",
)
.unwrap();
let mut model = maven::load_with_repo(&root, None);
apply_maven_cache(&root, &cache_dir, &mut model);
assert!(model.stale);
}
#[test]
fn gradle_cache_replaces_tasks_per_project() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("p");
let cache_dir = tmp.path().join("cache");
std::fs::create_dir_all(root.join("app")).unwrap();
std::fs::write(root.join("settings.gradle"), "include 'app'").unwrap();
std::fs::write(root.join("app/build.gradle"), "apply plugin: 'java'").unwrap();
let mut model = gradle::load(&root).unwrap();
let cache = GradleCache {
hash: hash_files(&gradle::build_files(&root, &model)),
at_ms: 1.0,
error: None,
tasks: vec![(":app".into(), GradleTask { name: "hello".into(), group: "demo".into(), description: None })],
};
write_cache(&cache_dir, GRADLE_CACHE, &cache).unwrap();
apply_gradle_cache(&root, &cache_dir, &mut model);
assert_eq!(model.projects[1].tasks.len(), 1);
assert!(model.projects[0].tasks.iter().any(|t| t.name == "wrapper"), "root keeps its defaults");
assert!(!model.stale);
}
#[test]
fn sync_reads_verbose_trees_with_a_pinned_plugin_unless_offline() {
let online = maven_sync_goals(Path::new("e.xml"), false);
assert_eq!(online, ["help:effective-pom", "-Doutput=e.xml", DEPENDENCY_TREE, "-Dverbose"]);
let offline = maven_sync_goals(Path::new("e.xml"), true);
assert_eq!(offline[2], "dependency:tree");
assert!(offline.contains(&"-Dverbose".to_owned()));
}
#[test]
fn cache_written_before_omissions_existed_still_loads() {
let json = r#"{"hash":"x","at_ms":1.0,"error":null,"projects":[{"key":"g:a","plugins":[],
"dependencies":[{"groupId":"g","artifactId":"d","version":"1","type":"jar","classifier":null,
"scope":"compile","optional":false,"children":[]}]}]}"#;
let cache: MavenCache = serde_json::from_str(json).unwrap();
assert!(cache.projects[0].dependencies[0].resolved());
assert!(!cache.verbose, "reloaded to read the omitted occurrences");
}
#[test]
fn progress_and_error_lines() {
assert_eq!(
maven_building("[INFO] Building core 1.0-SNAPSHOT [2/3]"),
Some(("core 1.0-SNAPSHOT".into(), 2, 3))
);
assert_eq!(maven_building("[INFO] Building jar: x.jar"), None);
let text = "[INFO] x\n[ERROR] Failed to execute goal\n[ERROR] \n[ERROR] Re-run Maven using -X\n";
assert_eq!(error_summary(text), "Failed to execute goal");
assert_eq!(error_summary("one\nlast line\n\n"), "last line");
}
}
+213
Переглянути файл
@@ -0,0 +1,213 @@
//! A small, forgiving XML element reader for POM files: elements, text and
//! CDATA; attributes, comments, processing instructions and DOCTYPEs are
//! skipped. Namespace prefixes are dropped (`<x:project>` → `project`).
//! Elements keep their byte offsets in the source, for edits ([`crate::pom_edit`]).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Element {
pub name: String,
pub text: String,
pub children: Vec<Element>,
/// Byte offset of the start tag's `<`.
pub start: usize,
/// Byte offset of the end tag's `<` (`end` for an empty or unclosed element).
pub inner_end: usize,
/// Byte offset just past the end tag (past the start tag for `<empty/>`).
pub end: usize,
}
impl Element {
pub fn child(&self, name: &str) -> Option<&Element> {
self.children.iter().find(|c| c.name == name)
}
pub fn children_named<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a Element> + 'a {
self.children.iter().filter(move |c| c.name == name)
}
/// Trimmed text of the child `name`, if present and non-empty.
pub fn text_of(&self, name: &str) -> Option<String> {
self.child(name).map(|c| c.text.trim().to_owned()).filter(|t| !t.is_empty())
}
/// Element at a `/`-separated path of child names.
pub fn at(&self, path: &str) -> Option<&Element> {
path.split('/').try_fold(self, |e, name| e.child(name))
}
/// Children of the element at `path` (empty when missing).
pub fn list<'a>(&'a self, path: &str, item: &'a str) -> Vec<&'a Element> {
self.at(path).map(|e| e.children_named(item).collect()).unwrap_or_default()
}
}
fn decode_entities(raw: &str, out: &mut String) {
let mut rest = raw;
while let Some(i) = rest.find('&') {
out.push_str(&rest[..i]);
let tail = &rest[i..];
let Some(end) = tail.find(';').filter(|&e| e <= 12) else {
out.push('&');
rest = &tail[1..];
continue;
};
let entity = &tail[1..end];
let ch = match entity {
"lt" => Some('<'),
"gt" => Some('>'),
"amp" => Some('&'),
"quot" => Some('"'),
"apos" => Some('\''),
e if e.starts_with("#x") || e.starts_with("#X") => {
u32::from_str_radix(&e[2..], 16).ok().and_then(char::from_u32)
}
e if e.starts_with('#') => e[1..].parse().ok().and_then(char::from_u32),
_ => None,
};
match ch {
Some(c) => out.push(c),
None => out.push_str(&tail[..=end]),
}
rest = &tail[end + 1..];
}
out.push_str(rest);
}
fn local(name: &str) -> String {
name.rsplit(':').next().unwrap_or(name).to_owned()
}
/// Parses `text` and returns its root element, or `None` when there is none.
/// Unbalanced end tags close the innermost matching element; unclosed elements
/// are closed at the end of input.
pub fn parse(text: &str) -> Option<Element> {
let mut stack: Vec<Element> = vec![Element::default()];
let mut rest = text;
let at = |rest: &str| text.len() - rest.len();
while !rest.is_empty() {
let Some(lt) = rest.find('<') else {
decode_entities(rest, &mut stack.last_mut()?.text);
break;
};
if lt > 0 {
decode_entities(&rest[..lt], &mut stack.last_mut()?.text);
}
let tag = &rest[lt..];
if let Some(body) = tag.strip_prefix("<!--") {
rest = body.find("-->").map_or("", |e| &body[e + 3..]);
} else if let Some(body) = tag.strip_prefix("<![CDATA[") {
let end = body.find("]]>").unwrap_or(body.len());
stack.last_mut()?.text.push_str(&body[..end]);
rest = body.get(end + 3..).unwrap_or("");
} else if tag.starts_with("<?") {
rest = tag.find("?>").map_or("", |e| &tag[e + 2..]);
} else if tag.starts_with("<!") {
rest = tag.find('>').map_or("", |e| &tag[e + 1..]);
} else if let Some(body) = tag.strip_prefix("</") {
let end = body.find('>').unwrap_or(body.len());
let name = local(body[..end].trim());
let close_start = at(tag);
rest = body.get(end + 1..).unwrap_or("");
let close_end = at(rest);
if let Some(i) = stack.iter().rposition(|e| e.name == name).filter(|&i| i > 0) {
while stack.len() > i {
let mut done = stack.pop()?;
// Elements closed implicitly by an outer end tag end where it starts.
let matched = stack.len() == i;
done.inner_end = close_start;
done.end = if matched { close_end } else { close_start };
stack.last_mut()?.children.push(done);
}
}
} else {
// Start or empty tag; skip quoted attribute values when looking for `>`.
let bytes = tag.as_bytes();
let mut i = 1;
let mut quote: Option<u8> = None;
while i < bytes.len() {
match (quote, bytes[i]) {
(Some(q), b) if b == q => quote = None,
(Some(_), _) => {}
(None, b'"' | b'\'') => quote = Some(bytes[i]),
(None, b'>') => break,
_ => {}
}
i += 1;
}
let inner = &tag[1..i.min(tag.len())];
let empty = inner.ends_with('/');
let inner = inner.trim_end_matches('/');
let name_end = inner.find(|c: char| c.is_whitespace()).unwrap_or(inner.len());
let start = at(tag);
rest = tag.get(i + 1..).unwrap_or("");
let after = at(rest);
let el =
Element { name: local(&inner[..name_end]), start, inner_end: after, end: after, ..Element::default() };
if empty {
stack.last_mut()?.children.push(el);
} else {
stack.push(el);
}
}
}
while stack.len() > 1 {
let mut done = stack.pop()?;
done.inner_end = text.len();
done.end = text.len();
stack.last_mut()?.children.push(done);
}
stack.pop()?.children.into_iter().next()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn elements_text_and_markup_noise() {
let doc = r#"<?xml version="1.0"?>
<!DOCTYPE x>
<!-- header -->
<p:project xmlns:p="urn" a="1 > 2">
<name>A &amp; B &#x41;&#66;</name>
<empty/>
<desc><![CDATA[<raw>]]></desc>
<!-- <name>ignored</name> -->
<list><item>1</item><item attr='/>'>2</item></list>
</p:project>"#;
let root = parse(doc).unwrap();
assert_eq!(root.name, "project");
assert_eq!(root.text_of("name").as_deref(), Some("A & B AB"));
assert!(root.child("empty").is_some());
assert_eq!(root.text_of("empty"), None);
assert_eq!(root.text_of("desc").as_deref(), Some("<raw>"));
let items: Vec<String> = root.list("list", "item").iter().map(|e| e.text.clone()).collect();
assert_eq!(items, ["1", "2"]);
assert_eq!(root.children_named("name").count(), 1);
}
#[test]
fn element_offsets() {
let doc = "<a>\n <b>x</b><c/>\n</a>";
let root = parse(doc).unwrap();
assert_eq!((root.start, root.inner_end, root.end), (0, doc.len() - 4, doc.len()));
let b = root.child("b").unwrap();
assert_eq!(&doc[b.start..b.end], "<b>x</b>");
assert_eq!(&doc[b.start..b.inner_end], "<b>x");
let c = root.child("c").unwrap();
assert_eq!(&doc[c.start..c.end], "<c/>");
let broken = parse("<a><b>x</a>").unwrap();
let b = broken.child("b").unwrap();
assert_eq!((b.inner_end, b.end), (7, 7));
assert_eq!(broken.end, 11);
}
#[test]
fn tolerates_broken_documents() {
let root = parse("<a><b>x</c><d>y").unwrap();
assert_eq!(root.at("b").unwrap().text, "x");
assert_eq!(root.at("b/d").unwrap().text, "y");
assert!(parse("").is_none());
assert!(parse("just text").is_none());
}
}
+24
Переглянути файл
@@ -0,0 +1,24 @@
[INFO] --- dependency:3.8.1:tree (default-cli) @ conflict-demo ---
[INFO] demo:conflict-demo:jar:1.0-SNAPSHOT
[INFO] +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] +- org.apache.commons:commons-text:jar:1.10.0:compile
[INFO] | \- org.apache.commons:commons-lang3:jar:3.12.0:compile
[INFO] +- org.apache.commons:commons-configuration2:jar:2.10.1:compile
[INFO] | +- (org.apache.commons:commons-lang3:jar:3.14.0:compile - omitted for conflict with 3.12.0)
[INFO] | +- (org.apache.commons:commons-text:jar:1.11.0:compile - omitted for conflict with 1.10.0)
[INFO] | \- commons-logging:commons-logging:jar:1.2:compile (version managed from 1.3.0)
[INFO] +- com.google.guava:guava:jar:32.1.3-jre:compile
[INFO] | +- com.google.guava:failureaccess:jar:1.0.1:compile
[INFO] | +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
[INFO] | +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO] | +- org.checkerframework:checker-qual:jar:3.37.0:compile
[INFO] | +- com.google.errorprone:error_prone_annotations:jar:2.21.1:compile
[INFO] | \- com.google.j2objc:j2objc-annotations:jar:2.8:compile
[INFO] +- com.google.inject:guice:jar:5.1.0:compile
[INFO] | +- javax.inject:javax.inject:jar:1:compile
[INFO] | +- aopalliance:aopalliance:jar:1.0:compile
[INFO] | \- (com.google.guava:guava:jar:30.1-jre:compile - omitted for conflict with 32.1.3-jre)
[INFO] \- junit:junit:jar:4.13.2:test
[INFO] \- (org.hamcrest:hamcrest-core:jar:1.3:test - omitted for duplicate)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
+8
Переглянути файл
@@ -0,0 +1,8 @@
[package]
name = "ide-clipboard"
description = "The OS clipboard's file list (Explorer <-> project view): Windows CF_HDROP read/write, no window or message loop"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
+293
Переглянути файл
@@ -0,0 +1,293 @@
//! The file list on the OS clipboard: what Explorer copies when the user
//! copies files, so the project view can paste them, and what the project view
//! writes so Explorer can paste the files it copied (IDEA does both).
//!
//! Windows keeps it as `CF_HDROP`: a `DROPFILES` header and a double-NUL
//! terminated list of absolute paths, wide when `fWide` is set. The data is set
//! in full rather than rendered on demand, so no window or message loop is
//! needed and the list survives after `CloseClipboard`.
//!
//! Other platforms keep file lists elsewhere (`public.file-url` on macOS,
//! `text/uri-list` on X11) and are not wired up: [`files`] is empty and
//! [`set_files`] fails there.
use std::path::PathBuf;
/// Size of `DROPFILES`: `pFiles`, `pt` (two `LONG`s), `fNC`, `fWide`.
const HEADER: usize = 20;
/// The absolute paths on the clipboard, empty when it holds no file list.
pub fn files() -> Vec<PathBuf> {
imp::files()
}
/// Whether the clipboard holds a file list, without reading it.
pub fn has_files() -> bool {
imp::has_files()
}
/// Replaces the clipboard with `paths`. False when it is busy or holds nothing
/// to write; the clipboard is a shared resource, so callers treat this as
/// best-effort.
pub fn set_files(paths: &[PathBuf]) -> bool {
if paths.is_empty() {
return false;
}
imp::set_files(paths)
}
/// `DROPFILES` for `paths`, in the wide form so names outside the ANSI code
/// page survive. Empty paths are skipped.
pub fn encode(paths: &[PathBuf]) -> Vec<u8> {
let mut units: Vec<u16> = Vec::new();
for path in paths {
let path = path.to_string_lossy();
if path.is_empty() {
continue;
}
units.extend(path.encode_utf16());
units.push(0);
}
// The list ends with an empty string: with one path, two NULs in a row.
units.push(0);
let mut out = Vec::with_capacity(HEADER + units.len() * 2);
out.extend_from_slice(&(HEADER as u32).to_le_bytes()); // pFiles: list starts after the header
out.extend_from_slice(&[0u8; 8]); // pt: the drop point, unused
out.extend_from_slice(&0u32.to_le_bytes()); // fNC: not a file manager drop
out.extend_from_slice(&1u32.to_le_bytes()); // fWide: UTF-16 paths
for unit in units {
out.extend_from_slice(&unit.to_le_bytes());
}
out
}
/// The paths of a `DROPFILES` block, stopping at the empty string that ends the
/// list. An ANSI list (pre-Unicode writers) is decoded as UTF-8, lossily.
pub fn decode(bytes: &[u8]) -> Vec<PathBuf> {
if bytes.len() < HEADER {
return Vec::new();
}
let offset = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize;
let wide = u32::from_le_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]) != 0;
if offset >= bytes.len() {
return Vec::new();
}
let body = &bytes[offset..];
let mut out = Vec::new();
if wide {
let mut current: Vec<u16> = Vec::new();
for chunk in body.as_chunks::<2>().0 {
let unit = u16::from_le_bytes(*chunk);
if unit != 0 {
current.push(unit);
} else if current.is_empty() {
break;
} else {
out.push(PathBuf::from(String::from_utf16_lossy(&current)));
current.clear();
}
}
} else {
for part in body.split(|b| *b == 0) {
if part.is_empty() {
break;
}
out.push(PathBuf::from(String::from_utf8_lossy(part).into_owned()));
}
}
out
}
#[cfg(windows)]
mod imp {
use super::{HEADER, decode, encode};
use std::ffi::c_void;
use std::path::PathBuf;
use std::ptr;
use std::time::Duration;
/// `CF_HDROP`, the clipboard format holding a file list.
const CF_HDROP: u32 = 15;
/// An `HGLOBAL` or a `HANDLE`: both are opaque pointers here.
type Handle = *mut c_void;
const GMEM_MOVEABLE: u32 = 0x0002;
/// Another process holds the clipboard only while it copies; retrying beats
/// reporting "no files" for a paste the user just made.
const OPEN_ATTEMPTS: usize = 10;
const OPEN_RETRY: Duration = Duration::from_millis(20);
#[link(name = "user32")]
unsafe extern "system" {
fn OpenClipboard(owner: Handle) -> i32;
fn CloseClipboard() -> i32;
fn EmptyClipboard() -> i32;
fn GetClipboardData(format: u32) -> Handle;
fn SetClipboardData(format: u32, memory: Handle) -> Handle;
fn IsClipboardFormatAvailable(format: u32) -> i32;
}
#[link(name = "kernel32")]
unsafe extern "system" {
fn GlobalAlloc(flags: u32, bytes: usize) -> Handle;
fn GlobalLock(memory: Handle) -> *mut c_void;
fn GlobalUnlock(memory: Handle) -> i32;
fn GlobalFree(memory: Handle) -> Handle;
fn GlobalSize(memory: Handle) -> usize;
}
/// Opens the clipboard as its owner: the list is written in full, so no
/// owning window is needed.
fn open() -> bool {
for _ in 0..OPEN_ATTEMPTS {
if unsafe { OpenClipboard(ptr::null_mut()) } != 0 {
return true;
}
std::thread::sleep(OPEN_RETRY);
}
false
}
pub fn has_files() -> bool {
(unsafe { IsClipboardFormatAvailable(CF_HDROP) }) != 0
}
pub fn files() -> Vec<PathBuf> {
if !has_files() || !open() {
return Vec::new();
}
let memory = unsafe { GetClipboardData(CF_HDROP) };
let paths = if memory.is_null() {
Vec::new()
} else {
// SAFETY: the clipboard keeps the block alive while it is open, and
// `GlobalSize` bounds the bytes read from it.
unsafe {
let data = GlobalLock(memory);
if data.is_null() {
Vec::new()
} else {
let bytes = std::slice::from_raw_parts(data as *const u8, GlobalSize(memory));
let paths = decode(bytes);
GlobalUnlock(memory);
paths
}
}
};
unsafe { CloseClipboard() };
paths
}
pub fn set_files(paths: &[PathBuf]) -> bool {
let bytes = encode(paths);
if bytes.len() <= HEADER {
return false;
}
// SAFETY: the block is allocated, filled, handed to the clipboard and
// freed only on the paths where the clipboard did not take ownership.
unsafe {
let memory = GlobalAlloc(GMEM_MOVEABLE, bytes.len());
if memory.is_null() {
return false;
}
let data = GlobalLock(memory);
if data.is_null() {
GlobalFree(memory);
return false;
}
ptr::copy_nonoverlapping(bytes.as_ptr(), data as *mut u8, bytes.len());
GlobalUnlock(memory);
if !open() {
GlobalFree(memory);
return false;
}
// Replacing the list also drops the text the user copied before.
if EmptyClipboard() == 0 {
CloseClipboard();
GlobalFree(memory);
return false;
}
let taken = !SetClipboardData(CF_HDROP, memory).is_null();
CloseClipboard();
if !taken {
GlobalFree(memory);
}
taken
}
}
}
#[cfg(not(windows))]
mod imp {
use std::path::PathBuf;
pub fn has_files() -> bool {
false
}
pub fn files() -> Vec<PathBuf> {
Vec::new()
}
pub fn set_files(_paths: &[PathBuf]) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
fn paths(list: &[&str]) -> Vec<PathBuf> {
list.iter().map(PathBuf::from).collect()
}
#[test]
fn a_written_list_reads_back_unchanged() {
let list = paths(&["C:/work/project/a.txt", "C:/work/project/sub"]);
assert_eq!(decode(&encode(&list)), list);
}
#[test]
fn the_header_says_where_the_paths_start_and_that_they_are_wide() {
let bytes = encode(&paths(&["C:/a"]));
assert_eq!(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]), HEADER as u32);
assert_eq!(u32::from_le_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]), 1);
// `C:/a` plus its NUL, then the empty string that ends the list.
assert_eq!(bytes.len(), HEADER + (4 + 1 + 1) * 2);
assert_eq!(&bytes[HEADER + 6..HEADER + 8], b"a\0");
assert_eq!(&bytes[bytes.len() - 4..], &[0, 0, 0, 0]);
}
#[test]
fn names_outside_the_ansi_code_page_survive() {
let list = paths(&["C:/工作/文档/测试.txt", "C:/a/"]);
assert_eq!(decode(&encode(&list)), list);
}
#[test]
fn an_empty_list_has_no_paths() {
assert_eq!(decode(&encode(&[])), Vec::<PathBuf>::new());
// A lone terminator is enough for the reader to stop.
assert_eq!(decode(&[0u8; HEADER]), Vec::<PathBuf>::new());
}
#[test]
fn a_truncated_or_foreign_block_reads_as_no_paths() {
assert_eq!(decode(&[]), Vec::<PathBuf>::new());
assert_eq!(decode(&[1, 2, 3]), Vec::<PathBuf>::new());
let mut far = encode(&paths(&["C:/a"]));
far[0] = 0xff; // pFiles past the end
assert_eq!(decode(&far), Vec::<PathBuf>::new());
}
#[test]
fn an_ansi_list_is_still_read_before_the_terminator() {
let mut bytes = vec![0u8; HEADER];
bytes[..4].copy_from_slice(&(HEADER as u32).to_le_bytes());
bytes[16] = 0; // fWide: the list is single-byte
bytes.extend_from_slice(b"C:\\a\\b.txt\0C:\\c\0\0");
assert_eq!(decode(&bytes), paths(&["C:\\a\\b.txt", "C:\\c"]));
}
}
+14
Переглянути файл
@@ -0,0 +1,14 @@
[package]
name = "ide-core"
description = "Shared vocabulary of OpenIDEA: ids, text coordinates, events, errors, settings"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
dunce.workspace = true
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
thiserror.workspace = true
tokio-util.workspace = true
+441
Переглянути файл
@@ -0,0 +1,441 @@
//! Settings › Editor sub-structures (IDEA's Editor group: General and its
//! sub-pages, Code Editing, Color Scheme, Code Style, File Encodings). The
//! top-level `EditorSettings` lives in `settings.rs` and embeds these.
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
/// Editor › General (mouse, scrolling, soft wraps, virtual space, saving).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct EditorGeneralSettings {
/// Change font size with ⌘ + mouse wheel.
pub wheel_zoom: bool,
/// Drag-and-drop of selected text inside the editor.
pub drag_and_drop: bool,
/// When the caret moves out of view: `minimize` scrolling | `center` the caret line.
pub caret_scroll: String,
/// Glob patterns (`;`-separated) of files soft-wrapped when `editor.softWrap` is on.
pub soft_wrap_files: String,
/// Wrapped parts keep the original line's indent.
pub soft_wrap_indent: bool,
/// Show virtual space at the file bottom.
pub scroll_past_end: bool,
/// Remove trailing spaces on save from: `modified` lines | `all` lines.
pub strip_trailing_scope: String,
/// Keep trailing spaces on the caret line when stripping.
pub keep_trailing_on_caret_line: bool,
pub remove_trailing_blank_lines: bool,
/// Copy as rich text (HTML with syntax colours) by default.
pub rich_text_copy: bool,
}
impl Default for EditorGeneralSettings {
fn default() -> Self {
Self {
wheel_zoom: false,
drag_and_drop: true,
caret_scroll: "minimize".into(),
soft_wrap_files: "*.md; *.txt; *.rst; *.adoc".into(),
soft_wrap_indent: true,
scroll_past_end: false,
strip_trailing_scope: "modified".into(),
keep_trailing_on_caret_line: true,
remove_trailing_blank_lines: false,
rich_text_copy: true,
}
}
}
/// Editor › General › Appearance.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct EditorAppearanceSettings {
pub caret_blinking: bool,
/// Blink period in ms.
pub caret_blink_period: u32,
pub block_caret: bool,
pub line_numbers: bool,
pub method_separators: bool,
/// Sub-options of `editor.showWhitespace`.
pub whitespace_leading: bool,
pub whitespace_inner: bool,
pub whitespace_trailing: bool,
pub indent_guides: bool,
/// Right margin and visual guides from Code Style.
pub hard_wrap_guides: bool,
pub intention_bulb: bool,
/// Distraction Free Mode: width of the centred text column, in characters.
pub distraction_free_width: u32,
}
impl Default for EditorAppearanceSettings {
fn default() -> Self {
Self {
caret_blinking: true,
caret_blink_period: 500,
block_caret: false,
line_numbers: true,
method_separators: false,
whitespace_leading: true,
whitespace_inner: true,
whitespace_trailing: true,
indent_guides: true,
hard_wrap_guides: true,
intention_bulb: true,
distraction_free_width: 80,
}
}
}
/// Editor › General › Breadcrumbs.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct BreadcrumbsSettings {
pub show: bool,
/// `top` | `bottom` of the editor.
pub placement: String,
/// Language ids breadcrumbs are turned off for.
pub disabled_languages: Vec<String>,
}
impl Default for BreadcrumbsSettings {
fn default() -> Self {
Self { show: true, placement: "bottom".into(), disabled_languages: Vec::new() }
}
}
/// Editor › General › Code Completion.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct CompletionSettings {
pub match_case: bool,
/// `first` letter only | `all` letters.
pub case_sensitivity: String,
/// Show suggestions as you type.
pub auto_popup: bool,
/// Insert the selected suggestion by pressing space, dot, or other context-dependent keys.
pub insert_on_special_keys: bool,
/// Insert a sole suggestion of explicit (⌃Space) completion right away.
pub auto_insert_single: bool,
/// Sort suggestions by name instead of relevance.
pub sort_alphabetically: bool,
/// Add `()` after function and method suggestions.
pub insert_parentheses: bool,
pub show_documentation: bool,
pub documentation_delay: u32,
pub parameter_info: bool,
pub parameter_info_delay: u32,
}
impl Default for CompletionSettings {
fn default() -> Self {
Self {
match_case: true,
case_sensitivity: "first".into(),
auto_popup: true,
insert_on_special_keys: false,
auto_insert_single: true,
sort_alphabetically: false,
insert_parentheses: true,
show_documentation: false,
documentation_delay: 500,
parameter_info: true,
parameter_info_delay: 1000,
}
}
}
/// Editor › General › Auto Import (LSP code actions).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct AutoImportSettings {
pub add_unambiguous: bool,
pub optimize_on_the_fly: bool,
}
/// Editor › General › Code Folding.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct FoldingSettings {
pub show_outline: bool,
pub file_header: bool,
pub imports: bool,
pub doc_comments: bool,
}
impl Default for FoldingSettings {
fn default() -> Self {
Self { show_outline: true, file_header: true, imports: true, doc_comments: false }
}
}
/// Editor › General › Editor Tabs.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct EditorTabsSettings {
/// `top` | `bottom` | `none`.
pub placement: String,
pub multiple_rows: bool,
/// One row that doesn't fit: `scroll` the tabs panel | `squeeze` the tabs.
pub one_row_overflow: String,
pub pinned_separate_row: bool,
pub show_file_icon: bool,
pub show_extension: bool,
pub show_directory_for_non_unique: bool,
/// Mark modified tabs with an asterisk.
pub mark_modified: bool,
pub show_full_path_on_hover: bool,
pub show_close_button: bool,
/// `right` | `left` (with `show_close_button`).
pub close_button_position: String,
pub sort_alphabetically: bool,
pub preview_tab: bool,
pub open_at_end: bool,
pub tab_limit: u32,
/// When the limit is reached close `unchanged` (non-modified) tabs first, or the `lru` one.
pub closing_policy: String,
/// When the current tab is closed activate the `left` | `right` | `mru` tab.
pub activate_on_close: String,
}
impl Default for EditorTabsSettings {
fn default() -> Self {
Self {
placement: "top".into(),
multiple_rows: false,
one_row_overflow: "scroll".into(),
pinned_separate_row: false,
show_file_icon: true,
show_extension: true,
show_directory_for_non_unique: true,
mark_modified: false,
show_full_path_on_hover: true,
show_close_button: true,
close_button_position: "right".into(),
sort_alphabetically: false,
preview_tab: true,
open_at_end: false,
tab_limit: 30,
closing_policy: "lru".into(),
activate_on_close: "left".into(),
}
}
}
/// Editor › General › Smart Keys.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct SmartKeysSettings {
/// Home moves caret to first non-whitespace character.
pub smart_home: bool,
/// Use "CamelHumps" words.
pub camel_humps: bool,
pub pair_brackets: bool,
pub pair_quotes: bool,
pub surround_selection: bool,
pub tab_out_of_pair: bool,
/// Reformat on paste: `none` | `indentBlock` | `indentEachLine`.
pub reformat_on_paste: String,
pub smart_indent: bool,
/// Backspace › Unindent: `autoindent` (proper indent position) | `nearest` indent position | `none`.
pub backspace_unindent: String,
}
impl Default for SmartKeysSettings {
fn default() -> Self {
Self {
smart_home: true,
camel_humps: false,
pair_brackets: true,
pair_quotes: true,
surround_selection: true,
tab_out_of_pair: true,
reformat_on_paste: "indentEachLine".into(),
smart_indent: true,
backspace_unindent: "autoindent".into(),
}
}
}
/// Editor › Code Editing.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct CodeEditingSettings {
/// Highlight usages of element at caret.
pub highlight_usages: bool,
pub highlight_braces: bool,
/// Highlight other occurrences of the selected text.
pub highlight_selection: bool,
pub quick_doc_on_hover: bool,
pub quick_doc_delay: u32,
/// Next / previous error goes to errors only (skips warnings).
pub next_error_errors_only: bool,
pub sticky_lines_max: u32,
}
impl Default for CodeEditingSettings {
fn default() -> Self {
Self {
highlight_usages: true,
highlight_braces: true,
highlight_selection: true,
quick_doc_on_hover: true,
quick_doc_delay: 500,
next_error_errors_only: true,
sticky_lines_max: 5,
}
}
}
/// Editor › Inlay Hints.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct InlayHintsSettings {
pub types: bool,
pub parameters: bool,
/// Hints of other kinds (chaining, closing labels…).
pub other: bool,
pub disabled_languages: Vec<String>,
/// Code vision: usages above declarations.
pub code_vision_usages: bool,
/// Code vision: last author of a declaration (VCS blame) beside its usages.
pub code_vision_author: bool,
}
impl Default for InlayHintsSettings {
fn default() -> Self {
Self {
types: true,
parameters: true,
other: true,
disabled_languages: Vec::new(),
code_vision_usages: true,
code_vision_author: true,
}
}
}
/// Editor › General › Inline Completion: ghost text from language servers that
/// support `textDocument/inlineCompletion` (LSP 3.18).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct InlineCompletionSettings {
pub enabled: bool,
/// Ask only once typing has paused for `pause_ms`, instead of right after each change.
pub only_on_pause: bool,
pub pause_ms: u32,
pub disabled_languages: Vec<String>,
}
impl Default for InlineCompletionSettings {
fn default() -> Self {
Self { enabled: true, only_on_pause: false, pause_ms: 500, disabled_languages: Vec::new() }
}
}
/// Editor › Color Scheme override of one text attribute.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct TextAttributesOverride {
#[serde(skip_serializing_if = "Option::is_none")]
pub foreground: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub background: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bold: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub italic: Option<bool>,
/// `underline` | `wave` | `strikeout` | `none`.
#[serde(skip_serializing_if = "Option::is_none")]
pub effect: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub effect_color: Option<String>,
}
/// Editor › Code Style › <Language> › Tabs and Indents (unset fields inherit).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct LanguageCodeStyle {
#[serde(skip_serializing_if = "Option::is_none")]
pub use_tab: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tab_size: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub indent: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hard_wrap_at: Option<u32>,
/// Comment Code › Line comment at first column.
#[serde(skip_serializing_if = "Option::is_none")]
pub line_comment_at_first_column: Option<bool>,
/// Comment Code › Add a space at line comment start.
#[serde(skip_serializing_if = "Option::is_none")]
pub line_comment_add_space: Option<bool>,
}
/// Editor › Code Style (General tab + per-language overrides).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct CodeStyleSettings {
/// Line separator for new files: `system` | `lf` | `crlf` | `cr`.
pub line_separator: String,
pub hard_wrap_at: u32,
pub visual_guides: Vec<u32>,
/// Detect and use existing file indents for editing.
pub detect_indents: bool,
/// Enable EditorConfig support.
pub editorconfig: bool,
pub languages: BTreeMap<String, LanguageCodeStyle>,
/// Formatter › Do not format: globs (`*.min.js`, `generated/**`) Reformat Code skips.
pub do_not_format: Vec<String>,
/// Formatter › Turn formatter on/off with markers in code comments.
pub formatter_tags_enabled: bool,
pub formatter_off_tag: String,
pub formatter_on_tag: String,
/// The markers are regular expressions.
pub formatter_tags_regexp: bool,
}
impl Default for CodeStyleSettings {
fn default() -> Self {
Self {
line_separator: "system".into(),
hard_wrap_at: 120,
visual_guides: Vec::new(),
detect_indents: true,
editorconfig: true,
languages: BTreeMap::new(),
do_not_format: Vec::new(),
formatter_tags_enabled: false,
formatter_off_tag: "@formatter:off".into(),
formatter_on_tag: "@formatter:on".into(),
formatter_tags_regexp: false,
}
}
}
/// Editor › File Encodings.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct EncodingSettings {
/// Fallback for files outside the project that are not valid UTF-8 (WHATWG label).
pub global: String,
/// Encoding of new files and fallback for project files that are not valid UTF-8.
pub project: String,
/// Fallback for `.properties` files.
pub properties: String,
/// Create UTF-8 files: `never` (no BOM) | `always` (with BOM) | `windows` (with BOM on Windows).
pub utf8_bom: String,
}
impl Default for EncodingSettings {
fn default() -> Self {
Self {
global: "utf-8".into(),
project: "utf-8".into(),
properties: "iso-8859-1".into(),
utf8_bom: "never".into(),
}
}
}
+67
Переглянути файл
@@ -0,0 +1,67 @@
use serde::{Deserialize, Serialize};
use specta::Type;
pub type IdeResult<T> = Result<T, IdeError>;
/// The single error type that crosses the IPC boundary.
///
/// Crates keep rich internal errors and convert at their public edge; the UI
/// switches on `kind` and shows `message`.
#[derive(Clone, Debug, thiserror::Error, Serialize, Deserialize, Type)]
#[error("{kind:?}: {message}")]
pub struct IdeError {
pub kind: ErrorKind,
pub message: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum ErrorKind {
NotFound,
AlreadyExists,
PermissionDenied,
InvalidInput,
/// Document version mismatch; the UI must resync the full text.
OutOfSync,
Cancelled,
/// A required external tool (language server, git, debugger) is unavailable.
Unavailable,
Io,
Protocol,
Internal,
}
impl IdeError {
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self { kind, message: message.into() }
}
pub fn not_found(m: impl Into<String>) -> Self {
Self::new(ErrorKind::NotFound, m)
}
pub fn invalid(m: impl Into<String>) -> Self {
Self::new(ErrorKind::InvalidInput, m)
}
pub fn internal(m: impl Into<String>) -> Self {
Self::new(ErrorKind::Internal, m)
}
pub fn unavailable(m: impl Into<String>) -> Self {
Self::new(ErrorKind::Unavailable, m)
}
pub fn cancelled() -> Self {
Self::new(ErrorKind::Cancelled, "cancelled")
}
}
impl From<std::io::Error> for IdeError {
fn from(e: std::io::Error) -> Self {
use std::io::ErrorKind as K;
let kind = match e.kind() {
K::NotFound => ErrorKind::NotFound,
K::AlreadyExists => ErrorKind::AlreadyExists,
K::PermissionDenied => ErrorKind::PermissionDenied,
K::InvalidInput | K::InvalidData => ErrorKind::InvalidInput,
_ => ErrorKind::Io,
};
Self::new(kind, e.to_string())
}
}
+219
Переглянути файл
@@ -0,0 +1,219 @@
//! The backend → UI event bus.
//!
//! Feature crates emit through an [`EventSink`]; `src-tauri` implements it by
//! forwarding to the webview as the single `ide://event` Tauri event. High
//! volume streams (terminal bytes, search hits, process output) do *not* use
//! this bus; they use per-request `tauri::ipc::Channel`s.
use crate::{DebugSessionId, Diagnostic, DocId, RunId, ServerId, TaskId, TerminalId, TextChange};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::sync::Arc;
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(tag = "type", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum IdeEvent {
/// Files or directories changed on disk (after debouncing, ignore rules applied).
FsChanged {
created: Vec<crate::IpcPath>,
modified: Vec<crate::IpcPath>,
removed: Vec<crate::IpcPath>,
},
/// The backend changed a document (reload from disk, formatting, refactoring).
/// The UI applies `changes` on top of `base_version`, yielding `version`.
DocPatched {
doc: DocId,
base_version: u32,
version: u32,
changes: Vec<TextChange>,
},
/// A document's dirty flag changed (saved / modified externally).
DocDirty {
doc: DocId,
dirty: bool,
},
/// An open document's language changed (Settings › File Types, Override File Type).
DocLanguage {
doc: DocId,
language: crate::LanguageId,
},
/// Full replacement of diagnostics for one file from one producer.
Diagnostics {
path: crate::IpcPath,
source: String,
items: Vec<Diagnostic>,
},
/// Progress of a background task; `fraction` is `None` when indeterminate.
Task {
id: TaskId,
title: String,
message: Option<String>,
fraction: Option<f32>,
done: bool,
},
/// Project indexing state changed (dumb mode on/off).
IndexState {
indexing: bool,
files: u32,
symbols: u32,
},
/// Git status / branch information should be refreshed.
VcsChanged {
root: crate::IpcPath,
},
LanguageServer {
server: ServerId,
name: String,
status: ServerStatus,
message: Option<String>,
},
/// A language server asked clients to re-request one kind of data for its
/// open documents (`workspace/inlayHint/refresh`, …), e.g. after indexing.
LspRefresh {
server: ServerId,
kind: LspRefreshKind,
},
TerminalExited {
terminal: TerminalId,
code: Option<i32>,
},
RunExited {
run: RunId,
code: Option<i32>,
},
Debug {
session: DebugSessionId,
event: DebugEvent,
},
Notify {
level: NotifyLevel,
title: String,
body: Option<String>,
},
/// A setting was written (any window); the windows it concerns (all for a
/// global value, the project's for a project value) re-read their effective settings.
SettingsChanged {
key: String,
},
/// The project's source / test / resource / excluded roots changed (build files
/// changed or folders were marked); the UI re-reads them with `project_roots`.
ProjectRootsChanged {
root: crate::IpcPath,
},
/// A child process (`ssh`, `git`) asks for a credential; answered with `ssh_askpass_answer`.
Askpass {
id: u64,
prompt: String,
kind: AskpassKind,
},
/// Prompt `id` was answered (in any window) or gave up.
AskpassDone {
id: u64,
},
}
/// What an askpass prompt asks for.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AskpassKind {
/// Password or passphrase: hidden input.
Secret,
/// User name: visible input.
Text,
/// Yes / no (an unknown host key); answered with `yes` or `no`.
Confirm,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum ServerStatus {
Starting,
Ready,
Busy,
Crashed,
Stopped,
Missing,
/// Not started: its configuration can't launch it (e.g. no JDK new enough);
/// the message says what to change, and a settings change retries.
Misconfigured,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum LspRefreshKind {
InlayHints,
SemanticTokens,
CodeLens,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum NotifyLevel {
Info,
Warning,
Error,
}
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum DebugEvent {
/// First event of every session (only build output of a root session's
/// `cargo` step can precede it). `parent` is set for adapter-started child sessions.
Started {
name: String,
adapter: String,
parent: Option<DebugSessionId>,
},
Initialized,
Stopped {
thread_id: i64,
reason: String,
description: Option<String>,
all_threads_stopped: bool,
},
Continued {
thread_id: i64,
all_threads_continued: bool,
},
Output {
category: String,
output: String,
},
Thread {
thread_id: i64,
started: bool,
},
BreakpointsChanged {
path: crate::IpcPath,
},
Terminated,
Exited {
code: i32,
},
/// java-debug's `hotcodereplace` event: `change_type` is `BUILD_COMPLETE` (classes
/// changed and can be reloaded), `STARTING`, `END`, `WARNING` or `ERROR`.
HotCodeReplace {
change_type: String,
message: String,
},
}
/// Where backend crates publish [`IdeEvent`]s. Cheap to clone via `Arc`.
pub trait EventSink: Send + Sync + 'static {
fn emit(&self, event: IdeEvent);
}
impl<F: Fn(IdeEvent) + Send + Sync + 'static> EventSink for F {
fn emit(&self, event: IdeEvent) {
self(event)
}
}
pub type SharedSink = Arc<dyn EventSink>;
/// Discards events; for tests and headless tools.
pub struct NullSink;
impl EventSink for NullSink {
fn emit(&self, _: IdeEvent) {}
}
+66
Переглянути файл
@@ -0,0 +1,66 @@
use serde::{Deserialize, Serialize};
use specta::Type;
use std::sync::atomic::{AtomicU32, Ordering};
macro_rules! id_type {
($($(#[$doc:meta])* $name:ident),* $(,)?) => {$(
$(#[$doc])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, Type)]
#[serde(transparent)]
pub struct $name(pub u32);
impl $name {
/// Allocates a process-unique id.
pub fn next() -> Self {
static NEXT: AtomicU32 = AtomicU32::new(1);
Self(NEXT.fetch_add(1, Ordering::Relaxed))
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}#{}", stringify!($name), self.0)
}
}
)*};
}
id_type!(
/// An open document buffer.
DocId,
/// A cancellable, streaming backend request issued by the UI.
RequestId,
/// An integrated terminal session.
TerminalId,
/// A process started from a run configuration.
RunId,
/// A debug adapter session.
DebugSessionId,
/// A running language server instance.
ServerId,
/// A long-running background task shown in the status bar.
TaskId,
);
/// Language identifier, e.g. `rust`, `typescript`, `kotlin`. Matches LSP `languageId`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, Type)]
#[serde(transparent)]
pub struct LanguageId(pub String);
impl LanguageId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn plain_text() -> Self {
Self::new("plaintext")
}
}
impl std::fmt::Display for LanguageId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
+106
Переглянути файл
@@ -0,0 +1,106 @@
//! "Ignored Files and Folders" (IDEA: Settings › File Types): file names the IDE
//! treats as nonexistent — hidden from the project view and navigation, never
//! indexed or searched, never offered as unversioned changes.
/// IDEA's default list, plus `.jj` and the Windows shell files.
pub const DEFAULT_IGNORED_NAMES: &[&str] = &[
"*.pyc",
"*.pyo",
"*.rbc",
"*.yarb",
"*~",
".DS_Store",
".git",
".hg",
".jj",
".svn",
"CVS",
"__pycache__",
"_svn",
"vssver.scc",
"vssver2.scc",
"Thumbs.db",
"desktop.ini",
];
/// Matches single path segments against name patterns (`*` and `?` wildcards).
#[derive(Clone, Debug, Default)]
pub struct IgnoredNames {
exact: Vec<String>,
wildcards: Vec<String>,
}
impl IgnoredNames {
pub fn new(patterns: &[String]) -> Self {
let mut out = Self::default();
for p in patterns.iter().map(|p| p.trim()).filter(|p| !p.is_empty() && !p.contains('/')) {
if p.contains(['*', '?']) {
out.wildcards.push(p.to_owned());
} else {
out.exact.push(p.to_owned());
}
}
out
}
pub fn is_ignored_name(&self, name: &str) -> bool {
self.exact.iter().any(|e| e == name) || self.wildcards.iter().any(|w| wildcard(w.as_bytes(), name.as_bytes()))
}
/// Whether any segment of a `/`-separated relative path is ignored.
pub fn is_ignored_path(&self, rel: &str) -> bool {
rel.split('/').any(|seg| !seg.is_empty() && self.is_ignored_name(seg))
}
}
fn wildcard(p: &[u8], s: &[u8]) -> bool {
let (mut pi, mut si, mut star, mut mark) = (0, 0, None, 0);
while si < s.len() {
if pi < p.len() && (p[pi] == b'?' || p[pi] == s[si]) {
pi += 1;
si += 1;
} else if pi < p.len() && p[pi] == b'*' {
star = Some(pi);
pi += 1;
mark = si;
} else if let Some(st) = star {
pi = st + 1;
mark += 1;
si = mark;
} else {
return false;
}
}
p[pi..].iter().all(|&c| c == b'*')
}
#[cfg(test)]
mod tests {
use super::*;
fn defaults() -> IgnoredNames {
IgnoredNames::new(&DEFAULT_IGNORED_NAMES.iter().map(|s| s.to_string()).collect::<Vec<_>>())
}
#[test]
fn matches_names_and_wildcards() {
let m = defaults();
for name in [".DS_Store", "__pycache__", "a.pyc", "notes.txt~", "Thumbs.db"] {
assert!(m.is_ignored_name(name), "{name}");
}
for name in ["DS_Store", "a.py", "pyc", "src", ".gitignore"] {
assert!(!m.is_ignored_name(name), "{name}");
}
assert!(wildcard(b"a?c*", b"abcdef"));
assert!(!wildcard(b"a?c", b"abcd"));
}
#[test]
fn matches_any_path_segment() {
let m = defaults();
assert!(m.is_ignored_path("src/.DS_Store"));
assert!(m.is_ignored_path("pkg/__pycache__/"));
assert!(m.is_ignored_path("pkg/__pycache__/mod.cpython-312.pyc"));
assert!(!m.is_ignored_path("src/main.rs"));
}
}
+114
Переглянути файл
@@ -0,0 +1,114 @@
//! Settings › Editor › Inspections: inspection profiles. `Default` and the
//! user's IDE-level copies live in the IDE (global) settings, `Project Default`
//! and project-level copies in the project's; the project picks which one is in
//! effect. The UI applies the profile to diagnostics. Also Editor › Natural
//! Languages › Spelling (the Typo inspection's dictionaries).
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct InspectionSettings {
/// Profile in effect for the project: `default`, `project`, or a copy
/// as `ide:<key>` / `project:<key>`.
pub profile: String,
/// The `Default` profile (IDE level): inspection id → override.
pub default: BTreeMap<String, InspectionOverride>,
/// The `Project Default` profile (stored with the project).
pub project: BTreeMap<String, InspectionOverride>,
/// Profiles copied at IDE level (global settings), by key.
pub ide_profiles: BTreeMap<String, CustomInspectionProfile>,
/// Profiles copied at project level (project settings), by key.
pub project_profiles: BTreeMap<String, CustomInspectionProfile>,
}
impl Default for InspectionSettings {
fn default() -> Self {
Self {
profile: "default".into(),
default: BTreeMap::new(),
project: BTreeMap::new(),
ide_profiles: BTreeMap::new(),
project_profiles: BTreeMap::new(),
}
}
}
/// A named profile made with Copy.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct CustomInspectionProfile {
pub name: String,
pub overrides: BTreeMap<String, InspectionOverride>,
}
/// Editor › Natural Languages › Spelling.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct SpellingSettings {
/// Application-level dictionary: accepted words (IDE settings).
pub ide_words: Vec<String>,
/// Project-level dictionary: accepted words (project settings).
pub project_words: Vec<String>,
/// Custom dictionaries: plain word lists or Hunspell `.dic` files.
pub custom_dictionaries: Vec<String>,
}
/// Changes to one inspection; absent fields keep the inspection's defaults.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct InspectionOverride {
#[specta(optional)]
pub enabled: Option<bool>,
#[specta(optional)]
pub severity: Option<InspectionLevel>,
/// The inspection's own options (Typo: `processCode`, `processLiterals`,
/// `processComments`); absent ones keep their defaults.
#[specta(optional)]
pub options: Option<BTreeMap<String, bool>>,
}
/// IDEA's highlighting levels.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum InspectionLevel {
Error,
Warning,
WeakWarning,
ServerProblem,
Typo,
/// Reported (Problems, Inspect Code) but not highlighted in the editor.
None,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn overrides_parse_partially() {
let s: InspectionSettings = serde_json::from_str(
r#"{"default":{"lsp:ruff":{"enabled":false},"lsp:ruff:F401":{"severity":"weakWarning"}}}"#,
)
.unwrap();
assert_eq!(s.profile, "default");
assert_eq!(s.default["lsp:ruff"].enabled, Some(false));
assert_eq!(s.default["lsp:ruff:F401"].severity, Some(InspectionLevel::WeakWarning));
assert!(s.project.is_empty());
assert!(s.ide_profiles.is_empty());
let s: InspectionSettings = serde_json::from_str(
r#"{"profile":"ide:strict","ideProfiles":{"strict":{"name":"Strict","overrides":{"lsp:ruff":{"severity":"error"}}}}}"#,
)
.unwrap();
assert_eq!(s.ide_profiles["strict"].name, "Strict");
assert_eq!(s.ide_profiles["strict"].overrides["lsp:ruff"].severity, Some(InspectionLevel::Error));
assert!(serde_json::from_str::<InspectionSettings>(r#"{"default":{"x":{"severity":"loud"}}}"#).is_err());
let s: InspectionSettings =
serde_json::from_str(r#"{"default":{"builtin:spelling":{"options":{"processCode":false}}}}"#).unwrap();
assert!(!s.default["builtin:spelling"].options.as_ref().unwrap()["processCode"]);
let back: InspectionSettings = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
assert_eq!(back, s);
}
}
+148
Переглянути файл
@@ -0,0 +1,148 @@
//! Paths on the IPC boundary are `/`-separated on every OS, so the UI can split,
//! join and compare them without caring about Windows. Path fields of IPC types
//! are [`IpcPath`]: a `PathBuf` (derefs to `Path`) that serializes `C:\a\b` (and `\\?\C:\a\b`) as
//! `C:/a/b` and is a plain `string` in the generated bindings.
//!
//! Incoming paths need nothing: Windows accepts `C:/a/b`, and `Path` equality
//! and hashing are component-wise.
use std::borrow::Borrow;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// `p` as the UI sees it: `C:\a\b` and `\\?\C:\a\b` become `C:/a/b`.
pub fn to_ipc(p: &Path) -> String {
#[cfg(windows)]
{
dunce::simplified(p).to_string_lossy().replace('\\', "/")
}
#[cfg(not(windows))]
{
p.to_string_lossy().into_owned()
}
}
/// A path field of an IPC type.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, specta::Type)]
#[specta(transparent)]
pub struct IpcPath(#[specta(type = String)] pub PathBuf);
impl IpcPath {
pub fn into_path_buf(self) -> PathBuf {
self.0
}
}
impl Serialize for IpcPath {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&to_ipc(&self.0))
}
}
impl<'de> Deserialize<'de> for IpcPath {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
PathBuf::deserialize(d).map(Self)
}
}
impl Deref for IpcPath {
type Target = Path;
fn deref(&self) -> &Path {
&self.0
}
}
impl AsRef<Path> for IpcPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl Borrow<Path> for IpcPath {
fn borrow(&self) -> &Path {
&self.0
}
}
impl From<PathBuf> for IpcPath {
fn from(p: PathBuf) -> Self {
Self(p)
}
}
impl From<&Path> for IpcPath {
fn from(p: &Path) -> Self {
Self(p.to_path_buf())
}
}
impl From<&PathBuf> for IpcPath {
fn from(p: &PathBuf) -> Self {
Self(p.clone())
}
}
impl From<String> for IpcPath {
fn from(p: String) -> Self {
Self(p.into())
}
}
impl From<&str> for IpcPath {
fn from(p: &str) -> Self {
Self(p.into())
}
}
impl From<IpcPath> for PathBuf {
fn from(p: IpcPath) -> Self {
p.0
}
}
impl PartialEq<PathBuf> for IpcPath {
fn eq(&self, other: &PathBuf) -> bool {
self.0 == *other
}
}
impl PartialEq<Path> for IpcPath {
fn eq(&self, other: &Path) -> bool {
self.0 == other
}
}
impl PartialEq<&Path> for IpcPath {
fn eq(&self, other: &&Path) -> bool {
self.0 == *other
}
}
impl PartialEq<IpcPath> for PathBuf {
fn eq(&self, other: &IpcPath) -> bool {
*self == other.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_through_json() {
let p = IpcPath::from("a/b");
assert_eq!(serde_json::to_string(&p).unwrap(), r#""a/b""#);
assert_eq!(serde_json::from_str::<IpcPath>(r#""a/b""#).unwrap(), p);
}
#[cfg(windows)]
#[test]
fn windows_paths_use_forward_slashes() {
assert_eq!(to_ipc(Path::new(r"C:\a\b")), "C:/a/b");
assert_eq!(to_ipc(Path::new(r"\\?\C:\a\b")), "C:/a/b");
assert_eq!(to_ipc(&Path::new("C:/a").join("b")), "C:/a/b");
assert_eq!(serde_json::to_string(&IpcPath::from(r"C:\a\b")).unwrap(), r#""C:/a/b""#);
}
}
+26
Переглянути файл
@@ -0,0 +1,26 @@
//! Shared vocabulary for every OpenIDEA crate.
//!
//! Anything that crosses a crate boundary or the IPC boundary lives here, so
//! feature crates depend on `ide-core` and never on each other's internals.
//! All IPC-facing types derive `specta::Type`, which generates `src/bindings.ts`.
pub mod editor_settings;
pub mod error;
pub mod event;
pub mod ids;
pub mod ignored;
pub mod inspection_settings;
pub mod ipc_path;
pub mod process;
pub mod project_dir;
pub mod remote_settings;
pub mod settings;
pub mod text;
pub mod workflow_settings;
pub use error::{IdeError, IdeResult};
pub use event::{EventSink, IdeEvent, NullSink, SharedSink};
pub use ids::*;
pub use ipc_path::IpcPath;
pub use text::*;
pub use tokio_util::sync::CancellationToken;
+53
Переглянути файл
@@ -0,0 +1,53 @@
//! Child-process helpers shared by every crate that spawns tools.
use std::ffi::{OsStr, OsString};
use std::process::Command;
use std::sync::OnceLock;
/// Windows `CREATE_NO_WINDOW` creation flag, for call sites that combine it
/// with other flags. Prefer [`no_window`] otherwise.
pub const CREATE_NO_WINDOW: u32 = 0x0800_0000;
/// Keeps a console child (java, node, curl, a language server, ...) from
/// opening its own console window on Windows. Release builds use the GUI
/// subsystem, so without this every spawn flashes a `cmd` window. A no-op
/// elsewhere. For a `tokio::process::Command`, pass `cmd.as_std_mut()`.
pub fn no_window(cmd: &mut Command) -> &mut Command {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(CREATE_NO_WINDOW);
}
cmd
}
static SHELL_ENV: OnceLock<Vec<(OsString, OsString)>> = OnceLock::new();
/// Records the login shell's environment, read once at startup off the main thread.
pub fn set_shell_env(vars: Vec<(OsString, OsString)>) {
let _ = SHELL_ENV.set(vars);
}
/// The login shell's variables this process lacks: an app started from Finder/Dock gets only
/// PATH from the shell, not what rc files export (API keys and provider switches, SDK homes...).
/// Apply before a call site's own variables so those win. Empty until the shell has been read.
pub fn shell_env() -> impl Iterator<Item = (&'static OsStr, &'static OsStr)> {
SHELL_ENV
.get()
.into_iter()
.flatten()
.filter(|(k, _)| std::env::var_os(k).is_none())
.map(|(k, v)| (k.as_os_str(), v.as_os_str()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shell_env_adds_only_what_the_process_lacks() {
set_shell_env(vec![("OPENIDEA_SHELL_ONLY_VAR".into(), "1".into()), ("PATH".into(), "/from/shell".into())]);
let vars: Vec<_> = shell_env().collect();
assert_eq!(vars, [(OsStr::new("OPENIDEA_SHELL_ONLY_VAR"), OsStr::new("1"))]);
}
}
+11
Переглянути файл
@@ -0,0 +1,11 @@
//! The per-project config folder (`<root>/.openidea/`).
use std::path::{Path, PathBuf};
/// Folder name under the project root.
pub const DIR: &str = ".openidea";
/// Where `rel` (e.g. `settings.toml`) is read from and written to.
pub fn path(root: &Path, rel: impl AsRef<Path>) -> PathBuf {
root.join(DIR).join(rel)
}
+150
Переглянути файл
@@ -0,0 +1,150 @@
//! Settings of remote work: SSH configurations, SFTP deployment and SSH run
//! targets. The fields hang off `Settings` in `settings.rs`.
use serde::{Deserialize, Serialize};
use specta::Type;
/// How an SSH connection authenticates.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum SshAuth {
Password,
/// A private key file, optionally protected by a passphrase.
KeyPair,
/// `~/.ssh/config` (HostName, User, Port, IdentityFile, ProxyJump) and the SSH agent.
#[default]
OpenSsh,
}
/// Tools › SSH Configurations.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct SshSettings {
pub configs: Vec<SshConfiguration>,
}
/// A named SSH host, shared by terminals, deployment, run targets and data sources.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct SshConfiguration {
pub id: String,
pub name: String,
/// Host name, or a `Host` alias of `~/.ssh/config` with [`SshAuth::OpenSsh`].
pub host: String,
pub port: Option<u16>,
pub user: String,
pub auth: SshAuth,
/// Private key for [`SshAuth::KeyPair`]; `~/` is the home directory.
pub key_file: String,
/// Keep the password / key passphrase in the saved passwords.
pub save_secret: bool,
}
impl SshConfiguration {
/// `user@host:port` with what is known (the name when the host is empty).
pub fn label(&self) -> String {
let host = self.host.trim();
if host.is_empty() {
return self.name.clone();
}
let mut s = String::new();
if !self.user.trim().is_empty() {
s.push_str(self.user.trim());
s.push('@');
}
s.push_str(host);
if let Some(p) = self.port {
s.push_str(&format!(":{p}"));
}
s
}
}
/// When local changes are uploaded to the default deployment server.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AutoUpload {
#[default]
Never,
/// Files saved with Save / Save All (⌘S).
OnExplicitSave,
/// Every save, including autosave and external changes.
Always,
}
/// Build, Execution, Deployment › Deployment (and its Options page).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct DeploymentSettings {
pub servers: Vec<DeploymentServer>,
/// Server used by Upload / Download / Sync and automatic upload; empty = none.
pub default_server: String,
pub auto_upload: AutoUpload,
/// Delete remote files when their local file is deleted (automatic upload).
pub delete_remote: bool,
/// Keep local modification times on uploaded files.
pub preserve_timestamps: bool,
/// File names never uploaded or downloaded (`*` and `?` wildcards; `;`-separated in the UI).
pub exclude: Vec<String>,
}
impl Default for DeploymentSettings {
fn default() -> Self {
Self {
servers: Vec::new(),
default_server: String::new(),
auto_upload: AutoUpload::Never,
delete_remote: false,
preserve_timestamps: true,
exclude: [".git", ".svn", ".hg", ".idea", ".openidea", ".DS_Store", "*.pyc", "__pycache__"]
.map(String::from)
.to_vec(),
}
}
}
/// An SFTP server and how project folders map onto it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct DeploymentServer {
pub id: String,
pub name: String,
/// [`SshConfiguration::id`].
pub ssh_config: String,
/// Absolute remote folder the mappings are relative to; empty = the login directory.
pub root_path: String,
/// Web server URL of the root (Open in Browser on the deployed file).
pub web_url: String,
pub mappings: Vec<DeploymentMapping>,
/// Remote paths (relative to the root) never touched.
pub excluded_paths: Vec<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct DeploymentMapping {
/// Project-relative folder; empty = the project root.
pub local: String,
/// Remote folder, relative to the server root (a leading `/` is the root too).
pub deployment: String,
/// Web path, relative to the server's URL.
pub web: String,
}
/// Build, Execution, Deployment › Run Targets: run configurations on another machine.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct RunTarget {
pub id: String,
pub name: String,
/// [`SshConfiguration::id`].
pub ssh_config: String,
/// Remote folder the project is synchronised to and run from.
pub project_root: String,
/// Upload changed project files before each run.
pub sync_before_run: bool,
/// Interpreters on the target; empty = the one on the remote `PATH`.
pub python: String,
pub node: String,
pub java: String,
}
+633
Переглянути файл
@@ -0,0 +1,633 @@
//! User-facing settings. Layered: defaults → global `settings.toml` →
//! project `.openidea/settings.toml`. Loading/merging lives in `ide-workspace`.
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
pub use crate::editor_settings::*;
pub use crate::inspection_settings::*;
pub use crate::remote_settings::*;
pub use crate::workflow_settings::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct Settings {
pub general: GeneralSettings,
pub appearance: AppearanceSettings,
pub editor: EditorSettings,
pub keymap: String,
pub files: FileSettings,
/// Per-action keybinding overrides on top of the `keymap` preset: action id →
/// bindings (`Meta+Shift+O`, chords space-separated). An empty list unbinds.
pub keymap_overrides: BTreeMap<String, Vec<String>>,
/// User keymaps (Keymap › Duplicate…): id → name, parent preset and the
/// keymap's own overrides. `keymap` may name one of these ids.
pub custom_keymaps: BTreeMap<String, CustomKeymap>,
pub notifications: NotificationSettings,
/// Interpreters / toolchains per language (Python, Node.js, Rust, Go, TypeScript).
pub languages: LanguagesSettings,
/// Project Structure: source / test / resource / excluded folders (project-relative).
pub project_structure: ProjectStructureSettings,
/// Version Control (Git, Commit, Confirmation, Issue Navigation).
pub vcs: VcsSettings,
/// Build, Execution, Deployment › Console.
pub console: ConsoleSettings,
pub debugger: DebuggerSettings,
pub run: RunSettings,
/// Tools › Terminal.
pub terminal: TerminalSettings,
pub actions_on_save: ActionsOnSaveSettings,
pub external_tools: Vec<ExternalTool>,
/// Language servers / debug adapters at a user-chosen location, by the
/// command the language definition launches (`elixir-ls`, `codelldb`).
pub tools: BTreeMap<String, ToolSettings>,
/// Editor › Inspections profiles.
pub inspections: InspectionSettings,
/// Editor › Natural Languages › Spelling.
pub spelling: SpellingSettings,
/// Tools › SSH Configurations.
pub ssh: SshSettings,
/// Build, Execution, Deployment › Deployment.
pub deployment: DeploymentSettings,
/// Build, Execution, Deployment › Run Targets.
pub run_targets: Vec<RunTarget>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct ToolSettings {
/// Executable to launch instead of the managed / `PATH` one; empty = auto-detect.
pub path: String,
}
impl Settings {
/// The configured executable for `command`, if any (`~/` not yet expanded).
pub fn tool_path(&self, command: &str) -> Option<&str> {
self.tools.get(command).map(|t| t.path.trim()).filter(|p| !p.is_empty())
}
}
impl Default for Settings {
fn default() -> Self {
Self {
general: GeneralSettings::default(),
appearance: AppearanceSettings::default(),
editor: EditorSettings::default(),
keymap: default_keymap().into(),
files: FileSettings::default(),
keymap_overrides: BTreeMap::new(),
custom_keymaps: BTreeMap::new(),
notifications: NotificationSettings::default(),
languages: LanguagesSettings::default(),
project_structure: ProjectStructureSettings::default(),
vcs: VcsSettings::default(),
console: ConsoleSettings::default(),
debugger: DebuggerSettings::default(),
run: RunSettings::default(),
terminal: TerminalSettings::default(),
actions_on_save: ActionsOnSaveSettings::default(),
external_tools: Vec::new(),
tools: BTreeMap::new(),
inspections: InspectionSettings::default(),
spelling: SpellingSettings::default(),
ssh: SshSettings::default(),
deployment: DeploymentSettings::default(),
run_targets: Vec::new(),
}
}
}
/// IDEA's keymap for the OS (`src/platform/keymaps/`).
fn default_keymap() -> &'static str {
if cfg!(target_os = "macos") { "intellij-macos" } else { "intellij-windows" }
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct CustomKeymap {
pub name: String,
/// Bundled preset the keymap derives from, e.g. `intellij-macos`.
pub parent: String,
/// Action id → bindings on top of `parent`; an empty list unbinds.
pub overrides: BTreeMap<String, Vec<String>>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct GeneralSettings {
pub reopen_last_project: bool,
pub confirm_exit: bool,
/// Where Open… puts a project while one is open: `ask` | `new` | `same`.
pub open_project_in: String,
/// Initial folder of the Open / Clone dialogs; empty = the OS default.
pub default_project_directory: String,
/// Save all files after `idle_seconds` without input.
pub save_on_idle: bool,
pub idle_seconds: u32,
pub date_format: DateFormatSettings,
/// File › Power Save Mode: no automatic popups (completion, parameter info,
/// quick documentation) or background highlighting (usages, code vision).
pub power_save_mode: bool,
/// File › Rename Project… (project layer): the name shown for the project; empty = its folder name.
pub project_name: String,
}
impl Default for GeneralSettings {
fn default() -> Self {
Self {
reopen_last_project: true,
confirm_exit: false,
open_project_in: "ask".into(),
default_project_directory: String::new(),
save_on_idle: false,
idle_seconds: 15,
date_format: DateFormatSettings::default(),
power_save_mode: false,
project_name: String::new(),
}
}
}
/// System Settings › Date Formats (Git log, Local History, blame).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct DateFormatSettings {
/// Use `pattern` / `use24_hour` instead of the system locale's format.
pub override_system: bool,
/// `dd`, `MM`, `MMM`, `yy`, `yyyy` placeholders.
pub pattern: String,
pub use24_hour: bool,
/// "Today", "Yesterday", "5 min ago".
pub pretty: bool,
}
impl Default for DateFormatSettings {
fn default() -> Self {
Self { override_system: false, pattern: "dd.MM.yyyy".into(), use24_hour: true, pretty: true }
}
}
/// Notifications page: global switches and the per-group display type.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct NotificationSettings {
pub balloons: bool,
pub sound: bool,
/// Group id → display; groups not listed use their default.
pub groups: BTreeMap<String, NotificationGroupSettings>,
}
impl Default for NotificationSettings {
fn default() -> Self {
Self { balloons: true, sound: false, groups: BTreeMap::new() }
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct NotificationGroupSettings {
/// `balloon` | `sticky` | `none`
pub display: String,
/// Also list it in the Notifications tool window.
pub log: bool,
}
impl Default for NotificationGroupSettings {
fn default() -> Self {
Self { display: "balloon".into(), log: true }
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct AppearanceSettings {
/// `dark` | `light` | `system` | `dark-hc` (high-contrast dark)
pub theme: String,
pub accent: String,
pub ui_font_size: f32,
pub locale: String,
/// Follow the OS light/dark mode with the preferred themes below.
pub sync_with_os: bool,
pub preferred_light_theme: String,
pub preferred_dark_theme: String,
/// IDE zoom in percent (50–300).
pub zoom: u32,
/// Apply `ui_font_family` / `ui_font_size` (otherwise the theme's).
pub use_custom_font: bool,
pub ui_font_family: String,
pub contrast_scrollbars: bool,
/// Adjust colours for red-green vision deficiency.
pub red_green_vision: bool,
pub different_tool_window_background: bool,
pub compact_mode: bool,
pub full_path_in_title: bool,
pub project_colors_in_toolbar: bool,
pub dnd_with_alt_only: bool,
pub menu_icons: bool,
/// `subpixel` | `greyscale` | `none`
pub ide_antialiasing: String,
pub editor_antialiasing: String,
pub show_tool_window_bars: bool,
pub show_tool_window_names: bool,
pub widescreen_tool_windows: bool,
/// Status bar widgets shown (`true`) or hidden (`false`) by the user
/// (status bar context menu); widgets not listed use their default.
pub status_bar_widgets: BTreeMap<String, bool>,
/// Customize Main Toolbar; `None` = the default toolbar.
pub main_toolbar: Option<MainToolbarSettings>,
}
/// Main toolbar entries per side, in order: title widget ids (`project`,
/// `vcs.branch`…) or `action:<id>`. Widgets in neither list nor `hidden`
/// (registered later) keep their default place.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct MainToolbarSettings {
pub left: Vec<String>,
pub right: Vec<String>,
/// Widgets the user removed.
pub hidden: Vec<String>,
}
impl Default for AppearanceSettings {
fn default() -> Self {
Self {
theme: "light".into(),
accent: "#3871e1".into(),
ui_font_size: 13.0,
locale: "en".into(),
sync_with_os: false,
preferred_light_theme: "light".into(),
preferred_dark_theme: "dark".into(),
zoom: 100,
use_custom_font: false,
ui_font_family: "Inter".into(),
contrast_scrollbars: false,
red_green_vision: false,
different_tool_window_background: false,
compact_mode: false,
full_path_in_title: false,
project_colors_in_toolbar: true,
dnd_with_alt_only: false,
menu_icons: true,
ide_antialiasing: "greyscale".into(),
editor_antialiasing: "greyscale".into(),
show_tool_window_bars: true,
show_tool_window_names: true,
widescreen_tool_windows: false,
status_bar_widgets: BTreeMap::new(),
main_toolbar: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct EditorSettings {
pub font_family: String,
pub font_size: f32,
pub line_height: f32,
pub tab_size: u32,
pub insert_spaces: bool,
pub soft_wrap: bool,
pub show_whitespace: bool,
pub inlay_hints: bool,
pub sticky_scroll: bool,
pub format_on_save: bool,
/// Editor colour scheme id; `theme-default` follows the IDE theme.
pub color_scheme: String,
/// Color Scheme edits: scheme id → attribute id → override.
pub color_overrides: BTreeMap<String, BTreeMap<String, TextAttributesOverride>>,
/// Color Scheme › Language Defaults › Semantic highlighting: local variables and
/// parameters get a colour derived from their name (IDEA's rainbow identifiers).
pub semantic_highlighting: bool,
/// Color Scheme › Language Defaults › Rainbow brackets: `()`, `[]`, `{}` coloured by depth.
pub rainbow_brackets: bool,
pub ligatures: bool,
/// Fallback font for glyphs the main font lacks; empty = none.
pub fallback_font: String,
/// Typography Settings › Main font weight (CSS 100–900), as IDEA.
pub font_weight: u16,
/// Typography Settings › Bold font weight: wherever the editor renders bold.
pub bold_font_weight: u16,
pub general: EditorGeneralSettings,
pub appearance: EditorAppearanceSettings,
pub breadcrumbs: BreadcrumbsSettings,
pub completion: CompletionSettings,
pub auto_import: AutoImportSettings,
pub folding: FoldingSettings,
pub tabs: EditorTabsSettings,
pub smart_keys: SmartKeysSettings,
pub code_editing: CodeEditingSettings,
pub inlay: InlayHintsSettings,
pub inline_completion: InlineCompletionSettings,
/// Gutter icon groups turned off (`run`, `breakpoints`, `bookmarks`, `vcs`, `blame`).
pub gutter_icons_disabled: Vec<String>,
pub show_gutter_icons: bool,
pub code_style: CodeStyleSettings,
pub encodings: EncodingSettings,
}
impl Default for EditorSettings {
fn default() -> Self {
Self {
font_family: "JetBrains Mono".into(),
font_size: 13.0,
line_height: 22.0,
tab_size: 4,
insert_spaces: true,
soft_wrap: true,
show_whitespace: false,
inlay_hints: true,
sticky_scroll: true,
format_on_save: false,
color_scheme: "theme-default".into(),
color_overrides: BTreeMap::new(),
semantic_highlighting: false,
rainbow_brackets: false,
ligatures: false,
fallback_font: String::new(),
font_weight: 400,
bold_font_weight: 700,
general: EditorGeneralSettings::default(),
appearance: EditorAppearanceSettings::default(),
breadcrumbs: BreadcrumbsSettings::default(),
completion: CompletionSettings::default(),
auto_import: AutoImportSettings::default(),
folding: FoldingSettings::default(),
tabs: EditorTabsSettings::default(),
smart_keys: SmartKeysSettings::default(),
code_editing: CodeEditingSettings::default(),
inlay: InlayHintsSettings::default(),
inline_completion: InlineCompletionSettings::default(),
gutter_icons_disabled: Vec::new(),
show_gutter_icons: true,
code_style: CodeStyleSettings::default(),
encodings: EncodingSettings::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct FileSettings {
/// Save on focus loss / tab switch, like IDEA.
pub auto_save: bool,
pub exclude: Vec<String>,
/// Ignored Files and Folders: name patterns hidden everywhere; see [`crate::ignored`].
pub ignored_names: Vec<String>,
pub trim_trailing_whitespace: bool,
pub ensure_final_newline: bool,
/// Local History keeps changes from this many working days (days with changes), like IDEA.
pub local_history_days: u32,
/// Upper bound for a project's Local History in MB; the oldest revisions go first.
pub local_history_max_mb: u32,
/// File Types › Recognized File Types: the user's changes, by language id.
pub file_types: BTreeMap<String, FileTypeAssociation>,
/// Override File Type: file (project-relative and `/`-separated, or absolute) → language id.
pub file_type_overrides: BTreeMap<String, String>,
}
/// One language's file name and hashbang patterns on top of its definition.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct FileTypeAssociation {
/// Added file name patterns: `*.foo`, `Jenkinsfile`, `Dockerfile.*` (`*` and `?` wildcards).
pub patterns: Vec<String>,
/// Patterns of the definition (`*.ext`, file names) the user removed.
pub removed_patterns: Vec<String>,
/// Interpreters run by a `#!` first line, e.g. `node` for `#!/usr/bin/env node`.
pub hashbangs: Vec<String>,
}
impl Default for FileSettings {
fn default() -> Self {
Self {
auto_save: true,
exclude: vec!["**/.git".into(), "**/node_modules".into(), "**/target".into()],
ignored_names: crate::ignored::DEFAULT_IGNORED_NAMES.iter().map(|s| (*s).into()).collect(),
trim_trailing_whitespace: true,
ensure_final_newline: true,
local_history_days: 5,
local_history_max_mb: 512,
file_types: BTreeMap::new(),
file_type_overrides: BTreeMap::new(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct LanguagesSettings {
pub python: PythonSettings,
pub node: NodeSettings,
pub rust: RustSettings,
pub go: GoSettings,
pub typescript: TypeScriptSettings,
pub java: JavaSettings,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct PythonSettings {
/// Project interpreter executable; empty = auto-detect (project venv, else `python3`).
pub interpreter: String,
/// Activate the interpreter's virtualenv in new terminal sessions.
pub activate_virtualenv: bool,
}
impl Default for PythonSettings {
fn default() -> Self {
Self { interpreter: String::new(), activate_virtualenv: true }
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct NodeSettings {
/// `node` executable; empty = the one on `PATH`.
pub interpreter: String,
/// `npm` | `pnpm` | `yarn`; empty = detect from the lock file / `packageManager`.
pub package_manager: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct RustSettings {
/// Directory holding `cargo` / `rustc`; empty = `PATH` / rustup default.
pub toolchain: String,
/// Standard library sources (`RUST_SRC_PATH`); empty = from the toolchain.
pub stdlib: String,
/// Run the external linter on save (rust-analyzer `checkOnSave`).
pub check_on_save: bool,
/// `check` | `clippy` (rust-analyzer `check.command`).
pub check_command: String,
/// Extra arguments for rustfmt (rust-analyzer `rustfmt.extraArgs`), space-separated.
pub rustfmt_args: String,
}
impl Default for RustSettings {
fn default() -> Self {
Self {
toolchain: String::new(),
stdlib: String::new(),
check_on_save: true,
check_command: "check".into(),
rustfmt_args: String::new(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct GoSettings {
/// Empty = the `go` on `PATH`.
pub goroot: String,
/// Empty = `go env GOPATH`.
pub gopath: String,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct JavaSettings {
/// Project JDK home; empty = per module, the installed JDK matching the
/// Java version it targets.
pub jdk: String,
/// JDK home the Java language server (jdtls) runs on, apart from the project
/// JDK (IDEA's "JDK for importer"); empty = the project JDK when new enough,
/// else the first installed one that is.
pub jdtls_jdk: String,
/// Extra JVM options for the Java language server (jdtls), space-separated
/// (VS Code's `java.jdt.ls.vmargs`).
pub jdtls_vm_args: String,
/// Build Tools › Maven.
pub maven: MavenSettings,
}
/// Build Tools › Maven.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct MavenSettings {
/// Maven home (or its `mvn` executable); empty = the project's `mvnw`, else `mvn` on `PATH`.
pub home: String,
/// User `settings.xml` (`-s`); empty = `~/.m2/settings.xml`.
pub user_settings: String,
/// Local repository (`-Dmaven.repo.local`); empty = from `settings.xml`.
pub local_repository: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct TypeScriptSettings {
/// `bundled` (the server's own TypeScript), `workspace` (`node_modules/typescript`),
/// or a path to a TypeScript `lib` directory.
pub version: String,
}
impl Default for TypeScriptSettings {
fn default() -> Self {
Self { version: "bundled".into() }
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct ProjectStructureSettings {
/// Project-relative folders, `/`-separated.
pub source_roots: Vec<String>,
pub test_roots: Vec<String>,
pub resource_roots: Vec<String>,
pub test_resource_roots: Vec<String>,
/// Excluded from the project tree, search and indexing.
pub excluded: Vec<String>,
/// Auto-detected roots (from `pom.xml`, `build.gradle`, `Cargo.toml`, …) the user unmarked.
pub unmarked: Vec<String>,
/// Content roots besides the project folder (absolute paths), shown and searched with it.
pub content_roots: Vec<String>,
/// "Exclude files": `;`-separated name patterns (`*.log;tmp*`) excluded at any depth.
pub exclude_files: String,
}
impl ProjectStructureSettings {
/// The "Exclude files" name patterns.
pub fn exclude_file_patterns(&self) -> Vec<String> {
self.exclude_files.split(';').map(str::trim).filter(|p| !p.is_empty()).map(str::to_owned).collect()
}
}
impl Settings {
/// `files.exclude`, the ignored names and the Project Structure excluded folders as globs.
pub fn exclude_globs(&self) -> Vec<String> {
let mut out = self.file_excludes();
for dir in &self.project_structure.excluded {
let dir = dir.trim().trim_matches('/');
if !dir.is_empty() {
out.push(format!("/{dir}"));
}
}
out
}
/// `files.exclude`, `files.ignoredNames` and the Project Structure "Exclude files"
/// patterns (a bare name matches at any depth).
pub fn file_excludes(&self) -> Vec<String> {
let mut out = self.files.exclude.clone();
out.extend(self.files.ignored_names.iter().filter(|n| !n.trim().is_empty()).cloned());
out.extend(self.project_structure.exclude_file_patterns().into_iter().filter(|p| !p.contains('/')));
out
}
pub fn ignored_names(&self) -> crate::ignored::IgnoredNames {
crate::ignored::IgnoredNames::new(&self.files.ignored_names)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn editor_font_weights_default_to_regular_and_bold() {
let json = serde_json::to_value(Settings::default()).unwrap();
assert_eq!(json["editor"]["fontWeight"], 400);
assert_eq!(json["editor"]["boldFontWeight"], 700);
let s: Settings = serde_json::from_str(r#"{"editor":{"fontWeight":300,"boldFontWeight":600}}"#).unwrap();
assert_eq!((s.editor.font_weight, s.editor.bold_font_weight), (300, 600));
assert_eq!(s.editor.font_size, 13.0);
}
#[test]
fn keymap_overrides_are_camel_case_and_default_empty() {
let s: Settings =
serde_json::from_str(r#"{"keymapOverrides":{"file.save":["Meta+S","Ctrl+S"],"vcs.commit":[]}}"#).unwrap();
assert_eq!(s.keymap_overrides["file.save"], vec!["Meta+S", "Ctrl+S"]);
assert!(s.keymap_overrides["vcs.commit"].is_empty());
assert_eq!(s.keymap, default_keymap());
let json = serde_json::to_value(Settings::default()).unwrap();
assert_eq!(json["keymapOverrides"], serde_json::json!({}));
}
#[test]
fn custom_keymaps_round_trip() {
let s: Settings = serde_json::from_str(
r#"{"keymap":"custom.mine","customKeymaps":{"custom.mine":{"name":"Mine","parent":"vscode-macos","overrides":{"file.save":[]}}}}"#,
)
.unwrap();
let k = &s.custom_keymaps["custom.mine"];
assert_eq!((k.name.as_str(), k.parent.as_str()), ("Mine", "vscode-macos"));
assert!(k.overrides["file.save"].is_empty());
assert_eq!(serde_json::to_value(Settings::default()).unwrap()["customKeymaps"], serde_json::json!({}));
}
#[test]
fn exclude_files_patterns_join_the_file_excludes() {
let s: Settings = serde_json::from_str(
r#"{"files":{"exclude":[],"ignoredNames":[]},"projectStructure":{"excludeFiles":" *.log ; tmp*;;"}}"#,
)
.unwrap();
assert_eq!(s.project_structure.exclude_file_patterns(), vec!["*.log", "tmp*"]);
assert_eq!(s.file_excludes(), vec!["*.log", "tmp*"]);
assert!(Settings::default().files.file_types.is_empty());
}
}
+205
Переглянути файл
@@ -0,0 +1,205 @@
//! Text coordinates used across the IPC boundary.
//!
//! Convention: **all offsets and columns crossing IPC are UTF-16 code units**,
//! matching JavaScript strings, CodeMirror and the LSP default encoding.
//! Lines are 0-based. Conversion to/from byte offsets lives in `ide-text`.
use serde::{Deserialize, Serialize};
use specta::Type;
use std::path::PathBuf;
/// A 0-based line / UTF-16 column position.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Type)]
pub struct Pos {
pub line: u32,
pub col: u32,
}
impl Pos {
pub const fn new(line: u32, col: u32) -> Self {
Self { line, col }
}
}
/// Half-open range `[start, end)`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Type)]
pub struct Range {
pub start: Pos,
pub end: Pos,
}
impl Range {
pub const fn new(start: Pos, end: Pos) -> Self {
Self { start, end }
}
pub const fn point(p: Pos) -> Self {
Self { start: p, end: p }
}
pub fn is_empty(&self) -> bool {
self.start == self.end
}
}
/// A range inside a specific file, or inside a virtual document (library
/// class contents, decompiled code…) that only a language server can provide.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
pub struct Location {
/// File path; for virtual targets the URI itself (equal to `uri`), which
/// the UI opens as a read-only virtual document.
pub path: crate::IpcPath,
pub range: Range,
/// Set for non-`file:` targets, e.g. `jdt://contents/…`.
#[specta(optional)]
pub uri: Option<String>,
}
/// Whether `s` is a non-`file:` URI (`scheme:` with a scheme of 2+ chars, so
/// Windows drive letters don't count).
pub fn is_virtual_uri(s: &str) -> bool {
let Some(colon) = s.find(':') else { return false };
let scheme = &s[..colon];
scheme.len() >= 2
&& !scheme.eq_ignore_ascii_case("file")
&& scheme.starts_with(|c: char| c.is_ascii_alphabetic())
&& scheme.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
}
impl Location {
pub fn file(path: PathBuf, range: Range) -> Self {
Self { path: path.into(), range, uri: None }
}
}
/// Replace the text in `[from, to)` (UTF-16 offsets into the document *before*
/// the change set is applied) with `insert`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct TextChange {
pub from: u32,
pub to: u32,
pub insert: String,
}
/// Ordered, non-overlapping changes, all expressed in the coordinates of the
/// original document (CodeMirror `ChangeSet` semantics).
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct ChangeSet {
pub changes: Vec<TextChange>,
}
/// An edit expressed in line/column coordinates (LSP `TextEdit`).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct TextEdit {
pub range: Range,
pub new_text: String,
}
/// Edits to many files, applied atomically from the user's point of view.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct WorkspaceEdit {
pub files: Vec<FileEdit>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum FileEdit {
Edit { path: crate::IpcPath, edits: Vec<TextEdit> },
Create { path: crate::IpcPath, overwrite: bool },
Rename { from: crate::IpcPath, to: crate::IpcPath },
Delete { path: crate::IpcPath, recursive: bool },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum Severity {
Error,
Warning,
Info,
Hint,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct Diagnostic {
pub range: Range,
pub severity: Severity,
pub message: String,
/// Producer, e.g. `rust-analyzer`, `eslint`.
pub source: Option<String>,
pub code: Option<String>,
#[serde(default)]
pub unnecessary: bool,
#[serde(default)]
pub deprecated: bool,
/// Language server (or tool) that reported it, e.g. `basedpyright`; `source`
/// is what the producer calls itself. Inspection profiles key on this.
#[serde(default)]
#[specta(optional)]
pub server: Option<String>,
}
/// Symbol kinds, a stable subset of LSP `SymbolKind`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum SymbolKind {
File,
Module,
Namespace,
Package,
Class,
Interface,
Struct,
Enum,
EnumMember,
Trait,
Function,
Method,
Constructor,
Field,
Property,
Variable,
Constant,
TypeParameter,
TypeAlias,
Macro,
Event,
Operator,
Key,
Unknown,
}
impl SymbolKind {
/// "Class-like" symbols are what IDEA's *Go to Class* (⌘O) searches.
pub fn is_type_like(self) -> bool {
matches!(self, Self::Class | Self::Interface | Self::Struct | Self::Enum | Self::Trait | Self::TypeAlias)
}
}
/// Declared (or conventional) visibility of an outline symbol.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum Visibility {
Public,
Protected,
/// Package / module / assembly scope (Java default, `internal`, `pub(crate)`, Go unexported).
Package,
Private,
}
/// A node of a document outline (Structure tool window, breadcrumbs).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct OutlineItem {
pub name: String,
pub detail: Option<String>,
pub kind: SymbolKind,
/// Whole extent of the symbol.
pub range: Range,
/// The identifier itself; where navigation lands.
pub selection_range: Range,
pub children: Vec<OutlineItem>,
/// Unknown (`None`) for language-server symbols and languages without the notion.
#[serde(default)]
#[specta(optional)]
pub visibility: Option<Visibility>,
}
/// Fuzzy-match highlight: indices (in chars of the displayed string) that matched.
pub type MatchIndices = Vec<u32>;
+333
Переглянути файл
@@ -0,0 +1,333 @@
//! Settings of IDEA's Version Control, Build, Execution, Deployment and Tools
//! groups. The fields hang off `Settings` in `settings.rs`.
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
/// Version Control (main page) and its sub-pages.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct VcsSettings {
/// Colour folders that contain changed files (project tree, tabs).
pub show_dirty_directories: bool,
/// Stop file history (Show History) after `history_limit` commits.
pub limit_history: bool,
pub history_limit: u32,
pub git: GitSettings,
pub commit: CommitSettings,
pub confirmation: VcsConfirmationSettings,
/// Issue Navigation: commit-message patterns turned into links.
pub issue_navigation: Vec<IssueLink>,
}
impl Default for VcsSettings {
fn default() -> Self {
Self {
// IDEA's VcsConfiguration.SHOW_DIRTY_RECURSIVELY is off by default.
show_dirty_directories: false,
limit_history: true,
history_limit: 1000,
git: GitSettings::default(),
commit: CommitSettings::default(),
confirmation: VcsConfirmationSettings::default(),
issue_navigation: Vec::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct GitSettings {
/// Path to the git executable; empty = auto-detect.
pub executable: String,
/// Default of Update Project: `merge` | `rebase`.
pub update_method: String,
/// Push rejected (non-fast-forward): update the branch and push again.
pub auto_update_on_push_rejected: bool,
pub warn_crlf: bool,
pub warn_detached_head: bool,
/// Background check for incoming commits: `auto` | `always` | `never`.
pub incoming_check: String,
/// Branch name globs, `;`/`,`-separated; force push to them is prohibited.
pub protected_branches: String,
/// Initial state of Commit Options › Sign-off Commit.
pub sign_off: bool,
}
impl Default for GitSettings {
fn default() -> Self {
Self {
executable: String::new(),
update_method: "merge".into(),
auto_update_on_push_rejected: false,
warn_crlf: true,
warn_detached_head: true,
incoming_check: "auto".into(),
protected_branches: "master; main".into(),
sign_off: false,
}
}
}
/// Version Control › Commit: commit checks and commit-message inspections.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct CommitSettings {
pub analyze_code: bool,
pub check_todo: bool,
pub reformat: bool,
pub optimize_imports: bool,
pub cleanup: bool,
pub blank_line_after_subject: bool,
pub limit_subject: bool,
pub subject_limit: u32,
pub limit_body: bool,
pub body_limit: u32,
pub show_right_margin: bool,
pub wrap_at_margin: bool,
}
impl Default for CommitSettings {
fn default() -> Self {
Self {
analyze_code: true,
check_todo: true,
reformat: false,
optimize_imports: false,
cleanup: false,
blank_line_after_subject: true,
limit_subject: true,
subject_limit: 72,
limit_body: false,
body_limit: 72,
show_right_margin: true,
wrap_at_margin: false,
}
}
}
/// Version Control › Confirmation. Values: `ask` | `silently` | `never`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct VcsConfirmationSettings {
pub on_create: String,
pub on_delete: String,
}
impl Default for VcsConfirmationSettings {
fn default() -> Self {
Self { on_create: "ask".into(), on_delete: "ask".into() }
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct IssueLink {
/// Regular expression matched in commit messages.
pub pattern: String,
/// Link with `$0` (whole match) / `$1`… (groups) placeholders.
pub link: String,
}
/// Build, Execution, Deployment › Console.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct ConsoleSettings {
pub soft_wrap: bool,
pub override_buffer: bool,
pub buffer_kb: u32,
/// Encoding of process output (`UTF-8`, `windows-1252`, `GBK`…).
pub encoding: String,
/// Consecutive lines containing any of these are folded.
pub fold_patterns: Vec<String>,
}
impl Default for ConsoleSettings {
fn default() -> Self {
Self {
soft_wrap: false,
override_buffer: true,
buffer_kb: 1024,
encoding: "UTF-8".into(),
fold_patterns: vec![
"at java.base/jdk.internal.reflect.".into(),
"at java.base/java.lang.reflect.Method.invoke".into(),
"at org.junit.".into(),
"at node:internal/".into(),
"<frozen importlib._bootstrap".into(),
],
}
}
}
/// Build, Execution, Deployment › Debugger (+ Data Views, Stepping, Python Debugger).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct DebuggerSettings {
pub show_on_breakpoint: bool,
pub focus_app_on_breakpoint: bool,
pub hide_on_termination: bool,
pub breakpoints_any_file: bool,
pub inline_values: bool,
pub value_tooltip: bool,
pub value_tooltip_delay: u32,
/// Stepping: do not step into library code (debugpy `justMyCode`, js-debug `skipFiles`).
pub skip_library_code: bool,
pub python: PythonDebuggerSettings,
}
impl Default for DebuggerSettings {
fn default() -> Self {
Self {
show_on_breakpoint: true,
focus_app_on_breakpoint: false,
hide_on_termination: false,
breakpoints_any_file: false,
inline_values: true,
value_tooltip: true,
value_tooltip_delay: 700,
skip_library_code: true,
python: PythonDebuggerSettings::default(),
}
}
}
/// Python Debugger (debugpy launch options).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct PythonDebuggerSettings {
pub attach_subprocess: bool,
pub gevent: bool,
}
impl Default for PythonDebuggerSettings {
fn default() -> Self {
Self { attach_subprocess: true, gevent: false }
}
}
/// Run/Debug configuration behaviour.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct RunSettings {
pub confirm_rerun: bool,
/// Ask before closing a Run tab whose process is still running.
pub confirm_terminate: bool,
pub show_on_start: bool,
/// Grace period between interrupt and kill when stopping a process.
pub termination_timeout_ms: u32,
}
impl Default for RunSettings {
fn default() -> Self {
Self { confirm_rerun: true, confirm_terminate: true, show_on_start: true, termination_timeout_ms: 3000 }
}
}
/// Tools › Terminal.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct TerminalSettings {
/// Empty = project root; relative paths resolve against it.
pub start_directory: String,
pub env: BTreeMap<String, String>,
/// Shell command line; empty = the login shell.
pub shell_path: String,
pub tab_name: String,
pub audible_bell: bool,
pub close_on_exit: bool,
pub mouse_reporting: bool,
pub copy_on_selection: bool,
pub paste_on_middle_click: bool,
pub override_ide_shortcuts: bool,
pub shell_integration: bool,
pub highlight_hyperlinks: bool,
pub option_as_meta: bool,
/// Escape moves focus to the editor instead of reaching the shell.
pub escape_to_editor: bool,
/// `block` | `underline` | `bar`.
pub cursor_shape: String,
pub cursor_blink: bool,
pub use_editor_font: bool,
pub font_family: String,
pub font_size: f32,
/// Multiple of the cell height.
pub line_height: f32,
/// Font tried for glyphs the main font lacks; empty = none.
pub fallback_font_family: String,
/// Multiple of the cell width.
pub column_width: f32,
}
impl Default for TerminalSettings {
fn default() -> Self {
Self {
start_directory: String::new(),
env: BTreeMap::new(),
shell_path: String::new(),
tab_name: "Local".into(),
audible_bell: true,
close_on_exit: true,
mouse_reporting: true,
copy_on_selection: false,
paste_on_middle_click: true,
override_ide_shortcuts: true,
shell_integration: true,
highlight_hyperlinks: true,
option_as_meta: false,
escape_to_editor: false,
cursor_shape: "block".into(),
cursor_blink: true,
use_editor_font: true,
font_family: "JetBrains Mono".into(),
font_size: 13.0,
line_height: 1.0,
fallback_font_family: String::new(),
column_width: 1.0,
}
}
}
/// Tools › Actions on Save (Reformat code is `editor.formatOnSave`).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct ActionsOnSaveSettings {
pub optimize_imports: bool,
/// Run code cleanup (`source.fixAll`).
pub cleanup: bool,
}
/// Tools › External Tools entry.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(default, rename_all = "camelCase")]
pub struct ExternalTool {
pub id: String,
pub name: String,
pub description: String,
pub group: String,
pub enabled: bool,
pub program: String,
/// Arguments with macros (`$FilePath$`, `$ProjectFileDir$`…), shell-quoted.
pub arguments: String,
pub working_directory: String,
pub synchronize_after: bool,
pub open_console: bool,
}
impl Default for ExternalTool {
fn default() -> Self {
Self {
id: String::new(),
name: String::new(),
description: String::new(),
group: "External Tools".into(),
enabled: true,
program: String::new(),
arguments: String::new(),
working_directory: "$ProjectFileDir$".into(),
synchronize_after: true,
open_console: true,
}
}
}
+18
Переглянути файл
@@ -0,0 +1,18 @@
[package]
name = "ide-coverage"
description = "Run with Coverage: coverage-tool launch plans (cargo-llvm-cov, vitest / jest / c8, coverage.py, go test, JaCoCo) and report parsers (lcov, Go cover profiles, JaCoCo XML)"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
ide-core.workspace = true
ide-run.workspace = true
quick-xml = "0.42"
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
tracing.workspace = true
[dev-dependencies]
tempfile.workspace = true
+106
Переглянути файл
@@ -0,0 +1,106 @@
//! Go cover profiles (`go test -coverprofile`):
//!
//! ```text
//! mode: set
//! example.com/m/pkg/calc.go:3.24,5.2 1 1
//! ```
//!
//! Each block `file:startLine.startCol,endLine.endCol statements count`
//! covers its lines; a line touched by covered and uncovered blocks is partial.
use crate::Accumulator;
use std::path::{Path, PathBuf};
/// File of an import path: below `module` → `module_dir`, absolute paths as they are.
fn resolve(file: &str, module: &str, module_dir: &Path) -> PathBuf {
let p = Path::new(file);
if p.is_absolute() {
return p.to_path_buf();
}
if !module.is_empty() {
if file == module {
return module_dir.to_path_buf();
}
if let Some(rest) = file.strip_prefix(module).and_then(|r| r.strip_prefix('/')) {
return module_dir.join(rest);
}
}
module_dir.join(file)
}
pub(crate) fn parse(text: &str, module: &str, module_dir: &Path) -> Accumulator {
let mut acc = Accumulator::default();
for raw in text.lines() {
let line = raw.trim();
if line.is_empty() || line.starts_with("mode:") {
continue;
}
// `name.go:3.24,5.2 1 1` — the file name may contain ':' only on Windows drives, so split from the right.
let mut fields = line.rsplitn(3, ' ');
let (Some(count), Some(_stmts), Some(loc)) = (fields.next(), fields.next(), fields.next()) else { continue };
let Ok(count) = count.parse::<u64>() else { continue };
let Some((file, range)) = loc.rsplit_once(':') else { continue };
let Some((start, end)) = range.split_once(',') else { continue };
let line_of = |s: &str| s.split('.').next().and_then(|l| l.parse::<u32>().ok());
let (Some(first), Some(last)) = (line_of(start), line_of(end)) else { continue };
if first == 0 || last < first {
continue;
}
let path = resolve(file, module, module_dir);
for n in first..=last {
acc.line(&path, n - 1, count);
acc.branch(&path, n - 1, range.to_owned(), count > 0);
}
}
// Blocks are not branches: keep only the partial marks of lines with mixed blocks.
acc.drop_unanimous_branches();
acc
}
impl Accumulator {
/// Removes the branch data of lines whose branches are all taken or all missed.
fn drop_unanimous_branches(&mut self) {
for lines in self.files.values_mut() {
for acc in lines.values_mut() {
let taken = acc.branches.values().filter(|t| **t).count();
if taken == 0 || taken == acc.branches.len() {
acc.branches.clear();
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn maps_import_paths_and_marks_mixed_lines_partial() {
let text = "mode: count\n\
example.com/m/calc/calc.go:3.24,5.2 1 4\n\
example.com/m/calc/calc.go:5.2,7.3 2 0\n\
example.com/m/calc/calc.go:9.1,9.20 1 0\n\
/abs/other.go:1.1,1.5 1 1\n";
let suite = parse(text, "example.com/m", Path::new("/proj")).finish("go");
assert!(!suite.has_branches);
assert_eq!(suite.files.len(), 2);
let other = &suite.files[0];
assert_eq!(other.path.as_ref() as &Path, Path::new("/abs/other.go"));
let calc = &suite.files[1];
assert_eq!(calc.path.as_ref() as &Path, Path::new("/proj/calc/calc.go"));
let at = |n: u32| calc.lines.iter().find(|l| l.line == n).unwrap();
assert_eq!(at(2).hits, 4);
assert!(!at(2).partial());
assert!(at(4).partial(), "line 5 has a covered and an uncovered block");
assert_eq!(at(5).hits, 0);
assert_eq!(at(8).hits, 0);
assert_eq!((calc.lines_total, calc.lines_covered), (6, 3));
}
#[test]
fn skips_malformed_lines() {
let suite = parse("mode: set\nnot a block\nx.go:a.1,2.1 1 1\n", "", Path::new("/p")).finish("go");
assert!(suite.files.is_empty());
}
}
+173
Переглянути файл
@@ -0,0 +1,173 @@
//! "Import a report collected in CI from disk": the format is sniffed from the
//! content (lcov, Go cover profile, JaCoCo XML) and paths recorded on another
//! machine are mapped into the project by their longest existing suffix.
use crate::{Accumulator, CoverageSuite, goprofile, jacoco, lcov};
use ide_core::{IdeError, IdeResult};
use std::path::{Component, Path, PathBuf};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Format {
Lcov,
GoProfile,
Jacoco,
}
pub(crate) fn detect(text: &str) -> Option<Format> {
let head = text.trim_start_matches('\u{feff}').trim_start();
if head.starts_with('<') {
return head.contains("<report").then_some(Format::Jacoco);
}
let first = head.lines().next().unwrap_or_default();
if first.starts_with("mode:") {
return Some(Format::GoProfile);
}
head.lines().any(|l| l.trim_start().starts_with("SF:")).then_some(Format::Lcov)
}
/// Reads the report at `path` for the project at `root`. Blocking.
pub fn import(path: &Path, root: &Path) -> IdeResult<CoverageSuite> {
let text = std::fs::read_to_string(path)?;
let mut suite = match detect(&text) {
Some(Format::Lcov) => lcov::parse(&text, root).finish("lcov"),
Some(Format::GoProfile) => {
let (module, module_dir) = go_module_for(root, &text);
goprofile::parse(&text, &module, &module_dir).finish("go test -cover")
}
Some(Format::Jacoco) => {
// A report inside the project (`<module>/target/site/jacoco/…`) knows its module.
let module = jacoco::module_dir(path);
let module =
if path.starts_with(root) && module.starts_with(root) && module != path.parent().unwrap_or(path) {
module
} else {
root.to_path_buf()
};
let mut acc = Accumulator { has_branches: true, ..Default::default() };
jacoco::parse_into(&text, &module, &mut acc)
.map_err(|e| IdeError::invalid(format!("{}: {e}", path.display())))?;
acc.finish("JaCoCo")
}
None => {
return Err(IdeError::invalid(format!(
"{} is not an lcov tracefile, Go cover profile or JaCoCo XML report",
path.display()
)));
}
};
for f in &mut suite.files {
let p: &Path = f.path.as_ref();
if !p.exists()
&& let Some(found) = relocate(p, root)
{
f.path = found.into();
}
}
suite.files.sort_by(|a, b| (a.path.as_ref() as &Path).cmp(b.path.as_ref()));
Ok(suite)
}
/// `/home/runner/work/app/app/src/lib.rs` → `<root>/src/lib.rs`: the longest suffix of
/// `path` that exists under `root`.
pub(crate) fn relocate(path: &Path, root: &Path) -> Option<PathBuf> {
let parts: Vec<&std::ffi::OsStr> = path
.components()
.filter_map(|c| match c {
Component::Normal(s) => Some(s),
_ => None,
})
.collect();
(0..parts.len()).find_map(|skip| {
let candidate = parts[skip..].iter().fold(root.to_path_buf(), |p, s| p.join(s));
candidate.is_file().then_some(candidate)
})
}
/// The Go module the profile's import paths belong to: the project's `go.mod`, else one
/// a level or two down whose module path prefixes the profile's files.
fn go_module_for(root: &Path, profile: &str) -> (String, PathBuf) {
let first = profile
.lines()
.map(str::trim)
.find(|l| !l.is_empty() && !l.starts_with("mode:"))
.and_then(|l| l.rsplit_once(':').map(|(f, _)| f.to_owned()))
.unwrap_or_default();
let matches = |module: &str| !module.is_empty() && (first == module || first.starts_with(&format!("{module}/")));
let read = |dir: &Path| -> Option<String> {
let text = std::fs::read_to_string(dir.join("go.mod")).ok()?;
text.lines().find_map(|l| l.trim().strip_prefix("module")).map(|m| m.trim().trim_matches('"').to_owned())
};
let mut candidates = vec![root.to_path_buf()];
for dir in std::fs::read_dir(root).into_iter().flatten().flatten().map(|e| e.path()).filter(|p| p.is_dir()) {
candidates
.extend(std::fs::read_dir(&dir).into_iter().flatten().flatten().map(|e| e.path()).filter(|p| p.is_dir()));
candidates.push(dir);
}
let found: Vec<(String, PathBuf)> = candidates.into_iter().filter_map(|d| read(&d).map(|m| (m, d))).collect();
found
.iter()
.find(|(m, _)| matches(m))
.or_else(|| found.iter().find(|(_, d)| d == root))
.cloned()
.unwrap_or_else(|| (String::new(), root.to_path_buf()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_formats() {
assert_eq!(detect("TN:\nSF:a.rs\nDA:1,1\nend_of_record\n"), Some(Format::Lcov));
assert_eq!(detect("mode: atomic\nm/a.go:1.1,2.2 1 0\n"), Some(Format::GoProfile));
assert_eq!(
detect("<?xml version=\"1.0\"?><!DOCTYPE report><report name=\"x\"></report>"),
Some(Format::Jacoco)
);
assert_eq!(detect("<?xml version=\"1.0\"?><coverage/>"), None);
assert_eq!(detect("hello"), None);
}
#[test]
fn imports_ci_lcov_with_foreign_paths() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("app");
std::fs::create_dir_all(root.join("src")).unwrap();
std::fs::write(root.join("src/lib.rs"), "").unwrap();
let report = tmp.path().join("lcov.info");
std::fs::write(&report, "SF:/home/runner/work/app/app/src/lib.rs\nDA:1,1\nDA:2,0\nend_of_record\n").unwrap();
let suite = import(&report, &root).unwrap();
assert_eq!(suite.runner, "lcov");
assert_eq!(suite.files[0].path.as_ref() as &Path, root.join("src/lib.rs"));
assert_eq!(suite.files[0].lines_covered, 1);
assert!(import(&tmp.path().join("missing"), &root).is_err());
std::fs::write(&report, "nothing").unwrap();
assert!(import(&report, &root).is_err());
}
#[test]
fn imports_go_profiles_of_nested_modules() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::create_dir_all(root.join("svc/calc")).unwrap();
std::fs::write(root.join("svc/go.mod"), "module example.com/svc\n").unwrap();
let report = root.join("cover.out");
std::fs::write(&report, "mode: set\nexample.com/svc/calc/calc.go:3.1,4.2 1 1\n").unwrap();
let suite = import(&report, root).unwrap();
assert_eq!(suite.files[0].path.as_ref() as &Path, root.join("svc/calc/calc.go"));
}
#[test]
fn imports_jacoco_against_the_project() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("p");
std::fs::create_dir_all(root.join("src/main/java/com/x")).unwrap();
std::fs::write(root.join("src/main/java/com/x/A.java"), "").unwrap();
let report = tmp.path().join("jacoco.xml");
let xml = r#"<report name="p"><package name="com/x"><sourcefile name="A.java"><line nr="2" mi="0" ci="1" mb="0" cb="0"/></sourcefile></package></report>"#;
std::fs::write(&report, xml).unwrap();
let suite = import(&report, &root).unwrap();
assert_eq!(suite.files[0].path.as_ref() as &Path, root.join("src/main/java/com/x/A.java"));
assert!(suite.has_branches);
}
}
+203
Переглянути файл
@@ -0,0 +1,203 @@
//! JaCoCo XML reports:
//!
//! ```xml
//! <report name="app">
//! <package name="com/example">
//! <sourcefile name="Calc.java">
//! <line nr="5" mi="0" ci="3" mb="1" cb="1"/>
//! ```
//!
//! Source files are found below the report's module in the conventional
//! source roots (`src/<set>/{java,kotlin,groovy,scala}`), falling back to
//! `src/<set>/java`.
use crate::Accumulator;
use quick_xml::Reader;
use quick_xml::events::{BytesStart, Event};
use std::path::{Path, PathBuf};
const LANGS: &[&str] = &["java", "kotlin", "groovy", "scala"];
const MAX_DEPTH: usize = 6;
/// The module a report belongs to: `<module>/target/site/jacoco/jacoco.xml` (Maven) or
/// `<module>/build/reports/jacoco/<task>/<task>.xml` (Gradle).
pub(crate) fn module_dir(report: &Path) -> PathBuf {
for dir in report.ancestors().skip(1) {
let name = dir.file_name().and_then(|n| n.to_str()).unwrap_or_default();
if (name == "target" || name == "build")
&& let Some(parent) = dir.parent()
{
return parent.to_path_buf();
}
}
report.parent().unwrap_or(report).to_path_buf()
}
/// JaCoCo XML reports below `dir` (skipping VCS / dependency directories).
pub(crate) fn find_reports(dir: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
walk(dir, 0, &mut out);
out.sort();
out
}
fn walk(dir: &Path, depth: usize, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else { return };
for e in entries.flatten() {
let path = e.path();
let name = e.file_name().to_string_lossy().into_owned();
let Ok(ft) = e.file_type() else { continue };
if ft.is_dir() {
if name.starts_with('.') || name == "node_modules" || name == "src" {
continue;
}
// `…/target/site/jacoco/` and `…/build/reports/jacoco/<task>/`.
if name == "jacoco"
&& path.parent().is_some_and(|p| p.ends_with("target/site") || p.ends_with("build/reports"))
{
collect_xml(&path, 0, out);
continue;
}
if depth < MAX_DEPTH {
walk(&path, depth + 1, out);
}
}
}
}
fn collect_xml(dir: &Path, depth: usize, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else { return };
for e in entries.flatten() {
let path = e.path();
if path.is_dir() && depth < 1 {
collect_xml(&path, depth + 1, out);
} else if path.extension().is_some_and(|x| x == "xml") {
out.push(path);
}
}
}
fn attr(e: &BytesStart, name: &str) -> Option<String> {
e.attributes().flatten().find(|a| a.key.as_ref() == name).map(|a| a.value.into_owned())
}
fn num(e: &BytesStart, name: &str) -> u32 {
attr(e, name).and_then(|v| v.parse().ok()).unwrap_or(0)
}
/// Source file of `package/name` below `module`.
fn locate(module: &Path, package: &str, name: &str) -> PathBuf {
let rel = if package.is_empty() { PathBuf::from(name) } else { Path::new(package).join(name) };
let src = module.join("src");
if let Ok(sets) = std::fs::read_dir(&src) {
let mut sets: Vec<PathBuf> = sets.flatten().map(|e| e.path()).filter(|p| p.is_dir()).collect();
// `main` first, so a class compiled from main sources does not resolve into tests.
sets.sort_by_key(|p| (!p.ends_with("main"), p.clone()));
for set in sets {
for lang in LANGS {
let candidate = set.join(lang).join(&rel);
if candidate.is_file() {
return candidate;
}
}
}
}
src.join("main").join("java").join(rel)
}
/// Parses one report of the module at `module` into `acc`.
pub(crate) fn parse_into(xml: &str, module: &Path, acc: &mut Accumulator) -> Result<(), quick_xml::Error> {
let mut reader = Reader::from_str(xml);
let mut package = String::new();
let mut file: Option<PathBuf> = None;
loop {
match reader.read_event()? {
Event::Start(e) | Event::Empty(e) => match e.name().as_ref() {
"package" => package = attr(&e, "name").unwrap_or_default(),
"sourcefile" => {
let name = attr(&e, "name").unwrap_or_default();
let path = locate(module, &package, &name);
acc.file(&path);
file = Some(path);
}
"line" => {
let Some(f) = file.as_deref() else { continue };
let nr = num(&e, "nr");
if nr == 0 {
continue;
}
let (ci, mb, cb) = (num(&e, "ci"), num(&e, "mb"), num(&e, "cb"));
acc.line(f, nr - 1, u64::from(ci));
for i in 0..mb + cb {
acc.branch(f, nr - 1, i.to_string(), i < cb);
}
}
_ => {}
},
Event::End(e) => match e.name().as_ref() {
"sourcefile" => file = None,
"package" => package.clear(),
_ => {}
},
Event::Eof => break,
_ => {}
}
}
Ok(())
}
#[cfg(test)]
/// Parses a single report whose sources live below `module`.
pub(crate) fn parse(xml: &str, module: &Path) -> Result<crate::CoverageSuite, String> {
let mut acc = Accumulator { has_branches: true, ..Default::default() };
parse_into(xml, module, &mut acc).map_err(|e| e.to_string())?;
Ok(acc.finish("JaCoCo"))
}
#[cfg(test)]
mod tests {
use super::*;
const REPORT: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.1//EN" "report.dtd"><report name="app"><sessioninfo id="x" start="1" dump="2"/><package name="com/example"><class name="com/example/Calc" sourcefilename="Calc.java"><method name="add" desc="(II)I" line="5"><counter type="LINE" missed="0" covered="1"/></method></class><sourcefile name="Calc.java"><line nr="5" mi="0" ci="3" mb="0" cb="0"/><line nr="6" mi="2" ci="2" mb="1" cb="1"/><line nr="9" mi="4" ci="0" mb="0" cb="0"/><counter type="LINE" missed="1" covered="2"/></sourcefile><sourcefile name="Util.kt"><line nr="1" mi="0" ci="1" mb="0" cb="0"/></sourcefile></package></report>"#;
#[test]
fn parses_lines_and_branches_and_locates_sources() {
let dir = tempfile::tempdir().unwrap();
let kt = dir.path().join("src/main/kotlin/com/example");
std::fs::create_dir_all(&kt).unwrap();
std::fs::write(kt.join("Util.kt"), "").unwrap();
let suite = parse(REPORT, dir.path()).unwrap();
assert!(suite.has_branches);
assert_eq!(suite.files.len(), 2);
let calc = &suite.files[0];
assert_eq!(calc.path.as_ref() as &Path, dir.path().join("src/main/java/com/example/Calc.java"));
assert_eq!((calc.lines_total, calc.lines_covered), (3, 2));
assert_eq!((calc.branches_total, calc.branches_covered), (2, 1));
assert!(calc.lines[1].partial());
assert_eq!(calc.lines[2].hits, 0);
assert_eq!(suite.files[1].path.as_ref() as &Path, kt.join("Util.kt"));
}
#[test]
fn finds_maven_and_gradle_reports() {
let dir = tempfile::tempdir().unwrap();
let maven = dir.path().join("core/target/site/jacoco");
let gradle = dir.path().join("app/build/reports/jacoco/test");
std::fs::create_dir_all(&maven).unwrap();
std::fs::create_dir_all(&gradle).unwrap();
std::fs::write(maven.join("jacoco.xml"), REPORT).unwrap();
std::fs::write(gradle.join("jacocoTestReport.xml"), REPORT).unwrap();
std::fs::write(maven.join("index.html"), "").unwrap();
let found = find_reports(dir.path());
assert_eq!(found.len(), 2);
assert_eq!(module_dir(&found[0]), dir.path().join("app"));
assert_eq!(module_dir(&found[1]), dir.path().join("core"));
}
#[test]
fn rejects_broken_xml() {
assert!(parse("<report><package name=\"a\"></sourcefile></report>", Path::new("/m")).is_err());
let suite = parse("<report></report>", Path::new("/m")).unwrap();
assert!(suite.files.is_empty());
}
}
+86
Переглянути файл
@@ -0,0 +1,86 @@
//! lcov tracefiles (`geninfo` format), as written by cargo-llvm-cov, istanbul
//! (vitest / jest / c8) and coverage.py:
//!
//! ```text
//! SF:src/lib.rs
//! DA:3,1
//! BRDA:4,0,0,1
//! BRDA:4,0,1,-
//! end_of_record
//! ```
use crate::Accumulator;
use std::path::{Path, PathBuf};
/// Parses `text`; relative `SF:` paths resolve against `base`.
pub(crate) fn parse(text: &str, base: &Path) -> Accumulator {
let mut acc = Accumulator::default();
let mut file: Option<PathBuf> = None;
for raw in text.lines() {
let line = raw.trim();
if let Some(sf) = line.strip_prefix("SF:") {
let p = Path::new(sf.trim());
let path = if p.is_absolute() { p.to_path_buf() } else { base.join(p) };
acc.file(&path);
file = Some(path);
continue;
}
if line == "end_of_record" {
file = None;
continue;
}
let Some(f) = file.as_deref() else { continue };
if let Some(da) = line.strip_prefix("DA:") {
let mut parts = da.split(',');
let (Some(n), Some(hits)) = (parts.next(), parts.next()) else { continue };
let (Ok(n), Ok(hits)) = (n.trim().parse::<u32>(), hits.trim().parse::<f64>()) else { continue };
if n == 0 {
continue;
}
acc.line(f, n - 1, hits.max(0.0) as u64);
} else if let Some(brda) = line.strip_prefix("BRDA:") {
let parts: Vec<&str> = brda.split(',').map(str::trim).collect();
let [n, block, branch, taken] = parts[..] else { continue };
let Ok(n) = n.parse::<u32>() else { continue };
if n == 0 {
continue;
}
let taken = taken != "-" && taken.parse::<f64>().is_ok_and(|t| t > 0.0);
acc.has_branches = true;
acc.branch(f, n - 1, format!("{block}:{branch}"), taken);
}
}
acc
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = "TN:\nSF:/abs/src/lib.rs\nFN:1,add\nFNDA:3,add\nDA:1,3\nDA:2,3\nDA:5,0\nBRDA:2,0,0,3\nBRDA:2,0,1,-\nLF:3\nLH:2\nend_of_record\nSF:src/util.ts\nDA:10,1\nend_of_record\n";
#[test]
fn parses_lines_and_branches() {
let suite = parse(SAMPLE, Path::new("/base")).finish("lcov");
assert!(suite.has_branches);
assert_eq!(suite.files.len(), 2);
let lib = &suite.files[0];
assert_eq!(lib.path.as_ref() as &Path, Path::new("/abs/src/lib.rs"));
assert_eq!((lib.lines_total, lib.lines_covered), (3, 2));
assert_eq!((lib.branches_total, lib.branches_covered), (2, 1));
assert_eq!(lib.lines[0].line, 0);
assert!(lib.lines[1].partial());
assert_eq!(lib.lines[2].hits, 0);
assert_eq!(suite.files[1].path.as_ref() as &Path, Path::new("/base/src/util.ts"));
}
#[test]
fn merges_repeated_records_and_ignores_garbage() {
let text = "SF:/a.py\nDA:1,0\nDA:x,1\nDA:0,4\nend_of_record\nDA:9,9\nSF:/a.py\nDA:1,2\nend_of_record\n";
let suite = parse(text, Path::new("/")).finish("lcov");
assert!(!suite.has_branches);
assert_eq!(suite.files.len(), 1);
assert_eq!(suite.files[0].lines.len(), 1);
assert_eq!(suite.files[0].lines[0].hits, 2);
}
}
+260
Переглянути файл
@@ -0,0 +1,260 @@
//! Run with Coverage.
//!
//! [`prepare`] turns a run configuration into the command that runs it under
//! the runtime's coverage tool (or explains which tool is missing), and
//! [`collect`] reads the report that run left behind into a [`CoverageSuite`]:
//!
//! | runtime | tool | report |
//! |---|---|---|
//! | Rust (`cargo test` / `run` / `nextest`) | `cargo llvm-cov` | lcov |
//! | JS / TS (vitest, jest scripts) | the runner's `--coverage` | lcov |
//! | Node.js scripts | `c8` | lcov |
//! | Python (pytest, scripts) | coverage.py (`coverage run`, then `coverage lcov`) | lcov |
//! | Go (`go test`) | `-coverprofile` | Go cover profile |
//! | Maven / Gradle tests | JaCoCo (plugin goals / an init script) | JaCoCo XML |
//!
//! Lines in the model are 0-based, like every IPC coordinate.
mod goprofile;
mod import;
mod jacoco;
mod lcov;
mod plan;
pub use import::import;
pub use plan::prepare;
use ide_core::{IdeError, IdeResult, IpcPath};
use ide_run::RunConfig;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Command;
/// Coverage of one source line.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct LineCoverage {
/// 0-based.
pub line: u32,
/// Execution count (JaCoCo: covered instructions); 0 = not covered.
pub hits: u32,
pub branches_total: u32,
pub branches_covered: u32,
}
impl LineCoverage {
/// Covered, but not every branch (or block) on the line.
pub fn partial(&self) -> bool {
self.hits > 0 && self.branches_covered < self.branches_total
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct FileCoverage {
pub path: IpcPath,
/// Sorted by line.
pub lines: Vec<LineCoverage>,
pub lines_total: u32,
pub lines_covered: u32,
pub branches_total: u32,
pub branches_covered: u32,
}
/// One coverage run's report.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct CoverageSuite {
/// Tool that produced it, e.g. `cargo-llvm-cov`, `JaCoCo`.
pub runner: String,
/// Whether branch counts are meaningful (lcov `BRDA`, JaCoCo); Go profiles only have blocks.
pub has_branches: bool,
/// Sorted by path.
pub files: Vec<FileCoverage>,
}
/// Where the report of a prepared run will be, and how to read it.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum CoverageReportSpec {
/// An lcov tracefile; relative `SF:` paths resolve against `base`.
Lcov { runner: String, path: IpcPath, base: IpcPath },
/// `go test -coverprofile`; import paths under `module` map to `module_dir`.
GoProfile { path: IpcPath, module: String, module_dir: IpcPath },
/// Every JaCoCo XML report (Maven `target/site/jacoco`, Gradle `build/reports/jacoco`) under `dir`.
Jacoco { dir: IpcPath },
/// The tool's raw data is exported to lcov by `argv` first (coverage.py: `… -m coverage lcov -o <lcov>`).
Exported { runner: String, argv: Vec<String>, cwd: IpcPath, env: BTreeMap<String, String>, lcov: IpcPath },
}
/// A command line that installs a missing tool (run in the Run tool window).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct InstallCommand {
pub command: String,
pub args: Vec<String>,
pub cwd: Option<IpcPath>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(tag = "status", rename_all = "camelCase")]
pub enum CoveragePrepared {
Ready {
runner: String,
/// What to launch instead of the configuration.
launch: Box<RunConfig>,
report: CoverageReportSpec,
},
MissingTool {
tool: String,
message: String,
install: Option<InstallCommand>,
},
Unsupported {
reason: String,
},
}
/// Line data merged from one or more report records.
#[derive(Default)]
pub(crate) struct Accumulator {
files: BTreeMap<PathBuf, BTreeMap<u32, LineAcc>>,
pub(crate) has_branches: bool,
}
#[derive(Default)]
struct LineAcc {
hits: u64,
/// Branch (or block) key → taken.
branches: BTreeMap<String, bool>,
}
impl Accumulator {
pub(crate) fn line(&mut self, file: &Path, line: u32, hits: u64) {
let acc = self.files.entry(file.to_path_buf()).or_default().entry(line).or_default();
acc.hits = acc.hits.saturating_add(hits);
}
/// A branch outcome on `line`; the same `key` from several records counts once (taken if any).
pub(crate) fn branch(&mut self, file: &Path, line: u32, key: String, taken: bool) {
let acc = self.files.entry(file.to_path_buf()).or_default().entry(line).or_default();
let b = acc.branches.entry(key).or_default();
*b |= taken;
}
/// Declares `file` (a report entry without line data still lists the file).
pub(crate) fn file(&mut self, file: &Path) {
self.files.entry(file.to_path_buf()).or_default();
}
pub(crate) fn finish(self, runner: &str) -> CoverageSuite {
let files = self
.files
.into_iter()
.map(|(path, lines)| {
let lines: Vec<LineCoverage> = lines
.into_iter()
.map(|(line, acc)| LineCoverage {
line,
hits: u32::try_from(acc.hits).unwrap_or(u32::MAX),
branches_total: acc.branches.len() as u32,
branches_covered: acc.branches.values().filter(|t| **t).count() as u32,
})
.collect();
FileCoverage {
path: path.into(),
lines_total: lines.len() as u32,
lines_covered: lines.iter().filter(|l| l.hits > 0).count() as u32,
branches_total: lines.iter().map(|l| l.branches_total).sum(),
branches_covered: lines.iter().map(|l| l.branches_covered).sum(),
lines,
}
})
.collect();
CoverageSuite { runner: runner.to_owned(), has_branches: self.has_branches, files }
}
}
/// Reads the report described by `spec` (running coverage.py's lcov export first).
/// Blocking: call from a blocking thread.
pub fn collect(spec: &CoverageReportSpec) -> IdeResult<CoverageSuite> {
match spec {
CoverageReportSpec::Lcov { runner, path, base } => {
let text = read_report(path)?;
Ok(lcov::parse(&text, base).finish(runner))
}
CoverageReportSpec::GoProfile { path, module, module_dir } => {
let text = read_report(path)?;
Ok(goprofile::parse(&text, module, module_dir).finish("go test -cover"))
}
CoverageReportSpec::Jacoco { dir } => {
let reports = jacoco::find_reports(dir);
if reports.is_empty() {
return Err(IdeError::not_found(format!("no JaCoCo report under {}", dir.display())));
}
let mut acc = Accumulator { has_branches: true, ..Default::default() };
for report in reports {
let xml = std::fs::read_to_string(&report)?;
let module = jacoco::module_dir(&report);
jacoco::parse_into(&xml, &module, &mut acc)
.map_err(|e| IdeError::invalid(format!("{}: {e}", report.display())))?;
}
Ok(acc.finish("JaCoCo"))
}
CoverageReportSpec::Exported { runner, argv, cwd, env, lcov } => {
let (program, args) =
argv.split_first().ok_or_else(|| IdeError::invalid("empty coverage export command"))?;
let out = ide_core::process::no_window(&mut Command::new(program))
.args(args)
.current_dir(cwd)
.envs(env)
.output()
.map_err(|e| IdeError::unavailable(format!("cannot run {program}: {e}")))?;
if !out.status.success() {
let text = String::from_utf8_lossy(&out.stderr).trim().to_owned();
let text = if text.is_empty() { String::from_utf8_lossy(&out.stdout).trim().to_owned() } else { text };
return Err(IdeError::not_found(format!("exporting the coverage data failed: {text}")));
}
let text = read_report(lcov)?;
Ok(lcov::parse(&text, cwd).finish(runner))
}
}
}
fn read_report(path: &Path) -> IdeResult<String> {
std::fs::read_to_string(path).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => {
IdeError::not_found(format!("the run produced no coverage report ({})", path.display()))
}
_ => e.into(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accumulator_merges_records_of_the_same_file() {
let mut acc = Accumulator::default();
let f = Path::new("/p/a.rs");
acc.line(f, 0, 1);
acc.line(f, 0, 2);
acc.line(f, 3, 0);
acc.branch(f, 0, "0:0".into(), true);
acc.branch(f, 0, "0:1".into(), false);
acc.branch(f, 0, "0:1".into(), false);
acc.file(Path::new("/p/empty.rs"));
let suite = acc.finish("t");
assert_eq!(suite.files.len(), 2);
let a = &suite.files[0];
assert_eq!(a.path.as_ref() as &Path, f);
assert_eq!((a.lines_total, a.lines_covered, a.branches_total, a.branches_covered), (2, 1, 2, 1));
assert_eq!(a.lines[0], LineCoverage { line: 0, hits: 3, branches_total: 2, branches_covered: 1 });
assert!(a.lines[0].partial());
assert!(!a.lines[1].partial());
assert_eq!(suite.files[1].lines_total, 0);
}
}
+409
Переглянути файл
@@ -0,0 +1,409 @@
//! Which coverage tool runs a configuration, and how its command line changes.
use crate::{CoveragePrepared, CoverageReportSpec, InstallCommand};
use ide_run::RunConfig;
use ide_run::wrap::{self, program_name};
use std::path::Path;
const GRADLE_INIT: &str = r#"// Written by OpenIDEA for Run with Coverage.
allprojects {
plugins.withId('java') {
apply plugin: 'jacoco'
tasks.withType(Test).configureEach { t -> t.finalizedBy(tasks.named('jacocoTestReport')) }
tasks.named('jacocoTestReport') { r -> r.reports { xml.required.set(true) } }
}
}
"#;
fn s(v: &str) -> String {
v.to_owned()
}
fn path(p: &Path) -> String {
p.to_string_lossy().into_owned()
}
fn unsupported(reason: impl Into<String>) -> CoveragePrepared {
CoveragePrepared::Unsupported { reason: reason.into() }
}
fn missing(tool: &str, message: impl Into<String>, install: Option<(Vec<String>, &Path)>) -> CoveragePrepared {
CoveragePrepared::MissingTool {
tool: tool.to_owned(),
message: message.into(),
install: install.and_then(|(argv, cwd)| {
let (command, args) = argv.split_first()?;
Some(InstallCommand { command: command.clone(), args: args.to_vec(), cwd: Some(cwd.to_path_buf().into()) })
}),
}
}
fn ready(
runner: &str,
config: &RunConfig,
command: String,
args: Vec<String>,
report: CoverageReportSpec,
) -> CoveragePrepared {
CoveragePrepared::Ready {
runner: runner.to_owned(),
launch: Box::new(RunConfig { command, args, ..config.clone() }),
report,
}
}
/// How to run `config` with coverage. `out` is an empty directory for the run's
/// data; it must exist (the Gradle init script is written there). Probes tools,
/// so call it from a blocking thread.
pub fn prepare(root: &Path, config: &RunConfig, out: &Path) -> CoveragePrepared {
let dir = wrap::run_dir(root, config);
let program = program_name(&config.command);
let first = config.args.first().map(String::as_str).unwrap_or_default();
match program.as_str() {
"cargo" => cargo(config, &dir, out),
"go" if first == "test" => go(config, &dir, out),
"go" => unsupported("Go coverage is collected by `go test`; run a test configuration with coverage."),
"mvn" | "mvnw" => maven(config, &dir),
"gradle" | "gradlew" => gradle(config, &dir, out),
"npm" | "pnpm" | "yarn" | "bun" => npm(root, config, &dir, out),
"node" | "npx" | "tsx" | "ts-node" => c8(root, config, &dir, out),
"deno" => unsupported("Coverage of Deno tasks is not supported yet."),
"java" => unsupported(
"Coverage of Java applications needs the JaCoCo agent; run a Maven or Gradle test configuration with coverage.",
),
_ => match wrap::python_command(config) {
Some(py) => python(config, &dir, out, py),
None => unsupported(format!("No coverage runner is known for '{}'.", config.name)),
},
}
}
fn cargo(config: &RunConfig, dir: &Path, out: &Path) -> CoveragePrepared {
// `cargo +nightly test …`: the toolchain stays in front.
let toolchain: Vec<String> = config.args.iter().take_while(|a| a.starts_with('+')).cloned().collect();
let rest = &config.args[toolchain.len()..];
let Some(sub) = rest.first().filter(|s| matches!(s.as_str(), "test" | "run" | "nextest")) else {
return unsupported("Coverage runs `cargo test`, `cargo run` or `cargo nextest` configurations.");
};
let probe: Vec<String> = std::iter::once(config.command.clone())
.chain(toolchain.iter().cloned())
.chain([s("llvm-cov"), s("--version")])
.collect();
if !wrap::probe(&probe, dir, config) {
return missing(
"cargo-llvm-cov",
"Rust coverage needs cargo-llvm-cov (and the llvm-tools-preview rustup component).",
Some((vec![s("cargo"), s("install"), s("cargo-llvm-cov")], dir)),
);
}
let lcov = out.join("lcov.info");
let mut args = toolchain;
args.extend([s("llvm-cov"), sub.clone(), s("--lcov"), s("--output-path"), path(&lcov)]);
args.extend(rest[1..].iter().cloned());
let report =
CoverageReportSpec::Lcov { runner: s("cargo-llvm-cov"), path: lcov.into(), base: dir.to_path_buf().into() };
ready("cargo-llvm-cov", config, config.command.clone(), args, report)
}
fn go(config: &RunConfig, dir: &Path, out: &Path) -> CoveragePrepared {
let profile = out.join("cover.out");
let mut args = vec![s("test"), format!("-coverprofile={}", path(&profile))];
args.extend(config.args[1..].iter().cloned());
let (module, module_dir) = wrap::go_module(dir).unwrap_or_else(|| (String::new(), dir.to_path_buf()));
let report = CoverageReportSpec::GoProfile { path: profile.into(), module, module_dir: module_dir.into() };
ready("go test -cover", config, config.command.clone(), args, report)
}
fn runs_tests(args: &[String]) -> bool {
args.iter().any(|a| {
let task = a.rsplit(':').next().unwrap_or(a);
matches!(task, "test" | "verify" | "check" | "integrationTest") || a.starts_with("-Dtest=") || a == "--tests"
})
}
fn maven(config: &RunConfig, dir: &Path) -> CoveragePrepared {
if !runs_tests(&config.args) {
return unsupported("Coverage runs Maven configurations that run tests (`test`, `verify`).");
}
let mut args = vec![s("org.jacoco:jacoco-maven-plugin:prepare-agent")];
args.extend(config.args.iter().cloned());
// Report even when tests fail, and from this run's data only.
args.extend([
s("org.jacoco:jacoco-maven-plugin:report"),
s("-Djacoco.append=false"),
s("-Dmaven.test.failure.ignore=true"),
]);
ready("JaCoCo", config, config.command.clone(), args, CoverageReportSpec::Jacoco { dir: dir.to_path_buf().into() })
}
fn gradle(config: &RunConfig, dir: &Path, out: &Path) -> CoveragePrepared {
if !runs_tests(&config.args) {
return unsupported("Coverage runs Gradle configurations that run tests (`test`, `check`).");
}
let script = out.join("openidea-jacoco.init.gradle");
if let Err(e) = std::fs::write(&script, GRADLE_INIT) {
return unsupported(format!("Cannot write the Gradle init script: {e}"));
}
let mut args = vec![s("--init-script"), path(&script)];
args.extend(config.args.iter().cloned());
ready("JaCoCo", config, config.command.clone(), args, CoverageReportSpec::Jacoco { dir: dir.to_path_buf().into() })
}
/// `npm install -D x` / `pnpm add -D x`.
fn add_dev(pm: &str, package: &str) -> Vec<String> {
let verb = if pm == "npm" { "install" } else { "add" };
vec![s(pm), s(verb), s("-D"), s(package)]
}
const SHELL_META: &[char] = &['&', '|', ';', '<', '>', '$', '`', '"', '\'', '(', ')'];
fn npm(root: &Path, config: &RunConfig, dir: &Path, out: &Path) -> CoveragePrepared {
let pm = program_name(&config.command);
let Some((name, script)) = wrap::npm_script(config, dir) else {
return unsupported("Coverage runs package.json scripts that use vitest or jest.");
};
let tokens: Vec<&str> = script.split_whitespace().collect();
let runner_at = tokens.iter().position(|t| matches!(*t, "vitest" | "jest"));
let Some(runner_at) = runner_at else {
return unsupported(format!("The '{name}' script does not run vitest or jest: {script}"));
};
let runner = tokens[runner_at];
let lcov = out.join("lcov.info");
let coverage_args: Vec<String> = if runner == "vitest" {
if !wrap::node_package("@vitest/coverage-v8", dir, root)
&& !wrap::node_package("@vitest/coverage-istanbul", dir, root)
{
// The provider must match the installed vitest exactly.
let provider = match wrap::node_package_version("vitest", dir, root) {
Some(v) => format!("@vitest/coverage-v8@{v}"),
None => s("@vitest/coverage-v8"),
};
return missing(
"@vitest/coverage-v8",
"Vitest coverage needs a coverage provider package.",
Some((add_dev(&pm, &provider), dir)),
);
}
vec![
s("--coverage.enabled=true"),
s("--coverage.reporter=lcov"),
format!("--coverage.reportsDirectory={}", path(out)),
]
} else {
vec![s("--coverage"), s("--coverageReporters=lcov"), format!("--coverageDirectory={}", path(out))]
};
let report =
CoverageReportSpec::Lcov { runner: runner.to_owned(), path: lcov.into(), base: dir.to_path_buf().into() };
// A plain `vitest run …` script runs the runner directly, so its arguments need no `--` games.
let simple =
!script.contains(SHELL_META) && tokens[..runner_at].iter().all(|t| matches!(*t, "npx" | "pnpm" | "exec"));
if simple && let Some(bin) = wrap::node_bin(runner, dir, root) {
let mut args: Vec<String> = tokens[runner_at + 1..].iter().map(|t| s(t)).collect();
args.extend(wrap::passthrough_args(config));
args.extend(coverage_args);
return ready(runner, config, path(&bin), args, report);
}
let mut args = config.args.clone();
if !args.iter().any(|a| a == "--") {
args.push(s("--"));
}
args.extend(coverage_args);
ready(runner, config, config.command.clone(), args, report)
}
fn c8(root: &Path, config: &RunConfig, dir: &Path, out: &Path) -> CoveragePrepared {
let Some(c8) = wrap::find_tool("c8", config, dir, root) else {
return missing(
"c8",
"Coverage of Node.js programs needs c8.",
Some((vec![s("npm"), s("install"), s("-g"), s("c8")], dir)),
);
};
let mut args = vec![
s("--reporter=lcov"),
format!("--reports-dir={}", path(out)),
format!("--temp-directory={}", path(&out.join("tmp"))),
config.command.clone(),
];
args.extend(config.args.iter().cloned());
let report = CoverageReportSpec::Lcov {
runner: s("c8"),
path: out.join("lcov.info").into(),
base: dir.to_path_buf().into(),
};
ready("c8", config, path(&c8), args, report)
}
fn python(config: &RunConfig, dir: &Path, out: &Path, py: wrap::PythonCommand) -> CoveragePrepared {
if py.target.is_empty() {
return unsupported("Coverage needs a Python script or module to run.");
}
let mut config = config.clone();
let data = out.join(".coverage");
config.env.insert(s("COVERAGE_FILE"), path(&data));
let with =
|extra: &[&str]| -> Vec<String> { py.interpreter.iter().cloned().chain(extra.iter().map(|e| s(e))).collect() };
if !wrap::probe(&with(&["-m", "coverage", "--version"]), dir, &config) {
let install = match py.manager.as_deref() {
Some("uv") => vec![s("uv"), s("add"), s("--dev"), s("coverage")],
Some("poetry") => vec![s("poetry"), s("add"), s("--group"), s("dev"), s("coverage")],
Some("pdm") => vec![s("pdm"), s("add"), s("-d"), s("coverage")],
_ => with(&["-m", "pip", "install", "coverage"]),
};
return missing(
"coverage",
"Python coverage needs coverage.py in the project's interpreter.",
Some((install, dir)),
);
}
let lcov = out.join("lcov.info");
let mut argv = with(&["-m", "coverage", "run", "--branch"]);
argv.extend(py.target.iter().cloned());
let report = CoverageReportSpec::Exported {
runner: s("coverage.py"),
argv: with(&["-m", "coverage", "lcov", "-o", &path(&lcov)]),
cwd: dir.to_path_buf().into(),
env: config.env.clone(),
lcov: lcov.into(),
};
let (command, args) = argv.split_first().map(|(c, a)| (c.clone(), a.to_vec())).unwrap_or_default();
ready("coverage.py", &config, command, args, report)
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
fn cfg(kind: &str, command: &str, args: &[&str]) -> RunConfig {
RunConfig {
id: "t".into(),
name: "t".into(),
kind: kind.into(),
command: command.into(),
args: args.iter().map(|s| s.to_string()).collect(),
cwd: None,
env: BTreeMap::new(),
detected: true,
is_test: true,
debug_adapter: None,
allow_parallel: false,
extras: Default::default(),
}
}
fn launch(p: &CoveragePrepared) -> (&RunConfig, &CoverageReportSpec) {
match p {
CoveragePrepared::Ready { launch, report, .. } => (launch, report),
other => panic!("not ready: {other:?}"),
}
}
#[test]
fn go_test_gets_a_cover_profile() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("go.mod"), "module example.com/m\n").unwrap();
let out = dir.path().join("out");
let p = prepare(dir.path(), &cfg("go", "go", &["test", "./...", "-run", "X"]), &out);
let (l, report) = launch(&p);
assert_eq!(l.args[0], "test");
assert!(l.args[1].starts_with("-coverprofile=") && l.args[1].ends_with("cover.out"));
assert_eq!(&l.args[2..], ["./...", "-run", "X"]);
assert!(matches!(report, CoverageReportSpec::GoProfile { module, .. } if module == "example.com/m"));
assert!(matches!(
prepare(dir.path(), &cfg("go", "go", &["run", "."]), &out),
CoveragePrepared::Unsupported { .. }
));
}
#[test]
fn maven_and_gradle_use_jacoco() {
let dir = tempfile::tempdir().unwrap();
let p = prepare(dir.path(), &cfg("maven", "./mvnw", &["test", "-Dtest=CalcTest"]), dir.path());
let (l, _) = launch(&p);
assert_eq!(l.args[0], "org.jacoco:jacoco-maven-plugin:prepare-agent");
assert!(l.args.contains(&"org.jacoco:jacoco-maven-plugin:report".to_owned()));
assert!(matches!(
prepare(dir.path(), &cfg("maven", "mvn", &["package"]), dir.path()),
CoveragePrepared::Unsupported { .. }
));
let p = prepare(dir.path(), &cfg("gradle", "./gradlew", &[":app:test", "--tests", "*Calc"]), dir.path());
let (l, report) = launch(&p);
assert_eq!(l.args[0], "--init-script");
assert!(std::fs::read_to_string(&l.args[1]).unwrap().contains("jacoco"));
assert!(matches!(report, CoverageReportSpec::Jacoco { .. }));
}
#[test]
fn vitest_scripts_run_the_runner_with_coverage() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
std::fs::write(root.join("package.json"), r#"{"scripts":{"test":"vitest run","t2":"tsc && jest"}}"#).unwrap();
let config = cfg("npm", "pnpm", &["run", "test", "--", "a.test.ts"]);
// No provider package: install hint.
match prepare(root, &config, root) {
CoveragePrepared::MissingTool { install: Some(i), .. } => {
assert_eq!(i.args, ["add", "-D", "@vitest/coverage-v8"])
}
other => panic!("{other:?}"),
}
// ... pinned to the installed vitest.
let vitest = root.join("node_modules/vitest");
std::fs::create_dir_all(&vitest).unwrap();
std::fs::write(vitest.join("package.json"), r#"{"version":"2.1.9"}"#).unwrap();
match prepare(root, &config, root) {
CoveragePrepared::MissingTool { install: Some(i), .. } => {
assert_eq!(i.args, ["add", "-D", "@vitest/[email protected]"])
}
other => panic!("{other:?}"),
}
let pkg = root.join("node_modules/@vitest/coverage-v8");
std::fs::create_dir_all(&pkg).unwrap();
std::fs::write(pkg.join("package.json"), "{}").unwrap();
let bin = root.join("node_modules/.bin");
std::fs::create_dir_all(&bin).unwrap();
std::fs::write(bin.join(if cfg!(windows) { "vitest.cmd" } else { "vitest" }), "").unwrap();
let p = prepare(root, &config, root);
let (l, _) = launch(&p);
assert!(l.command.contains("node_modules"));
assert_eq!(&l.args[..2], ["run", "a.test.ts"]);
assert!(l.args.contains(&"--coverage.reporter=lcov".to_owned()));
// A compound script: arguments go after `--`.
let p = prepare(root, &cfg("npm", "npm", &["run", "t2"]), root);
let (l, _) = launch(&p);
assert_eq!(l.command, "npm");
assert_eq!(&l.args[..3], ["run", "t2", "--"]);
assert!(l.args.contains(&"--coverage".to_owned()));
}
#[test]
fn cargo_without_llvm_cov_suggests_installing_it() {
let dir = tempfile::tempdir().unwrap();
let mut config = cfg("cargo", "cargo", &["test"]);
// A search path without cargo: the probe fails.
config.env.insert("PATH".into(), dir.path().to_string_lossy().into_owned());
config.command = dir.path().join("cargo").to_string_lossy().into_owned();
match prepare(dir.path(), &config, dir.path()) {
CoveragePrepared::MissingTool { tool, install: Some(i), .. } => {
assert_eq!(tool, "cargo-llvm-cov");
assert_eq!(i.args, ["install", "cargo-llvm-cov"]);
}
other => panic!("{other:?}"),
}
assert!(matches!(
prepare(dir.path(), &cfg("cargo", "cargo", &["build"]), dir.path()),
CoveragePrepared::Unsupported { .. }
));
}
#[test]
fn unknown_commands_are_unsupported() {
let dir = tempfile::tempdir().unwrap();
assert!(matches!(
prepare(dir.path(), &cfg("make", "make", &["all"]), dir.path()),
CoveragePrepared::Unsupported { .. }
));
}
}
+22
Переглянути файл
@@ -0,0 +1,22 @@
[package]
name = "ide-dap"
description = "Debug adapter protocol client: sessions, breakpoints, threads, frames, variables, evaluation"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
ide-core.workspace = true
ide-language.workspace = true
ide-jsonrpc.workspace = true
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
tokio = { workspace = true, features = ["net"] }
parking_lot.workspace = true
memchr = "2"
tracing.workspace = true
[dev-dependencies]
tracing-subscriber.workspace = true
tokio = { workspace = true, features = ["net", "rt-multi-thread", "macros"] }
+587
Переглянути файл
@@ -0,0 +1,587 @@
//! Resolving and starting debug adapters.
//!
//! Adapters come from `languages/*.toml` `[debugger]` tables, with built-in
//! fallbacks for the common ones. `${port}` in the arguments of a `tcp`
//! adapter is replaced by a free local port; the client then connects to it.
//! Adapters that run inside another process (java-debug inside jdtls) are
//! reached through a `debugServer` port in the launch arguments instead.
use ide_core::{IdeError, IdeResult};
use ide_language::{DebuggerConfig, LanguageRegistry};
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::process::Stdio;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::process::{Child, Command};
pub const PORT_PLACEHOLDER: &str = "${port}";
/// Replaced by the session's working directory (the project) in adapter arguments.
pub const ROOT_PLACEHOLDER: &str = "${root}";
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Transport {
Stdio,
Tcp,
}
/// A fully resolved adapter command line.
#[derive(Clone, Debug)]
pub struct AdapterSpec {
pub adapter: String,
pub command: String,
pub args: Vec<String>,
pub transport: Transport,
pub install_hint: Option<String>,
pub cwd: PathBuf,
/// Extra environment of the adapter process (debugpy's private copy on `PYTHONPATH`).
pub env: Vec<(String, String)>,
/// Host of an adapter that is already running (`debugServer`); `None` is the loopback interface.
pub server_host: Option<String>,
/// `host:port` of an adapter inside another program (the Godot editor): connected to
/// when it accepts, else `command` is started.
pub connect: Option<String>,
}
/// Built-in adapters used when no language definition names `adapter`.
fn builtin(adapter: &str) -> Option<DebuggerConfig> {
let cfg = |command: &str, args: &[&str], transport: &str, hint: &str| DebuggerConfig {
adapter: adapter.to_owned(),
command: command.to_owned(),
args: args.iter().map(|a| (*a).to_owned()).collect(),
transport: transport.to_owned(),
connect: None,
install_hint: Some(hint.to_owned()),
};
Some(match adapter {
"codelldb" => cfg(
"codelldb",
&["--port", PORT_PLACEHOLDER],
"tcp",
"Install the CodeLLDB adapter (https://github.com/vadimcn/codelldb/releases) and put `codelldb` on PATH",
),
"debugpy" => cfg("python3", &["-m", "debugpy.adapter"], "stdio", "pip install debugpy"),
"js-debug" => cfg(
"js-debug-adapter",
// Without a host it listens on `localhost`, which may resolve to ::1 only.
&[PORT_PLACEHOLDER, "127.0.0.1"],
"tcp",
"Install vscode-js-debug's DAP server (js-debug-dap-*.tar.gz from https://github.com/microsoft/vscode-js-debug/releases) as `js-debug-adapter`",
),
"delve" => cfg(
"dlv",
&["dap", "--listen", "127.0.0.1:${port}"],
"tcp",
"go install github.com/go-delve/delve/cmd/dlv@latest",
),
"netcoredbg" => cfg(
"netcoredbg",
&["--interpreter=vscode"],
"stdio",
"Install Samsung netcoredbg (https://github.com/Samsung/netcoredbg/releases)",
),
"lldb-dap" => {
cfg("lldb-dap", &[], "stdio", "Install LLVM's lldb-dap (brew install llvm; on macOS `xcrun -f lldb-dap`)")
}
// A jdtls bundle: the session is started through jdtls and reached via `debugServer`.
"java-debug" => cfg(
"com.microsoft.java.debug.plugin.jar",
&[],
"tcp",
"Install Microsoft java-debug (Settings › Languages & Frameworks › Language Servers)",
),
_ => return None,
})
}
/// The adapter definition for `adapter`: a language's `[debugger]` first, then built-ins.
pub fn find_config(languages: &LanguageRegistry, adapter: &str) -> Option<DebuggerConfig> {
languages.all().into_iter().filter_map(|l| l.debugger).find(|d| d.adapter == adapter).or_else(|| builtin(adapter))
}
/// Default DAP `type` attribute per adapter (some adapters dispatch on it).
pub fn default_type(adapter: &str) -> Option<&'static str> {
Some(match adapter {
"codelldb" => "lldb",
"debugpy" => "debugpy",
"js-debug" => "pwa-node",
"delve" => "go",
"netcoredbg" => "coreclr",
"lldb-dap" => "lldb-dap",
"java-debug" => "java",
_ => return None,
})
}
pub fn resolve(config: &DebuggerConfig, cwd: PathBuf) -> AdapterSpec {
AdapterSpec {
adapter: config.adapter.clone(),
command: config.command.clone(),
args: config.args.clone(),
transport: if config.transport.eq_ignore_ascii_case("tcp") { Transport::Tcp } else { Transport::Stdio },
install_hint: config.install_hint.clone(),
cwd,
env: Vec::new(),
server_host: None,
connect: config.connect.clone(),
}
}
/// Replaces every `${port}` occurrence (also inside larger arguments like `127.0.0.1:${port}`).
pub fn substitute_port(args: &[String], port: u16) -> Vec<String> {
args.iter().map(|a| a.replace(PORT_PLACEHOLDER, &port.to_string())).collect()
}
/// Replaces every `${root}` occurrence with `root`.
pub fn substitute_root(args: &[String], root: &Path) -> Vec<String> {
let root = root.to_string_lossy();
args.iter().map(|a| a.replace(ROOT_PLACEHOLDER, &root)).collect()
}
/// Splits `host:port` (`[v6]:port`).
pub fn parse_address(addr: &str) -> Option<(String, u16)> {
let (host, port) = addr.trim().rsplit_once(':')?;
let host = host.trim_start_matches('[').trim_end_matches(']');
Some((host.to_owned(), port.parse().ok().filter(|p| *p != 0)?))
}
/// A port that was free a moment ago on the loopback interface.
pub fn free_port() -> std::io::Result<u16> {
let listener = std::net::TcpListener::bind(("127.0.0.1", 0))?;
Ok(listener.local_addr()?.port())
}
static TOOLS_ROOT: parking_lot::RwLock<Option<PathBuf>> = parking_lot::RwLock::new(None);
/// The IDE's managed tools directory (ide-tools); adapters installed there win over `PATH`.
pub fn set_tools_root(root: PathBuf) {
*TOOLS_ROOT.write() = Some(root);
}
pub fn tools_root() -> Option<PathBuf> {
TOOLS_ROOT.read().clone()
}
/// Executable directories of managed installs, read at lookup time so tools installed
/// after startup are found without a restart: `<tool>/bin`, `<tool>/venv/bin`, npm's `.bin`.
fn managed_dirs(root: &Path) -> Vec<PathBuf> {
let mut dirs = vec![root.join("node_modules").join(".bin")];
if let Ok(entries) = std::fs::read_dir(root) {
let mut tools: Vec<PathBuf> = entries.flatten().map(|e| e.path()).filter(|p| p.is_dir()).collect();
tools.sort();
for t in tools {
dirs.push(t.join("bin"));
dirs.push(t.join("venv").join(if cfg!(windows) { "Scripts" } else { "bin" }));
}
}
dirs
}
/// Directories searched for adapter binaries: managed tools first, then `PATH` and the usual install spots.
fn search_dirs() -> Vec<PathBuf> {
let mut dirs: Vec<PathBuf> = tools_root().map(|r| managed_dirs(&r)).unwrap_or_default();
dirs.extend(std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).unwrap_or_default());
if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
for rel in [".cargo/bin", ".local/bin", "go/bin", ".dotnet/tools", ".bun/bin", ".npm-global/bin", ".volta/bin"]
{
dirs.push(home.join(rel));
}
}
for d in ["/opt/homebrew/bin", "/opt/homebrew/opt/llvm/bin", "/usr/local/bin", "/usr/bin", "/bin"] {
dirs.push(PathBuf::from(d));
}
dirs
}
fn is_executable(path: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
path.metadata().map(|m| m.is_file() && m.permissions().mode() & 0o111 != 0).unwrap_or(false)
}
#[cfg(not(unix))]
{
path.is_file()
}
}
/// Resolves `command` to an executable: paths are taken as-is (relative to `cwd`), bare names are looked up on PATH.
pub fn find_binary(command: &str, cwd: &Path) -> Option<PathBuf> {
let p = Path::new(command);
if p.components().count() > 1 || p.is_absolute() {
let full = if p.is_absolute() { p.to_path_buf() } else { cwd.join(p) };
return is_executable(&full).then_some(full);
}
let exts: &[&str] = if cfg!(windows) { &["", ".exe", ".cmd", ".bat"] } else { &[""] };
search_dirs()
.into_iter()
.flat_map(|d| exts.iter().map(move |e| d.join(format!("{command}{e}"))))
.find(|c| is_executable(c))
}
/// [`find_binary`], plus Xcode's toolchain on macOS (`xcrun -f lldb-dap`): Xcode ships
/// lldb-dap without putting it on `PATH`.
pub fn resolve_binary(command: &str, cwd: &Path) -> Option<PathBuf> {
find_binary(command, cwd).or_else(|| xcrun_tool(command))
}
#[cfg(target_os = "macos")]
fn xcrun_tool(command: &str) -> Option<PathBuf> {
if command != "lldb-dap" {
return None;
}
let out = std::process::Command::new("/usr/bin/xcrun").args(["-f", command]).stderr(Stdio::null()).output().ok()?;
let path = PathBuf::from(String::from_utf8_lossy(&out.stdout).trim());
(out.status.success() && is_executable(&path)).then_some(path)
}
#[cfg(not(target_os = "macos"))]
fn xcrun_tool(_command: &str) -> Option<PathBuf> {
None
}
/// Name of the managed debugpy install (ide-tools `PipTarget`, unpacked into `<tools>/debugpy/lib`).
pub const DEBUGPY_TOOL: &str = "debugpy";
/// Message prefix of the "debugpy missing" error; the UI offers the one-click install on it.
pub const DEBUGPY_MISSING: &str = "debugpy is not installed";
/// Makes `spec` (`<python> -m debugpy.adapter`) runnable like PyCharm's bundled debugger:
/// the interpreter's own debugpy when it has one (venvs), else OpenIDEA's private copy on
/// `PYTHONPATH`. Neither: an `unavailable` error naming the interpreter.
pub async fn prepare_debugpy(spec: &mut AdapterSpec) -> IdeResult<()> {
let (command, cwd) = (spec.command.clone(), spec.cwd.clone());
let Some(python) = tokio::task::spawn_blocking(move || resolve_binary(&command, &cwd)).await.ok().flatten() else {
return Ok(()); // Reported as a missing adapter when it is started.
};
let mut probe = Command::new(&python);
ide_core::process::no_window(probe.as_std_mut());
let probe = probe
.args(["-c", "import debugpy.adapter"])
.current_dir(&spec.cwd)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.kill_on_drop(true)
.status();
if let Ok(Ok(status)) = tokio::time::timeout(Duration::from_secs(15), probe).await
&& status.success()
{
return Ok(());
}
let lib = tools_root().map(|r| r.join(DEBUGPY_TOOL).join("lib"));
if let Some(lib) = lib.filter(|l| l.join("debugpy").join("__init__.py").is_file()) {
let mut paths = vec![lib];
paths.extend(
std::env::var_os("PYTHONPATH").map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).unwrap_or_default(),
);
let joined = std::env::join_paths(paths).map_err(|e| IdeError::internal(e.to_string()))?;
spec.env.push(("PYTHONPATH".into(), joined.to_string_lossy().into_owned()));
return Ok(());
}
Err(IdeError::unavailable(format!(
"{DEBUGPY_MISSING} for {}. Install OpenIDEA's copy (works with every interpreter) or run `{} -m pip install debugpy`",
python.display(),
python.display()
)))
}
pub fn missing(spec: &AdapterSpec) -> IdeError {
let mut msg = format!(
"debug adapter `{}` not found (`{}` is neither installed by OpenIDEA nor on PATH)",
spec.adapter, spec.command
);
if let Some(hint) = &spec.install_hint {
msg.push_str(". ");
msg.push_str(hint);
}
IdeError::unavailable(msg)
}
pub type BoxReader = Box<dyn AsyncRead + Unpin + Send>;
pub type BoxWriter = Box<dyn AsyncWrite + Unpin + Send>;
/// A connected adapter: byte streams plus the process (if we spawned one).
pub struct AdapterIo {
pub reader: BoxReader,
pub writer: BoxWriter,
pub child: Option<Child>,
/// TCP port of a server-mode adapter; child sessions (`startDebugging`) connect to it again.
pub port: Option<u16>,
/// Recent stderr lines of the adapter process, for error messages.
pub stderr: Arc<parking_lot::Mutex<Vec<String>>>,
}
pub type LaunchFuture = Pin<Box<dyn Future<Output = IdeResult<AdapterIo>> + Send>>;
/// Starts adapters. Tests substitute in-memory adapters.
pub trait AdapterLauncher: Send + Sync + 'static {
/// Starts `spec`, or, when `reuse_port` is set, opens another connection to
/// an already running server-mode adapter.
fn launch(&self, spec: AdapterSpec, reuse_port: Option<u16>) -> LaunchFuture;
}
pub struct ProcessLauncher;
const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
/// How long an adapter inside another program (a headless Godot editor) may take to listen.
const EMBEDDED_START_TIMEOUT: Duration = Duration::from_secs(120);
/// Probe of a `connect` address before starting the command.
const PROBE_TIMEOUT: Duration = Duration::from_millis(500);
impl AdapterLauncher for ProcessLauncher {
fn launch(&self, mut spec: AdapterSpec, reuse_port: Option<u16>) -> LaunchFuture {
Box::pin(async move {
let stderr = Arc::new(parking_lot::Mutex::new(Vec::new()));
if let Some(port) = reuse_port {
// A remote adapter (`dlv --headless` on another machine) may answer slowly.
let (host, timeout) = match spec.server_host.as_deref() {
Some(host) => (host, CONNECT_TIMEOUT),
None => (LOOPBACK, Duration::from_secs(5)),
};
let stream = connect_with_retries(host, port, None, timeout).await?;
let (r, w) = stream.into_split();
return Ok(AdapterIo {
reader: Box::new(r),
writer: Box::new(w),
child: None,
port: Some(port),
stderr,
});
}
let embedded = match spec.connect.as_deref().filter(|_| spec.transport == Transport::Tcp) {
Some(addr) => Some(
parse_address(addr)
.ok_or_else(|| IdeError::invalid(format!("invalid debug adapter address `{addr}`")))?,
),
None => None,
};
// An adapter inside a program the user runs (the Godot editor): use it, never stop it.
if let Some((host, port)) = &embedded
&& let Ok(Ok(stream)) =
tokio::time::timeout(PROBE_TIMEOUT, tokio::net::TcpStream::connect((host.as_str(), *port))).await
{
let _ = stream.set_nodelay(true);
let (r, w) = stream.into_split();
return Ok(AdapterIo {
reader: Box::new(r),
writer: Box::new(w),
child: None,
port: Some(*port),
stderr,
});
}
if spec.adapter == DEBUGPY_TOOL {
prepare_debugpy(&mut spec).await?;
}
let (command, cwd) = (spec.command.clone(), spec.cwd.clone());
let binary = tokio::task::spawn_blocking(move || resolve_binary(&command, &cwd))
.await
.ok()
.flatten()
.ok_or_else(|| missing(&spec))?;
let port = match spec.transport {
// An embedded adapter started without `${port}` listens on its usual address.
Transport::Tcp => match &embedded {
Some((_, port)) if !spec.args.iter().any(|a| a.contains(PORT_PLACEHOLDER)) => Some(*port),
_ => Some(free_port()?),
},
Transport::Stdio => None,
};
let args = substitute_root(&spec.args, &spec.cwd);
let args = match port {
Some(port) => substitute_port(&args, port),
None => args,
};
let mut cmd = Command::new(&binary);
ide_core::process::no_window(cmd.as_std_mut());
cmd.args(&args)
.envs(spec.env.iter().map(|(k, v)| (k, v)))
.current_dir(&spec.cwd)
.stdin(if port.is_some() { Stdio::null() } else { Stdio::piped() })
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
let mut child =
cmd.spawn().map_err(|e| IdeError::unavailable(format!("failed to start {}: {e}", binary.display())))?;
if let Some(err) = child.stderr.take() {
pump_lines(err, spec.adapter.clone(), stderr.clone());
}
match port {
None => {
let stdin = child.stdin.take().ok_or_else(|| IdeError::internal("adapter stdin unavailable"))?;
let stdout = child.stdout.take().ok_or_else(|| IdeError::internal("adapter stdout unavailable"))?;
Ok(AdapterIo {
reader: Box::new(stdout),
writer: Box::new(stdin),
child: Some(child),
port: None,
stderr,
})
}
Some(port) => {
if let Some(out) = child.stdout.take() {
pump_lines(out, spec.adapter.clone(), stderr.clone());
}
let timeout = if embedded.is_some() { EMBEDDED_START_TIMEOUT } else { CONNECT_TIMEOUT };
let stream = match connect_with_retries(LOOPBACK, port, Some(&mut child), timeout).await {
Ok(s) => s,
Err(e) => {
let _ = child.start_kill();
let tail = stderr.lock().join("\n");
return Err(if tail.is_empty() {
e
} else {
IdeError::unavailable(format!("{}\n{tail}", e.message))
});
}
};
let (r, w) = stream.into_split();
Ok(AdapterIo {
reader: Box::new(r),
writer: Box::new(w),
child: Some(child),
port: Some(port),
stderr,
})
}
}
})
}
}
fn pump_lines<R: AsyncRead + Unpin + Send + 'static>(
stream: R,
name: String,
sink: Arc<parking_lot::Mutex<Vec<String>>>,
) {
tokio::spawn(ide_jsonrpc::output::read_lines(stream, move |line| {
tracing::debug!(target: "ide_dap::adapter", "[{name}] {line}");
push_line(&sink, line);
}));
}
/// Appends to the recent-output buffer of an adapter (the last 40 lines).
pub(crate) fn push_line(sink: &parking_lot::Mutex<Vec<String>>, line: String) {
let mut buf = sink.lock();
if buf.len() >= 40 {
buf.remove(0);
}
buf.push(line);
}
const LOOPBACK: &str = "127.0.0.1";
async fn connect_with_retries(
host: &str,
port: u16,
mut child: Option<&mut Child>,
timeout: Duration,
) -> IdeResult<tokio::net::TcpStream> {
let deadline = Instant::now() + timeout;
loop {
match tokio::net::TcpStream::connect((host, port)).await {
Ok(s) => {
let _ = s.set_nodelay(true);
return Ok(s);
}
Err(e) => {
if let Some(child) = child.as_deref_mut()
&& let Ok(Some(status)) = child.try_wait()
{
return Err(IdeError::unavailable(format!(
"debug adapter exited before accepting connections ({status})"
)));
}
if Instant::now() >= deadline {
return Err(IdeError::unavailable(format!(
"could not connect to debug adapter at {host}:{port}: {e}"
)));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Managed tools win over `PATH`, and debugpy falls back to the private copy.
#[cfg(unix)]
#[tokio::test]
async fn managed_tools_and_private_debugpy() {
use std::os::unix::fs::PermissionsExt;
let root = std::env::temp_dir().join(format!("openidea-dap-tools-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let exe = |p: &Path, body: &str| {
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, body).unwrap();
std::fs::set_permissions(p, std::fs::Permissions::from_mode(0o755)).unwrap();
};
let adapter = root.join("js-debug").join("bin").join("openidea-test-js-debug-adapter");
exe(&adapter, "#!/bin/sh\n");
// An interpreter without debugpy.
let python = root.join("fake-python");
exe(&python, "#!/bin/sh\nexit 1\n");
set_tools_root(root.clone());
assert_eq!(find_binary("openidea-test-js-debug-adapter", &root), Some(adapter));
let mut spec = resolve(&builtin("debugpy").unwrap(), root.clone());
spec.command = python.to_string_lossy().into_owned();
let err = prepare_debugpy(&mut spec).await.unwrap_err();
assert!(err.message.starts_with(DEBUGPY_MISSING), "{}", err.message);
let lib = root.join(DEBUGPY_TOOL).join("lib");
std::fs::create_dir_all(lib.join("debugpy")).unwrap();
std::fs::write(lib.join("debugpy").join("__init__.py"), "").unwrap();
prepare_debugpy(&mut spec).await.unwrap();
let (k, v) = &spec.env[0];
assert_eq!(k, "PYTHONPATH");
assert!(v.starts_with(&*lib.to_string_lossy()), "{v}");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn substitutes_port_everywhere() {
let args: Vec<String> = ["dap", "--listen", "127.0.0.1:${port}", "${port}"].map(String::from).into();
assert_eq!(substitute_port(&args, 4711), ["dap", "--listen", "127.0.0.1:4711", "4711"]);
assert_eq!(substitute_port(&["--x".into()], 1), ["--x"]);
}
#[test]
fn builtins_cover_common_adapters() {
for name in ["codelldb", "debugpy", "js-debug", "delve", "netcoredbg", "java-debug"] {
let cfg = builtin(name).unwrap();
assert!(cfg.install_hint.is_some());
}
let delve = resolve(&builtin("delve").unwrap(), PathBuf::from("/"));
assert_eq!(delve.transport, Transport::Tcp);
assert!(builtin("nope").is_none());
}
#[test]
fn free_port_is_nonzero() {
assert_ne!(free_port().unwrap(), 0);
}
#[test]
fn missing_binary_reports_hint() {
let spec = resolve(&builtin("netcoredbg").unwrap(), std::env::temp_dir());
assert!(find_binary("definitely-not-a-real-binary-openidea", &spec.cwd).is_none());
let err = missing(&spec);
assert_eq!(err.kind, ide_core::error::ErrorKind::Unavailable);
assert!(err.message.contains("netcoredbg/releases"));
}
#[tokio::test]
async fn process_launcher_reports_missing_binary() {
let mut spec = resolve(&builtin("netcoredbg").unwrap(), std::env::temp_dir());
spec.command = "definitely-not-a-real-binary-openidea".into();
let err = ProcessLauncher.launch(spec, None).await.err().unwrap();
assert_eq!(err.kind, ide_core::error::ErrorKind::Unavailable);
}
}
+326
Переглянути файл
@@ -0,0 +1,326 @@
//! Attach to Process: the current user's processes, grouped by the debugger that can
//! attach to them (IDEA's `XAttachDebuggerProvider`s): JVMs with a JDWP agent, Node,
//! Python (debugpy injection), Go binaries (Delve) and anything else natively (LLDB).
use crate::adapter;
use ide_core::{IdeError, IdeResult};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::path::Path;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AttachKind {
Java,
Node,
Python,
Go,
Native,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct AttachableProcess {
pub pid: u32,
/// Executable name.
pub name: String,
pub command_line: String,
pub kind: AttachKind,
/// Debug adapter that attaches to it.
pub adapter: String,
/// Debug port the process listens on (JDWP, `--inspect`, `debugpy --listen`).
pub host: Option<String>,
pub port: Option<u16>,
}
/// One row of the process table.
#[derive(Clone, Debug)]
pub struct RawProcess {
pub pid: u32,
pub exe: String,
pub args: String,
}
/// `[host:]port` of a debug agent address (`*` and empty hosts mean loopback).
fn host_port(addr: &str) -> Option<(Option<String>, u16)> {
let addr = addr.trim();
let (host, port) = match addr.rsplit_once(':') {
Some((h, p)) => (Some(h), p),
None => (None, addr),
};
let port: u16 = port.parse().ok()?;
let host = host.filter(|h| !h.is_empty() && *h != "*" && *h != "0.0.0.0").map(str::to_owned);
Some((host, port))
}
/// `-agentlib:jdwp=transport=dt_socket,server=y,address=*:5005` (or `-Xrunjdwp:`).
pub fn jdwp_address(args: &str) -> Option<(Option<String>, u16)> {
let opts = args
.split_whitespace()
.find_map(|a| a.strip_prefix("-agentlib:jdwp=").or_else(|| a.strip_prefix("-Xrunjdwp:")))?;
let mut server = false;
let mut address = None;
for kv in opts.split(',') {
match kv.split_once('=') {
Some(("server", v)) => server = v == "y",
Some(("address", v)) => address = Some(v),
_ => {}
}
}
if !server {
return None;
}
host_port(address?)
}
/// `--inspect`, `--inspect-brk=9230`, `--inspect=127.0.0.1:9230` (default port 9229).
fn inspect_address(args: &str) -> Option<(Option<String>, u16)> {
args.split_whitespace().find_map(|a| {
let rest = a.strip_prefix("--inspect-brk").or_else(|| a.strip_prefix("--inspect"))?;
if rest.is_empty() { Some((None, 9229)) } else { host_port(rest.strip_prefix('=')?) }
})
}
/// `-m debugpy --listen [host:]port`.
fn debugpy_listen(args: &str) -> Option<(Option<String>, u16)> {
let words: Vec<&str> = args.split_whitespace().collect();
if !words.contains(&"debugpy") {
return None;
}
let i = words.iter().position(|w| *w == "--listen")?;
host_port(words.get(i + 1)?)
}
fn file_name(path: &str) -> &str {
path.rsplit(['/', '\\']).next().unwrap_or(path)
}
fn is_python(name: &str) -> bool {
let n = name.strip_suffix(".exe").unwrap_or(name);
n == "Python" || n.strip_prefix("python").is_some_and(|v| v.chars().all(|c| c.is_ascii_digit() || c == '.'))
}
/// Classifies a process; `is_go` is asked only for processes no interpreter claims.
pub fn classify(p: &RawProcess, is_go: impl Fn(&str) -> bool, native_adapter: &str) -> Option<AttachableProcess> {
let name = file_name(&p.exe).to_owned();
let make = |kind, adapter: &str, addr: Option<(Option<String>, u16)>| {
let (host, port) = match addr {
Some((h, p)) => (h, Some(p)),
None => (None, None),
};
Some(AttachableProcess {
pid: p.pid,
name: name.clone(),
command_line: p.args.clone(),
kind,
adapter: adapter.to_owned(),
host,
port,
})
};
let base = name.strip_suffix(".exe").unwrap_or(&name);
if base == "java" || base == "javaw" {
// Only JVMs started with a JDWP agent can be attached to.
return jdwp_address(&p.args).and_then(|a| make(AttachKind::Java, "java-debug", Some(a)));
}
if base == "node" || base == "nodejs" {
return make(AttachKind::Node, "js-debug", inspect_address(&p.args));
}
if is_python(&name) {
return make(AttachKind::Python, "debugpy", debugpy_listen(&p.args));
}
if is_go(&p.exe) {
return make(AttachKind::Go, "delve", None);
}
make(AttachKind::Native, native_adapter, None)
}
const GO_BUILDINFO_MAGIC: &[u8] = b"\xff Go buildinf:";
/// Go binaries carry their build info near the start of the data segment.
const GO_SCAN_LIMIT: u64 = 96 << 20;
/// Whether `exe` is a Go binary (has a `Go buildinf:` header). System locations are skipped.
pub fn is_go_binary(exe: &str) -> bool {
use std::io::Read;
let skip = ["/System/", "/usr/", "/bin/", "/sbin/", "/Library/Apple/", "/Applications/"];
if skip.iter().any(|p| exe.starts_with(p)) || !Path::new(exe).is_absolute() {
return false;
}
let Ok(file) = std::fs::File::open(exe) else { return false };
let mut reader = std::io::BufReader::with_capacity(1 << 20, file.take(GO_SCAN_LIMIT));
let finder = memchr::memmem::Finder::new(GO_BUILDINFO_MAGIC);
let mut buf = vec![0u8; 1 << 20];
let mut carry = Vec::new();
loop {
let n = match reader.read(&mut buf) {
Ok(0) | Err(_) => return false,
Ok(n) => n,
};
carry.extend_from_slice(&buf[..n]);
if finder.find(&carry).is_some() {
return true;
}
let keep = carry.len().min(GO_BUILDINFO_MAGIC.len() - 1);
carry.drain(..carry.len() - keep);
}
}
/// The current user's processes (without this one).
#[cfg(unix)]
pub fn list_processes() -> IdeResult<Vec<RawProcess>> {
use std::collections::HashMap;
let ps = |fields: &str| -> IdeResult<String> {
let out = std::process::Command::new("ps")
.args(["-x", "-ww", "-o", fields])
.output()
.map_err(|e| IdeError::unavailable(format!("cannot list processes: {e}")))?;
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
};
let split = |line: &str| -> Option<(u32, String)> {
let line = line.trim_start();
let (pid, rest) = line.split_once(char::is_whitespace)?;
Some((pid.parse().ok()?, rest.trim().to_owned()))
};
// `comm` is the executable path on macOS; on Linux it is truncated, so read /proc.
let exes: HashMap<u32, String> =
if cfg!(target_os = "linux") { HashMap::new() } else { ps("pid=,comm=")?.lines().filter_map(split).collect() };
let me = std::process::id();
Ok(ps("pid=,args=")?
.lines()
.filter_map(split)
.filter(|(pid, _)| *pid != me)
.map(|(pid, args)| {
let exe = exes
.get(&pid)
.cloned()
.or_else(|| std::fs::read_link(format!("/proc/{pid}/exe")).ok().map(|p| p.display().to_string()))
.unwrap_or_else(|| args.split_whitespace().next().unwrap_or_default().to_owned());
RawProcess { pid, exe, args }
})
.collect())
}
#[cfg(not(unix))]
pub fn list_processes() -> IdeResult<Vec<RawProcess>> {
Err(IdeError::unavailable("Attach to Process is not supported on this platform yet"))
}
/// Node's default inspector port, which `SIGUSR1` opens.
pub const NODE_INSPECTOR_PORT: u16 = 9229;
/// Turns on the inspector of a running Node process (`SIGUSR1`, as js-debug's process
/// picker does) and waits until it listens. Returns the inspector port.
#[cfg(unix)]
pub async fn enable_node_inspector(pid: u32) -> IdeResult<u16> {
let status = tokio::process::Command::new("kill")
.args(["-USR1", &pid.to_string()])
.status()
.await
.map_err(|e| IdeError::unavailable(format!("cannot signal process {pid}: {e}")))?;
if !status.success() {
return Err(IdeError::unavailable(format!("cannot signal process {pid}")));
}
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while std::time::Instant::now() < deadline {
if tokio::net::TcpStream::connect(("127.0.0.1", NODE_INSPECTOR_PORT)).await.is_ok() {
return Ok(NODE_INSPECTOR_PORT);
}
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
Err(IdeError::unavailable(format!(
"process {pid} did not open its inspector on port {NODE_INSPECTOR_PORT} (is the port taken?)"
)))
}
#[cfg(not(unix))]
pub async fn enable_node_inspector(pid: u32) -> IdeResult<u16> {
Err(IdeError::unavailable(format!("start process {pid} with --inspect to attach to it")))
}
/// Attachable processes, sorted by kind then name. Blocking (runs `ps`, reads binaries).
pub fn attachable_processes() -> IdeResult<Vec<AttachableProcess>> {
let cwd = std::env::temp_dir();
let native = if adapter::resolve_binary("codelldb", &cwd).is_some() { "codelldb" } else { "lldb-dap" };
let mut out: Vec<AttachableProcess> =
list_processes()?.iter().filter_map(|p| classify(p, is_go_binary, native)).collect();
out.sort_by(|a, b| a.kind.cmp(&b.kind).then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase())));
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
fn raw(exe: &str, args: &str) -> RawProcess {
RawProcess { pid: 7, exe: exe.into(), args: args.into() }
}
fn kind(exe: &str, args: &str) -> Option<(AttachKind, Option<u16>)> {
classify(&raw(exe, args), |e| e.ends_with("/gosrv"), "codelldb").map(|p| (p.kind, p.port))
}
#[test]
fn parses_jdwp_agents() {
let a = "java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar app.jar";
assert_eq!(jdwp_address(a), Some((None, 5005)));
assert_eq!(
jdwp_address("java -Xrunjdwp:transport=dt_socket,server=y,address=10.0.0.2:8000 X"),
Some((Some("10.0.0.2".into()), 8000))
);
// A client-mode agent connects out; nothing to attach to.
assert_eq!(jdwp_address("java -agentlib:jdwp=transport=dt_socket,server=n,address=5005 X"), None);
assert_eq!(kind("/jdk/bin/java", a), Some((AttachKind::Java, Some(5005))));
assert_eq!(kind("/jdk/bin/java", "java -jar app.jar"), None);
}
#[test]
fn classifies_interpreters() {
assert_eq!(kind("/usr/local/bin/node", "node --inspect server.js"), Some((AttachKind::Node, Some(9229))));
assert_eq!(
kind("/usr/local/bin/node", "node --inspect-brk=127.0.0.1:9333 a.js"),
Some((AttachKind::Node, Some(9333)))
);
assert_eq!(kind("/usr/local/bin/node", "node a.js"), Some((AttachKind::Node, None)));
assert_eq!(kind("/usr/bin/python3.12", "python3.12 app.py"), Some((AttachKind::Python, None)));
assert_eq!(
kind("/usr/bin/python3", "python3 -m debugpy --listen 5678 --wait-for-client app.py"),
Some((AttachKind::Python, Some(5678)))
);
assert_eq!(
kind("/Library/Frameworks/Python.framework/Versions/3.12/Resources/Python.app/Contents/MacOS/Python", "x"),
Some((AttachKind::Python, None))
);
assert_eq!(kind("/home/me/gosrv", "./gosrv"), Some((AttachKind::Go, None)));
assert_eq!(kind("/home/me/a.out", "./a.out"), Some((AttachKind::Native, None)));
assert!(!is_python("pythonista"));
}
#[test]
fn detects_go_binaries_by_buildinfo() {
let dir = std::env::temp_dir().join(format!("openidea-attach-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let go = dir.join("gobin");
let mut bytes = vec![0u8; (1 << 20) - 5];
bytes.extend_from_slice(GO_BUILDINFO_MAGIC);
std::fs::write(&go, &bytes).unwrap();
let other = dir.join("cbin");
std::fs::write(&other, vec![0u8; 4096]).unwrap();
assert!(is_go_binary(go.to_str().unwrap()));
assert!(!is_go_binary(other.to_str().unwrap()));
assert!(!is_go_binary("/usr/bin/true"));
std::fs::remove_dir_all(&dir).unwrap();
}
#[cfg(unix)]
#[test]
fn lists_own_processes() {
let mut child = std::process::Command::new("sleep").arg("30").spawn().unwrap();
let list = list_processes().unwrap();
let _ = child.kill();
let _ = child.wait();
let found = list.iter().find(|p| p.pid == child.id()).expect("sleep is listed");
assert!(found.args.contains("sleep 30"));
assert!(found.exe.ends_with("sleep"));
assert!(list.iter().all(|p| p.pid != std::process::id()));
}
}
+111
Переглянути файл
@@ -0,0 +1,111 @@
//! `cargo` launch integration (what the CodeLLDB VS Code extension does):
//! a launch request may carry `"cargo": {"args": ["build", …], "filter": {…}}`
//! instead of `program`; we run the build with JSON messages and launch the
//! produced executable.
use crate::adapter::find_binary;
use ide_core::{IdeError, IdeResult};
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use tokio::io::{AsyncBufReadExt, BufReader};
#[derive(Debug, Default, Clone)]
pub struct CargoFilter {
pub name: Option<String>,
pub kind: Option<String>,
}
impl CargoFilter {
pub fn from_json(v: Option<&Value>) -> Self {
let get = |k: &str| v.and_then(|v| v.get(k)).and_then(Value::as_str).map(str::to_owned);
Self { name: get("name"), kind: get("kind") }
}
}
/// Picks the executable from one `cargo --message-format=json` stdout line.
pub fn artifact_executable(line: &str, filter: &CargoFilter) -> Option<PathBuf> {
let msg: Value = serde_json::from_str(line).ok()?;
if msg.get("reason")?.as_str()? != "compiler-artifact" {
return None;
}
let exe = msg.get("executable")?.as_str()?;
let target = msg.get("target");
if let Some(name) = &filter.name
&& target.and_then(|t| t.get("name")).and_then(Value::as_str) != Some(name.as_str())
{
return None;
}
if let Some(kind) = &filter.kind {
let kinds = target.and_then(|t| t.get("kind")).and_then(Value::as_array);
if !kinds.is_some_and(|k| k.iter().any(|x| x.as_str() == Some(kind.as_str()))) {
return None;
}
}
Some(PathBuf::from(exe))
}
/// Runs cargo and returns the (last matching) executable artifact. Human
/// readable compiler output is passed to `on_output` line by line.
pub async fn build(
cwd: &Path,
args: &[String],
filter: &CargoFilter,
on_output: impl Fn(String) + Send + Sync + 'static,
) -> IdeResult<PathBuf> {
let cargo = find_binary("cargo", cwd).ok_or_else(|| IdeError::unavailable("`cargo` not found on PATH"))?;
let mut cmd = tokio::process::Command::new(cargo);
ide_core::process::no_window(cmd.as_std_mut());
cmd.args(args)
.arg("--message-format=json-render-diagnostics")
.current_dir(cwd)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
on_output(format!("> cargo {}\n", args.join(" ")));
let mut child = cmd.spawn().map_err(|e| IdeError::unavailable(format!("failed to run cargo: {e}")))?;
let stderr = child.stderr.take().ok_or_else(|| IdeError::internal("cargo stderr unavailable"))?;
let stdout = child.stdout.take().ok_or_else(|| IdeError::internal("cargo stdout unavailable"))?;
let on_output = std::sync::Arc::new(on_output);
let err_out = on_output.clone();
let err_task = tokio::spawn(async move {
let mut lines = BufReader::new(stderr).lines();
while let Ok(Some(line)) = lines.next_line().await {
err_out(format!("{line}\n"));
}
});
let mut found = None;
let mut lines = BufReader::new(stdout).lines();
while let Ok(Some(line)) = lines.next_line().await {
if let Some(exe) = artifact_executable(&line, filter) {
found = Some(exe);
}
}
let _ = err_task.await;
let status = child.wait().await?;
if !status.success() {
return Err(IdeError::invalid(format!("cargo {} failed ({status})", args.first().map_or("", |s| s))));
}
found.ok_or_else(|| IdeError::invalid("cargo produced no executable to debug"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn picks_executables_with_filters() {
let line =
r#"{"reason":"compiler-artifact","target":{"name":"app","kind":["bin"]},"executable":"/t/debug/app"}"#;
let lib = r#"{"reason":"compiler-artifact","target":{"name":"core","kind":["lib"]},"executable":null}"#;
let any = CargoFilter::default();
assert_eq!(artifact_executable(line, &any), Some(PathBuf::from("/t/debug/app")));
assert_eq!(artifact_executable(lib, &any), None);
assert_eq!(artifact_executable("not json", &any), None);
let other = CargoFilter { name: Some("other".into()), kind: None };
assert_eq!(artifact_executable(line, &other), None);
let bin = CargoFilter { name: None, kind: Some("bin".into()) };
assert!(artifact_executable(line, &bin).is_some());
}
}
+316
Переглянути файл
@@ -0,0 +1,316 @@
//! One DAP wire connection: `Content-Length` framing (shared with LSP via
//! `ide_jsonrpc::framing`), sequence numbers, request/response correlation,
//! and delivery of events and reverse requests to the session task.
//!
//! DAP is not JSON-RPC: every message is `{seq, type, ...}` where `type` is
//! `request` (`command`, `arguments`), `response` (`request_seq`, `success`,
//! `command`, `message`, `body`) or `event` (`event`, `body`).
use ide_jsonrpc::framing;
use parking_lot::Mutex;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncWrite, BufReader};
use tokio::sync::{mpsc, oneshot};
#[derive(Debug, Clone)]
pub enum DapError {
/// The adapter answered `success: false`.
Failed {
command: String,
message: String,
},
Closed,
Timeout(String),
Protocol(String),
}
impl std::fmt::Display for DapError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Failed { command, message } => write!(f, "{command} failed: {message}"),
Self::Closed => f.write_str("debug adapter connection closed"),
Self::Timeout(command) => write!(f, "debug adapter did not answer `{command}` in time"),
Self::Protocol(m) => write!(f, "invalid debug adapter message: {m}"),
}
}
}
impl From<DapError> for ide_core::IdeError {
fn from(e: DapError) -> Self {
use ide_core::error::ErrorKind;
let kind = match e {
DapError::Closed => ErrorKind::Unavailable,
DapError::Failed { .. } => ErrorKind::InvalidInput,
DapError::Timeout(_) | DapError::Protocol(_) => ErrorKind::Protocol,
};
ide_core::IdeError::new(kind, e.to_string())
}
}
/// Adapter-initiated traffic, delivered in arrival order.
#[derive(Debug)]
pub enum Incoming {
Event {
event: String,
body: Value,
},
/// Reverse request (`runInTerminal`, `startDebugging`); answer with [`Connection::respond`].
Request {
seq: i64,
command: String,
arguments: Value,
},
/// The transport is gone; always the last message.
Closed,
}
type Pending = HashMap<i64, (String, oneshot::Sender<Result<Value, DapError>>)>;
struct Inner {
out: mpsc::UnboundedSender<Vec<u8>>,
pending: Mutex<Pending>,
seq: AtomicI64,
closed: AtomicBool,
cancel: ide_core::CancellationToken,
}
impl Inner {
fn close(&self) {
self.closed.store(true, Ordering::Release);
for (_, (_, tx)) in std::mem::take(&mut *self.pending.lock()) {
let _ = tx.send(Err(DapError::Closed));
}
}
}
/// A connected adapter. Cloning shares the connection.
#[derive(Clone)]
pub struct Connection {
inner: Arc<Inner>,
}
impl Connection {
/// Starts reader/writer tasks over `reader`/`writer`. Must be called inside a Tokio runtime.
pub fn start<R, W>(reader: R, writer: W) -> (Self, mpsc::UnboundedReceiver<Incoming>)
where
R: AsyncRead + Unpin + Send + 'static,
W: AsyncWrite + Unpin + Send + 'static,
{
Self::start_with_output(reader, writer, Box::new(|_| {}))
}
/// [`start`](Self::start) that hands lines outside the protocol (an adapter's
/// own output on stdout, decoded) to `on_output`.
pub fn start_with_output<R, W>(
reader: R,
mut writer: W,
on_output: Box<dyn Fn(String) + Send + Sync>,
) -> (Self, mpsc::UnboundedReceiver<Incoming>)
where
R: AsyncRead + Unpin + Send + 'static,
W: AsyncWrite + Unpin + Send + 'static,
{
let (out_tx, mut out_rx) = mpsc::unbounded_channel::<Vec<u8>>();
let (in_tx, in_rx) = mpsc::unbounded_channel();
let inner = Arc::new(Inner {
out: out_tx,
pending: Mutex::new(HashMap::new()),
seq: AtomicI64::new(1),
closed: false.into(),
cancel: ide_core::CancellationToken::new(),
});
let weak = Arc::downgrade(&inner);
let cancel = inner.cancel.clone();
tokio::spawn(async move {
loop {
let body = tokio::select! {
body = out_rx.recv() => body,
_ = cancel.cancelled() => None,
};
let Some(body) = body else { break };
if let Err(e) = framing::write_message(&mut writer, &body).await {
tracing::debug!("dap write failed: {e}");
if let Some(inner) = weak.upgrade() {
inner.close();
}
break;
}
}
let _ = tokio::io::AsyncWriteExt::shutdown(&mut writer).await;
});
let reader_inner = inner.clone();
tokio::spawn(async move {
let mut reader = BufReader::new(reader);
let mut noise = |line: &[u8]| {
let text = ide_jsonrpc::output::decode(line);
tracing::warn!("dap: output outside the protocol: {text}");
on_output(text);
};
loop {
let msg = tokio::select! {
msg = framing::read_message_or_noise(&mut reader, &mut noise) => msg,
_ = reader_inner.cancel.cancelled() => break,
};
match msg {
Ok(Some(body)) => dispatch(&reader_inner, &in_tx, &body),
Ok(None) => break,
Err(e) => {
tracing::warn!("dap read failed: {e}");
break;
}
}
}
reader_inner.close();
let _ = in_tx.send(Incoming::Closed);
});
(Self { inner }, in_rx)
}
/// Drops the transport; pending requests fail and the incoming stream ends.
pub fn close(&self) {
self.inner.close();
self.inner.cancel.cancel();
}
pub fn is_closed(&self) -> bool {
self.inner.closed.load(Ordering::Acquire)
}
fn send(&self, value: &Value) -> Result<(), DapError> {
if self.is_closed() {
return Err(DapError::Closed);
}
let body = serde_json::to_vec(value).map_err(|e| DapError::Protocol(e.to_string()))?;
tracing::trace!(target: "ide_dap::wire", "--> {}", String::from_utf8_lossy(&body));
self.inner.out.send(body).map_err(|_| DapError::Closed)
}
/// Sends a request and returns a receiver for its response body, without waiting.
pub fn send_request(
&self,
command: &str,
arguments: Value,
) -> Result<oneshot::Receiver<Result<Value, DapError>>, DapError> {
let seq = self.inner.seq.fetch_add(1, Ordering::Relaxed);
let (tx, rx) = oneshot::channel();
self.inner.pending.lock().insert(seq, (command.to_owned(), tx));
let mut msg = json!({ "seq": seq, "type": "request", "command": command });
if !arguments.is_null() {
msg["arguments"] = arguments;
}
if let Err(e) = self.send(&msg) {
self.inner.pending.lock().remove(&seq);
return Err(e);
}
Ok(rx)
}
/// Sends a request and waits for the response body (`null` when absent).
pub async fn request(&self, command: &str, arguments: Value) -> Result<Value, DapError> {
let rx = self.send_request(command, arguments)?;
rx.await.unwrap_or(Err(DapError::Closed))
}
pub async fn request_timeout(&self, command: &str, arguments: Value, timeout: Duration) -> Result<Value, DapError> {
match tokio::time::timeout(timeout, self.request(command, arguments)).await {
Ok(r) => r,
Err(_) => Err(DapError::Timeout(command.to_owned())),
}
}
/// Answers a reverse request.
pub fn respond(&self, request_seq: i64, command: &str, result: Result<Value, String>) -> Result<(), DapError> {
let seq = self.inner.seq.fetch_add(1, Ordering::Relaxed);
let mut msg = json!({
"seq": seq,
"type": "response",
"request_seq": request_seq,
"command": command,
"success": result.is_ok(),
});
match result {
Ok(body) if !body.is_null() => msg["body"] = body,
Ok(_) => {}
Err(message) => msg["message"] = Value::String(message),
}
self.send(&msg)
}
}
fn dispatch(inner: &Inner, incoming: &mpsc::UnboundedSender<Incoming>, body: &[u8]) {
tracing::trace!(target: "ide_dap::wire", "<-- {}", String::from_utf8_lossy(body));
let msg: Value = match serde_json::from_slice(body) {
Ok(v) => v,
Err(e) => {
tracing::warn!("dap: unparsable message: {e}");
return;
}
};
match msg.get("type").and_then(Value::as_str) {
Some("response") => {
let Some(request_seq) = msg.get("request_seq").and_then(Value::as_i64) else {
tracing::warn!("dap: response without request_seq");
return;
};
let Some((command, tx)) = inner.pending.lock().remove(&request_seq) else {
tracing::debug!("dap: response for unknown request {request_seq}");
return;
};
let result = if msg.get("success").and_then(Value::as_bool).unwrap_or(false) {
Ok(msg.get("body").cloned().unwrap_or(Value::Null))
} else {
Err(DapError::Failed { command, message: error_message(&msg) })
};
let _ = tx.send(result);
}
Some("event") => {
let event = msg.get("event").and_then(Value::as_str).unwrap_or_default().to_owned();
let body = msg.get("body").cloned().unwrap_or(Value::Null);
let _ = incoming.send(Incoming::Event { event, body });
}
Some("request") => {
let seq = msg.get("seq").and_then(Value::as_i64).unwrap_or_default();
let command = msg.get("command").and_then(Value::as_str).unwrap_or_default().to_owned();
let arguments = msg.get("arguments").cloned().unwrap_or(Value::Null);
let _ = incoming.send(Incoming::Request { seq, command, arguments });
}
other => tracing::warn!("dap: unknown message type {other:?}"),
}
}
/// `message`, or the formatted `body.error` (`{format, variables}`) when present.
fn error_message(msg: &Value) -> String {
if let Some(err) = msg.pointer("/body/error")
&& let Some(format) = err.get("format").and_then(Value::as_str)
{
let mut text = format.to_owned();
if let Some(vars) = err.get("variables").and_then(Value::as_object) {
for (k, v) in vars {
let v = v.as_str().map(str::to_owned).unwrap_or_else(|| v.to_string());
text = text.replace(&format!("{{{k}}}"), &v);
}
}
return text;
}
msg.get("message").and_then(Value::as_str).unwrap_or("request failed").to_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_structured_errors() {
let msg =
json!({"message": "x", "body": {"error": {"format": "cannot find {file}", "variables": {"file": "a.py"}}}});
assert_eq!(error_message(&msg), "cannot find a.py");
assert_eq!(error_message(&json!({"message": "nope"})), "nope");
}
}
+97
Переглянути файл
@@ -0,0 +1,97 @@
//! "Listen to remote JVM" (IDEA's Remote JVM Debug in listen mode): the IDE listens,
//! the remote JVM (`-agentlib:jdwp=…,server=n,address=<ide-host>:<port>`) connects to
//! it. java-debug can only attach, so the connection is relayed: once the JVM has
//! connected, java-debug attaches to a one-shot loopback port that forwards the JDWP
//! stream (handshake included) both ways.
use ide_core::{CancellationToken, IdeError, IdeResult};
use std::io::ErrorKind;
use std::time::Duration;
use tokio::net::{TcpListener, TcpStream};
/// How long the relay waits for java-debug to attach once the JVM is connected.
const ATTACH_TIMEOUT: Duration = Duration::from_secs(60);
/// The address to listen on for `host`: the interface it names, or every interface when
/// it is not one of this machine's (a DNS name or a NAT address the JVM connects to).
async fn bind(host: &str, port: u16) -> IdeResult<TcpListener> {
let host = host.trim();
let host = if host.is_empty() { "0.0.0.0" } else { host.trim_start_matches('[').trim_end_matches(']') };
match TcpListener::bind((host, port)).await {
Ok(l) => Ok(l),
Err(e) if e.kind() == ErrorKind::AddrNotAvailable || e.kind() == ErrorKind::Other => {
TcpListener::bind(("0.0.0.0", port))
.await
.map_err(|e| IdeError::unavailable(format!("cannot listen on port {port}: {e}")))
}
Err(e) => Err(IdeError::unavailable(format!("cannot listen on {host}:{port}: {e}"))),
}
}
/// Waits for a JVM to connect to `host:port`, then returns a loopback port that relays
/// to it (accepts one connection). Cancelled through `cancel` while waiting.
pub async fn listen_for_jvm(host: &str, port: u16, cancel: CancellationToken) -> IdeResult<u16> {
let listener = bind(host, port).await?;
let jvm = tokio::select! {
_ = cancel.cancelled() => return Err(IdeError::cancelled()),
accepted = listener.accept() => accepted.map_err(IdeError::from)?.0,
};
drop(listener);
let _ = jvm.set_nodelay(true);
let relay = TcpListener::bind(("127.0.0.1", 0)).await?;
let local = relay.local_addr()?.port();
tokio::spawn(relay_one(relay, jvm));
Ok(local)
}
async fn relay_one(relay: TcpListener, mut jvm: TcpStream) {
let debugger = match tokio::time::timeout(ATTACH_TIMEOUT, relay.accept()).await {
Ok(Ok((s, _))) => s,
_ => return,
};
drop(relay);
let _ = debugger.set_nodelay(true);
let mut debugger = debugger;
if let Err(e) = tokio::io::copy_bidirectional(&mut debugger, &mut jvm).await {
tracing::debug!(target: "ide_dap::jdwp", "JDWP relay closed: {e}");
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::test]
async fn relays_the_jdwp_handshake_both_ways() {
let port = TcpListener::bind(("127.0.0.1", 0)).await.unwrap().local_addr().unwrap().port();
let waiting = tokio::spawn(listen_for_jvm("127.0.0.1", port, CancellationToken::new()));
// The remote JVM (`server=n`) connects to the IDE.
let mut jvm = loop {
if let Ok(s) = TcpStream::connect(("127.0.0.1", port)).await {
break s;
}
tokio::time::sleep(Duration::from_millis(20)).await;
};
let local = waiting.await.unwrap().unwrap();
// java-debug attaches to the relay and starts the handshake.
let mut debugger = TcpStream::connect(("127.0.0.1", local)).await.unwrap();
debugger.write_all(b"JDWP-Handshake").await.unwrap();
let mut buf = [0u8; 14];
jvm.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"JDWP-Handshake");
jvm.write_all(b"JDWP-Handshake").await.unwrap();
debugger.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"JDWP-Handshake");
}
#[tokio::test]
async fn waiting_is_cancellable() {
let cancel = CancellationToken::new();
let waiting = tokio::spawn(listen_for_jvm("127.0.0.1", 0, cancel.clone()));
tokio::time::sleep(Duration::from_millis(50)).await;
cancel.cancel();
let err = waiting.await.unwrap().unwrap_err();
assert_eq!(err.kind, ide_core::error::ErrorKind::Cancelled);
}
}
File diff suppressed because it is too large. Load diff
+487
Переглянути файл
@@ -0,0 +1,487 @@
//! One debug session: the adapter connection, its capabilities, the event
//! loop translating DAP events into `IdeEvent::Debug`, and reverse requests.
use crate::adapter::AdapterSpec;
use crate::connection::{Connection, DapError, Incoming};
use crate::{Breakpoint, BreakpointState, DataBreakpoint, DebugManager, FunctionBreakpoint, SessionOwner};
use ide_core::event::DebugEvent;
use ide_core::{DebugSessionId, IdeEvent, TaskId};
use parking_lot::Mutex;
use serde_json::{Value, json};
use std::collections::{BTreeSet, HashMap};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Arc;
use std::sync::Weak;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio::process::Child;
use tokio::sync::{mpsc, watch};
/// What has to be re-sent on the next stop.
#[derive(Default)]
pub(crate) struct PendingRestore {
pub files: BTreeSet<PathBuf>,
pub functions: bool,
pub data: bool,
}
pub(crate) struct Session {
pub id: DebugSessionId,
/// Who started the session (inherited by child sessions); all its events go to the owner's sink.
pub owner: SessionOwner,
pub name: String,
pub adapter: String,
pub parent: Option<DebugSessionId>,
pub request: String,
pub conn: Connection,
pub spec: AdapterSpec,
/// TCP port of a server-mode adapter (for `startDebugging` child sessions).
pub port: Option<u16>,
pub caps: Mutex<Value>,
pub adapter_process: Mutex<Option<Child>>,
/// Processes started for `runInTerminal`.
pub debuggees: Mutex<Vec<Child>>,
/// Set once `configurationDone` was sent; breakpoint updates go out live after that.
pub configured: AtomicBool,
pub initialized: watch::Sender<bool>,
pub terminated: AtomicBool,
/// Breakpoints replaced for run to cursor / force stepping, restored on the next stop.
pub pending_restore: Mutex<PendingRestore>,
/// Adapter breakpoint id → (file, index in the stored list).
pub breakpoint_ids: Mutex<HashMap<i64, (PathBuf, usize)>>,
/// Resolved path → the path as the IDE knows it, for adapters given resolved paths.
resolved_paths: Mutex<HashMap<PathBuf, PathBuf>>,
progress: Mutex<HashMap<String, TaskId>>,
}
/// Adapters that only match files by their resolved path: Godot maps a script to
/// `res://` by prefix and silently never stops at a breakpoint set through a
/// symlinked path (`/tmp/game` while the editor has `/private/tmp/game`).
const RESOLVES_PATHS: &[&str] = &["godot"];
impl Session {
#[allow(clippy::too_many_arguments)]
pub fn new(
id: DebugSessionId,
owner: SessionOwner,
name: String,
adapter: String,
parent: Option<DebugSessionId>,
request: String,
conn: Connection,
spec: AdapterSpec,
port: Option<u16>,
child: Option<Child>,
) -> Self {
Self {
id,
owner,
name,
adapter,
parent,
request,
conn,
spec,
port,
caps: Mutex::new(Value::Null),
adapter_process: Mutex::new(child),
debuggees: Mutex::new(Vec::new()),
configured: AtomicBool::new(false),
initialized: watch::channel(false).0,
terminated: AtomicBool::new(false),
pending_restore: Mutex::default(),
breakpoint_ids: Mutex::new(HashMap::new()),
resolved_paths: Mutex::new(HashMap::new()),
progress: Mutex::new(HashMap::new()),
}
}
pub fn emit(&self, event: DebugEvent) {
self.owner.sink.emit(IdeEvent::Debug { session: self.id, event });
}
/// `path` as sent to the adapter (resolved for [`RESOLVES_PATHS`] adapters).
pub fn adapter_path(&self, path: &Path) -> PathBuf {
if !RESOLVES_PATHS.contains(&self.adapter.as_str()) {
return path.to_path_buf();
}
match std::fs::canonicalize(path) {
Ok(real) if real != path => {
self.resolved_paths.lock().insert(real.clone(), path.to_path_buf());
real
}
_ => path.to_path_buf(),
}
}
/// A path from the adapter as the IDE knows it (undoes [`Session::adapter_path`]).
pub fn client_path(&self, path: &str) -> Option<PathBuf> {
self.resolved_paths.lock().get(Path::new(path)).cloned()
}
pub fn supports(&self, cap: &str) -> bool {
self.caps.lock().get(cap).and_then(Value::as_bool).unwrap_or(false)
}
/// Sends the breakpoints of one file (disabled ones omitted, plus an optional
/// temporary line) and returns verification states aligned with `breakpoints`.
pub async fn send_breakpoints(
&self,
path: &Path,
breakpoints: &[Breakpoint],
temp_line: Option<u32>,
) -> Result<Vec<BreakpointState>, DapError> {
let enabled: Vec<usize> = (0..breakpoints.len()).filter(|&i| breakpoints[i].enabled).collect();
let mut source_bps: Vec<Value> = enabled
.iter()
.map(|&i| {
let b = &breakpoints[i];
let mut v = json!({ "line": b.line + 1 });
if let Some(c) = b.condition.as_deref().filter(|c| !c.trim().is_empty()) {
v["condition"] = c.into();
}
if let Some(c) = b.hit_condition.as_deref().filter(|c| !c.trim().is_empty()) {
v["hitCondition"] = c.into();
}
if let Some(m) = b.log_message.as_deref().filter(|m| !m.is_empty()) {
v["logMessage"] = m.into();
}
v
})
.collect();
if let Some(line) = temp_line
&& !enabled.iter().any(|&i| breakpoints[i].line == line)
{
source_bps.push(json!({ "line": line + 1 }));
}
let name = path.file_name().map(|n| n.to_string_lossy().into_owned());
let body = self
.conn
.request_timeout(
"setBreakpoints",
json!({
"source": { "path": self.adapter_path(path), "name": name },
"breakpoints": source_bps,
"sourceModified": false,
}),
Duration::from_secs(10),
)
.await?;
let returned = body.get("breakpoints").and_then(Value::as_array).cloned().unwrap_or_default();
let mut ids = self.breakpoint_ids.lock();
ids.retain(|_, (p, _)| p != path);
let mut states: Vec<BreakpointState> = breakpoints
.iter()
.map(|b| BreakpointState {
line: b.line,
verified: false,
message: (!b.enabled).then(|| "Disabled".to_owned()),
})
.collect();
for (k, &i) in enabled.iter().enumerate() {
let Some(r) = returned.get(k) else { continue };
if let Some(id) = r.get("id").and_then(Value::as_i64) {
ids.insert(id, (path.to_path_buf(), i));
}
states[i] = state_from(r, breakpoints[i].line);
}
Ok(states)
}
/// Sends method breakpoints (disabled ones omitted); states aligned with `list`.
pub async fn send_function_breakpoints(
&self,
list: &[FunctionBreakpoint],
) -> Result<Vec<BreakpointState>, DapError> {
let enabled: Vec<usize> = (0..list.len()).filter(|&i| list[i].enabled).collect();
let sent: Vec<Value> = enabled
.iter()
.map(|&i| {
let b = &list[i];
let mut v = json!({ "name": b.name });
with_conditions(&mut v, b.condition.as_deref(), b.hit_condition.as_deref());
v
})
.collect();
let body = self
.conn
.request_timeout("setFunctionBreakpoints", json!({ "breakpoints": sent }), Duration::from_secs(10))
.await?;
Ok(align_states(list.iter().map(|b| b.enabled), &enabled, &body))
}
/// Sends field watchpoints (disabled ones omitted); states aligned with `list`.
pub async fn send_data_breakpoints(&self, list: &[DataBreakpoint]) -> Result<Vec<BreakpointState>, DapError> {
let enabled: Vec<usize> = (0..list.len()).filter(|&i| list[i].enabled).collect();
let sent: Vec<Value> = enabled
.iter()
.map(|&i| {
let b = &list[i];
let mut v = json!({ "dataId": b.data_id });
if let Some(a) = b.access_type.as_deref().filter(|a| !a.is_empty()) {
v["accessType"] = a.into();
}
with_conditions(&mut v, b.condition.as_deref(), b.hit_condition.as_deref());
v
})
.collect();
let body = self
.conn
.request_timeout("setDataBreakpoints", json!({ "breakpoints": sent }), Duration::from_secs(10))
.await?;
Ok(align_states(list.iter().map(|b| b.enabled), &enabled, &body))
}
/// Kills the adapter and every debuggee we started.
pub fn kill_processes(&self) {
if let Some(mut c) = self.adapter_process.lock().take() {
let _ = c.start_kill();
}
for mut c in std::mem::take(&mut *self.debuggees.lock()) {
let _ = c.start_kill();
}
}
}
fn with_conditions(v: &mut Value, condition: Option<&str>, hit_condition: Option<&str>) {
if let Some(c) = condition.filter(|c| !c.trim().is_empty()) {
v["condition"] = c.into();
}
if let Some(c) = hit_condition.filter(|c| !c.trim().is_empty()) {
v["hitCondition"] = c.into();
}
}
/// States for a list whose enabled entries (`sent`, by index) went out in order.
fn align_states(all: impl Iterator<Item = bool>, sent: &[usize], body: &Value) -> Vec<BreakpointState> {
let returned = body.get("breakpoints").and_then(Value::as_array).cloned().unwrap_or_default();
let mut states: Vec<BreakpointState> = all
.map(|e| BreakpointState { line: 0, verified: false, message: (!e).then(|| "Disabled".to_owned()) })
.collect();
for (k, &i) in sent.iter().enumerate() {
if let Some(r) = returned.get(k) {
states[i] = state_from(r, 0);
}
}
states
}
pub(crate) fn state_from(r: &Value, fallback_line: u32) -> BreakpointState {
BreakpointState {
line: r.get("line").and_then(Value::as_u64).map(|l| (l as u32).saturating_sub(1)).unwrap_or(fallback_line),
verified: r.get("verified").and_then(Value::as_bool).unwrap_or(false),
message: r.get("message").and_then(Value::as_str).map(str::to_owned),
}
}
/// Processes adapter traffic until the connection closes.
pub(crate) async fn event_loop(
manager: Weak<DebugManager>,
session: Arc<Session>,
mut incoming: mpsc::UnboundedReceiver<Incoming>,
) {
while let Some(msg) = incoming.recv().await {
let Some(mgr) = manager.upgrade() else { break };
match msg {
Incoming::Event { event, body } => on_event(&mgr, &session, &event, body).await,
Incoming::Request { seq, command, arguments } => {
on_reverse_request(&mgr, &session, seq, &command, arguments)
}
Incoming::Closed => break,
}
}
if let Some(mgr) = manager.upgrade() {
mgr.session_closed(&session);
}
}
async fn on_event(mgr: &Arc<DebugManager>, s: &Arc<Session>, event: &str, body: Value) {
let int = |k: &str| body.get(k).and_then(Value::as_i64);
let str_ = |k: &str| body.get(k).and_then(Value::as_str).map(str::to_owned);
match event {
"initialized" => {
s.initialized.send_replace(true);
s.emit(DebugEvent::Initialized);
}
"stopped" => {
mgr.restore_breakpoints(s).await;
s.emit(DebugEvent::Stopped {
thread_id: int("threadId").unwrap_or(0),
reason: str_("reason").unwrap_or_else(|| "pause".into()),
description: str_("description").or_else(|| str_("text")),
all_threads_stopped: body.get("allThreadsStopped").and_then(Value::as_bool).unwrap_or(false),
});
}
"continued" => s.emit(DebugEvent::Continued {
thread_id: int("threadId").unwrap_or(0),
all_threads_continued: body.get("allThreadsContinued").and_then(Value::as_bool).unwrap_or(true),
}),
"output" => {
let category = str_("category").unwrap_or_else(|| "console".into());
if category == "telemetry" {
return;
}
let output = str_("output").unwrap_or_default();
s.emit(DebugEvent::Output { category, output });
}
"thread" => s.emit(DebugEvent::Thread {
thread_id: int("threadId").unwrap_or(0),
started: body.get("reason").and_then(Value::as_str) != Some("exited"),
}),
"breakpoint" => {
let Some(bp) = body.get("breakpoint") else { return };
let Some(id) = bp.get("id").and_then(Value::as_i64) else { return };
let Some((path, index)) = s.breakpoint_ids.lock().get(&id).cloned() else { return };
if mgr.update_breakpoint_state(&path, index, bp) {
s.emit(DebugEvent::BreakpointsChanged { path: path.into() });
}
}
// Godot repeats `exited` / `terminated` when the client disconnects afterwards.
"exited" if !s.terminated.load(Ordering::Acquire) => {
s.emit(DebugEvent::Exited { code: int("exitCode").unwrap_or(0) as i32 })
}
"exited" => {}
"terminated" => {
if !s.terminated.swap(true, Ordering::AcqRel) {
s.emit(DebugEvent::Terminated);
}
let mgr = mgr.clone();
let s = s.clone();
tokio::spawn(async move { mgr.shutdown(&s, false).await });
}
"capabilities" => {
if let Some(new) = body.get("capabilities").and_then(Value::as_object) {
let mut caps = s.caps.lock();
if !caps.is_object() {
*caps = json!({});
}
if let Some(obj) = caps.as_object_mut() {
obj.extend(new.clone());
}
}
}
"progressStart" | "progressUpdate" | "progressEnd" => {
let Some(pid) = str_("progressId") else { return };
let done = event == "progressEnd";
let (id, title) = {
let mut progress = s.progress.lock();
let id =
if done { progress.remove(&pid) } else { Some(*progress.entry(pid).or_insert_with(TaskId::next)) };
(id, str_("title").unwrap_or_else(|| s.name.clone()))
};
let Some(id) = id else { return };
s.owner.sink.emit(IdeEvent::Task {
id,
title,
message: str_("message"),
fraction: body.get("percentage").and_then(Value::as_f64).map(|p| (p / 100.0) as f32),
done,
});
}
"hotcodereplace" => s.emit(DebugEvent::HotCodeReplace {
change_type: crate::hot_code_replace_type(body.get("changeType")),
message: str_("message").unwrap_or_default(),
}),
_ => tracing::trace!("dap: ignored event {event}"),
}
}
fn on_reverse_request(mgr: &Arc<DebugManager>, s: &Arc<Session>, seq: i64, command: &str, args: Value) {
match command {
"runInTerminal" => {
let result = run_in_terminal(s, &args);
let _ = s.conn.respond(seq, command, result);
}
"startDebugging" => {
let mgr = mgr.clone();
let s = s.clone();
tokio::spawn(async move {
let request = args.get("request").and_then(Value::as_str).unwrap_or("launch").to_owned();
let configuration = args.get("configuration").cloned().unwrap_or_else(|| json!({}));
let result = mgr.start_child(&s, request, configuration).await;
let _ = s.conn.respond(seq, "startDebugging", result.map(|_| Value::Null).map_err(|e| e.message));
});
}
_ => {
let _ = s.conn.respond(seq, command, Err(format!("unsupported reverse request {command}")));
}
}
}
/// `runInTerminal`: spawns the debuggee directly and forwards its output to the console.
pub(crate) fn run_in_terminal(s: &Arc<Session>, args: &Value) -> Result<Value, String> {
let argv: Vec<String> = args
.get("args")
.and_then(Value::as_array)
.map(|a| a.iter().filter_map(|v| v.as_str().map(str::to_owned)).collect())
.unwrap_or_default();
let Some((program, rest)) = argv.split_first() else {
return Err("runInTerminal without args".into());
};
let mut cmd = tokio::process::Command::new(program);
ide_core::process::no_window(cmd.as_std_mut());
cmd.args(rest)
.envs(ide_core::process::shell_env())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
if let Some(cwd) = args.get("cwd").and_then(Value::as_str).filter(|c| !c.is_empty()) {
cmd.current_dir(cwd);
}
if let Some(env) = args.get("env").and_then(Value::as_object) {
for (k, v) in env {
match v.as_str() {
Some(v) => cmd.env(k, v),
None => cmd.env_remove(k),
};
}
}
let mut child = cmd.spawn().map_err(|e| format!("failed to start {program}: {e}"))?;
let pid = child.id();
if let Some(out) = child.stdout.take() {
forward_output(s, out, "stdout");
}
if let Some(err) = child.stderr.take() {
forward_output(s, err, "stderr");
}
s.debuggees.lock().push(child);
Ok(json!({ "processId": pid }))
}
fn forward_output<R: AsyncRead + Unpin + Send + 'static>(s: &Session, mut stream: R, category: &'static str) {
let session = s.id;
let sink = s.owner.sink.clone();
tokio::spawn(async move {
let mut buf = vec![0u8; 8192];
let mut pending: Vec<u8> = Vec::new();
loop {
match stream.read(&mut buf).await {
Ok(0) | Err(_) => break,
Ok(n) => {
pending.extend_from_slice(&buf[..n]);
// Keep incomplete UTF-8 sequences for the next chunk.
let valid = match std::str::from_utf8(&pending) {
Ok(_) => pending.len(),
Err(e) if e.error_len().is_none() => e.valid_up_to(),
Err(_) => pending.len(),
};
let text = String::from_utf8_lossy(&pending[..valid]).into_owned();
pending.drain(..valid);
if !text.is_empty() {
sink.emit(IdeEvent::Debug {
session,
event: DebugEvent::Output { category: category.into(), output: text },
});
}
}
}
}
if !pending.is_empty() {
let output = String::from_utf8_lossy(&pending).into_owned();
sink.emit(IdeEvent::Debug { session, event: DebugEvent::Output { category: category.into(), output } });
}
});
}
File diff suppressed because it is too large. Load diff
+82
Переглянути файл
@@ -0,0 +1,82 @@
//! End-to-end against a real debugpy (`python3 -m debugpy --version` must work).
//! Run with `cargo test -p ide-dap --test debugpy -- --ignored`.
use ide_core::event::DebugEvent;
use ide_core::{IdeEvent, SharedSink};
use ide_dap::{Breakpoint, DebugManager, LaunchConfig, SessionOwner};
use ide_language::LanguageRegistry;
use parking_lot::Mutex;
use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Duration;
async fn wait(events: &Mutex<Vec<IdeEvent>>, pred: impl Fn(&DebugEvent) -> bool) -> DebugEvent {
for _ in 0..1500 {
let hit = events.lock().iter().find_map(|e| match e {
IdeEvent::Debug { event, .. } if pred(event) => Some(event.clone()),
_ => None,
});
if let Some(e) = hit {
return e;
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
panic!("timed out; events: {:?}", events.lock());
}
#[tokio::test(flavor = "multi_thread")]
#[ignore = "needs python3 with debugpy installed"]
async fn debugpy_end_to_end() {
let ok = std::process::Command::new("python3").args(["-m", "debugpy", "--version"]).output();
if !ok.is_ok_and(|o| o.status.success()) {
eprintln!("debugpy not installed; skipping");
return;
}
let dir = std::env::temp_dir().join(format!("openidea-dap-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let script = dir.join("prog.py");
std::fs::write(
&script,
"def main():\n total = 0\n for i in range(3):\n total += i\n print('total', total)\n\nmain()\n",
)
.unwrap();
let script = script.canonicalize().unwrap();
let events: Arc<Mutex<Vec<IdeEvent>>> = Arc::default();
let sink_events = events.clone();
let sink: SharedSink = Arc::new(move |e: IdeEvent| sink_events.lock().push(e));
let manager = DebugManager::new(Arc::new(LanguageRegistry::from_configs(Vec::new())));
let bp = Breakpoint { line: 3, condition: None, hit_condition: None, log_message: None, enabled: true };
manager.set_breakpoints(script.clone(), vec![bp]).await.unwrap();
let id = manager
.start(
LaunchConfig {
name: "prog".into(),
adapter: "debugpy".into(),
request: "launch".into(),
program: Some(script.to_string_lossy().into_owned()),
args: vec![],
cwd: Some(dir.clone().into()),
env: BTreeMap::new(),
extra_json: Some(r#"{"console": "internalConsole", "justMyCode": true}"#.into()),
},
SessionOwner::new("main", sink),
)
.await
.unwrap();
let DebugEvent::Stopped { thread_id, .. } = wait(&events, |e| matches!(e, DebugEvent::Stopped { .. })).await else {
unreachable!()
};
let frames = manager.stack_trace(id, thread_id).await.unwrap();
assert_eq!(frames[0].line, 3, "0-based");
let scopes = manager.scopes(id, frames[0].id).await.unwrap();
let vars = manager.variables(id, scopes[0].variables_reference).await.unwrap();
assert!(vars.iter().any(|v| v.name == "total"), "{vars:?}");
let v = manager.evaluate(id, Some(frames[0].id), "total + 100".into()).await.unwrap();
assert_eq!(v.value, "100");
manager.set_breakpoints(script.clone(), vec![]).await.unwrap();
manager.resume(id, thread_id).await.unwrap();
wait(&events, |e| matches!(e, DebugEvent::Output { output, .. } if output.contains("total 3"))).await;
wait(&events, |e| matches!(e, DebugEvent::Terminated)).await;
let _ = std::fs::remove_dir_all(&dir);
}
+157
Переглянути файл
@@ -0,0 +1,157 @@
//! Drives Godot's debug adapter (inside the Godot editor) with the GDScript
//! language definition: a headless editor is started for the project, the main
//! scene is played, a breakpoint hits, stack / variables / stepping work, and the
//! game and the editor end with the session.
//!
//! Ignored by default (needs Godot 4). Run with
//! `GODOT_BIN=<godot executable> cargo test -p ide-dap --test godot -- --ignored --nocapture`.
use ide_core::event::DebugEvent;
use ide_core::{IdeEvent, SharedSink};
use ide_dap::*;
use ide_language::{LanguageConfig, LanguageRegistry};
use parking_lot::Mutex;
use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};
const SCRIPT: &str = "extends Node
var health: int = 3
func _ready() -> void:
\tvar total := add(2, 3)
\tprint(\"total = \", total)
\thealth -= 1
\tget_tree().quit()
func add(a: int, b: int) -> int:
\treturn a + b
";
/// 0-based line of `return a + b`.
const BREAK_LINE: u32 = 11;
fn project(dir: &Path) {
std::fs::write(
dir.join("project.godot"),
"config_version=5\n\n[application]\nconfig/name=\"Probe\"\nrun/main_scene=\"res://main.tscn\"\n",
)
.unwrap();
std::fs::write(
dir.join("main.tscn"),
"[gd_scene load_steps=2 format=3]\n\n[ext_resource type=\"Script\" path=\"res://player.gd\" id=\"1\"]\n\n[node name=\"Main\" type=\"Node\"]\nscript = ExtResource(\"1\")\n",
)
.unwrap();
std::fs::write(dir.join("player.gd"), SCRIPT).unwrap();
}
/// The built-in GDScript definition, with the editor's usual port moved aside so an
/// open editor can't answer instead of the one this test starts.
fn registry() -> Arc<LanguageRegistry> {
let builtin = LanguageRegistry::load(None).unwrap();
let mut gd: LanguageConfig = builtin.get(&ide_core::LanguageId::new("gdscript")).unwrap();
let dbg = gd.debugger.as_mut().unwrap();
let unused = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap().local_addr().unwrap().port();
dbg.connect = Some(format!("127.0.0.1:{unused}"));
Arc::new(LanguageRegistry::from_configs(vec![gd]))
}
async fn wait_for(events: &Mutex<Vec<IdeEvent>>, timeout: Duration, pred: impl Fn(&DebugEvent) -> bool) -> DebugEvent {
let start = Instant::now();
loop {
if let Some(e) = events.lock().iter().find_map(|e| match e {
IdeEvent::Debug { event, .. } if pred(event) => Some(event.clone()),
_ => None,
}) {
return e;
}
assert!(start.elapsed() < timeout, "timed out; events: {:#?}", events.lock());
tokio::time::sleep(Duration::from_millis(50)).await;
}
}
fn godot_processes(project: &Path) -> usize {
let out = std::process::Command::new("pgrep").args(["-f", &project.to_string_lossy()]).output().unwrap();
String::from_utf8_lossy(&out.stdout).lines().count()
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn breakpoints_stepping_and_variables_in_a_real_godot() {
let Ok(godot) = std::env::var("GODOT_BIN") else {
eprintln!("GODOT_BIN not set; skipping");
return;
};
let dir = std::env::temp_dir().join(format!("openidea-godot-dap-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let root = std::fs::canonicalize(&dir).unwrap();
project(&root);
let manager = DebugManager::new(registry());
let events: Arc<Mutex<Vec<IdeEvent>>> = Arc::default();
let sink: SharedSink = {
let events = events.clone();
Arc::new(move |e: IdeEvent| events.lock().push(e))
};
let script_as_opened = dir.join("player.gd");
manager
.set_breakpoints(
script_as_opened.clone(),
vec![Breakpoint {
line: BREAK_LINE,
condition: None,
hit_condition: None,
log_message: None,
enabled: true,
}],
)
.await
.unwrap();
// As the UI builds it for "Godot Project" (`godot --path .`).
let config = LaunchConfig {
name: "Godot Project".into(),
adapter: "godot".into(),
request: "launch".into(),
program: None,
args: vec![],
// The project as opened, through a symlink on macOS (`/var` → `/private/var`).
cwd: Some(dir.clone().into()),
env: BTreeMap::new(),
extra_json: Some(serde_json::json!({ "project": dir, "scene": "main" }).to_string()),
};
let id = manager
.start_with(config, SessionOwner::new("main", sink), |_| Some(godot.clone()))
.await
.unwrap_or_else(|e| panic!("start: {e:?}"));
let stopped = wait_for(&events, Duration::from_secs(120), |e| matches!(e, DebugEvent::Stopped { .. })).await;
let DebugEvent::Stopped { thread_id, .. } = stopped else { unreachable!() };
let frames = manager.stack_trace(id, thread_id).await.unwrap();
assert_eq!(frames[0].name, "add");
assert_eq!(frames[0].line, BREAK_LINE);
// Frames name the file as the IDE opened it, not Godot's resolved path.
assert_eq!(frames[0].path.as_deref().map(Path::new), Some(script_as_opened.as_path()));
assert_eq!(frames[1].name, "_ready");
let scopes = manager.scopes(id, frames[0].id).await.unwrap();
let locals = scopes.iter().find(|s| s.name == "Locals").expect("Locals scope");
let vars = manager.variables(id, locals.variables_reference).await.unwrap();
let value = |n: &str| vars.iter().find(|v| v.name == n).map(|v| v.value.clone());
assert_eq!((value("a").as_deref(), value("b").as_deref()), (Some("2"), Some("3")), "{vars:?}");
events.lock().clear();
manager.step(id, thread_id, StepKind::Over).await.unwrap();
wait_for(&events, Duration::from_secs(30), |e| matches!(e, DebugEvent::Stopped { .. })).await;
let frames = manager.stack_trace(id, thread_id).await.unwrap();
assert_eq!((frames[0].name.as_str(), frames[0].line), ("_ready", 6), "stepped back into `_ready`, at the print");
manager.resume(id, thread_id).await.unwrap();
// (The game's last output is not always forwarded when it quits right away.)
wait_for(&events, Duration::from_secs(30), |e| matches!(e, DebugEvent::Terminated)).await;
let _ = manager.stop(id).await;
// The headless editor started for the session ends with it, the game too.
let start = Instant::now();
while godot_processes(&root) > 0 {
assert!(start.elapsed() < Duration::from_secs(10), "Godot still running for {}", root.display());
tokio::time::sleep(Duration::from_millis(200)).await;
}
std::fs::remove_dir_all(&root).ok();
}
+647
Переглянути файл
@@ -0,0 +1,647 @@
//! Drives real debug adapters against tiny programs (one per language), checking the
//! launch templates and the IDEA-parity features: line / method breakpoints, Smart Step
//! Into, Reset Frame, Force Step Over, Force Run to Cursor and Set Value.
//!
//! Ignored by default (needs the adapters installed). Run with
//! `OPENIDEA_DAP_FIXTURES=<dir> cargo test -p ide-dap --test real_adapters -- --ignored --nocapture`
//! where `<dir>` holds `py/app.py`, `node/app.js`, `go/demo` (built with `-N -l`),
//! `rust/target/debug/demo` and `c/main`. Adapters not found on `PATH` are skipped.
use ide_core::event::DebugEvent;
use ide_core::{DebugSessionId, IdeEvent, SharedSink};
use ide_dap::*;
use ide_language::LanguageRegistry;
use parking_lot::Mutex;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
struct Case {
adapter: &'static str,
/// Source file and the 0-based line of `total += parse(...)` inside the loop.
source: PathBuf,
loop_line: u32,
/// Line of the `return total` after the loop (Force Run to Cursor target).
after_loop: u32,
function: &'static str,
/// Persistent field watchpoint id, when the adapter has one (java-debug).
field: Option<&'static str>,
config: LaunchConfig,
}
fn launch(adapter: &str, program: &Path, cwd: &Path, extra: serde_json::Value) -> LaunchConfig {
LaunchConfig {
name: format!("{adapter} fixture"),
adapter: adapter.into(),
request: "launch".into(),
program: Some(program.display().to_string()),
args: vec![],
cwd: Some(cwd.into()),
env: BTreeMap::from([("OPENIDEA_FIXTURE".into(), "1".into())]),
extra_json: Some(extra.to_string()),
}
}
fn cases(dir: &Path) -> Vec<Case> {
let py = dir.join("py");
let node = dir.join("node");
let go = dir.join("go");
let rust = dir.join("rust");
let c = dir.join("c");
let java = dir.join("java");
// java-debug runs inside jdtls: OPENIDEA_JAVA_DEBUG_SERVER is the port the IDE's
// `java_debug_server` returned for this folder (classes compiled to java/out).
let java_case =
std::env::var("OPENIDEA_JAVA_DEBUG_SERVER").ok().and_then(|p| p.parse::<u16>().ok()).map(|port| Case {
adapter: "java-debug",
source: java.join("src/demo/Main.java"),
loop_line: 14,
after_loop: 16,
function: "demo.Main#parse",
field: Some("demo.Main#count"),
config: LaunchConfig {
program: None,
..launch(
"java-debug",
Path::new(""),
&java,
serde_json::json!({
"debugServer": port,
"mainClass": "demo.Main",
"classPaths": [java.join("out")],
"console": "internalConsole",
}),
)
},
});
let mut out: Vec<Case> = java_case.into_iter().collect();
out.extend(vec![
Case {
adapter: "debugpy",
source: py.join("app.py"),
loop_line: 8,
after_loop: 9,
function: "parse",
field: None,
config: launch(
"debugpy",
&py.join("app.py"),
&py,
serde_json::json!({
"console": "internalConsole",
"justMyCode": true,
"redirectOutput": true,
"python": python(),
}),
),
},
Case {
adapter: "js-debug",
source: node.join("app.js"),
loop_line: 8,
after_loop: 10,
function: "parse",
field: None,
config: launch(
"js-debug",
&node.join("app.js"),
&node,
serde_json::json!({"type": "pwa-node", "console": "internalConsole", "outputCapture": "std"}),
),
},
Case {
adapter: "delve",
source: go.join("main.go"),
loop_line: 15,
after_loop: 17,
function: "main.parse",
field: None,
config: launch("delve", &go.join("demo"), &go, serde_json::json!({"mode": "exec"})),
},
Case {
adapter: "codelldb",
source: rust.join("src/main.rs"),
loop_line: 8,
after_loop: 10,
function: "parse",
field: None,
config: launch(
"codelldb",
&rust.join("target/debug/demo"),
&rust,
serde_json::json!({"sourceLanguages": ["rust"]}),
),
},
Case {
adapter: "codelldb",
source: c.join("main.c"),
loop_line: 13,
after_loop: 15,
function: "parse",
field: None,
config: launch("codelldb", &c.join("main"), &c, serde_json::json!({})),
},
Case {
adapter: "lldb-dap",
source: c.join("main.c"),
loop_line: 13,
after_loop: 15,
function: "parse",
field: None,
config: launch("lldb-dap", &c.join("main"), &c, serde_json::json!({"env": ["OPENIDEA_FIXTURE=1"]})),
},
]);
out
}
struct Run {
manager: Arc<DebugManager>,
events: Arc<Mutex<Vec<(DebugSessionId, DebugEvent)>>>,
owner: SessionOwner,
}
impl Run {
fn new() -> Self {
let events: Arc<Mutex<Vec<(DebugSessionId, DebugEvent)>>> = Arc::default();
let sink_events = events.clone();
let sink: SharedSink = Arc::new(move |e: IdeEvent| {
if let IdeEvent::Debug { session, event } = e {
sink_events.lock().push((session, event));
}
});
let languages = Arc::new(LanguageRegistry::from_configs(Vec::new()));
Self { manager: DebugManager::new(languages), events, owner: SessionOwner::new("test", sink) }
}
/// Waits for a `stopped` event after `seen` events; returns (session, thread, reason).
async fn stopped_after(&self, seen: usize) -> Option<(DebugSessionId, i64, String)> {
let deadline = Instant::now() + Duration::from_secs(20);
while Instant::now() < deadline {
let found = self.events.lock().iter().skip(seen).find_map(|(s, e)| match e {
DebugEvent::Stopped { thread_id, reason, .. } => Some((*s, *thread_id, reason.clone())),
_ => None,
});
if found.is_some() {
return found;
}
if self.events.lock().iter().skip(seen).any(|(_, e)| matches!(e, DebugEvent::Terminated)) {
return None;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
None
}
fn seen(&self) -> usize {
self.events.lock().len()
}
async fn top(&self, s: DebugSessionId, thread: i64) -> StackFrame {
self.manager.stack_trace(s, thread).await.unwrap().into_iter().next().unwrap()
}
}
/// Bin dirs of the IDE's managed tools (the app prepends them to `PATH` at startup).
fn add_managed_tools_to_path() {
// `RUST_LOG=ide_dap::wire=trace` prints the DAP traffic.
let _ = tracing_subscriber::fmt().with_env_filter(tracing_subscriber::EnvFilter::from_default_env()).try_init();
let Some(home) = std::env::var_os("HOME").map(PathBuf::from) else { return };
// Like the app: adapters installed by OpenIDEA are found without being on PATH.
adapter::set_tools_root(home.join("Library/Application Support/ai.l0ng.openidea/tools"));
}
/// The debuggee interpreter (`OPENIDEA_PYTHON`, e.g. one without debugpy).
fn python() -> String {
std::env::var("OPENIDEA_PYTHON").unwrap_or_else(|_| "python3".into())
}
fn installed(adapter: &str) -> bool {
let cmd = match adapter {
"debugpy" => {
let managed = adapter::tools_root()
.is_some_and(|r| r.join(adapter::DEBUGPY_TOOL).join("lib/debugpy/__init__.py").is_file());
return managed
|| std::process::Command::new(python())
.args(["-c", "import debugpy"])
.status()
.is_ok_and(|s| s.success());
}
"js-debug" => "js-debug-adapter",
"delve" => "dlv",
"java-debug" => return true,
other => other,
};
adapter::resolve_binary(cmd, Path::new("/")).is_some()
}
async fn scenario(case: &Case) -> Result<Vec<String>, String> {
let mut report = Vec::new();
let run = Run::new();
let m = &run.manager;
let line_bp =
Breakpoint { line: case.loop_line, condition: None, hit_condition: None, log_message: None, enabled: true };
m.set_breakpoints(case.source.clone(), vec![line_bp]).await.map_err(|e| e.message)?;
let seen = run.seen();
let id = m.start(case.config.clone(), run.owner.clone()).await.map_err(|e| format!("start: {}", e.message))?;
let Some((s, thread, reason)) = run.stopped_after(seen).await else {
let log: Vec<String> = run.events.lock().iter().map(|(_, e)| format!("{e:?}")).collect();
return Err(format!(
"no stop at the line breakpoint; events: {log:#?}; states: {:?}",
m.breakpoint_states(&case.source)
));
};
let top = run.top(s, thread).await;
if top.line != case.loop_line {
return Err(format!("stopped at line {} ({reason}), expected {}", top.line, case.loop_line));
}
report.push(format!("launch + line breakpoint: stopped at {}:{}", top.name, top.line + 1));
let caps = m.capabilities(s).map_err(|e| e.message)?;
report.push(format!("capabilities: {caps:?}"));
// Set Value on the first local that is an integer.
let scopes = m.scopes(s, top.id).await.map_err(|e| e.message)?;
for scope in scopes.iter().filter(|sc| !sc.expensive) {
let vars = m.variables(s, scope.variables_reference).await.map_err(|e| e.message)?;
if let Some(v) = vars.iter().find(|v| v.name == "total") {
match m
.set_variable(
s,
scope.variables_reference,
v.name.clone(),
v.evaluate_name.clone(),
Some(top.id),
"40".into(),
)
.await
{
Ok(nv) => report.push(format!("set value: total = {}", nv.value)),
Err(e) => report.push(format!("set value: FAILED {}", e.message)),
}
break;
}
}
// Reset Frame: one line further, then back to the start of `run`.
if caps.restart_frame {
let seen = run.seen();
m.step(s, thread, StepKind::Over).await.map_err(|e| e.message)?;
run.stopped_after(seen).await.ok_or("no stop after step over")?;
let before = run.top(s, thread).await;
let seen = run.seen();
match m.restart_frame(s, before.id).await {
Ok(()) => {
run.stopped_after(seen).await.ok_or("no stop after reset frame")?;
let r = run.top(s, thread).await;
report.push(format!("reset frame: {}:{} -> {}:{}", before.name, before.line + 1, r.name, r.line + 1));
}
Err(e) => report.push(format!("reset frame: FAILED {}", e.message)),
}
let seen = run.seen();
m.resume(s, thread).await.map_err(|e| e.message)?;
run.stopped_after(seen).await.ok_or("no stop at the loop after reset frame")?;
}
// Smart Step Into.
if caps.step_in_targets {
let targets = m.step_in_targets(s, top.id).await.map_err(|e| e.message)?;
report.push(format!("step-in targets: {:?}", targets.iter().map(|t| t.label.as_str()).collect::<Vec<_>>()));
if let Some(t) = targets.iter().find(|t| t.label.contains("parse")) {
let seen = run.seen();
m.step_in_target(s, thread, t.id).await.map_err(|e| e.message)?;
run.stopped_after(seen).await.ok_or("no stop after smart step into")?;
let f = run.top(s, thread).await;
report.push(format!("smart step into: now in {}", f.name));
// Reset Frame back to the start of parse.
if caps.restart_frame {
let seen = run.seen();
match m.restart_frame(s, f.id).await {
Ok(()) => {
run.stopped_after(seen).await.ok_or("no stop after reset frame")?;
let r = run.top(s, thread).await;
report.push(format!("reset frame: at {}:{}", r.name, r.line + 1));
}
Err(e) => report.push(format!("reset frame: FAILED {}", e.message)),
}
}
let seen = run.seen();
m.step(s, thread, StepKind::Out).await.map_err(|e| e.message)?;
run.stopped_after(seen).await.ok_or("no stop after step out")?;
}
} else {
report.push("step-in targets: not supported".into());
}
// Method breakpoint: continue → stops in `parse`.
if caps.function_breakpoints {
let fb = FunctionBreakpoint { name: case.function.into(), condition: None, hit_condition: None, enabled: true };
let states = m.set_function_breakpoints(vec![fb]).await.map_err(|e| e.message)?;
report.push(format!("method breakpoint `{}`: verified={}", case.function, states[0].verified));
m.set_breakpoints(case.source.clone(), vec![]).await.map_err(|e| e.message)?;
let seen = run.seen();
m.resume(s, thread).await.map_err(|e| e.message)?;
match run.stopped_after(seen).await {
Some((_, t, reason)) => {
let f = run.top(s, t).await;
report.push(format!("method breakpoint hit: {reason} in {}", f.name));
}
None => report.push("method breakpoint: NOT HIT".into()),
}
m.set_function_breakpoints(vec![]).await.map_err(|e| e.message)?;
m.set_breakpoints(case.source.clone(), vec![line_bp_at(case.loop_line)]).await.map_err(|e| e.message)?;
let seen = run.seen();
m.step(s, thread, StepKind::Out).await.map_err(|e| e.message)?;
run.stopped_after(seen).await.ok_or("no stop after step out of parse")?;
} else {
report.push("method breakpoints: not supported".into());
}
// Force Step Over ignores the breakpoint inside parse.
m.set_breakpoints(case.source.clone(), vec![line_bp_at(case.loop_line)]).await.map_err(|e| e.message)?;
let seen = run.seen();
m.force_step_over(s, thread).await.map_err(|e| e.message)?;
let (_, t, reason) = run.stopped_after(seen).await.ok_or("no stop after force step over")?;
let f = run.top(s, t).await;
report.push(format!("force step over: {reason} at line {}", f.line + 1));
// Field watchpoint / data breakpoint on `total` (java-debug: a field id).
if caps.data_breakpoints {
let frame = run.top(s, thread).await;
let mut data_id = case.field.map(str::to_owned);
if data_id.is_none() {
for scope in m.scopes(s, frame.id).await.map_err(|e| e.message)?.iter().filter(|sc| !sc.expensive) {
if let Ok(info) =
m.data_breakpoint_info(s, scope.variables_reference, "total".into(), Some(frame.id)).await
&& info.data_id.is_some()
{
data_id = info.data_id;
break;
}
}
}
match data_id {
Some(data_id) => {
let wp = DataBreakpoint {
data_id: data_id.clone(),
access_type: Some("write".into()),
condition: None,
hit_condition: None,
enabled: true,
};
let states = m.set_data_breakpoints(vec![wp]).await.map_err(|e| e.message)?;
m.set_breakpoints(case.source.clone(), vec![]).await.map_err(|e| e.message)?;
let seen = run.seen();
m.resume(s, thread).await.map_err(|e| e.message)?;
match run.stopped_after(seen).await {
Some((_, t, reason)) => {
let f = run.top(s, t).await;
report.push(format!(
"watchpoint `{data_id}` (verified={}): {reason} at {}:{}",
states[0].verified,
f.name,
f.line + 1
));
}
None => report.push(format!("watchpoint `{data_id}`: NOT HIT")),
}
m.set_data_breakpoints(vec![]).await.map_err(|e| e.message)?;
m.set_breakpoints(case.source.clone(), vec![line_bp_at(case.loop_line)])
.await
.map_err(|e| e.message)?;
}
None => report.push("watchpoint: no data id for `total`".into()),
}
}
// Force Run to Cursor past the loop, ignoring the loop breakpoint.
let seen = run.seen();
m.run_to(s, thread, case.source.clone(), case.after_loop, true).await.map_err(|e| e.message)?;
let (_, t, reason) = run.stopped_after(seen).await.ok_or("no stop after force run to cursor")?;
let f = run.top(s, t).await;
report.push(format!("force run to cursor: {reason} at line {} (want {})", f.line + 1, case.after_loop + 1));
m.stop(id).await.map_err(|e| e.message)?;
Ok(report)
}
fn line_bp_at(line: u32) -> Breakpoint {
Breakpoint { line, condition: None, hit_condition: None, log_message: None, enabled: true }
}
#[tokio::test(flavor = "multi_thread")]
#[ignore = "needs real debug adapters and OPENIDEA_DAP_FIXTURES"]
async fn real_adapters() {
let Some(dir) = std::env::var_os("OPENIDEA_DAP_FIXTURES").map(PathBuf::from) else {
eprintln!("OPENIDEA_DAP_FIXTURES not set");
return;
};
add_managed_tools_to_path();
let only = std::env::var("OPENIDEA_DAP_ONLY").ok();
let mut failures = Vec::new();
for case in cases(&dir) {
if only.as_deref().is_some_and(|o| o != case.adapter) {
continue;
}
let name = format!("{} ({})", case.adapter, case.source.file_name().unwrap().to_string_lossy());
if !installed(case.adapter) {
println!("== {name}: SKIPPED (not installed)");
continue;
}
match tokio::time::timeout(Duration::from_secs(240), scenario(&case)).await {
Ok(Ok(lines)) => {
println!("== {name}: OK");
for l in lines {
println!(" {l}");
}
}
Ok(Err(e)) => {
println!("== {name}: FAILED {e}");
failures.push(name);
}
Err(_) => {
println!("== {name}: TIMED OUT");
failures.push(name);
}
}
}
assert!(failures.is_empty(), "failed: {failures:?}");
}
struct AttachCase {
kind: attach::AttachKind,
command: Vec<String>,
source: PathBuf,
/// 0-based line inside `tick`.
line: u32,
extra: fn(u32, Option<u16>) -> serde_json::Value,
}
/// Attach to Process: each fixture server is listed with the right debugger, and attaching
/// with the request `attach.ts` builds stops at a breakpoint in its loop.
#[tokio::test(flavor = "multi_thread")]
#[ignore = "needs real debug adapters and OPENIDEA_DAP_FIXTURES"]
async fn attach_to_processes() {
let Some(dir) = std::env::var_os("OPENIDEA_DAP_FIXTURES").map(PathBuf::from) else { return };
add_managed_tools_to_path();
let s = |p: &Path| p.display().to_string();
let java_server = std::env::var("OPENIDEA_JAVA_DEBUG_SERVER").ok().and_then(|p| p.parse::<u16>().ok());
let mut cases = vec![
// `debugpy --pid` injection ships only an x86_64 helper on macOS, so on Apple
// Silicon the attach goes through a `debugpy --listen` process.
AttachCase {
kind: attach::AttachKind::Python,
command: vec![
"python3".into(),
"-m".into(),
"debugpy".into(),
"--listen".into(),
"127.0.0.1:5931".into(),
s(&dir.join("py/server.py")),
],
source: dir.join("py/server.py"),
line: 4,
extra: |pid, port| match port {
// `debugpy --listen` runs its own adapter on that port: talk DAP to it directly.
Some(port) => serde_json::json!({"debugServer": port, "justMyCode": true}),
None => serde_json::json!({"processId": pid, "justMyCode": true}),
},
},
AttachCase {
kind: attach::AttachKind::Node,
command: match std::env::var("OPENIDEA_NODE_INSPECT") {
Ok(flag) => vec!["node".into(), flag, s(&dir.join("node/server.js"))],
Err(_) => vec!["node".into(), s(&dir.join("node/server.js"))],
},
source: dir.join("node/server.js"),
line: 2,
extra: |pid, port| match port {
Some(port) => {
serde_json::json!({"type": "pwa-node", "address": "127.0.0.1", "port": port, "continueOnAttach": true})
}
None => serde_json::json!({"type": "pwa-node", "processId": pid.to_string(), "continueOnAttach": true}),
},
},
AttachCase {
kind: attach::AttachKind::Go,
command: vec![s(&dir.join("goserver/server"))],
source: dir.join("goserver/main.go"),
line: 5,
extra: |pid, _| serde_json::json!({"mode": "local", "processId": pid}),
},
AttachCase {
kind: attach::AttachKind::Native,
command: vec![s(&dir.join("c/server"))],
source: dir.join("c/server.c"),
line: 3,
extra: |pid, _| serde_json::json!({"pid": pid}),
},
];
if java_server.is_some() {
cases.insert(
0,
AttachCase {
kind: attach::AttachKind::Java,
command: vec![
"java".into(),
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5917".into(),
"-cp".into(),
s(&dir.join("java/out")),
"demo.Server".into(),
],
source: dir.join("java/src/demo/Server.java"),
line: 4,
extra: |_, port| serde_json::json!({"hostName": "localhost", "port": port}),
},
);
}
let only = std::env::var("OPENIDEA_DAP_ONLY").ok();
let mut failures = Vec::new();
for case in cases {
let label = format!("{:?}", case.kind);
if only.as_deref().is_some_and(|o| !label.eq_ignore_ascii_case(o)) {
continue;
}
let mut child = std::process::Command::new(&case.command[0])
.args(&case.command[1..])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.unwrap();
tokio::time::sleep(Duration::from_millis(1500)).await;
let result = async {
let list =
tokio::task::spawn_blocking(attach::attachable_processes).await.unwrap().map_err(|e| e.message)?;
let p = list.iter().find(|p| p.pid == child.id()).ok_or("process not listed")?;
if p.kind != case.kind {
return Err(format!("listed as {:?}", p.kind));
}
let mut extra = (case.extra)(p.pid, p.port);
if p.kind == attach::AttachKind::Java {
extra["debugServer"] = java_server.unwrap().into();
}
let config = LaunchConfig {
name: format!("{} ({})", p.name, p.pid),
adapter: p.adapter.clone(),
request: "attach".into(),
program: None,
args: vec![],
cwd: case.source.parent().map(Into::into),
env: BTreeMap::new(),
extra_json: Some(extra.to_string()),
};
let run = Run::new();
run.manager
.set_breakpoints(case.source.clone(), vec![line_bp_at(case.line)])
.await
.map_err(|e| e.message)?;
let id =
run.manager.start(config, run.owner.clone()).await.map_err(|e| format!("attach: {}", e.message))?;
let Some((s, thread, reason)) = run.stopped_after(0).await else {
let mut log: Vec<String> = run.events.lock().iter().map(|(_, e)| format!("{e:?}")).collect();
for info in run.manager.sessions(None) {
let threads = run.manager.threads(info.id).await;
log.push(format!("session {:?} threads {threads:?}", info.id));
if let Ok(t) = threads
&& let Some(t) = t.first()
{
let seen = run.seen();
let _ = run.manager.pause(info.id, t.id).await;
if let Some((s, th, r)) = run.stopped_after(seen).await {
let frames = run.manager.stack_trace(s, th).await;
log.push(format!(
"paused ({r}): {:?}",
frames.map(|f| f
.into_iter()
.take(3)
.map(|f| (f.name, f.path, f.line))
.collect::<Vec<_>>())
));
}
}
}
log.push(format!("states: {:?}", run.manager.breakpoint_states(&case.source)));
let _ = run.manager.stop(id).await;
return Err(format!("no stop after attaching: {log:#?}"));
};
let top = run.top(s, thread).await;
let _ = run.manager.stop(id).await;
Ok::<_, String>(format!("{} via {}: {reason} at {}:{}", p.name, p.adapter, top.name, top.line + 1))
};
let outcome = tokio::time::timeout(Duration::from_secs(150), result).await;
let _ = child.kill();
let _ = child.wait();
match outcome {
Ok(Ok(line)) => println!("== attach {label}: OK {line}"),
Ok(Err(e)) => {
println!("== attach {label}: FAILED {e}");
failures.push(label);
}
Err(_) => {
println!("== attach {label}: TIMED OUT");
failures.push(label);
}
}
}
assert!(failures.is_empty(), "failed: {failures:?}");
}
+406
Переглянути файл
@@ -0,0 +1,406 @@
//! Drives the script-language debuggers (debugpy, js-debug for Node / TypeScript / npm
//! scripts / Deno) through a whole session with the launch arguments the UI builds
//! (`src/features/debugger/launch.ts`): breakpoint verified and hit, stack, scopes,
//! variables, evaluate, step into / over / out, continue, run to exit, then a restart
//! that is terminated while paused.
//!
//! Ignored by default (needs the adapters and runtimes). Run with
//! `OPENIDEA_DAP_FIXTURES=<dir> cargo test -p ide-dap --test script_debuggers -- --ignored --nocapture`
//! where `<dir>` holds `py/app.py`, `node/app.js`, `ts/app.ts`, `tsmap/app.ts` compiled
//! with `tsc --sourceMap --outDir out`, `tsx/app.ts` (with `npm i tsx` there), `npm/`
//! (script `start` = `node app.js`) and `deno/app.ts`, all with the same layout (see `LOOP_LINE`). `OPENIDEA_DAP_ONLY=<case>`
//! runs one case. Cases whose adapter or runtime is missing are skipped.
use ide_core::event::DebugEvent;
use ide_core::{DebugSessionId, IdeEvent, SharedSink};
use ide_dap::*;
use ide_language::LanguageRegistry;
use parking_lot::Mutex;
use serde_json::{Value, json};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
/// 0-based line of `total += parse(s)` in every fixture.
const LOOP_LINE: u32 = 8;
/// First line of `parse`'s body, and the one after it.
const PARSE_BODY: u32 = 2;
/// `console.log` / `print` of the result: `total 12`.
const OUTPUT: &str = "total 12";
struct Case {
name: &'static str,
/// Breakpoints are set here (the TypeScript source for source-mapped cases).
source: PathBuf,
/// The runtime that must be installed, besides the adapter.
runtime: &'static str,
config: LaunchConfig,
}
fn config(adapter: &str, program: Option<PathBuf>, cwd: &Path, extra: Value) -> LaunchConfig {
LaunchConfig {
name: "fixture".into(),
adapter: adapter.into(),
request: "launch".into(),
program: program.map(|p| p.display().to_string()),
args: vec![],
cwd: Some(cwd.into()),
env: BTreeMap::new(),
extra_json: Some(extra.to_string()),
}
}
/// `jsDebugLaunch` in launch.ts, plus `more`.
fn js_extra(more: Value) -> Value {
let mut extra = json!({
"type": "pwa-node",
"console": "internalConsole",
"outputCapture": "std",
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"autoAttachChildProcesses": true,
});
if let (Some(e), Value::Object(more)) = (extra.as_object_mut(), more) {
e.extend(more);
}
extra
}
fn cases(dir: &Path) -> Vec<Case> {
let d = |s: &str| dir.join(s);
vec![
Case {
name: "python",
source: d("py/app.py"),
runtime: "python3",
config: config(
"debugpy",
Some(d("py/app.py")),
&d("py"),
json!({
"python": "python3",
"console": "internalConsole",
"justMyCode": true,
"subProcess": true,
"redirectOutput": true,
}),
),
},
Case {
name: "node",
source: d("node/app.js"),
runtime: "node",
config: config("js-debug", Some(d("node/app.js")), &d("node"), js_extra(json!({}))),
},
Case {
// Node 22.18+ strips types natively.
name: "typescript",
source: d("ts/app.ts"),
runtime: "node",
config: config("js-debug", Some(d("ts/app.ts")), &d("ts"), js_extra(json!({}))),
},
Case {
name: "typescript-flag",
source: d("ts/app.ts"),
runtime: "node",
config: config(
"js-debug",
None,
&d("ts"),
js_extra(json!({"runtimeExecutable": "node", "runtimeArgs": ["--experimental-strip-types", "app.ts"]})),
),
},
Case {
// `tsx/node_modules/.bin/tsx` (`npm i tsx` there): a runner on Node, source-mapped.
name: "tsx",
source: d("tsx/app.ts"),
runtime: "node",
config: config(
"js-debug",
None,
&d("tsx"),
js_extra(json!({
"runtimeExecutable": d("tsx/node_modules/.bin/tsx"),
"runtimeArgs": ["app.ts"],
})),
),
},
Case {
name: "sourcemap",
source: d("tsmap/app.ts"),
runtime: "node",
config: config("js-debug", Some(d("tsmap/out/app.js")), &d("tsmap"), js_extra(json!({}))),
},
Case {
name: "npm",
source: d("npm/app.js"),
runtime: "npm",
config: config(
"js-debug",
None,
&d("npm"),
js_extra(json!({"runtimeExecutable": "npm", "runtimeArgs": ["run", "start"]})),
),
},
Case {
name: "deno",
source: d("deno/app.ts"),
runtime: "deno",
config: config(
"js-debug",
None,
&d("deno"),
js_extra(json!({
"runtimeExecutable": "deno",
"runtimeArgs": ["run", "--inspect-wait=127.0.0.1:${freePort}", "app.ts"],
"attachSimplePort": "${freePort}",
})),
),
},
]
}
struct Run {
manager: Arc<DebugManager>,
events: Arc<Mutex<Vec<(DebugSessionId, DebugEvent)>>>,
owner: SessionOwner,
}
impl Run {
fn new() -> Self {
let events: Arc<Mutex<Vec<(DebugSessionId, DebugEvent)>>> = Arc::default();
let sink_events = events.clone();
let sink: SharedSink = Arc::new(move |e: IdeEvent| {
if let IdeEvent::Debug { session, event } = e {
sink_events.lock().push((session, event));
}
});
let languages = Arc::new(LanguageRegistry::from_configs(Vec::new()));
Self { manager: DebugManager::new(languages), events, owner: SessionOwner::new("test", sink) }
}
fn seen(&self) -> usize {
self.events.lock().len()
}
fn log(&self) -> String {
let log: Vec<String> = self.events.lock().iter().map(|(s, e)| format!("{}: {e:?}", s.0)).collect();
log.join("\n")
}
async fn wait<T>(
&self,
seen: usize,
what: &str,
f: impl Fn(DebugSessionId, &DebugEvent) -> Option<T>,
) -> Result<T, String> {
let deadline = Instant::now() + Duration::from_secs(30);
while Instant::now() < deadline {
if let Some(hit) = self.events.lock().iter().skip(seen).find_map(|(s, e)| f(*s, e)) {
return Ok(hit);
}
tokio::time::sleep(Duration::from_millis(30)).await;
}
Err(format!("timed out waiting for {what}; events:\n{}", self.log()))
}
async fn stopped(&self, seen: usize, what: &str) -> Result<(DebugSessionId, i64), String> {
self.wait(seen, what, |s, e| match e {
DebugEvent::Stopped { thread_id, .. } => Some((s, *thread_id)),
_ => None,
})
.await
}
async fn step(&self, s: DebugSessionId, thread: i64, kind: StepKind) -> Result<StackFrame, String> {
let seen = self.seen();
self.manager.step(s, thread, kind).await.map_err(|e| e.message)?;
self.stopped(seen, &format!("{kind:?}")).await?;
self.top(s, thread).await
}
async fn top(&self, s: DebugSessionId, thread: i64) -> Result<StackFrame, String> {
let frames = self.manager.stack_trace(s, thread).await.map_err(|e| e.message)?;
frames.into_iter().next().ok_or_else(|| "empty stack".into())
}
/// Every session of the run has ended (js-debug runs the program in a child session).
async fn all_terminated(&self) -> Result<(), String> {
let deadline = Instant::now() + Duration::from_secs(30);
while Instant::now() < deadline {
if self.manager.sessions(None).is_empty() {
return Ok(());
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
Err(format!("sessions still live: {:?}", self.manager.sessions(None)))
}
}
fn bp(line: u32) -> Breakpoint {
Breakpoint { line, condition: None, hit_condition: None, log_message: None, enabled: true }
}
fn expect_line(f: &StackFrame, line: u32, what: &str) -> Result<(), String> {
if f.line == line {
Ok(())
} else {
Err(format!("{what}: at {}:{} ({:?}), expected line {}", f.name, f.line + 1, f.path, line + 1))
}
}
async fn scenario(case: &Case) -> Result<Vec<String>, String> {
let mut report = Vec::new();
let run = Run::new();
let m = &run.manager;
m.set_breakpoints(case.source.clone(), vec![bp(LOOP_LINE)]).await.map_err(|e| e.message)?;
let id = m.start(case.config.clone(), run.owner.clone()).await.map_err(|e| format!("start: {}", e.message))?;
let (s, thread) = run.stopped(0, "the loop breakpoint").await?;
// js-debug verifies in a `breakpoint` event right after the stop.
let deadline = Instant::now() + Duration::from_secs(5);
while !m.breakpoint_states(&case.source).first().is_some_and(|st| st.verified) {
if Instant::now() > deadline {
return Err(format!("breakpoint not verified: {:?}", m.breakpoint_states(&case.source)));
}
tokio::time::sleep(Duration::from_millis(30)).await;
}
let top = run.top(s, thread).await?;
expect_line(&top, LOOP_LINE, "breakpoint")?;
if top.path.as_deref().map(|p| p.canonicalize().ok()) != Some(case.source.canonicalize().ok()) {
return Err(format!("frame source {:?}, expected {}", top.path, case.source.display()));
}
report.push(format!("hit {}:{} (verified)", top.name, top.line + 1));
let scopes = m.scopes(s, top.id).await.map_err(|e| e.message)?;
// js-debug puts the loop variable in a Block scope before Local.
let mut vars = Vec::new();
for scope in scopes.iter().filter(|sc| !sc.expensive) {
vars.extend(m.variables(s, scope.variables_reference).await.map_err(|e| e.message)?);
}
let total = vars.iter().find(|v| v.name == "total").ok_or_else(|| format!("no `total` in {vars:?}"))?;
let items = vars.iter().find(|v| v.name == "items").ok_or_else(|| format!("no `items` in {vars:?}"))?;
let children = m.variables(s, items.variables_reference).await.map_err(|e| e.message)?;
report.push(format!(
"scopes {:?}; total = {}; items has {} children",
scopes.iter().map(|sc| sc.name.as_str()).collect::<Vec<_>>(),
total.value,
children.len()
));
let v = m.evaluate(s, Some(top.id), "total + 100".into()).await.map_err(|e| e.message)?;
if v.value != "100" {
return Err(format!("evaluate: {}", v.value));
}
let f = run.step(s, thread, StepKind::Into).await?;
if !f.name.ends_with("parse") {
return Err(format!("step into: in {} at {}", f.name, f.line + 1));
}
expect_line(&f, PARSE_BODY, "step into")?;
let f = run.step(s, thread, StepKind::Over).await?;
expect_line(&f, PARSE_BODY + 1, "step over")?;
let f = run.step(s, thread, StepKind::Out).await?;
if !f.name.ends_with("run") {
return Err(format!("step out: in {} at {}", f.name, f.line + 1));
}
report.push(format!("step into / over / out: back in {}:{}", f.name, f.line + 1));
// Continue: the loop breakpoint again (second item).
let seen = run.seen();
m.resume(s, thread).await.map_err(|e| e.message)?;
let (s2, t2) = run.stopped(seen, "the second hit").await?;
expect_line(&run.top(s2, t2).await?, LOOP_LINE, "continue")?;
// Without breakpoints, the program runs to its end.
m.set_breakpoints(case.source.clone(), vec![]).await.map_err(|e| e.message)?;
let seen = run.seen();
m.resume(s2, t2).await.map_err(|e| e.message)?;
run.wait(seen, "terminated", |s, e| (s == id && matches!(e, DebugEvent::Terminated)).then_some(())).await?;
// debugpy may deliver one `print` in several chunks.
let stdout: String = run
.events
.lock()
.iter()
.filter_map(|(_, e)| match e {
DebugEvent::Output { category, output } if category == "stdout" => Some(output.as_str()),
_ => None,
})
.collect();
if !stdout.contains(OUTPUT) {
return Err(format!("stdout {stdout:?} lacks {OUTPUT:?}"));
}
run.all_terminated().await?;
report.push("continue to exit: output and terminated".into());
// Restart (the UI starts the same configuration again), then Stop while paused.
m.set_breakpoints(case.source.clone(), vec![bp(LOOP_LINE)]).await.map_err(|e| e.message)?;
let seen = run.seen();
let again = m.start(case.config.clone(), run.owner.clone()).await.map_err(|e| format!("restart: {}", e.message))?;
let (s, thread) = run.stopped(seen, "the breakpoint after restart").await?;
expect_line(&run.top(s, thread).await?, LOOP_LINE, "restart")?;
let seen = run.seen();
m.stop(again).await.map_err(|e| e.message)?;
run.wait(seen, "terminated after stop", |s, e| (s == again && matches!(e, DebugEvent::Terminated)).then_some(()))
.await?;
run.all_terminated().await?;
report.push("restart + stop while paused: terminated".into());
Ok(report)
}
fn on_path(cmd: &str) -> bool {
adapter::resolve_binary(cmd, Path::new("/")).is_some()
}
fn installed(case: &Case) -> bool {
let adapter = match case.config.adapter.as_str() {
"debugpy" => {
std::process::Command::new("python3").args(["-c", "import debugpy"]).status().is_ok_and(|s| s.success())
|| adapter::tools_root()
.is_some_and(|r| r.join(adapter::DEBUGPY_TOOL).join("lib/debugpy/__init__.py").is_file())
}
_ => on_path("js-debug-adapter"),
};
adapter && on_path(case.runtime) && case.source.is_file()
}
#[tokio::test(flavor = "multi_thread")]
#[ignore = "needs real debug adapters and OPENIDEA_DAP_FIXTURES"]
async fn script_debuggers() {
let Some(dir) = std::env::var_os("OPENIDEA_DAP_FIXTURES").map(PathBuf::from) else {
eprintln!("OPENIDEA_DAP_FIXTURES not set");
return;
};
let dir = dir.canonicalize().unwrap();
let _ = tracing_subscriber::fmt().with_env_filter(tracing_subscriber::EnvFilter::from_default_env()).try_init();
if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
adapter::set_tools_root(home.join("Library/Application Support/ai.l0ng.openidea/tools"));
}
let only = std::env::var("OPENIDEA_DAP_ONLY").ok();
let mut failures = Vec::new();
for case in cases(&dir) {
if only.as_deref().is_some_and(|o| o != case.name) {
continue;
}
if !installed(&case) {
println!("== {}: SKIPPED (adapter, runtime or fixture missing)", case.name);
continue;
}
match tokio::time::timeout(Duration::from_secs(180), scenario(&case)).await {
Ok(Ok(lines)) => {
println!("== {}: OK", case.name);
for l in lines {
println!(" {l}");
}
}
Ok(Err(e)) => {
println!("== {}: FAILED {e}", case.name);
failures.push(case.name);
}
Err(_) => {
println!("== {}: TIMED OUT", case.name);
failures.push(case.name);
}
}
}
assert!(failures.is_empty(), "failed: {failures:?}");
}
+24
Переглянути файл
@@ -0,0 +1,24 @@
[package]
name = "ide-db"
description = "Database tools: data sources, connections, introspection, query execution and table editing"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
ide-core.workspace = true
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
toml.workspace = true
tokio.workspace = true
tracing.workspace = true
parking_lot.workspace = true
futures.workspace = true
sqlx.workspace = true
dirs.workspace = true
ide-ssh.workspace = true
[dev-dependencies]
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
tempfile.workspace = true
+652
Переглянути файл
@@ -0,0 +1,652 @@
//! Live connections (one small pool per data source) and statement execution.
//!
//! Statements run through `raw_sql` (PostgreSQL's simple query protocol,
//! MySQL's text protocol), so every value arrives as text and is shown as is,
//! whatever its type.
use crate::source::{DataSource, DbDriver, DbSslMode};
use ide_core::{CancellationToken, IdeError, IdeResult, error::ErrorKind};
use serde::{Deserialize, Serialize};
use specta::Type;
use sqlx::{
Column, ColumnIndex, Connection, Decode, Executor, MySql, Postgres, Row, SqlSafeStr, Sqlite, TypeInfo, ValueRef,
mysql::{MySqlConnectOptions, MySqlPool, MySqlPoolOptions, MySqlSslMode},
pool::PoolConnection,
postgres::{PgConnectOptions, PgPool, PgPoolOptions, PgSslMode},
sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions},
};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "DbResultColumn")]
pub struct ResultColumn {
pub name: String,
pub type_name: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "DbQueryResult")]
pub struct QueryResult {
pub statement: String,
pub columns: Vec<ResultColumn>,
/// Cell text; `None` is SQL NULL. Binary values are `0x…` hex.
pub rows: Vec<Vec<Option<String>>>,
/// Rows changed by a DML statement (`None` for queries).
pub affected: Option<f64>,
/// More rows exist beyond the limit.
pub has_more: bool,
pub elapsed_ms: f64,
/// Until the server's first answer; the rest of `elapsed_ms` is fetching.
pub execution_ms: f64,
}
pub enum Pool {
Sqlite(SqlitePool),
Postgres(PgPool),
Mysql(MySqlPool),
}
/// Runs `$body` with `$p` bound to the concrete pool of each driver.
macro_rules! with_pool {
($pool:expr, $p:ident => $body:expr) => {
match $pool {
Pool::Sqlite($p) => $body,
Pool::Postgres($p) => $body,
Pool::Mysql($p) => $body,
}
};
}
pub(crate) fn db_err(e: sqlx::Error) -> IdeError {
match &e {
sqlx::Error::Database(d) => {
let code = d.code().map(|c| c.into_owned());
// Wrong password: PostgreSQL 28P01, MySQL 1045 (SQLSTATE 28000).
let auth = matches!(code.as_deref(), Some("28P01" | "28000" | "1045"));
let msg = match code {
Some(c) => format!("[{c}] {}", d.message()),
None => d.message().to_owned(),
};
IdeError::new(if auth { ErrorKind::PermissionDenied } else { ErrorKind::InvalidInput }, msg)
}
sqlx::Error::PoolTimedOut => IdeError::new(ErrorKind::Io, "timed out connecting to the database"),
sqlx::Error::Io(_) | sqlx::Error::Tls(_) => IdeError::new(ErrorKind::Io, e.to_string()),
_ => IdeError::new(ErrorKind::Protocol, e.to_string()),
}
}
fn hex(bytes: &[u8]) -> String {
let mut s = String::with_capacity(2 + bytes.len() * 2);
s.push_str("0x");
for b in bytes {
s.push_str(&format!("{b:02X}"));
}
s
}
/// The text of column `i` of `row`.
fn cell<R: Row>(row: &R, i: usize) -> Option<String>
where
usize: ColumnIndex<R>,
for<'r> String: Decode<'r, R::Database>,
for<'r> Vec<u8>: Decode<'r, R::Database>,
{
let raw = row.try_get_raw(i).ok()?;
if raw.is_null() {
return None;
}
let ty = raw.type_info().name().to_ascii_uppercase();
if ty.contains("BLOB") || ty.contains("BINARY") {
let raw = row.try_get_raw(i).ok()?;
return <Vec<u8> as Decode<R::Database>>::decode(raw).ok().map(|b| hex(&b));
}
match <String as Decode<R::Database>>::decode(raw) {
Ok(s) => Some(s),
Err(_) => {
let raw = row.try_get_raw(i).ok()?;
<Vec<u8> as Decode<R::Database>>::decode(raw)
.ok()
.map(|b| String::from_utf8(b).unwrap_or_else(|e| hex(e.as_bytes())))
}
}
}
fn result_columns<R: Row>(row: &R) -> Vec<ResultColumn> {
row.columns()
.iter()
.map(|c| ResultColumn { name: c.name().to_owned(), type_name: c.type_info().name().to_owned() })
.collect()
}
/// Streams the results of `$sql` on executor `$exec`, keeping at most `$max` rows.
macro_rules! fetch {
($exec:expr, $sql:expr, $max:expr) => {{
use futures::StreamExt;
let mut stream = sqlx::raw_sql(sqlx::AssertSqlSafe($sql.to_owned())).fetch_many($exec);
let mut columns: Vec<ResultColumn> = Vec::new();
let mut rows: Vec<Vec<Option<String>>> = Vec::new();
let mut affected: Option<u64> = None;
let mut has_more = false;
let mut failure = None;
let mut first: Option<Instant> = None;
while let Some(item) = stream.next().await {
first.get_or_insert_with(Instant::now);
match item {
Ok(sqlx::Either::Left(done)) => affected = Some(affected.unwrap_or(0) + done.rows_affected()),
Ok(sqlx::Either::Right(row)) => {
if columns.is_empty() {
columns = result_columns(&row);
}
if rows.len() >= $max {
has_more = true;
break;
}
rows.push((0..row.len()).map(|i| cell(&row, i)).collect());
}
Err(e) => {
failure = Some(db_err(e));
break;
}
}
}
drop(stream);
match failure {
Some(e) => Err(e),
None => Ok((columns, rows, affected, has_more, first)),
}
}};
}
/// Columns, rows, rows affected, more rows beyond the limit, time of the first answer.
type Fetched = (Vec<ResultColumn>, Vec<Vec<Option<String>>>, Option<u64>, bool, Option<Instant>);
/// The server-side id of a connection (to cancel its statement from another one).
macro_rules! backend_id {
($conn:expr, $sql:expr) => {{
let fetched: IdeResult<Fetched> = fetch!($conn, $sql, 1);
fetched
.ok()
.and_then(|f| f.1.into_iter().next())
.and_then(|r| r.into_iter().next().flatten())
.filter(|id| id.chars().all(|c| c.is_ascii_digit()))
}};
}
fn looks_like_query(sql: &str) -> bool {
let first = sql
.lines()
.map(str::trim)
.find(|l| !l.is_empty() && !l.starts_with("--"))
.and_then(|l| l.split_whitespace().next())
.unwrap_or("")
.to_ascii_uppercase();
matches!(
first.as_str(),
"SELECT" | "WITH" | "VALUES" | "TABLE" | "SHOW" | "PRAGMA" | "EXPLAIN" | "DESCRIBE" | "DESC"
)
}
fn file_setting(path: &str) -> Option<PathBuf> {
let path = path.trim();
(!path.is_empty()).then(|| ide_ssh::expand(path))
}
fn is_identifier(name: &str) -> bool {
let mut chars = name.chars();
chars.next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '.')
}
/// A MySQL `SET` value: numbers and keywords as they are, anything else quoted.
fn mysql_value(v: &str) -> String {
let keyword = ["ON", "OFF", "TRUE", "FALSE", "DEFAULT"].iter().any(|k| k.eq_ignore_ascii_case(v));
if keyword || v.parse::<f64>().is_ok() {
v.to_owned()
} else {
format!("'{}'", v.replace('\\', "\\\\").replace('\'', "''"))
}
}
/// How the Options and Advanced tabs reach a connection.
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct Setup {
/// PostgreSQL run-time parameters (startup `options`), SQLite pragmas.
pub params: Vec<(String, String)>,
/// Run on every new connection: MySQL session settings, then the startup script.
pub statements: Vec<String>,
pub application_name: Option<String>,
pub charset: Option<String>,
pub collation: Option<String>,
pub statement_cache: Option<usize>,
pub connect_timeout: Option<Duration>,
}
impl Setup {
pub fn of(source: &DataSource) -> IdeResult<Self> {
let mut s = Setup::default();
let o = &source.options;
match source.driver {
DbDriver::Postgres => {
if source.read_only {
s.params.push(("default_transaction_read_only".into(), "on".into()));
}
if !o.default_schema.trim().is_empty() {
s.params.push(("search_path".into(), o.default_schema.trim().into()));
}
if !o.time_zone.trim().is_empty() {
s.params.push(("TimeZone".into(), o.time_zone.trim().into()));
}
}
DbDriver::Mysql if source.read_only => s.statements.push("SET SESSION TRANSACTION READ ONLY".into()),
_ => {}
}
for p in &source.properties {
let value = p.value.trim();
let name = p.name.trim().replace('-', "_");
if name.is_empty() {
continue;
}
if !is_identifier(&name) {
return Err(IdeError::invalid(format!("invalid driver property name: {}", p.name.trim())));
}
let number = |what: &str| {
value.parse::<u64>().map_err(|_| IdeError::invalid(format!("{what} must be a whole number: {value}")))
};
match (source.driver, name.to_ascii_lowercase().as_str()) {
(_, "statement_cache_capacity") => s.statement_cache = Some(number(&name)? as usize),
(_, "connect_timeout") => s.connect_timeout = Some(Duration::from_secs(number(&name)?)),
(DbDriver::Postgres, "application_name") => s.application_name = Some(value.to_owned()),
(DbDriver::Mysql, "charset") => s.charset = Some(value.to_owned()),
(DbDriver::Mysql, "collation") => s.collation = Some(value.to_owned()),
(DbDriver::Mysql, _) => s.statements.push(format!("SET SESSION {name} = {}", mysql_value(value))),
_ => s.params.push((name, value.to_owned())),
}
}
let script = o.startup_script.trim();
if !script.is_empty() {
s.statements.extend(crate::split::split(script, source.driver).into_iter().map(|st| st.text));
}
Ok(s)
}
}
impl Pool {
pub fn driver(&self) -> DbDriver {
match self {
Pool::Sqlite(_) => DbDriver::Sqlite,
Pool::Postgres(_) => DbDriver::Postgres,
Pool::Mysql(_) => DbDriver::Mysql,
}
}
/// Opens a pool and one connection (so bad settings fail here).
pub async fn connect(source: &DataSource, password: Option<&str>, root: Option<&Path>) -> IdeResult<Self> {
const MAX: u32 = 4;
let setup = Setup::of(source)?;
let timeout = setup.connect_timeout.unwrap_or(Duration::from_secs(30));
let init = Arc::new(setup.statements);
// Runs the session statements (startup script…) on every new connection.
macro_rules! after_connect {
() => {{
let init = init.clone();
move |conn, _| {
let init = init.clone();
Box::pin(async move {
for sql in init.iter() {
sqlx::raw_sql(sqlx::AssertSqlSafe(sql.clone())).execute(&mut *conn).await?;
}
Ok(())
})
}
}};
}
match source.driver {
DbDriver::Sqlite => {
let file = source.file.as_deref().ok_or_else(|| IdeError::invalid("no database file"))?;
let file = match root {
Some(r) if file.is_relative() => r.join(file),
_ => file.to_path_buf(),
};
let mut opts =
SqliteConnectOptions::new().filename(&file).create_if_missing(false).read_only(source.read_only);
for (k, v) in setup.params {
opts = opts.pragma(k, v);
}
if let Some(n) = setup.statement_cache {
opts = opts.statement_cache_capacity(n);
}
let pool = SqlitePoolOptions::new()
.max_connections(MAX)
.acquire_timeout(timeout)
.after_connect(after_connect!())
.connect_with(opts)
.await
.map_err(db_err)?;
Ok(Pool::Sqlite(pool))
}
DbDriver::Postgres => {
let mut opts = PgConnectOptions::new()
.host(&source.host)
.port(source.port.unwrap_or(5432))
.username(&source.user)
.application_name(setup.application_name.as_deref().unwrap_or("OpenIDEA"))
.ssl_mode(match source.ssl {
DbSslMode::Disable => PgSslMode::Disable,
DbSslMode::Prefer => PgSslMode::Prefer,
DbSslMode::Require => PgSslMode::Require,
DbSslMode::VerifyCa => PgSslMode::VerifyCa,
DbSslMode::VerifyFull => PgSslMode::VerifyFull,
});
if let Some(f) = file_setting(&source.ssl_ca_file) {
opts = opts.ssl_root_cert(f);
}
if let Some(f) = file_setting(&source.ssl_cert_file) {
opts = opts.ssl_client_cert(f);
}
if let Some(f) = file_setting(&source.ssl_key_file) {
opts = opts.ssl_client_key(f);
}
if !source.database.is_empty() {
opts = opts.database(&source.database);
}
if let Some(p) = password {
opts = opts.password(p);
}
if !setup.params.is_empty() {
opts = opts.options(setup.params);
}
if let Some(n) = setup.statement_cache {
opts = opts.statement_cache_capacity(n);
}
let pool = PgPoolOptions::new()
.max_connections(MAX)
.acquire_timeout(timeout)
.after_connect(after_connect!())
.connect_with(opts)
.await
.map_err(db_err)?;
Ok(Pool::Postgres(pool))
}
DbDriver::Mysql => {
let mut opts = MySqlConnectOptions::new()
.host(&source.host)
.port(source.port.unwrap_or(3306))
.username(&source.user)
.ssl_mode(match source.ssl {
DbSslMode::Disable => MySqlSslMode::Disabled,
DbSslMode::Prefer => MySqlSslMode::Preferred,
DbSslMode::Require => MySqlSslMode::Required,
DbSslMode::VerifyCa => MySqlSslMode::VerifyCa,
DbSslMode::VerifyFull => MySqlSslMode::VerifyIdentity,
});
if let Some(f) = file_setting(&source.ssl_ca_file) {
opts = opts.ssl_ca(f);
}
if let Some(f) = file_setting(&source.ssl_cert_file) {
opts = opts.ssl_client_cert(f);
}
if let Some(f) = file_setting(&source.ssl_key_file) {
opts = opts.ssl_client_key(f);
}
if !source.database.is_empty() {
opts = opts.database(&source.database);
}
if let Some(p) = password {
opts = opts.password(p);
}
if let Some(c) = &setup.charset {
opts = opts.charset(c);
}
if let Some(c) = &setup.collation {
opts = opts.collation(c);
}
if let Some(n) = setup.statement_cache {
opts = opts.statement_cache_capacity(n);
}
let tz = source.options.time_zone.trim();
if !tz.is_empty() {
opts = opts.timezone(Some(tz.to_owned()));
}
let pool = MySqlPoolOptions::new()
.max_connections(MAX)
.acquire_timeout(timeout)
.after_connect(after_connect!())
.connect_with(opts)
.await
.map_err(db_err)?;
Ok(Pool::Mysql(pool))
}
}
}
pub async fn close(&self) {
with_pool!(self, p => p.close().await)
}
pub fn is_closed(&self) -> bool {
with_pool!(self, p => p.is_closed())
}
/// Runs `sql` (no row limit beyond `max`), for introspection.
pub async fn rows(&self, sql: &str, max: usize) -> IdeResult<Vec<Vec<Option<String>>>> {
let fetched: IdeResult<Fetched> = with_pool!(self, p => fetch!(p, sql, max));
Ok(fetched?.1)
}
/// Runs one statement and keeps at most `max_rows` rows. Cancelling stops
/// the statement on the server (PostgreSQL, MySQL) and drops the connection.
pub async fn execute(&self, sql: &str, max_rows: usize, cancel: &CancellationToken) -> IdeResult<QueryResult> {
let mut conn = Held::acquire(self).await?;
let r = conn.execute(self, sql, max_rows, cancel).await;
if cancel.is_cancelled() {
conn.discard().await;
return Err(IdeError::cancelled());
}
r
}
/// Runs `statements` in one transaction; each must change `expect_one`
/// row when flagged (table editor submits).
pub async fn transaction(&self, statements: &[(String, bool)]) -> IdeResult<u64> {
with_pool!(self, p => {
let mut tx = p.begin().await.map_err(db_err)?;
let mut total = 0;
for (sql, expect_one) in statements {
let done = sqlx::raw_sql(sqlx::AssertSqlSafe(sql.clone())).execute(&mut *tx).await.map_err(db_err)?;
let n = done.rows_affected();
// MySQL reports 0 for an UPDATE that leaves the row unchanged.
if *expect_one && (n > 1 || (n == 0 && !matches!(self, Pool::Mysql(_)))) {
return Err(IdeError::invalid(format!("{sql}: expected to change 1 row, changed {n}; nothing was saved")));
}
total += n;
}
tx.commit().await.map_err(db_err)?;
Ok(total)
})
}
}
/// A connection taken out of a pool: for one statement, or held by a
/// manual-commit console across statements with its transaction open.
pub enum Held {
Sqlite(PoolConnection<Sqlite>),
Postgres(PoolConnection<Postgres>),
Mysql(PoolConnection<MySql>),
}
impl Held {
pub async fn acquire(pool: &Pool) -> IdeResult<Self> {
Ok(match pool {
Pool::Sqlite(p) => Held::Sqlite(p.acquire().await.map_err(db_err)?),
Pool::Postgres(p) => Held::Postgres(p.acquire().await.map_err(db_err)?),
Pool::Mysql(p) => Held::Mysql(p.acquire().await.map_err(db_err)?),
})
}
/// Runs a statement without results (`BEGIN`, `COMMIT`, `ROLLBACK`).
pub async fn command(&mut self, sql: &str) -> IdeResult<()> {
let sql = sqlx::AssertSqlSafe(sql.to_owned());
match self {
Held::Sqlite(c) => sqlx::raw_sql(sql).execute(&mut **c).await.map(drop),
Held::Postgres(c) => sqlx::raw_sql(sql).execute(&mut **c).await.map(drop),
Held::Mysql(c) => sqlx::raw_sql(sql).execute(&mut **c).await.map(drop),
}
.map_err(db_err)
}
/// Closes the connection instead of returning it to the pool (its statement was
/// interrupted; an open transaction is rolled back by the server).
pub async fn discard(self) {
match self {
Held::Sqlite(c) => drop(c.detach()),
Held::Postgres(c) => {
let _ = c.detach().close().await;
}
Held::Mysql(c) => {
let _ = c.detach().close().await;
}
}
}
/// Runs one statement on this connection (see [`Pool::execute`]); after a
/// cancellation the caller must [`discard`](Self::discard) it.
pub async fn execute(
&mut self,
pool: &Pool,
sql: &str,
max_rows: usize,
cancel: &CancellationToken,
) -> IdeResult<QueryResult> {
let started = Instant::now();
// `$id`: the query naming the server process, `$kill`: how another connection stops it.
macro_rules! run {
($conn:expr, $id:expr, $kill:expr) => {{
let backend = match $id {
Some(q) => backend_id!(&mut **$conn, q),
None => None,
};
let r = tokio::select! {
r = async { fetch!(&mut **$conn, sql, max_rows) } => r,
() = cancel.cancelled() => Err(IdeError::cancelled()),
};
if cancel.is_cancelled() {
if let Some(b) = backend {
let _ = pool.rows(&$kill(b), 1).await;
}
return Err(IdeError::cancelled());
}
let mut r = r?;
if r.0.is_empty() && looks_like_query(sql) {
r.0 = (&mut **$conn)
.prepare(sqlx::AssertSqlSafe(sql.to_owned()).into_sql_str())
.await
.map(|s| statement_columns(&s))
.unwrap_or_default();
}
r
}};
}
let fetched: Fetched = match self {
Held::Sqlite(c) => run!(c, None::<&str>, |_: String| String::new()),
Held::Postgres(c) => {
run!(c, Some("SELECT pg_backend_pid()"), |b: String| format!("SELECT pg_cancel_backend({b})"))
}
Held::Mysql(c) => run!(c, Some("SELECT CONNECTION_ID()"), |b: String| format!("KILL QUERY {b}")),
};
let (columns, rows, affected, has_more, first) = fetched;
let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
Ok(QueryResult {
statement: sql.to_owned(),
affected: if columns.is_empty() { Some(affected.unwrap_or(0) as f64) } else { None },
columns,
rows,
has_more,
elapsed_ms,
execution_ms: first.map_or(elapsed_ms, |t| t.duration_since(started).as_secs_f64() * 1000.0),
})
}
}
/// The statement opening a transaction.
pub fn begin_statement(driver: DbDriver) -> &'static str {
match driver {
DbDriver::Mysql => "START TRANSACTION",
DbDriver::Sqlite | DbDriver::Postgres => "BEGIN",
}
}
fn statement_columns<S: sqlx::Statement>(s: &S) -> Vec<ResultColumn> {
s.columns()
.iter()
.map(|c| ResultColumn { name: c.name().to_owned(), type_name: c.type_info().name().to_owned() })
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::{DbOptions, DbProperty};
fn prop(name: &str, value: &str) -> DbProperty {
DbProperty { name: name.into(), value: value.into() }
}
#[test]
fn postgres_options_become_runtime_parameters() {
let s = DataSource {
driver: DbDriver::Postgres,
read_only: true,
options: DbOptions {
default_schema: "app, public".into(),
time_zone: "Europe/Paris".into(),
startup_script: "SET work_mem = '64MB'; select 1;".into(),
..Default::default()
},
properties: vec![prop("statement_timeout", "5s"), prop("application_name", "me"), prop("", "x")],
..Default::default()
};
let setup = Setup::of(&s).unwrap();
let names: Vec<_> = setup.params.iter().map(|(k, _)| k.as_str()).collect();
assert_eq!(names, ["default_transaction_read_only", "search_path", "TimeZone", "statement_timeout"]);
assert_eq!(setup.application_name.as_deref(), Some("me"));
assert_eq!(setup.statements, ["SET work_mem = '64MB'", "select 1"]);
}
#[test]
fn mysql_properties_become_session_variables() {
let s = DataSource {
driver: DbDriver::Mysql,
read_only: true,
properties: vec![
prop("sql_mode", "ANSI_QUOTES"),
prop("max_execution_time", "1000"),
prop("charset", "utf8mb4"),
prop("connect-timeout", "5"),
],
..Default::default()
};
let setup = Setup::of(&s).unwrap();
assert_eq!(
setup.statements,
[
"SET SESSION TRANSACTION READ ONLY",
"SET SESSION sql_mode = 'ANSI_QUOTES'",
"SET SESSION max_execution_time = 1000"
]
);
assert_eq!(setup.charset.as_deref(), Some("utf8mb4"));
assert_eq!(setup.connect_timeout, Some(Duration::from_secs(5)));
assert!(Setup::of(&DataSource { properties: vec![prop("a; drop", "1")], ..s.clone() }).is_err());
assert!(Setup::of(&DataSource { properties: vec![prop("connect_timeout", "soon")], ..s }).is_err());
}
#[test]
fn sqlite_properties_become_pragmas() {
let s = DataSource { properties: vec![prop("cache_size", "-2000")], ..Default::default() };
assert_eq!(Setup::of(&s).unwrap().params, [("cache_size".to_owned(), "-2000".to_owned())]);
}
}
+137
Переглянути файл
@@ -0,0 +1,137 @@
//! Query history (IDEA's Browse Query History): the statements each data
//! source ran, newest first. One JSON file in the app's data directory for all
//! projects, keyed by project root, then data source id — never committed.
use ide_core::{IdeError, IdeResult};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
/// Statements kept per data source.
pub const MAX_ENTRIES: usize = 500;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "DbHistoryEntry")]
pub struct HistoryEntry {
pub text: String,
/// Milliseconds since the Unix epoch.
pub time: f64,
}
/// Project root → data source id → entries, newest first.
type Contents = BTreeMap<String, BTreeMap<String, Vec<HistoryEntry>>>;
/// Serialises read-modify-write across the projects sharing the file.
static LOCK: Mutex<()> = Mutex::new(());
pub struct HistoryStore {
path: PathBuf,
project: String,
}
impl HistoryStore {
pub fn new(path: PathBuf, root: &Path) -> Self {
Self { path, project: root.to_string_lossy().into_owned() }
}
pub fn list(&self, source: &str) -> Vec<HistoryEntry> {
let _g = LOCK.lock();
self.read().remove(&self.project).and_then(|mut p| p.remove(source)).unwrap_or_default()
}
/// Records `text`; running a statement again moves it to the top.
pub fn add(&self, source: &str, text: &str, time: f64) -> IdeResult<()> {
let text = text.trim();
if text.is_empty() {
return Ok(());
}
let _g = LOCK.lock();
let mut all = self.read();
let entries = all.entry(self.project.clone()).or_default().entry(source.to_owned()).or_default();
entries.retain(|e| e.text != text);
entries.insert(0, HistoryEntry { text: text.to_owned(), time });
entries.truncate(MAX_ENTRIES);
self.write(&all)
}
/// Removes the entries with these texts (all of them for `None`).
pub fn remove(&self, source: &str, texts: Option<&[String]>) -> IdeResult<()> {
let _g = LOCK.lock();
let mut all = self.read();
let Some(project) = all.get_mut(&self.project) else { return Ok(()) };
match texts {
None => {
project.remove(source);
}
Some(texts) => {
if let Some(entries) = project.get_mut(source) {
entries.retain(|e| !texts.contains(&e.text));
}
}
}
self.write(&all)
}
fn read(&self) -> Contents {
match std::fs::read(&self.path) {
Ok(bytes) => serde_json::from_slice(&bytes).unwrap_or_else(|e| {
tracing::warn!("query history: {}: {e}", self.path.display());
Contents::new()
}),
Err(_) => Contents::new(),
}
}
fn write(&self, all: &Contents) -> IdeResult<()> {
let err = |e: std::io::Error| IdeError::unavailable(format!("query history: {e}"));
if let Some(dir) = self.path.parent() {
std::fs::create_dir_all(dir).map_err(err)?;
}
let tmp = self.path.with_extension("json.tmp");
let bytes = serde_json::to_vec(all).map_err(|e| IdeError::internal(e.to_string()))?;
std::fs::write(&tmp, bytes).map_err(err)?;
std::fs::rename(&tmp, &self.path).map_err(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keeps_newest_first_without_duplicates_per_project_and_source() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("h.json");
let a = HistoryStore::new(file.clone(), Path::new("/p/a"));
let b = HistoryStore::new(file, Path::new("/p/b"));
a.add("s1", "select 1", 1.0).unwrap();
a.add("s1", " select 2 ", 2.0).unwrap();
a.add("s1", "select 1", 3.0).unwrap();
a.add("s2", "select 3", 4.0).unwrap();
b.add("s1", "select 4", 5.0).unwrap();
let texts = |v: Vec<HistoryEntry>| v.into_iter().map(|e| e.text).collect::<Vec<_>>();
assert_eq!(texts(a.list("s1")), ["select 1", "select 2"]);
assert_eq!(texts(a.list("s2")), ["select 3"]);
assert_eq!(texts(b.list("s1")), ["select 4"]);
a.remove("s1", Some(&["select 2".to_owned()])).unwrap();
assert_eq!(texts(a.list("s1")), ["select 1"]);
a.remove("s1", None).unwrap();
assert!(a.list("s1").is_empty());
assert_eq!(texts(b.list("s1")), ["select 4"]);
}
#[test]
fn caps_entries() {
let dir = tempfile::tempdir().unwrap();
let h = HistoryStore::new(dir.path().join("h.json"), Path::new("/p"));
for i in 0..MAX_ENTRIES + 5 {
h.add("s", &format!("select {i}"), i as f64).unwrap();
}
let list = h.list("s");
assert_eq!(list.len(), MAX_ENTRIES);
assert_eq!(list[0].text, format!("select {}", MAX_ENTRIES + 4));
}
}
File diff suppressed because it is too large. Load diff
+268
Переглянути файл
@@ -0,0 +1,268 @@
//! Execution plans (IDEA's Explain Plan): the `EXPLAIN` statement of each
//! dialect and its output read into one flat tree of operations.
use crate::DbDriver;
use crate::conn::QueryResult;
use ide_core::{IdeError, IdeResult};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use specta::Type;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "DbPlanProperty")]
pub struct PlanProperty {
pub name: String,
pub value: String,
}
/// One operation of a plan; `parent` indexes the plan's node list (roots: `None`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "DbPlanNode")]
pub struct PlanNode {
pub parent: Option<u32>,
/// `Full Scan`, `Index Scan`, `Hash Join`…
pub operation: String,
/// The object it works on and its key details (`table: o;`).
pub params: String,
pub properties: Vec<PlanProperty>,
}
/// The statement that explains `statement` without running it.
pub fn explain_statement(driver: DbDriver, statement: &str) -> String {
let s = statement.trim().trim_end_matches(';');
match driver {
DbDriver::Sqlite => format!("EXPLAIN QUERY PLAN {s}"),
DbDriver::Postgres => format!("EXPLAIN (FORMAT JSON) {s}"),
DbDriver::Mysql => format!("EXPLAIN {s}"),
}
}
/// Reads the result of [`explain_statement`].
pub fn parse(driver: DbDriver, result: &QueryResult) -> IdeResult<Vec<PlanNode>> {
match driver {
DbDriver::Sqlite => Ok(sqlite(&result.rows)),
DbDriver::Postgres => {
let text: String =
result.rows.iter().filter_map(|r| r.first().cloned().flatten()).collect::<Vec<_>>().join("\n");
let json: Value = serde_json::from_str(&text).map_err(|e| IdeError::invalid(format!("plan: {e}")))?;
let mut out = Vec::new();
for root in json.as_array().into_iter().flatten() {
if let Some(plan) = root.get("Plan") {
postgres(plan, None, &mut out);
}
}
Ok(out)
}
DbDriver::Mysql => {
let names: Vec<&str> = result.columns.iter().map(|c| c.name.as_str()).collect();
Ok(mysql(&names, &result.rows))
}
}
}
fn prop(name: &str, value: impl Into<String>) -> PlanProperty {
PlanProperty { name: name.to_owned(), value: value.into() }
}
/// SQLite rows: `id, parent, notused, detail` (`SCAN o`, `SEARCH c USING INTEGER PRIMARY KEY (rowid=?)`).
fn sqlite(rows: &[Vec<Option<String>>]) -> Vec<PlanNode> {
let mut ids: Vec<String> = Vec::new();
let mut out = Vec::new();
for r in rows {
let cell = |i: usize| r.get(i).cloned().flatten().unwrap_or_default();
let (id, parent, detail) = (cell(0), cell(1), cell(3));
let parent = ids.iter().position(|x| *x == parent).map(|i| i as u32);
let (operation, params) = sqlite_operation(&detail);
out.push(PlanNode { parent, operation, params, properties: vec![prop("Detail", detail.clone())] });
ids.push(id);
}
out
}
fn sqlite_operation(detail: &str) -> (String, String) {
let words: Vec<&str> = detail.split_whitespace().collect();
let object = |i: usize| words.get(i).map(|t| format!("table: {t};")).unwrap_or_default();
let using = || detail.find(" USING ").map(|i| detail[i + 1..].to_owned()).unwrap_or_default();
match words.first().copied() {
Some("SCAN") if detail.contains(" USING ") => ("Index Scan".into(), format!("{} {}", object(1), using())),
Some("SCAN") => ("Full Scan".into(), object(1)),
Some("SEARCH") => ("Index Scan".into(), format!("{} {}", object(1), using()).trim().to_owned()),
Some("USE") if detail.contains("ORDER BY") => ("Order By".into(), String::new()),
Some("USE") if detail.contains("GROUP BY") => ("Group By".into(), String::new()),
Some("USE") if detail.contains("DISTINCT") => ("Distinct".into(), String::new()),
_ => (detail.to_owned(), String::new()),
}
}
/// PostgreSQL JSON plan node: `Node Type`, `Relation Name`, nested `Plans`.
fn postgres(node: &Value, parent: Option<u32>, out: &mut Vec<PlanNode>) {
let text = |k: &str| node.get(k).and_then(Value::as_str).unwrap_or_default();
let mut operation = text("Node Type").to_owned();
if let Some(j) = node.get("Join Type").and_then(Value::as_str).filter(|j| *j != "Inner") {
operation = format!("{operation} ({j})");
}
let mut params = Vec::new();
if let Some(r) = node.get("Relation Name").and_then(Value::as_str) {
let alias = text("Alias");
params.push(if alias.is_empty() || alias == r {
format!("table: {r};")
} else {
format!("table: {r} {alias};")
});
}
if let Some(i) = node.get("Index Name").and_then(Value::as_str) {
params.push(format!("index: {i};"));
}
for k in ["Index Cond", "Hash Cond", "Merge Cond", "Filter", "Join Filter"] {
if let Some(c) = node.get(k).and_then(Value::as_str) {
params.push(format!("{}: {c};", k.to_lowercase()));
}
}
let properties = node
.as_object()
.into_iter()
.flatten()
.filter(|(k, _)| k.as_str() != "Plans")
.map(|(k, v)| {
let value = match v {
Value::String(s) => s.clone(),
Value::Array(a) => a
.iter()
.map(|x| x.as_str().map_or_else(|| x.to_string(), str::to_owned))
.collect::<Vec<_>>()
.join(", "),
other => other.to_string(),
};
prop(k, value)
})
.collect();
let me = out.len() as u32;
out.push(PlanNode { parent, operation, params: params.join(" "), properties });
for child in node.get("Plans").and_then(Value::as_array).into_iter().flatten() {
postgres(child, Some(me), out);
}
}
/// MySQL's tabular `EXPLAIN`: one row per table access, flat.
fn mysql(names: &[&str], rows: &[Vec<Option<String>>]) -> Vec<PlanNode> {
let col = |r: &[Option<String>], n: &str| {
names.iter().position(|c| c.eq_ignore_ascii_case(n)).and_then(|i| r.get(i).cloned().flatten())
};
rows.iter()
.map(|r| {
let access = col(r, "type").unwrap_or_default();
let operation = match access.as_str() {
"ALL" => "Full Scan".to_owned(),
"index" => "Full Index Scan".to_owned(),
"range" => "Index Range Scan".to_owned(),
"ref" | "eq_ref" | "ref_or_null" => "Index Lookup".to_owned(),
"const" | "system" => "Constant Lookup".to_owned(),
"" => col(r, "Extra").unwrap_or_else(|| "Select".to_owned()),
other => other.to_owned(),
};
let mut params = Vec::new();
if let Some(t) = col(r, "table") {
params.push(format!("table: {t};"));
}
if let Some(k) = col(r, "key") {
params.push(format!("key: {k};"));
}
let properties = names.iter().zip(r).filter_map(|(n, v)| v.as_ref().map(|v| prop(n, v.clone()))).collect();
PlanNode { parent: None, operation, params: params.join(" "), properties }
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::conn::ResultColumn;
fn result(columns: &[&str], rows: Vec<Vec<Option<&str>>>) -> QueryResult {
QueryResult {
statement: String::new(),
columns: columns.iter().map(|c| ResultColumn { name: (*c).into(), type_name: String::new() }).collect(),
rows: rows.into_iter().map(|r| r.into_iter().map(|v| v.map(str::to_owned)).collect()).collect(),
affected: None,
has_more: false,
elapsed_ms: 0.0,
execution_ms: 0.0,
}
}
#[test]
fn explains_per_dialect() {
assert_eq!(explain_statement(DbDriver::Sqlite, "select 1;"), "EXPLAIN QUERY PLAN select 1");
assert_eq!(explain_statement(DbDriver::Postgres, " select 1 "), "EXPLAIN (FORMAT JSON) select 1");
assert_eq!(explain_statement(DbDriver::Mysql, "select 1"), "EXPLAIN select 1");
}
#[test]
fn reads_sqlite_plans_like_idea() {
let r = result(
&["id", "parent", "notused", "detail"],
vec![
vec![Some("3"), Some("0"), Some("0"), Some("SCAN o")],
vec![Some("5"), Some("0"), Some("0"), Some("SEARCH c USING INTEGER PRIMARY KEY (rowid=?)")],
vec![Some("9"), Some("0"), Some("0"), Some("USE TEMP B-TREE FOR ORDER BY")],
],
);
let plan = parse(DbDriver::Sqlite, &r).unwrap();
let ops: Vec<(&str, &str)> = plan.iter().map(|n| (n.operation.as_str(), n.params.as_str())).collect();
assert_eq!(
ops,
[
("Full Scan", "table: o;"),
("Index Scan", "table: c; USING INTEGER PRIMARY KEY (rowid=?)"),
("Order By", "")
]
);
assert!(plan.iter().all(|n| n.parent.is_none()));
}
#[test]
fn nests_sqlite_children_under_their_parent() {
let r = result(
&["id", "parent", "notused", "detail"],
vec![
vec![Some("2"), Some("0"), Some("0"), Some("CO-ROUTINE sub")],
vec![Some("4"), Some("2"), Some("0"), Some("SCAN t")],
],
);
let plan = parse(DbDriver::Sqlite, &r).unwrap();
assert_eq!(plan[1].parent, Some(0));
}
#[test]
fn reads_postgres_json_trees() {
let json = r#"[{"Plan": {"Node Type": "Hash Join", "Join Type": "Inner", "Hash Cond": "(o.customer_id = c.id)",
"Total Cost": 12.5, "Plans": [
{"Node Type": "Seq Scan", "Relation Name": "orders", "Alias": "o", "Filter": "(status <> 'x')"},
{"Node Type": "Hash", "Plans": [{"Node Type": "Seq Scan", "Relation Name": "customers", "Alias": "c"}]}
]}}]"#;
let plan = parse(DbDriver::Postgres, &result(&["QUERY PLAN"], vec![vec![Some(json)]])).unwrap();
assert_eq!(plan.len(), 4);
assert_eq!(plan[0].operation, "Hash Join");
assert_eq!(plan[0].params, "hash cond: (o.customer_id = c.id);");
assert!(plan[0].properties.iter().any(|p| p.name == "Total Cost" && p.value == "12.5"));
assert_eq!((plan[1].parent, plan[1].params.as_str()), (Some(0), "table: orders o; filter: (status <> 'x');"));
assert_eq!((plan[3].parent, plan[3].operation.as_str()), (Some(2), "Seq Scan"));
}
#[test]
fn reads_mysql_rows() {
let r = result(
&["id", "select_type", "table", "type", "key", "rows", "Extra"],
vec![
vec![Some("1"), Some("SIMPLE"), Some("o"), Some("ALL"), None, Some("20"), Some("Using where")],
vec![Some("1"), Some("SIMPLE"), Some("c"), Some("eq_ref"), Some("PRIMARY"), Some("1"), None],
],
);
let plan = parse(DbDriver::Mysql, &r).unwrap();
assert_eq!((plan[0].operation.as_str(), plan[0].params.as_str()), ("Full Scan", "table: o;"));
assert_eq!((plan[1].operation.as_str(), plan[1].params.as_str()), ("Index Lookup", "table: c; key: PRIMARY;"));
assert!(plan[0].properties.iter().any(|p| p.name == "rows" && p.value == "20"));
}
}
+109
Переглянути файл
@@ -0,0 +1,109 @@
//! Saved data source passwords, in the app's data directory rather than the OS
//! credential store (no system prompts). One JSON file for all projects, keyed
//! by project root, then credential key; owner-only on Unix.
use ide_core::{IdeError, IdeResult};
use parking_lot::Mutex;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
/// Project root → credential key → secret.
type Contents = BTreeMap<String, BTreeMap<String, String>>;
/// Serialises read-modify-write across the projects sharing the file.
static LOCK: Mutex<()> = Mutex::new(());
pub struct SecretStore {
path: PathBuf,
project: String,
}
/// Credential key of the SSH password / key passphrase of data source `id`.
pub fn ssh_key(id: &str) -> String {
format!("{id}#ssh")
}
impl SecretStore {
pub fn new(path: PathBuf, root: &Path) -> Self {
Self { path, project: root.to_string_lossy().into_owned() }
}
pub fn get(&self, key: &str) -> Option<String> {
let _g = LOCK.lock();
self.read().remove(&self.project)?.remove(key)
}
pub fn set(&self, key: &str, secret: &str) -> IdeResult<()> {
let _g = LOCK.lock();
let mut all = self.read();
all.entry(self.project.clone()).or_default().insert(key.to_owned(), secret.to_owned());
self.write(&all)
}
pub fn delete(&self, key: &str) {
let _g = LOCK.lock();
let mut all = self.read();
let Some(project) = all.get_mut(&self.project) else { return };
if project.remove(key).is_none() {
return;
}
if project.is_empty() {
all.remove(&self.project);
}
if let Err(e) = self.write(&all) {
tracing::warn!("saved passwords: {}", e.message);
}
}
fn read(&self) -> Contents {
match std::fs::read(&self.path) {
Ok(bytes) => serde_json::from_slice(&bytes).unwrap_or_else(|e| {
tracing::warn!("saved passwords: {}: {e}", self.path.display());
Contents::new()
}),
Err(_) => Contents::new(),
}
}
fn write(&self, all: &Contents) -> IdeResult<()> {
let err = |e: std::io::Error| IdeError::unavailable(format!("saved passwords: {e}"));
if let Some(dir) = self.path.parent() {
std::fs::create_dir_all(dir).map_err(err)?;
}
let tmp = self.path.with_extension("json.tmp");
let mut options = std::fs::OpenOptions::new();
options.write(true).create(true).truncate(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
let bytes = serde_json::to_vec_pretty(all).map_err(|e| IdeError::internal(e.to_string()))?;
std::io::Write::write_all(&mut options.open(&tmp).map_err(err)?, &bytes).map_err(err)?;
std::fs::rename(&tmp, &self.path).map_err(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keeps_projects_apart_and_drops_empty_ones() {
let tmp = tempfile::tempdir().unwrap();
let file = tmp.path().join("db-passwords.json");
let a = SecretStore::new(file.clone(), Path::new("/p/a"));
let b = SecretStore::new(file.clone(), Path::new("/p/b"));
a.set("s1", "pw-a").unwrap();
b.set("s1", "pw-b").unwrap();
b.set(&ssh_key("s1"), "pass").unwrap();
assert_eq!(a.get("s1").as_deref(), Some("pw-a"));
assert_eq!(b.get("s1#ssh").as_deref(), Some("pass"));
a.delete("s1");
assert_eq!(a.get("s1"), None);
assert_eq!(b.get("s1").as_deref(), Some("pw-b"));
assert!(!std::fs::read_to_string(&file).unwrap().contains("/p/a"));
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(std::fs::metadata(&file).unwrap().permissions().mode() & 0o777, 0o600);
}
}
}
+510
Переглянути файл
@@ -0,0 +1,510 @@
//! Data sources: per-project connection settings in `.openidea/data-sources.toml`.
//! Passwords never go there: saved ones live in [`crate::secrets`], the others
//! only in memory for the session.
use ide_core::{IdeError, IdeResult, IpcPath, project_dir};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::path::Path;
const FILE: &str = "data-sources.toml";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum DbDriver {
Sqlite,
Postgres,
/// MySQL and MariaDB.
Mysql,
}
impl DbDriver {
pub fn default_port(self) -> Option<u16> {
match self {
DbDriver::Sqlite => None,
DbDriver::Postgres => Some(5432),
DbDriver::Mysql => Some(3306),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum DbSslMode {
Disable,
#[default]
Prefer,
Require,
/// Require, and check the server certificate against the CA file.
VerifyCa,
/// [`DbSslMode::VerifyCa`], and check that the certificate names the host.
VerifyFull,
}
/// IDEA's data source colours (tree node and console tab tint), e.g. red for production.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum DbColor {
Red,
Orange,
Yellow,
Green,
Blue,
Violet,
}
/// Transaction control of the data source's consoles.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum DbTxMode {
#[default]
Auto,
Manual,
}
/// Options tab: session settings applied to every connection.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase", default)]
pub struct DbOptions {
pub transaction: DbTxMode,
/// PostgreSQL `search_path` (unqualified names resolve there).
pub default_schema: String,
/// Session time zone (`TimeZone` / `time_zone`); empty keeps the default.
pub time_zone: String,
/// Statements run on every new connection.
pub startup_script: String,
/// Run `SELECT 1` this often while connected.
pub keep_alive_secs: Option<u32>,
/// Disconnect after this long without statements (enforced by the UI, which sees the activity).
pub auto_disconnect_secs: Option<u32>,
/// PostgreSQL: show only the configured database, its schemas right under the data source.
pub single_database: bool,
}
/// Schemas tab: the databases / schemas the tree shows. Names may use `*` and `?`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase", default)]
pub struct DbIntrospection {
pub all_databases: bool,
pub databases: Vec<String>,
pub all_schemas: bool,
pub schemas: Vec<String>,
/// `pg_catalog`, `information_schema`, `mysql`…, and service databases (`rdsadmin`…).
pub show_system: bool,
/// PostgreSQL template databases (`template0`, `template1`).
pub show_templates: bool,
/// Object filter: the tree lists only tables, views, routines and sequences matching
/// one of these patterns (`*`, `?`; separated by commas or spaces). Empty: all.
pub object_filter: String,
}
impl Default for DbIntrospection {
fn default() -> Self {
Self {
all_databases: true,
databases: Vec::new(),
all_schemas: true,
schemas: Vec::new(),
show_system: false,
show_templates: false,
object_filter: String::new(),
}
}
}
/// Advanced tab: a driver property. PostgreSQL: run-time parameters (and
/// `application_name`); MySQL: session variables (and `charset`, `collation`);
/// SQLite: pragmas. See [`crate::conn`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase", default)]
pub struct DbProperty {
pub name: String,
pub value: String,
}
/// How the SSH tunnel authenticates (shared with Settings › SSH Configurations).
pub use ide_core::remote_settings::SshAuth as DbSshAuth;
/// Connect through an SSH tunnel: the database host and port are reached from `host`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase", default)]
pub struct DbSsh {
pub enabled: bool,
/// A Settings › SSH Configurations id to connect with; empty = the fields below.
pub config: String,
/// Host name, or a `Host` alias of `~/.ssh/config` with [`DbSshAuth::OpenSsh`].
pub host: String,
pub port: Option<u16>,
pub user: String,
pub auth: DbSshAuth,
/// Private key for [`DbSshAuth::KeyPair`]; `~/` is the home directory.
pub key_file: String,
/// Keep the SSH password / key passphrase in the saved passwords.
pub save_secret: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase", default)]
pub struct DataSource {
pub id: String,
pub name: String,
pub driver: DbDriver,
pub host: String,
pub port: Option<u16>,
pub database: String,
pub user: String,
/// SQLite database file (absolute, or relative to the project root).
pub file: Option<IpcPath>,
pub ssl: DbSslMode,
/// Keep the password in the saved passwords.
pub save_password: bool,
/// Refuse statements other than queries (enforced by the database session where possible).
pub read_only: bool,
/// SSH tunnel settings (`None`: never configured; kept but unused when not `enabled`).
pub ssh: Option<DbSsh>,
/// Wire-compatible product preset (`cockroachdb`, `tidb`…; empty: the driver itself).
pub vendor: String,
pub color: Option<DbColor>,
pub comment: String,
/// Connect with [`DataSource::url`] instead of the host / port / database fields.
pub url_only: bool,
pub url: String,
/// PEM files; `~/` is the home directory.
pub ssl_ca_file: String,
pub ssl_cert_file: String,
pub ssl_key_file: String,
pub options: DbOptions,
pub introspection: DbIntrospection,
pub properties: Vec<DbProperty>,
}
impl Default for DataSource {
fn default() -> Self {
Self {
id: String::new(),
name: String::new(),
driver: DbDriver::Sqlite,
host: "localhost".into(),
port: None,
database: String::new(),
user: String::new(),
file: None,
ssl: DbSslMode::Prefer,
save_password: false,
read_only: false,
ssh: None,
vendor: String::new(),
color: None,
comment: String::new(),
url_only: false,
url: String::new(),
ssl_ca_file: String::new(),
ssl_cert_file: String::new(),
ssl_key_file: String::new(),
options: DbOptions::default(),
introspection: DbIntrospection::default(),
properties: Vec::new(),
}
}
}
/// The connection settings of a URL (`jdbc:` prefix allowed).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedUrl {
pub driver: Option<DbDriver>,
pub host: String,
pub port: Option<u16>,
pub database: String,
pub user: String,
pub password: Option<String>,
/// Query parameters, in order.
pub params: Vec<(String, String)>,
/// SQLite file.
pub file: Option<String>,
}
fn percent_decode(s: &str) -> String {
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
let hex = |i: usize| b.get(i).and_then(|&c| (c as char).to_digit(16));
while i < b.len() {
if b[i] == b'%'
&& let (Some(h), Some(l)) = (hex(i + 1), hex(i + 2))
{
out.push((h * 16 + l) as u8);
i += 3;
continue;
}
out.push(b[i]);
i += 1;
}
String::from_utf8_lossy(&out).into_owned()
}
/// The driver speaking the protocol of URL scheme `scheme` (wire-compatible products included).
pub fn scheme_driver(scheme: &str) -> Option<DbDriver> {
match scheme.to_ascii_lowercase().as_str() {
"postgres" | "postgresql" | "pgsql" | "redshift" | "cockroachdb" | "cockroach" | "yugabytedb"
| "timescaledb" | "greenplum" => Some(DbDriver::Postgres),
"mysql" | "mariadb" | "tidb" | "singlestore" | "memsql" | "oceanbase" => Some(DbDriver::Mysql),
"sqlite" | "sqlite3" => Some(DbDriver::Sqlite),
_ => None,
}
}
/// Parses `scheme://[user[:password]@]host[:port][/database][?k=v&…]`, `jdbc:` forms
/// included, and `sqlite:path`.
pub fn parse_url(raw: &str) -> IdeResult<ParsedUrl> {
let bad = || IdeError::invalid(format!("not a database URL: {raw}"));
let url = raw.trim();
let url = url.strip_prefix("jdbc:").or_else(|| url.strip_prefix("JDBC:")).unwrap_or(url);
let (scheme, rest) = url.split_once(':').ok_or_else(bad)?;
let driver = scheme_driver(scheme).ok_or_else(bad)?;
if driver == DbDriver::Sqlite {
let file = rest.strip_prefix("//").unwrap_or(rest);
let file = file.split('?').next().unwrap_or_default();
if file.is_empty() {
return Err(bad());
}
return Ok(ParsedUrl { driver: Some(driver), file: Some(percent_decode(file)), ..Default::default() });
}
let rest = rest.strip_prefix("//").ok_or_else(bad)?;
let (main, query) = rest.split_once('?').unwrap_or((rest, ""));
let (authority, database) = main.split_once('/').unwrap_or((main, ""));
let (userinfo, hostport) = match authority.rsplit_once('@') {
Some((u, h)) => (Some(u), h),
None => (None, authority),
};
// Several hosts (failover lists): the first.
let hostport = hostport.split(',').next().unwrap_or_default();
let (host, port) = if let Some(v6) = hostport.strip_prefix('[') {
let (h, after) = v6.split_once(']').ok_or_else(bad)?;
(h.to_owned(), after.strip_prefix(':'))
} else {
match hostport.rsplit_once(':') {
Some((h, p)) => (h.to_owned(), Some(p)),
None => (hostport.to_owned(), None),
}
};
let port = match port.filter(|p| !p.is_empty()) {
Some(p) => Some(p.parse::<u16>().map_err(|_| bad())?),
None => None,
};
let mut parsed = ParsedUrl {
driver: Some(driver),
host,
port,
database: percent_decode(database.trim_end_matches('/')),
..Default::default()
};
if let Some(u) = userinfo {
let (user, password) = match u.split_once(':') {
Some((u, p)) => (u, Some(percent_decode(p))),
None => (u, None),
};
parsed.user = percent_decode(user);
parsed.password = password;
}
for pair in query.split('&').filter(|p| !p.is_empty()) {
let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
let (k, v) = (percent_decode(k), percent_decode(v));
match k.as_str() {
"user" if parsed.user.is_empty() => parsed.user = v,
"password" if parsed.password.is_none() => parsed.password = Some(v),
_ => parsed.params.push((k, v)),
}
}
Ok(parsed)
}
impl DataSource {
/// The settings to connect with: with [`DataSource::url_only`], host, port,
/// database, user, SSL and driver properties come from the URL (properties
/// of the Advanced tab still apply after the URL's).
pub fn resolved(&self) -> IdeResult<DataSource> {
if !self.url_only {
return Ok(self.clone());
}
let u = parse_url(&self.url)?;
let mut s = self.clone();
if let Some(d) = u.driver
&& d != s.driver
{
return Err(IdeError::invalid(format!("the URL is not for the {:?} driver", s.driver)));
}
if s.driver == DbDriver::Sqlite {
s.file = u.file.map(IpcPath::from);
return Ok(s);
}
s.host = u.host;
s.port = u.port;
s.database = u.database;
if !u.user.is_empty() {
s.user = u.user;
}
let mut properties = Vec::new();
for (k, v) in u.params {
match k.to_ascii_lowercase().as_str() {
"sslmode" | "ssl-mode" => {
s.ssl = match v.to_ascii_lowercase().replace('-', "_").as_str() {
"disable" | "disabled" => DbSslMode::Disable,
"require" | "required" => DbSslMode::Require,
"verify_ca" => DbSslMode::VerifyCa,
"verify_full" | "verify_identity" => DbSslMode::VerifyFull,
_ => DbSslMode::Prefer,
}
}
"sslrootcert" | "ssl-ca" | "sslca" => s.ssl_ca_file = v,
"sslcert" | "ssl-cert" => s.ssl_cert_file = v,
"sslkey" | "ssl-key" => s.ssl_key_file = v,
_ => properties.push(DbProperty { name: k, value: v }),
}
}
properties.append(&mut s.properties);
s.properties = properties;
Ok(s)
}
/// The password written in the URL of a [`DataSource::url_only`] data source.
pub fn url_password(&self) -> Option<String> {
if !self.url_only {
return None;
}
parse_url(&self.url).ok()?.password
}
}
#[derive(Debug, Default, Serialize, Deserialize)]
struct SourcesFile {
#[serde(default, rename = "data-source")]
sources: Vec<DataSource>,
}
pub fn load(root: &Path) -> IdeResult<Vec<DataSource>> {
let path = project_dir::path(root, FILE);
let text = match std::fs::read_to_string(&path) {
Ok(t) => t,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(e.into()),
};
let file: SourcesFile = toml::from_str(&text).map_err(|e| IdeError::invalid(format!("{}: {e}", path.display())))?;
Ok(file.sources)
}
pub fn save(root: &Path, sources: &[DataSource]) -> IdeResult<()> {
for s in sources {
if s.id.is_empty() || s.name.trim().is_empty() {
return Err(IdeError::invalid("every data source needs an id and a name"));
}
}
let path = project_dir::path(root, FILE);
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
let text = toml::to_string_pretty(&SourcesFile { sources: sources.to_vec() })
.map_err(|e| IdeError::internal(e.to_string()))?;
std::fs::write(path, text)?;
Ok(())
}
/// Credential store access. Blocking (may show an OS prompt): call off the async runtime.
/// Keys are data source ids; [`secrets::ssh_key`] names the SSH password / passphrase.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_without_passwords() {
let tmp = tempfile::tempdir().unwrap();
assert!(load(tmp.path()).unwrap().is_empty());
let s = DataSource {
id: "a1".into(),
name: "local".into(),
driver: DbDriver::Postgres,
port: Some(5433),
database: "app".into(),
user: "me".into(),
save_password: true,
..Default::default()
};
save(tmp.path(), std::slice::from_ref(&s)).unwrap();
let text = std::fs::read_to_string(tmp.path().join(".openidea/data-sources.toml")).unwrap();
assert!(!text.contains("password ="), "{text}");
assert_eq!(load(tmp.path()).unwrap(), vec![s]);
assert!(save(tmp.path(), &[DataSource::default()]).is_err());
}
#[test]
fn reads_configs_written_before_the_new_settings() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join(".openidea");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join(FILE),
"[[data-source]]\nid = \"a\"\nname = \"old\"\ndriver = \"postgres\"\nhost = \"db\"\nssl = \"require\"\n",
)
.unwrap();
let s = load(tmp.path()).unwrap().remove(0);
assert_eq!(s.ssl, DbSslMode::Require);
assert!(s.introspection.all_databases && s.introspection.all_schemas);
assert_eq!((s.color, s.url_only, s.options.transaction), (None, false, DbTxMode::Auto));
}
#[test]
fn round_trips_the_new_settings() {
let tmp = tempfile::tempdir().unwrap();
let s = DataSource {
id: "b".into(),
name: "prod".into(),
driver: DbDriver::Mysql,
vendor: "tidb".into(),
color: Some(DbColor::Red),
comment: "careful".into(),
ssl: DbSslMode::VerifyFull,
ssl_ca_file: "~/ca.pem".into(),
options: DbOptions { time_zone: "UTC".into(), keep_alive_secs: Some(60), ..Default::default() },
introspection: DbIntrospection { all_schemas: false, schemas: vec!["app*".into()], ..Default::default() },
properties: vec![DbProperty { name: "sql_mode".into(), value: "ANSI".into() }],
..Default::default()
};
save(tmp.path(), std::slice::from_ref(&s)).unwrap();
assert_eq!(load(tmp.path()).unwrap(), vec![s]);
}
#[test]
fn parses_urls() {
let u = parse_url("jdbc:postgresql://ann:s%[email protected]:5433/shop?sslmode=require&search_path=app").unwrap();
assert_eq!(u.driver, Some(DbDriver::Postgres));
assert_eq!((u.host.as_str(), u.port, u.database.as_str()), ("db.local", Some(5433), "shop"));
assert_eq!((u.user.as_str(), u.password.as_deref()), ("ann", Some("s@cret")));
assert_eq!(u.params, vec![("sslmode".into(), "require".into()), ("search_path".into(), "app".into())]);
let u = parse_url("mysql://[::1]/app?user=root").unwrap();
assert_eq!((u.host.as_str(), u.port, u.user.as_str()), ("::1", None, "root"));
assert_eq!(parse_url("jdbc:redshift://r:5439/dev").unwrap().driver, Some(DbDriver::Postgres));
assert_eq!(parse_url("tidb://h:4000").unwrap().driver, Some(DbDriver::Mysql));
assert_eq!(parse_url("sqlite:/tmp/a.db").unwrap().file.as_deref(), Some("/tmp/a.db"));
assert!(parse_url("oracle://x").is_err());
assert!(parse_url("postgres://h:99999/db").is_err());
}
#[test]
fn url_only_sources_connect_with_the_url() {
let s = DataSource {
driver: DbDriver::Postgres,
user: "fallback".into(),
url_only: true,
url: "postgres://ann:pw@h:6543/app?sslmode=verify-full&sslrootcert=/ca.pem&statement_timeout=5000".into(),
properties: vec![DbProperty { name: "work_mem".into(), value: "64MB".into() }],
..Default::default()
};
let r = s.resolved().unwrap();
assert_eq!((r.host.as_str(), r.port, r.database.as_str(), r.user.as_str()), ("h", Some(6543), "app", "ann"));
assert_eq!((r.ssl, r.ssl_ca_file.as_str()), (DbSslMode::VerifyFull, "/ca.pem"));
let names: Vec<_> = r.properties.iter().map(|p| p.name.as_str()).collect();
assert_eq!(names, ["statement_timeout", "work_mem"]);
assert_eq!(s.url_password().as_deref(), Some("pw"));
assert!(DataSource { url: "mysql://h/db".into(), ..s.clone() }.resolved().is_err());
assert_eq!(DataSource { url_only: false, ..s }.resolved().unwrap().host, "localhost");
}
}
+269
Переглянути файл
@@ -0,0 +1,269 @@
//! Splitting SQL scripts into statements (`;`-terminated), aware of string
//! literals, quoted identifiers, comments and PostgreSQL dollar quoting.
//! Offsets are UTF-16 code units, like everything on the IPC boundary.
use crate::DbDriver;
use serde::{Deserialize, Serialize};
use specta::Type;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct SqlStatement {
/// UTF-16 offsets of the statement text (whitespace trimmed, `;` excluded).
pub from: u32,
pub to: u32,
pub text: String,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum State {
Code,
Single,
Double,
Backtick,
LineComment,
BlockComment,
}
/// Every statement of `text` that contains more than whitespace and comments.
pub fn split(text: &str, driver: DbDriver) -> Vec<SqlStatement> {
let mysql = driver == DbDriver::Mysql;
let chars: Vec<(usize, char)> = text.char_indices().collect();
// UTF-16 offset of each char index (plus the end).
let mut u16_at = Vec::with_capacity(chars.len() + 1);
let mut u = 0u32;
for &(_, c) in &chars {
u16_at.push(u);
u += c.len_utf16() as u32;
}
u16_at.push(u);
let byte_at = |i: usize| chars.get(i).map_or(text.len(), |c| c.0);
let mut out = Vec::new();
let mut state = State::Code;
let mut start = 0usize;
// Whether the current segment has anything but whitespace / comments.
let mut has_code = false;
let mut i = 0usize;
let push = |out: &mut Vec<SqlStatement>, s: usize, e: usize, has_code: bool| {
if !has_code {
return;
}
let seg = &text[byte_at(s)..byte_at(e)];
let lead = seg.len() - seg.trim_start().len();
let trimmed = seg.trim();
if trimmed.is_empty() {
return;
}
let lead_chars = seg[..lead].chars().count();
let len_chars = trimmed.chars().count();
out.push(SqlStatement {
from: u16_at[s + lead_chars],
to: u16_at[s + lead_chars + len_chars],
text: trimmed.to_owned(),
});
};
while i < chars.len() {
let c = chars[i].1;
let next = chars.get(i + 1).map(|c| c.1);
match state {
State::Code => match c {
';' => {
push(&mut out, start, i, has_code);
start = i + 1;
has_code = false;
}
'\'' => {
state = State::Single;
has_code = true;
}
'"' => {
state = State::Double;
has_code = true;
}
'`' if mysql => {
state = State::Backtick;
has_code = true;
}
'-' if next == Some('-') => {
state = State::LineComment;
i += 1;
}
'#' if mysql => state = State::LineComment,
'/' if next == Some('*') => {
state = State::BlockComment;
i += 1;
}
'$' if driver == DbDriver::Postgres => {
// `$tag$ … $tag$` (tag may be empty); `$1` parameters are not quotes.
let rest = &text[byte_at(i)..];
let tag_len = rest[1..].find('$').filter(|&n| {
rest[1..1 + n].chars().all(|c| c.is_alphanumeric() || c == '_')
&& !rest[1..1 + n].starts_with(|c: char| c.is_ascii_digit())
});
has_code = true;
if let Some(n) = tag_len {
let tag = &rest[..n + 2];
let body_start = byte_at(i) + tag.len();
let close = text[body_start..].find(tag).map_or(text.len(), |p| body_start + p + tag.len());
// Jump to the char at byte `close`.
while i < chars.len() && chars[i].0 < close {
i += 1;
}
continue;
}
}
c if !c.is_whitespace() => has_code = true,
_ => {}
},
State::Single => {
if mysql && c == '\\' {
i += 1;
} else if c == '\'' {
if next == Some('\'') {
i += 1;
} else {
state = State::Code;
}
}
}
State::Double => {
if c == '"' {
if next == Some('"') {
i += 1;
} else {
state = State::Code;
}
}
}
State::Backtick => {
if c == '`' {
state = State::Code;
}
}
State::LineComment => {
if c == '\n' {
state = State::Code;
}
}
State::BlockComment => {
if c == '*' && next == Some('/') {
state = State::Code;
i += 1;
}
}
}
i += 1;
}
push(&mut out, start, chars.len(), has_code);
out
}
/// The statement to run for a caret at `offset` (UTF-16): the one containing
/// it, else the nearest one before it (caret after `;` or on a blank line
/// below), else the next one.
pub fn statement_at(text: &str, offset: u32, driver: DbDriver) -> Option<SqlStatement> {
let all = split(text, driver);
if let Some(s) = all.iter().find(|s| s.from <= offset && offset <= s.to) {
return Some(s.clone());
}
// Between statements: the previous one when only whitespace / `;` separates it from the caret.
let prev = all.iter().rev().find(|s| s.to <= offset);
let next = all.iter().find(|s| s.from > offset);
match (prev, next) {
(Some(p), Some(n)) => {
let gap = utf16_slice(text, p.to, offset);
let blank_lines = gap.matches('\n').count();
if blank_lines <= 1 { Some(p.clone()) } else { Some(n.clone()) }
}
(Some(p), None) => Some(p.clone()),
(None, n) => n.cloned(),
}
}
/// Statements overlapping the UTF-16 range `from..to` (a selection): the
/// selected text itself, split.
pub fn statements_in(text: &str, from: u32, to: u32, driver: DbDriver) -> Vec<SqlStatement> {
let selected = utf16_slice(text, from, to);
split(selected, driver).into_iter().map(|s| SqlStatement { from: s.from + from, to: s.to + from, ..s }).collect()
}
fn utf16_slice(text: &str, from: u32, to: u32) -> &str {
let mut u = 0u32;
let (mut a, mut b) = (text.len(), text.len());
for (i, c) in text.char_indices() {
if u == from && a == text.len() {
a = i;
}
if u == to {
b = i;
break;
}
u += c.len_utf16() as u32;
}
if a > b { "" } else { &text[a..b] }
}
#[cfg(test)]
mod tests {
use super::*;
fn texts(sql: &str, d: DbDriver) -> Vec<String> {
split(sql, d).into_iter().map(|s| s.text).collect()
}
#[test]
fn splits_on_semicolons_outside_literals_and_comments() {
let sql = "select 'a;b' as x; -- c;d\nselect \"x;y\" from t /* ; */;\n\n-- only a comment;\n";
assert_eq!(texts(sql, DbDriver::Sqlite), vec!["select 'a;b' as x", "-- c;d\nselect \"x;y\" from t /* ; */"]);
}
#[test]
fn mysql_backticks_hashes_and_backslashes() {
let sql = "select `a;b`, 'it\\'s;' from t # x;y\n; select 2";
assert_eq!(texts(sql, DbDriver::Mysql), vec!["select `a;b`, 'it\\'s;' from t # x;y", "select 2"]);
}
#[test]
fn postgres_dollar_quotes() {
let sql =
"create function f() returns int as $$ select 1; $$ language sql; select $1::int; do $x$ begin; end $x$";
assert_eq!(
texts(sql, DbDriver::Postgres),
vec![
"create function f() returns int as $$ select 1; $$ language sql",
"select $1::int",
"do $x$ begin; end $x$"
]
);
}
#[test]
fn utf16_offsets() {
let sql = "select '😀';\nselect 2;";
let s = split(sql, DbDriver::Sqlite);
assert_eq!((s[0].from, s[0].to), (0, 11));
assert_eq!((s[1].from, s[1].to), (13, 21));
}
#[test]
fn statement_at_caret() {
let sql = "select 1;\nselect 2;\n\n\nselect 3";
let at = |o| statement_at(sql, o, DbDriver::Sqlite).map(|s| s.text);
assert_eq!(at(0).as_deref(), Some("select 1"));
assert_eq!(at(9).as_deref(), Some("select 1"), "right after ;");
assert_eq!(at(12).as_deref(), Some("select 2"));
assert_eq!(at(20).as_deref(), Some("select 2"), "end of line after ;");
assert_eq!(at(21).as_deref(), Some("select 3"), "after blank lines, the next one");
assert_eq!(at(30).as_deref(), Some("select 3"));
assert_eq!(statement_at(" ", 1, DbDriver::Sqlite), None);
}
#[test]
fn selection() {
let sql = "x; select 1; select 2; y";
let s = statements_in(sql, 3, 21, DbDriver::Sqlite);
assert_eq!(s.iter().map(|s| s.text.as_str()).collect::<Vec<_>>(), vec!["select 1", "select 2"]);
assert_eq!((s[1].from, s[1].to), (13, 21));
}
}
+636
Переглянути файл
@@ -0,0 +1,636 @@
//! SQL text generation per dialect: quoting, introspection queries, table
//! pages and row edits. Pure functions, so they are unit-tested without a server.
use crate::DbDriver;
use serde::{Deserialize, Serialize};
use specta::Type;
pub fn quote_ident(driver: DbDriver, name: &str) -> String {
match driver {
DbDriver::Mysql => format!("`{}`", name.replace('`', "``")),
_ => format!("\"{}\"", name.replace('"', "\"\"")),
}
}
pub fn quote_literal(driver: DbDriver, value: &str) -> String {
let mut s = value.replace('\'', "''");
if driver == DbDriver::Mysql {
s = s.replace('\\', "\\\\");
}
format!("'{s}'")
}
fn literal_or_null(driver: DbDriver, v: Option<&str>) -> String {
v.map_or_else(|| "NULL".to_owned(), |v| quote_literal(driver, v))
}
pub fn qualified(driver: DbDriver, schema: &str, table: &str) -> String {
if schema.is_empty() {
quote_ident(driver, table)
} else {
format!("{}.{}", quote_ident(driver, schema), quote_ident(driver, table))
}
}
/// PostgreSQL databases one can connect to; template databases only with `templates`.
pub fn databases(templates: bool) -> &'static str {
if templates {
"SELECT datname FROM pg_database WHERE datallowconn ORDER BY datname"
} else {
"SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn ORDER BY datname"
}
}
/// Databases of managed services that their users cannot use (hidden unless connected to).
pub const INTERNAL_DATABASES: &[&str] = &["rdsadmin", "azure_maintenance", "azure_sys", "cloudsqladmin"];
pub fn schemas(driver: DbDriver, system: bool) -> &'static str {
match driver {
DbDriver::Sqlite => "SELECT name FROM pragma_database_list WHERE name <> 'temp' ORDER BY seq",
DbDriver::Postgres if system => {
"SELECT schema_name FROM information_schema.schemata \
WHERE schema_name NOT LIKE 'pg\\_toast%' AND schema_name NOT LIKE 'pg\\_temp%' ORDER BY 1"
}
DbDriver::Mysql if system => "SELECT schema_name FROM information_schema.schemata ORDER BY 1",
DbDriver::Postgres => {
"SELECT schema_name FROM information_schema.schemata \
WHERE schema_name NOT IN ('pg_catalog', 'information_schema') \
AND schema_name NOT LIKE 'pg\\_toast%' AND schema_name NOT LIKE 'pg\\_temp%' ORDER BY 1"
}
DbDriver::Mysql => {
"SELECT schema_name FROM information_schema.schemata \
WHERE schema_name NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys') ORDER BY 1"
}
}
}
/// Rows: `name, kind` where kind is `table`, `view` or `materialized view`.
pub fn tables(driver: DbDriver, schema: &str) -> String {
match driver {
DbDriver::Sqlite => format!(
"SELECT name, type FROM {}.sqlite_master WHERE type IN ('table', 'view') \
AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\' ORDER BY name",
quote_ident(driver, schema)
),
DbDriver::Postgres => format!(
"SELECT c.relname, CASE c.relkind WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' ELSE 'table' END \
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace \
WHERE n.nspname = {} AND c.relkind IN ('r', 'v', 'm', 'p', 'f') ORDER BY 1",
quote_literal(driver, schema)
),
DbDriver::Mysql => format!(
"SELECT table_name, CASE WHEN table_type LIKE '%VIEW%' THEN 'view' ELSE 'table' END \
FROM information_schema.tables WHERE table_schema = {} ORDER BY 1",
quote_literal(driver, schema)
),
}
}
/// Rows: `name, type, nullable (YES/NO or 0/1), default, is_pk (0/1)`.
pub fn columns(driver: DbDriver, schema: &str, table: &str) -> String {
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
match driver {
DbDriver::Sqlite => format!(
"SELECT name, type, CASE WHEN \"notnull\" = 0 THEN 'YES' ELSE 'NO' END, dflt_value, \
CASE WHEN pk > 0 THEN 1 ELSE 0 END FROM pragma_table_info({t}, {s}) ORDER BY cid"
),
DbDriver::Postgres => format!(
"SELECT c.column_name, c.data_type, c.is_nullable, c.column_default, \
CASE WHEN EXISTS (SELECT 1 FROM information_schema.table_constraints tc \
JOIN information_schema.key_column_usage k ON k.constraint_name = tc.constraint_name \
AND k.table_schema = tc.table_schema AND k.table_name = tc.table_name \
WHERE tc.constraint_type = 'PRIMARY KEY' AND tc.table_schema = c.table_schema \
AND tc.table_name = c.table_name AND k.column_name = c.column_name) THEN 1 ELSE 0 END \
FROM information_schema.columns c WHERE c.table_schema = {s} AND c.table_name = {t} \
ORDER BY c.ordinal_position"
),
DbDriver::Mysql => format!(
"SELECT column_name, column_type, is_nullable, column_default, \
CASE WHEN column_key = 'PRI' THEN 1 ELSE 0 END \
FROM information_schema.columns WHERE table_schema = {s} AND table_name = {t} \
ORDER BY ordinal_position"
),
}
}
/// Rows: `index name, unique (0/1), column` (one row per indexed column, in order).
pub fn indexes(driver: DbDriver, schema: &str, table: &str) -> String {
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
match driver {
DbDriver::Sqlite => format!(
"SELECT il.name, il.\"unique\", ii.name FROM pragma_index_list({t}, {s}) il \
JOIN pragma_index_info(il.name, {s}) ii ORDER BY il.name, ii.seqno"
),
DbDriver::Postgres => format!(
"SELECT i.relname, CASE WHEN ix.indisunique THEN 1 ELSE 0 END, a.attname \
FROM pg_index ix JOIN pg_class t ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid \
JOIN pg_namespace n ON n.oid = t.relnamespace \
JOIN LATERAL unnest(ix.indkey) WITH ORDINALITY AS k(attnum, ord) ON true \
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum \
WHERE n.nspname = {s} AND t.relname = {t} ORDER BY i.relname, k.ord"
),
DbDriver::Mysql => format!(
"SELECT index_name, CASE WHEN non_unique = 0 THEN 1 ELSE 0 END, column_name \
FROM information_schema.statistics WHERE table_schema = {s} AND table_name = {t} \
ORDER BY index_name, seq_in_index"
),
}
}
/// Rows: `schema, table, kind (table/view), column, type, nullable (YES/NO), is_pk (0/1), has_default (0/1)`
/// for SQL completion, grouped by table in column order.
pub fn completion_columns(driver: DbDriver) -> &'static str {
match driver {
DbDriver::Sqlite => {
"SELECT 'main', m.name, m.type, p.name, p.type, CASE WHEN p.\"notnull\" = 0 THEN 'YES' ELSE 'NO' END, \
CASE WHEN p.pk > 0 THEN 1 ELSE 0 END, \
CASE WHEN p.dflt_value IS NOT NULL THEN 1 \
WHEN p.pk = 1 AND upper(p.type) = 'INTEGER' \
AND (SELECT count(*) FROM pragma_table_info(m.name) k WHERE k.pk > 0) = 1 THEN 1 ELSE 0 END \
FROM sqlite_master m JOIN pragma_table_info(m.name) p \
WHERE m.type IN ('table', 'view') AND m.name NOT LIKE 'sqlite\\_%' ESCAPE '\\' ORDER BY m.name, p.cid"
}
DbDriver::Postgres => {
"SELECT n.nspname, c.relname, CASE c.relkind WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' ELSE 'table' END, a.attname, \
format_type(a.atttypid, a.atttypmod), CASE WHEN a.attnotnull THEN 'NO' ELSE 'YES' END, \
CASE WHEN EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisprimary \
AND a.attnum = ANY(i.indkey)) THEN 1 ELSE 0 END, \
CASE WHEN a.atthasdef OR a.attidentity <> '' THEN 1 ELSE 0 END \
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_attribute a ON a.attrelid = c.oid \
WHERE c.relkind IN ('r', 'v', 'm', 'p', 'f') AND a.attnum > 0 AND NOT a.attisdropped \
AND n.nspname NOT IN ('pg_catalog', 'information_schema') \
AND n.nspname NOT LIKE 'pg\\_toast%' AND n.nspname NOT LIKE 'pg\\_temp%' \
ORDER BY n.nspname, c.relname, a.attnum LIMIT 50000"
}
DbDriver::Mysql => {
"SELECT c.table_schema, c.table_name, CASE WHEN t.table_type LIKE '%VIEW%' THEN 'view' ELSE 'table' END, \
c.column_name, c.column_type, c.is_nullable, CASE WHEN c.column_key = 'PRI' THEN 1 ELSE 0 END, \
CASE WHEN c.column_default IS NOT NULL OR c.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END \
FROM information_schema.columns c JOIN information_schema.tables t \
ON t.table_schema = c.table_schema AND t.table_name = c.table_name \
WHERE c.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys') \
ORDER BY c.table_schema, c.table_name, c.ordinal_position LIMIT 50000"
}
}
}
/// Rows: `schema, table, constraint, column, ref schema, ref table, ref column`
/// (one row per column pair, in key order). SQLite leaves the ref column NULL
/// when the key references the primary key implicitly.
pub fn foreign_keys(driver: DbDriver) -> &'static str {
match driver {
DbDriver::Sqlite => {
"SELECT 'main', m.name, f.id, f.\"from\", 'main', f.\"table\", f.\"to\" \
FROM sqlite_master m JOIN pragma_foreign_key_list(m.name) f \
WHERE m.type = 'table' ORDER BY m.name, f.id, f.seq"
}
DbDriver::Postgres => {
"SELECT sn.nspname, sc.relname, con.conname, sa.attname, tn.nspname, tc.relname, ta.attname \
FROM pg_constraint con \
JOIN pg_class sc ON sc.oid = con.conrelid JOIN pg_namespace sn ON sn.oid = sc.relnamespace \
JOIN pg_class tc ON tc.oid = con.confrelid JOIN pg_namespace tn ON tn.oid = tc.relnamespace \
CROSS JOIN LATERAL unnest(con.conkey, con.confkey) WITH ORDINALITY AS k(s, t, ord) \
JOIN pg_attribute sa ON sa.attrelid = sc.oid AND sa.attnum = k.s \
JOIN pg_attribute ta ON ta.attrelid = tc.oid AND ta.attnum = k.t \
WHERE con.contype = 'f' AND sn.nspname NOT IN ('pg_catalog', 'information_schema') \
ORDER BY 1, 2, 3, k.ord LIMIT 20000"
}
DbDriver::Mysql => {
"SELECT table_schema, table_name, constraint_name, column_name, \
referenced_table_schema, referenced_table_name, referenced_column_name \
FROM information_schema.key_column_usage WHERE referenced_table_name IS NOT NULL \
AND table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys') \
ORDER BY table_schema, table_name, constraint_name, ordinal_position LIMIT 20000"
}
}
}
/// [`foreign_keys`] rows of one table.
pub fn table_foreign_keys(driver: DbDriver, schema: &str, table: &str) -> String {
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
match driver {
DbDriver::Sqlite => format!(
"SELECT {s}, {t}, f.id, f.\"from\", {s}, f.\"table\", f.\"to\" FROM pragma_foreign_key_list({t}, {s}) f ORDER BY f.id, f.seq"
),
DbDriver::Postgres => format!(
"SELECT sn.nspname, sc.relname, con.conname, sa.attname, tn.nspname, tc.relname, ta.attname FROM pg_constraint con JOIN pg_class sc ON sc.oid = con.conrelid JOIN pg_namespace sn ON sn.oid = sc.relnamespace JOIN pg_class tc ON tc.oid = con.confrelid JOIN pg_namespace tn ON tn.oid = tc.relnamespace CROSS JOIN LATERAL unnest(con.conkey, con.confkey) WITH ORDINALITY AS k(s, t, ord) JOIN pg_attribute sa ON sa.attrelid = sc.oid AND sa.attnum = k.s JOIN pg_attribute ta ON ta.attrelid = tc.oid AND ta.attnum = k.t WHERE con.contype = 'f' AND sn.nspname = {s} AND sc.relname = {t} ORDER BY 3, k.ord"
),
DbDriver::Mysql => format!(
"SELECT table_schema, table_name, constraint_name, column_name, referenced_table_schema, referenced_table_name, referenced_column_name FROM information_schema.key_column_usage WHERE referenced_table_name IS NOT NULL AND table_schema = {s} AND table_name = {t} ORDER BY constraint_name, ordinal_position"
),
}
}
/// The schema unqualified names resolve to (`None`: always `main`).
pub fn current_schema(driver: DbDriver) -> Option<&'static str> {
match driver {
DbDriver::Sqlite => None,
DbDriver::Postgres => Some("SELECT current_schema()"),
DbDriver::Mysql => Some("SELECT DATABASE()"),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "DbSortKey")]
pub struct SortKey {
pub column: String,
pub descending: bool,
}
/// One page of a table: `limit + 1` rows so the caller knows whether more exist.
pub fn table_page(
driver: DbDriver,
schema: &str,
table: &str,
filter: Option<&str>,
sort: &[SortKey],
offset: u64,
limit: u64,
) -> String {
let mut sql = format!("SELECT * FROM {}", qualified(driver, schema, table));
if let Some(f) = filter.map(str::trim).filter(|f| !f.is_empty()) {
let f = f.strip_prefix("WHERE ").or_else(|| f.strip_prefix("where ")).unwrap_or(f);
sql.push_str(&format!(" WHERE {f}"));
}
if !sort.is_empty() {
let keys: Vec<String> = sort
.iter()
.map(|k| format!("{}{}", quote_ident(driver, &k.column), if k.descending { " DESC" } else { "" }))
.collect();
sql.push_str(&format!(" ORDER BY {}", keys.join(", ")));
}
sql.push_str(&format!(" LIMIT {} OFFSET {offset}", limit + 1));
sql
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "DbCellValue")]
pub struct CellValue {
pub column: String,
/// `None` is SQL NULL.
pub value: Option<String>,
}
/// A pending change of the table editor. `key` identifies the row: its primary
/// key columns with their original values.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum DbRowEdit {
Update { key: Vec<CellValue>, values: Vec<CellValue> },
Insert { values: Vec<CellValue> },
Delete { key: Vec<CellValue> },
}
fn where_key(driver: DbDriver, key: &[CellValue]) -> String {
key.iter()
.map(|c| match &c.value {
Some(v) => format!("{} = {}", quote_ident(driver, &c.column), quote_literal(driver, v)),
None => format!("{} IS NULL", quote_ident(driver, &c.column)),
})
.collect::<Vec<_>>()
.join(" AND ")
}
/// The DML statement for `edit`. Values are written as quoted literals, which
/// every supported database coerces to the column type.
pub fn edit_statement(driver: DbDriver, schema: &str, table: &str, edit: &DbRowEdit) -> Result<String, String> {
let target = qualified(driver, schema, table);
match edit {
DbRowEdit::Update { key, values } => {
if key.is_empty() {
return Err("rows can only be updated in tables with a primary key".into());
}
if values.is_empty() {
return Err("nothing to update".into());
}
let set: Vec<String> = values
.iter()
.map(|c| {
format!("{} = {}", quote_ident(driver, &c.column), literal_or_null(driver, c.value.as_deref()))
})
.collect();
Ok(format!("UPDATE {target} SET {} WHERE {}", set.join(", "), where_key(driver, key)))
}
DbRowEdit::Insert { values } => {
if values.is_empty() {
return Ok(match driver {
DbDriver::Mysql => format!("INSERT INTO {target} () VALUES ()"),
_ => format!("INSERT INTO {target} DEFAULT VALUES"),
});
}
let cols: Vec<String> = values.iter().map(|c| quote_ident(driver, &c.column)).collect();
let vals: Vec<String> = values.iter().map(|c| literal_or_null(driver, c.value.as_deref())).collect();
Ok(format!("INSERT INTO {target} ({}) VALUES ({})", cols.join(", "), vals.join(", ")))
}
DbRowEdit::Delete { key } => {
if key.is_empty() {
return Err("rows can only be deleted in tables with a primary key".into());
}
Ok(format!("DELETE FROM {target} WHERE {}", where_key(driver, key)))
}
}
}
/// Rows: `sql` — the statements that created a SQLite table or view, then its indexes and triggers.
pub fn sqlite_ddl(schema: &str, name: &str) -> String {
format!(
"SELECT sql FROM {}.sqlite_master WHERE tbl_name = {} AND sql IS NOT NULL \
ORDER BY CASE type WHEN 'index' THEN 1 WHEN 'trigger' THEN 2 ELSE 0 END, name",
quote_ident(DbDriver::Sqlite, schema),
quote_literal(DbDriver::Sqlite, name)
)
}
/// Rows: `oid, relkind` of a PostgreSQL relation.
pub fn postgres_relation(schema: &str, name: &str) -> String {
format!(
"SELECT c.oid::text, c.relkind::text FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace \
WHERE n.nspname = {} AND c.relname = {}",
quote_literal(DbDriver::Postgres, schema),
quote_literal(DbDriver::Postgres, name)
)
}
/// Rows: `name, type, not null (0/1), default` of relation `oid`.
pub fn postgres_ddl_columns(oid: u32) -> String {
format!(
"SELECT a.attname, format_type(a.atttypid, a.atttypmod), CASE WHEN a.attnotnull THEN 1 ELSE 0 END, \
pg_get_expr(d.adbin, d.adrelid) FROM pg_attribute a \
LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum \
WHERE a.attrelid = {oid} AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum"
)
}
/// Rows: `name, definition` of the constraints of relation `oid` (primary key first).
pub fn postgres_ddl_constraints(oid: u32) -> String {
format!(
"SELECT conname, pg_get_constraintdef(oid, true) FROM pg_constraint WHERE conrelid = {oid} \
ORDER BY CASE contype WHEN 'p' THEN 0 WHEN 'u' THEN 1 WHEN 'c' THEN 2 WHEN 'f' THEN 3 ELSE 4 END, conname"
)
}
/// Rows: `CREATE INDEX …` of relation `oid`, except the indexes behind constraints.
pub fn postgres_ddl_indexes(oid: u32) -> String {
format!(
"SELECT pg_get_indexdef(i.indexrelid) FROM pg_index i WHERE i.indrelid = {oid} \
AND NOT EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = i.indexrelid AND c.conrelid = i.indrelid) \
ORDER BY 1"
)
}
/// Rows: `name, timing, events` of the triggers of a table (SQLite: `name, sql`).
pub fn triggers(driver: DbDriver, schema: &str, table: &str) -> String {
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
match driver {
DbDriver::Sqlite => format!(
"SELECT name, sql FROM {}.sqlite_master WHERE type = 'trigger' AND tbl_name = {t} ORDER BY name",
quote_ident(driver, schema)
),
DbDriver::Postgres => format!(
"SELECT t.tgname, CASE WHEN t.tgtype & 2 <> 0 THEN 'before' WHEN t.tgtype & 64 <> 0 THEN 'instead of' \
ELSE 'after' END, concat_ws(' or ', CASE WHEN t.tgtype & 4 <> 0 THEN 'insert' END, \
CASE WHEN t.tgtype & 16 <> 0 THEN 'update' END, CASE WHEN t.tgtype & 8 <> 0 THEN 'delete' END, \
CASE WHEN t.tgtype & 32 <> 0 THEN 'truncate' END) \
FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid JOIN pg_namespace n ON n.oid = c.relnamespace \
WHERE NOT t.tgisinternal AND n.nspname = {s} AND c.relname = {t} ORDER BY 1"
),
DbDriver::Mysql => format!(
"SELECT trigger_name, lower(action_timing), lower(event_manipulation) FROM information_schema.triggers \
WHERE event_object_schema = {s} AND event_object_table = {t} ORDER BY 1"
),
}
}
/// `before`, `insert` of SQLite's `CREATE TRIGGER name BEFORE INSERT ON …` (its catalog keeps only the text).
pub fn sqlite_trigger_event(sql: &str) -> (String, String) {
let upper = sql.to_ascii_uppercase();
let words: Vec<&str> = upper.split_whitespace().collect();
let timing = if words.contains(&"BEFORE") {
"before"
} else if words.windows(2).any(|w| w == ["INSTEAD", "OF"]) {
"instead of"
} else {
"after"
};
let event = ["INSERT", "UPDATE", "DELETE"].into_iter().find(|e| words.contains(e)).unwrap_or_default();
(timing.to_owned(), event.to_ascii_lowercase())
}
/// Rows: `name, kind (function/procedure), arguments, result` of a schema's routines (none in SQLite).
pub fn routines(driver: DbDriver, schema: &str) -> Option<String> {
let s = quote_literal(driver, schema);
match driver {
DbDriver::Sqlite => None,
// Functions of extensions are left out, as in IDEA.
DbDriver::Postgres => Some(format!(
"SELECT p.proname, CASE WHEN p.prokind = 'p' THEN 'procedure' ELSE 'function' END, \
pg_get_function_identity_arguments(p.oid), pg_get_function_result(p.oid) \
FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace \
WHERE n.nspname = {s} AND p.prokind IN ('f', 'p') \
AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.objid = p.oid AND d.deptype = 'e') ORDER BY 1, 3"
)),
DbDriver::Mysql => Some(format!(
"SELECT r.routine_name, lower(r.routine_type), \
(SELECT GROUP_CONCAT(CONCAT(p.parameter_name, ' ', p.dtd_identifier) ORDER BY p.ordinal_position \
SEPARATOR ', ') FROM information_schema.parameters p WHERE p.specific_schema = r.routine_schema \
AND p.specific_name = r.specific_name AND p.ordinal_position > 0), r.dtd_identifier \
FROM information_schema.routines r WHERE r.routine_schema = {s} ORDER BY 1"
)),
}
}
/// Rows: `name, data type` of a schema's sequences (PostgreSQL).
pub fn sequences(driver: DbDriver, schema: &str) -> Option<String> {
(driver == DbDriver::Postgres).then(|| {
format!(
"SELECT sequence_name, data_type FROM information_schema.sequences WHERE sequence_schema = {} ORDER BY 1",
quote_literal(driver, schema)
)
})
}
/// The statement reading the definition of a routine (a row per overload), trigger
/// or sequence, and the column that holds it.
pub fn object_ddl(driver: DbDriver, schema: &str, kind: crate::DbObjectKind, name: &str) -> Option<(String, usize)> {
use crate::DbObjectKind as K;
let (s, n) = (quote_literal(driver, schema), quote_literal(driver, name));
let target = qualified(driver, schema, name);
Some(match (driver, kind) {
(DbDriver::Sqlite, K::Trigger) => (
format!(
"SELECT sql FROM {}.sqlite_master WHERE type = 'trigger' AND name = {n}",
quote_ident(driver, schema)
),
0,
),
(DbDriver::Postgres, K::Function | K::Procedure) => (
format!(
"SELECT pg_get_functiondef(p.oid) FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace \
WHERE n.nspname = {s} AND p.proname = {n} ORDER BY pg_get_function_identity_arguments(p.oid)"
),
0,
),
(DbDriver::Postgres, K::Trigger) => (
format!(
"SELECT pg_get_triggerdef(t.oid, true) FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid \
JOIN pg_namespace n ON n.oid = c.relnamespace \
WHERE NOT t.tgisinternal AND n.nspname = {s} AND t.tgname = {n}"
),
0,
),
(DbDriver::Postgres, K::Sequence) => (
format!(
"SELECT format(E'create sequence %s\\n as %s\\n increment by %s\\n minvalue %s\\n \
maxvalue %s\\n start with %s%s', {}, data_type, increment_by, min_value, max_value, start_value, \
CASE WHEN cycle THEN E'\\n cycle' ELSE '' END) \
FROM pg_sequences WHERE schemaname = {s} AND sequencename = {n}",
quote_literal(driver, &target)
),
0,
),
(DbDriver::Mysql, K::Function) => (format!("SHOW CREATE FUNCTION {target}"), 2),
(DbDriver::Mysql, K::Procedure) => (format!("SHOW CREATE PROCEDURE {target}"), 2),
(DbDriver::Mysql, K::Trigger) => (format!("SHOW CREATE TRIGGER {target}"), 2),
_ => return None,
})
}
/// A column of [`postgres_table_ddl`].
pub struct DdlColumn {
pub name: String,
pub data_type: String,
pub not_null: bool,
pub default: Option<String>,
}
/// `create table` of a PostgreSQL table from its catalog (the server has no `SHOW CREATE`).
pub fn postgres_table_ddl(
schema: &str,
table: &str,
columns: &[DdlColumn],
constraints: &[(String, String)],
indexes: &[String],
) -> String {
let d = DbDriver::Postgres;
let mut items: Vec<String> = columns
.iter()
.map(|c| {
let mut s = format!(" {} {}", quote_ident(d, &c.name), c.data_type);
if let Some(v) = &c.default {
s.push_str(&format!(" default {v}"));
}
if c.not_null {
s.push_str(" not null");
}
s
})
.collect();
items.extend(constraints.iter().map(|(name, def)| format!(" constraint {} {def}", quote_ident(d, name))));
let mut out = format!("create table {}\n(\n{}\n);\n", qualified(d, schema, table), items.join(",\n"));
for i in indexes {
out.push_str(&format!("\n{i};\n"));
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn cv(c: &str, v: Option<&str>) -> CellValue {
CellValue { column: c.into(), value: v.map(Into::into) }
}
#[test]
fn quoting() {
assert_eq!(quote_ident(DbDriver::Postgres, "a\"b"), "\"a\"\"b\"");
assert_eq!(quote_ident(DbDriver::Mysql, "a`b"), "`a``b`");
assert_eq!(quote_literal(DbDriver::Sqlite, "it's \\"), "'it''s \\'");
assert_eq!(quote_literal(DbDriver::Mysql, "it's \\"), "'it''s \\\\'");
}
#[test]
fn pages() {
let sort = [SortKey { column: "id".into(), descending: true }];
assert_eq!(
table_page(DbDriver::Postgres, "public", "users", Some("WHERE age > 3"), &sort, 100, 50),
"SELECT * FROM \"public\".\"users\" WHERE age > 3 ORDER BY \"id\" DESC LIMIT 51 OFFSET 100"
);
assert_eq!(table_page(DbDriver::Mysql, "", "t", Some(" "), &[], 0, 10), "SELECT * FROM `t` LIMIT 11 OFFSET 0");
}
#[test]
fn edits() {
let key = vec![cv("id", Some("1"))];
assert_eq!(
edit_statement(
DbDriver::Sqlite,
"main",
"t",
&DbRowEdit::Update { key: key.clone(), values: vec![cv("name", Some("O'Brien")), cv("note", None)] }
)
.unwrap(),
"UPDATE \"main\".\"t\" SET \"name\" = 'O''Brien', \"note\" = NULL WHERE \"id\" = '1'"
);
assert_eq!(
edit_statement(DbDriver::Mysql, "db", "t", &DbRowEdit::Insert { values: vec![cv("a", Some("x"))] })
.unwrap(),
"INSERT INTO `db`.`t` (`a`) VALUES ('x')"
);
assert_eq!(
edit_statement(DbDriver::Postgres, "s", "t", &DbRowEdit::Delete { key: vec![cv("k", None)] }).unwrap(),
"DELETE FROM \"s\".\"t\" WHERE \"k\" IS NULL"
);
assert!(edit_statement(DbDriver::Postgres, "s", "t", &DbRowEdit::Delete { key: vec![] }).is_err());
}
#[test]
fn sqlite_trigger_events() {
let ev = sqlite_trigger_event;
assert_eq!(
ev("CREATE TRIGGER t BEFORE UPDATE OF a ON x BEGIN SELECT 1; END"),
("before".into(), "update".into())
);
assert_eq!(ev("create trigger t instead of insert on v begin end"), ("instead of".into(), "insert".into()));
assert_eq!(ev("CREATE TRIGGER t DELETE ON x BEGIN END"), ("after".into(), "delete".into()));
}
#[test]
fn schema_objects_per_dialect() {
use crate::DbObjectKind as K;
assert!(routines(DbDriver::Sqlite, "main").is_none());
assert!(sequences(DbDriver::Mysql, "db").is_none());
assert!(routines(DbDriver::Postgres, "public").unwrap().contains("n.nspname = 'public'"));
assert_eq!(
object_ddl(DbDriver::Mysql, "db", K::Procedure, "p"),
Some(("SHOW CREATE PROCEDURE `db`.`p`".into(), 2))
);
assert!(object_ddl(DbDriver::Sqlite, "main", K::Function, "f").is_none());
assert!(object_ddl(DbDriver::Postgres, "s", K::Sequence, "q").unwrap().0.contains("sequencename = 'q'"));
}
#[test]
fn postgres_ddl() {
let cols = [
DdlColumn { name: "id".into(), data_type: "integer".into(), not_null: true, default: None },
DdlColumn {
name: "name".into(),
data_type: "character varying(40)".into(),
not_null: false,
default: Some("'x'::character varying".into()),
},
];
assert_eq!(
postgres_table_ddl(
"public",
"t",
&cols,
&[("t_pkey".into(), "PRIMARY KEY (id)".into())],
&["CREATE INDEX t_name ON public.t USING btree (name)".into()]
),
"create table \"public\".\"t\"\n(\n \"id\" integer not null,\n \"name\" character varying(40) default 'x'::character varying,\n constraint \"t_pkey\" PRIMARY KEY (id)\n);\n\nCREATE INDEX t_name ON public.t USING btree (name);\n"
);
}
}
+320
Переглянути файл
@@ -0,0 +1,320 @@
//! Introspection, queries and table edits against a real SQLite file.
use ide_core::{CancellationToken, IdeResult};
use ide_db::{CellValue, DataSource, DbDriver, DbManager, DbOptions, DbProperty, DbRowEdit, DbTableKind, SortKey};
async fn setup() -> (tempfile::TempDir, DbManager) {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().to_path_buf();
std::fs::File::create(root.join("app.db")).unwrap();
let ssh = std::sync::Arc::new(ide_ssh::SshManager::new(root.join("ssh-passwords.json")));
let db = DbManager::new(root.clone(), root.join("passwords.json"), root.join("history.json"), ssh);
db.save_sources(vec![DataSource {
id: "s1".into(),
name: "app".into(),
driver: DbDriver::Sqlite,
file: Some("app.db".into()),
..Default::default()
}])
.await
.unwrap();
let c = CancellationToken::new();
for stmt in [
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, note TEXT, avatar BLOB)",
"CREATE UNIQUE INDEX users_name ON users (name)",
"CREATE VIEW named AS SELECT name FROM users",
"INSERT INTO users (name, note, avatar) VALUES ('ann', NULL, x'CAFE'), ('bob', 'hi', NULL), ('cy', 'x', NULL)",
] {
db.execute("s1", stmt, 100, &c).await.unwrap();
}
(tmp, db)
}
#[tokio::test]
async fn introspects_schemas_tables_columns_and_indexes() -> IdeResult<()> {
let (_tmp, db) = setup().await;
assert_eq!(db.schemas("s1").await?, vec!["main"]);
let tables = db.tables("s1", "main").await?;
let names: Vec<_> = tables.iter().map(|t| (t.name.as_str(), t.kind)).collect();
assert_eq!(names, vec![("named", DbTableKind::View), ("users", DbTableKind::Table)]);
let d = db.table_details("s1", "main", "users").await?;
let cols: Vec<_> = d.columns.iter().map(|c| (c.name.as_str(), c.primary_key, c.nullable)).collect();
assert_eq!(cols, vec![("id", true, true), ("name", false, false), ("note", false, true), ("avatar", false, true)]);
assert_eq!(d.indexes.len(), 1);
assert_eq!(d.indexes[0].name, "users_name");
assert!(d.indexes[0].unique);
assert_eq!(d.indexes[0].columns, vec!["name"]);
let completion = db.completion("s1").await?;
assert_eq!(completion.default_schema, "main");
let users = completion.tables.iter().find(|t| t.name == "users").unwrap();
let cols: Vec<_> = users.columns.iter().map(|c| (c.name.as_str(), c.data_type.as_str(), c.primary_key)).collect();
assert_eq!(
cols,
vec![("id", "INTEGER", true), ("name", "TEXT", false), ("note", "TEXT", false), ("avatar", "BLOB", false)]
);
assert!(!users.columns[1].nullable);
// `INTEGER PRIMARY KEY` is the rowid: SQLite assigns it.
let defaults: Vec<_> = users.columns.iter().map(|c| c.has_default).collect();
assert_eq!(defaults, vec![true, false, false, false]);
let named = completion.tables.iter().find(|t| t.name == "named").unwrap();
assert_eq!(named.kind, DbTableKind::View);
Ok(())
}
#[tokio::test]
async fn completion_model_has_foreign_keys() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let c = CancellationToken::new();
for stmt in [
"CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER REFERENCES users(id), status TEXT DEFAULT 'new')",
// Implicit reference to the primary key; composite key.
"CREATE TABLE notes (user_id INTEGER REFERENCES users, a INT, b INT, \
FOREIGN KEY (a, b) REFERENCES pairs (x, y))",
"CREATE TABLE pairs (x INT, y INT, PRIMARY KEY (x, y))",
] {
db.execute("s1", stmt, 1, &c).await?;
}
let m = db.completion("s1").await?;
let orders = m.tables.iter().find(|t| t.name == "orders").unwrap();
let defaults: Vec<_> = orders.columns.iter().map(|c| (c.name.as_str(), c.has_default)).collect();
assert_eq!(defaults, vec![("id", true), ("user_id", false), ("status", true)]);
let pairs = m.tables.iter().find(|t| t.name == "pairs").unwrap();
assert!(pairs.columns.iter().all(|c| !c.has_default));
let keys: Vec<_> = m
.foreign_keys
.iter()
.map(|k| (k.table.as_str(), k.columns.join(","), k.ref_table.as_str(), k.ref_columns.join(",")))
.collect();
assert_eq!(
keys,
vec![
("notes", "a,b".into(), "pairs", "x,y".into()),
("notes", "user_id".into(), "users", "id".into()),
("orders", "user_id".into(), "users", "id".into()),
]
);
let d = db.table_details("s1", "main", "notes").await?;
let keys: Vec<_> =
d.foreign_keys.iter().map(|k| (k.columns.join(","), k.ref_table.as_str(), k.ref_columns.join(","))).collect();
assert_eq!(keys, vec![("a,b".into(), "pairs", "x,y".into()), ("user_id".into(), "users", "id".into())]);
Ok(())
}
#[tokio::test]
async fn queries_with_limits_nulls_and_blobs() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let c = CancellationToken::new();
let r = db.execute("s1", "SELECT id, name, note, avatar FROM users ORDER BY id", 2, &c).await?;
assert_eq!(r.columns.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(), vec!["id", "name", "note", "avatar"]);
assert_eq!(r.rows.len(), 2);
assert!(r.has_more);
assert_eq!(r.rows[0], vec![Some("1".into()), Some("ann".into()), None, Some("0xCAFE".into())]);
assert_eq!(r.affected, None);
let empty = db.execute("s1", "SELECT name FROM users WHERE id < 0", 10, &c).await?;
assert!(empty.rows.is_empty());
assert_eq!(empty.columns.len(), 1, "columns of an empty result come from the prepared statement");
let upd = db.execute("s1", "UPDATE users SET note = 'n' WHERE id > 1", 10, &c).await?;
assert_eq!(upd.affected, Some(2.0));
let err = db.execute("s1", "SELECT nope FROM users", 10, &c).await.unwrap_err();
assert!(err.message.contains("nope"), "{}", err.message);
Ok(())
}
#[tokio::test]
async fn pages_sorts_filters_and_edits_in_a_transaction() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let c = CancellationToken::new();
let sort = [SortKey { column: "name".into(), descending: true }];
let page = db.table_page("s1", "main", "users", Some("id > 0"), &sort, 0, 2, &c).await?;
assert_eq!(page.result.rows.len(), 2);
assert!(page.result.has_more);
assert_eq!(page.result.rows[0][1].as_deref(), Some("cy"));
let last = db.table_page("s1", "main", "users", None, &sort, 2, 2, &c).await?;
assert_eq!(last.result.rows.len(), 1);
assert!(!last.result.has_more);
let key = |id: &str| vec![CellValue { column: "id".into(), value: Some(id.into()) }];
let n = db
.apply_edits(
"s1",
"main",
"users",
&[
DbRowEdit::Update {
key: key("1"),
values: vec![CellValue { column: "note".into(), value: Some("it's".into()) }],
},
DbRowEdit::Delete { key: key("2") },
DbRowEdit::Insert { values: vec![CellValue { column: "name".into(), value: Some("dee".into()) }] },
],
)
.await?;
assert_eq!(n, 3);
let r = db.execute("s1", "SELECT name, note FROM users ORDER BY id", 10, &c).await?;
let rows: Vec<_> = r.rows.iter().map(|r| (r[0].clone().unwrap(), r[1].clone())).collect();
assert_eq!(rows, vec![("ann".into(), Some("it's".into())), ("cy".into(), Some("x".into())), ("dee".into(), None)]);
// A failing edit rolls back the whole batch.
let err = db
.apply_edits(
"s1",
"main",
"users",
&[
DbRowEdit::Delete { key: key("1") },
DbRowEdit::Insert { values: vec![CellValue { column: "name".into(), value: Some("cy".into()) }] },
],
)
.await;
assert!(err.is_err());
let count = db.execute("s1", "SELECT count(*) FROM users", 10, &c).await?;
assert_eq!(count.rows[0][0].as_deref(), Some("3"));
Ok(())
}
#[tokio::test]
async fn test_connection_and_disconnect() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let source = db.source("s1")?;
assert!(db.test(&source, None, None).await?.starts_with("SQLite "));
assert!(db.is_connected("s1"));
db.disconnect("s1").await;
assert!(!db.is_connected("s1"));
let missing = DataSource { file: Some("missing/nope.db".into()), ..source };
assert!(db.test(&missing, None, None).await.is_err());
Ok(())
}
async fn count(db: &DbManager) -> String {
let r = db.execute("s1", "SELECT count(*) FROM users", 1, &CancellationToken::new()).await.unwrap();
r.rows[0][0].clone().unwrap()
}
#[tokio::test]
async fn manual_commit_keeps_a_transaction_until_commit_or_rollback() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let c = CancellationToken::new();
db.set_manual_commit("s1", true).await;
db.execute("s1", "INSERT INTO users (name) VALUES ('dee')", 1, &c).await?;
// The session sees its own change.
assert_eq!(count(&db).await, "4");
db.finish_transaction("s1", false).await?;
assert_eq!(count(&db).await, "3");
db.execute("s1", "INSERT INTO users (name) VALUES ('eve')", 1, &c).await?;
db.finish_transaction("s1", true).await?;
// Leaving manual mode rolls back what is still open.
db.execute("s1", "DELETE FROM users", 1, &c).await?;
db.set_manual_commit("s1", false).await;
assert_eq!(count(&db).await, "4");
// Disconnecting with an open transaction does not hang.
db.set_manual_commit("s1", true).await;
db.execute("s1", "DELETE FROM users", 1, &c).await?;
db.disconnect("s1").await;
assert_eq!(count(&db).await, "4");
Ok(())
}
#[tokio::test]
async fn targets_name_a_database_of_a_data_source() -> IdeResult<()> {
let (_tmp, db) = setup().await;
assert_eq!(ide_db::split_target("s1"), ("s1", None));
assert_eq!(ide_db::split_target("s1:app_dev"), ("s1", Some("app_dev")));
assert_eq!(ide_db::split_target("s1:a:b"), ("s1", Some("a:b")));
assert_eq!(ide_db::split_target("s1:"), ("s1", None));
assert_eq!(db.source("s1:other")?.database, "other");
assert_eq!(db.source("s1:other")?.id, "s1");
assert!(db.source("nope:other").is_err());
// Only PostgreSQL lists databases.
assert!(db.databases("s1").await.is_err());
db.disconnect_source("s1").await;
assert!(db.connected().is_empty());
Ok(())
}
#[tokio::test]
async fn ddl_of_tables_and_views() -> IdeResult<()> {
let (_tmp, db) = setup().await;
assert_eq!(
db.ddl("s1", "main", "users").await?,
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, note TEXT, avatar BLOB);\n\n\
CREATE UNIQUE INDEX users_name ON users (name);\n"
);
assert_eq!(db.ddl("s1", "main", "named").await?, "CREATE VIEW named AS SELECT name FROM users;\n");
assert!(db.ddl("s1", "main", "nope").await.is_err());
Ok(())
}
#[tokio::test]
async fn explains_queries_without_running_them() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let c = CancellationToken::new();
let plan = db.explain("s1", "SELECT * FROM users WHERE id > 1 ORDER BY note;", &c).await?;
let ops: Vec<_> = plan.iter().map(|n| n.operation.as_str()).collect();
assert_eq!(ops, vec!["Index Scan", "Order By"]);
assert!(plan[0].params.starts_with("table: users;"), "{}", plan[0].params);
db.explain("s1", "DELETE FROM users", &c).await?;
assert_eq!(count(&db).await, "3");
Ok(())
}
#[tokio::test]
async fn records_executed_statements_in_the_query_history() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let c = CancellationToken::new();
db.execute("s1", "SELECT 1", 1, &c).await?;
assert!(db.execute("s1", "SELECT nope", 1, &c).await.is_err());
// Recorded off the async threads.
let mut texts = Vec::new();
for _ in 0..50 {
texts = db.history("s1").await?.into_iter().map(|e| e.text).collect::<Vec<_>>();
if texts.first().map(String::as_str) == Some("SELECT nope") {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
assert_eq!(texts[..2], ["SELECT nope", "SELECT 1"]);
db.remove_history("s1", Some(vec!["SELECT 1".into()])).await?;
assert!(!db.history("s1").await?.iter().any(|e| e.text == "SELECT 1"));
db.remove_history("s1", None).await?;
assert!(db.history("s1").await?.is_empty());
Ok(())
}
#[tokio::test]
async fn lists_triggers_and_their_ddl() -> IdeResult<()> {
let (_tmp, db) = setup().await;
let c = CancellationToken::new();
db.execute("s1", "CREATE TRIGGER users_touch AFTER UPDATE ON users BEGIN SELECT 1; END", 1, &c).await?;
let d = db.table_details("s1", "main", "users").await?;
let triggers: Vec<_> = d.triggers.iter().map(|t| (t.name.as_str(), t.timing.as_str(), t.events.as_str())).collect();
assert_eq!(triggers, vec![("users_touch", "after", "update")]);
let ddl = db.object_ddl("s1", "main", ide_db::DbObjectKind::Trigger, "users_touch").await?;
assert!(ddl.starts_with("CREATE TRIGGER users_touch"), "{ddl}");
// SQLite has neither routines nor sequences.
assert_eq!(db.schema_objects("s1", "main").await?, ide_db::SchemaObjects::default());
Ok(())
}
#[tokio::test]
async fn applies_driver_properties_and_the_startup_script_on_connect() -> IdeResult<()> {
let (tmp, db) = setup().await;
let mut s = db.sources()?.remove(0);
s.properties = vec![DbProperty { name: "cache_size".into(), value: "-1234".into() }];
s.options = DbOptions {
startup_script: "CREATE TEMP TABLE boot (x); INSERT INTO boot VALUES (7);".into(),
..Default::default()
};
db.save_sources(vec![s.clone()]).await?;
let c = CancellationToken::new();
let r = db.execute("s1", "SELECT (SELECT x FROM boot), (SELECT cache_size FROM pragma_cache_size)", 10, &c).await?;
assert_eq!(r.rows, vec![vec![Some("7".into()), Some("-1234".into())]]);
// URL only: the file comes from the URL.
let url = format!("jdbc:sqlite:{}", tmp.path().join("app.db").display());
db.save_sources(vec![DataSource { url_only: true, url, file: None, ..s }]).await?;
assert_eq!(db.schemas("s1").await?, vec!["main"]);
Ok(())
}
+20
Переглянути файл
@@ -0,0 +1,20 @@
[package]
name = "ide-docker"
description = "Docker integration for the Services tool window, driven through the docker CLI"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
ide-core.workspace = true
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
tracing.workspace = true
which = "8"
[target.'cfg(unix)'.dependencies]
libc = "0.2"
[dev-dependencies]
tempfile.workspace = true
+402
Переглянути файл
@@ -0,0 +1,402 @@
//! Docker connections (which daemon the CLI talks to) and registries, as the
//! Services tool window's "Add Service" menu creates them. Both are app-wide
//! like in IDEA: `docker.json` in the app data directory; registry passwords
//! live beside it in an owner-only `docker-passwords.json`.
use ide_core::{IdeError, IdeResult};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Command;
/// Which daemon a docker command talks to.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum DockerEndpoint {
/// Whatever the CLI is configured for (`DOCKER_HOST`, the current context).
#[default]
Default,
/// A local daemon socket, e.g. `/var/run/docker.sock` (or a `npipe://` URL on Windows).
Socket { path: String },
/// A remote daemon, e.g. `tcp://build:2376` or `ssh://me@build`; with TLS when
/// `tls_cert_dir` holds `ca.pem`, `cert.pem` and `key.pem`.
Tcp { host: String, tls_cert_dir: Option<String> },
/// A named `docker context`.
Context { name: String },
}
impl DockerEndpoint {
/// Global CLI options selecting this endpoint (before the subcommand).
pub fn global_args(&self) -> Vec<String> {
match self {
Self::Default => Vec::new(),
Self::Socket { path } => {
let path = path.trim();
let url = if path.contains("://") { path.to_owned() } else { format!("unix://{path}") };
vec!["--host".into(), url]
}
Self::Tcp { host, tls_cert_dir } => {
let host = host.trim();
let url = if host.contains("://") { host.to_owned() } else { format!("tcp://{host}") };
let mut args = vec!["--host".into(), url];
if let Some(dir) = tls_cert_dir.as_deref().map(str::trim).filter(|d| !d.is_empty()) {
let file = |name: &str| Path::new(dir).join(name).to_string_lossy().into_owned();
args.extend([
"--tlsverify".into(),
"--tlscacert".into(),
file("ca.pem"),
"--tlscert".into(),
file("cert.pem"),
"--tlskey".into(),
file("key.pem"),
]);
}
args
}
Self::Context { name } => vec!["--context".into(), name.trim().to_owned()],
}
}
/// Adds the global options to `cmd`; an explicit endpoint also drops the
/// inherited `DOCKER_HOST` / `DOCKER_CONTEXT`, which would conflict.
pub(crate) fn apply(&self, cmd: &mut Command) {
if *self != Self::Default {
cmd.env_remove("DOCKER_HOST").env_remove("DOCKER_CONTEXT").env_remove("DOCKER_TLS_VERIFY");
}
cmd.args(self.global_args());
}
/// The same selection as environment variables, for docker command lines run by
/// others (run configurations). An empty value clears an inherited variable: the
/// CLI treats `DOCKER_HOST=""` as unset.
pub fn env_vars(&self) -> BTreeMap<String, String> {
let mut env = BTreeMap::new();
let mut set = |k: &str, v: String| {
env.insert(k.to_owned(), v);
};
match self {
Self::Default => {}
Self::Socket { path } => {
let path = path.trim();
set("DOCKER_HOST", if path.contains("://") { path.to_owned() } else { format!("unix://{path}") });
set("DOCKER_CONTEXT", String::new());
set("DOCKER_TLS_VERIFY", String::new());
}
Self::Tcp { host, tls_cert_dir } => {
let host = host.trim();
set("DOCKER_HOST", if host.contains("://") { host.to_owned() } else { format!("tcp://{host}") });
set("DOCKER_CONTEXT", String::new());
match tls_cert_dir.as_deref().map(str::trim).filter(|d| !d.is_empty()) {
// `DOCKER_CERT_PATH` holds `ca.pem`, `cert.pem` and `key.pem`, as `global_args` expects.
Some(dir) => {
set("DOCKER_TLS_VERIFY", "1".into());
set("DOCKER_CERT_PATH", dir.to_owned());
}
None => set("DOCKER_TLS_VERIFY", String::new()),
}
}
Self::Context { name } => {
set("DOCKER_CONTEXT", name.trim().to_owned());
set("DOCKER_HOST", String::new());
set("DOCKER_TLS_VERIFY", String::new());
}
}
env
}
pub fn validate(&self) -> IdeResult<()> {
let empty = match self {
Self::Default => false,
Self::Socket { path } => path.trim().is_empty(),
Self::Tcp { host, .. } => host.trim().is_empty(),
Self::Context { name } => name.trim().is_empty() || name.trim().starts_with('-'),
};
if empty { Err(IdeError::invalid("the Docker connection needs a socket, host or context")) } else { Ok(()) }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerConnection {
pub id: String,
pub name: String,
pub endpoint: DockerEndpoint,
}
/// A registry to log in to (Docker Hub, GHCR, a private one). The password is stored separately.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerRegistry {
pub id: String,
pub name: String,
/// `docker.io`, `ghcr.io`, `registry.example.com:5000`.
pub address: String,
#[serde(default)]
pub username: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerSettings {
#[serde(default = "default_connections")]
pub connections: Vec<DockerConnection>,
#[serde(default)]
pub registries: Vec<DockerRegistry>,
}
impl Default for DockerSettings {
fn default() -> Self {
Self { connections: default_connections(), registries: Vec::new() }
}
}
/// Before anything is configured the CLI's own daemon is shown, as before connections existed.
fn default_connections() -> Vec<DockerConnection> {
vec![DockerConnection { id: "default".into(), name: "Docker".into(), endpoint: DockerEndpoint::Default }]
}
const SETTINGS_FILE: &str = "docker.json";
const PASSWORDS_FILE: &str = "docker-passwords.json";
pub fn load_settings(data_dir: &Path) -> DockerSettings {
match std::fs::read(data_dir.join(SETTINGS_FILE)) {
Ok(bytes) => serde_json::from_slice(&bytes).unwrap_or_else(|e| {
tracing::warn!("{SETTINGS_FILE}: {e}");
DockerSettings::default()
}),
Err(_) => DockerSettings::default(),
}
}
pub fn save_settings(data_dir: &Path, settings: &DockerSettings) -> IdeResult<()> {
for c in &settings.connections {
if c.id.is_empty() || c.name.trim().is_empty() {
return Err(IdeError::invalid("every Docker connection needs a name"));
}
c.endpoint.validate()?;
}
for r in &settings.registries {
if r.id.is_empty() || r.address.trim().is_empty() {
return Err(IdeError::invalid("every Docker registry needs an address"));
}
}
let bytes = serde_json::to_vec_pretty(settings).map_err(|e| IdeError::internal(e.to_string()))?;
write_file(&data_dir.join(SETTINGS_FILE), &bytes, false)?;
// Forget passwords of registries that are gone.
let mut passwords = read_passwords(data_dir);
let before = passwords.len();
passwords.retain(|id, _| settings.registries.iter().any(|r| &r.id == id));
if passwords.len() != before { write_passwords(data_dir, &passwords) } else { Ok(()) }
}
fn read_passwords(data_dir: &Path) -> BTreeMap<String, String> {
std::fs::read(data_dir.join(PASSWORDS_FILE)).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default()
}
fn write_passwords(data_dir: &Path, all: &BTreeMap<String, String>) -> IdeResult<()> {
let bytes = serde_json::to_vec_pretty(all).map_err(|e| IdeError::internal(e.to_string()))?;
write_file(&data_dir.join(PASSWORDS_FILE), &bytes, true)
}
pub fn registry_password(data_dir: &Path, id: &str) -> Option<String> {
read_passwords(data_dir).remove(id)
}
/// Saves (or with `None` forgets) the password of registry `id`.
pub fn set_registry_password(data_dir: &Path, id: &str, password: Option<&str>) -> IdeResult<()> {
let mut all = read_passwords(data_dir);
match password {
Some(p) => all.insert(id.to_owned(), p.to_owned()),
None => all.remove(id),
};
write_passwords(data_dir, &all)
}
fn write_file(path: &Path, bytes: &[u8], private: bool) -> IdeResult<()> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
let tmp = path.with_extension("json.tmp");
let mut options = std::fs::OpenOptions::new();
options.write(true).create(true).truncate(true);
#[cfg(unix)]
if private {
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
}
#[cfg(not(unix))]
let _ = private;
std::io::Write::write_all(&mut options.open(&tmp)?, bytes)?;
std::fs::rename(&tmp, path)?;
Ok(())
}
/// Canonical registry host: no scheme or path; Docker Hub's aliases become `docker.io`.
pub fn registry_host(address: &str) -> String {
let a = address.trim();
let a = a.split_once("://").map_or(a, |(_, rest)| rest);
let host = a.split('/').next().unwrap_or_default().to_ascii_lowercase();
match host.as_str() {
"index.docker.io" | "registry-1.docker.io" | "registry.hub.docker.com" | "hub.docker.com" => "docker.io".into(),
_ => host,
}
}
/// Registry host an image reference pulls from (`postgres:16` → `docker.io`).
pub fn reference_registry(reference: &str) -> String {
match reference.split_once('/') {
Some((first, _)) if first.contains('.') || first.contains(':') || first == "localhost" => registry_host(first),
_ => "docker.io".into(),
}
}
/// Credentials for a `docker login`.
#[derive(Clone, Debug)]
pub struct RegistryAuth {
pub address: String,
pub username: String,
pub password: String,
}
/// The saved registry (with a password) matching `reference`, if any.
pub fn auth_for(data_dir: &Path, reference: &str) -> Option<RegistryAuth> {
let host = reference_registry(reference);
let settings = load_settings(data_dir);
let registry =
settings.registries.into_iter().find(|r| registry_host(&r.address) == host && !r.username.is_empty())?;
let password = registry_password(data_dir, &registry.id)?;
Some(RegistryAuth { address: registry.address, username: registry.username, password })
}
/// A throwaway docker config directory: logins happen there, never in the user's
/// `~/.docker/config.json`. Contexts are copied so a context endpoint still resolves.
pub(crate) struct TempConfig {
pub dir: PathBuf,
}
impl TempConfig {
pub fn new() -> IdeResult<Self> {
let nanos =
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);
let dir = std::env::temp_dir().join(format!("openidea-docker-{}-{nanos}", std::process::id()));
std::fs::create_dir_all(&dir)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700));
}
if let Some(contexts) = user_config_dir().map(|d| d.join("contexts")).filter(|d| d.is_dir()) {
let _ = copy_dir(&contexts, &dir.join("contexts"));
}
Ok(Self { dir })
}
}
impl Drop for TempConfig {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.dir);
}
}
fn user_config_dir() -> Option<PathBuf> {
if let Some(dir) = std::env::var_os("DOCKER_CONFIG") {
return Some(PathBuf::from(dir));
}
let home = std::env::var_os(if cfg!(windows) { "USERPROFILE" } else { "HOME" })?;
Some(PathBuf::from(home).join(".docker"))
}
fn copy_dir(from: &Path, to: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(to)?;
for e in std::fs::read_dir(from)?.flatten() {
let target = to.join(e.file_name());
if e.file_type()?.is_dir() {
copy_dir(&e.path(), &target)?;
} else {
std::fs::copy(e.path(), target)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn endpoint_args() {
assert!(DockerEndpoint::Default.global_args().is_empty());
assert_eq!(
DockerEndpoint::Socket { path: "/var/run/docker.sock".into() }.global_args(),
["--host", "unix:///var/run/docker.sock"]
);
let tls = DockerEndpoint::Tcp { host: "build:2376".into(), tls_cert_dir: Some("/certs".into()) }.global_args();
assert_eq!(tls[..3], ["--host", "tcp://build:2376", "--tlsverify"]);
assert!(tls.contains(&Path::new("/certs").join("key.pem").to_string_lossy().into_owned()));
assert_eq!(
DockerEndpoint::Tcp { host: "ssh://me@box".into(), tls_cert_dir: Some(" ".into()) }.global_args(),
["--host", "ssh://me@box"]
);
assert_eq!(DockerEndpoint::Context { name: "orbstack".into() }.global_args(), ["--context", "orbstack"]);
assert!(DockerEndpoint::Context { name: "--x".into() }.validate().is_err());
}
#[test]
fn endpoint_env() {
assert!(DockerEndpoint::Default.env_vars().is_empty());
let socket = DockerEndpoint::Socket { path: "/var/run/docker.sock".into() }.env_vars();
assert_eq!(socket["DOCKER_HOST"], "unix:///var/run/docker.sock");
assert_eq!(socket["DOCKER_CONTEXT"], "");
let tls = DockerEndpoint::Tcp { host: "build:2376".into(), tls_cert_dir: Some("/certs".into()) }.env_vars();
assert_eq!(
(tls["DOCKER_HOST"].as_str(), tls["DOCKER_TLS_VERIFY"].as_str(), tls["DOCKER_CERT_PATH"].as_str()),
("tcp://build:2376", "1", "/certs")
);
let ssh = DockerEndpoint::Tcp { host: "ssh://me@box".into(), tls_cert_dir: None }.env_vars();
assert_eq!((ssh["DOCKER_HOST"].as_str(), ssh["DOCKER_TLS_VERIFY"].as_str()), ("ssh://me@box", ""));
assert!(!ssh.contains_key("DOCKER_CERT_PATH"));
let ctx = DockerEndpoint::Context { name: " orbstack ".into() }.env_vars();
assert_eq!((ctx["DOCKER_CONTEXT"].as_str(), ctx["DOCKER_HOST"].as_str()), ("orbstack", ""));
}
#[test]
fn registries_match_references() {
assert_eq!(reference_registry("postgres:16"), "docker.io");
assert_eq!(reference_registry("library/postgres"), "docker.io");
assert_eq!(reference_registry("ghcr.io/org/app:1"), "ghcr.io");
assert_eq!(reference_registry("localhost:5000/app"), "localhost:5000");
assert_eq!(registry_host("https://index.docker.io/v1/"), "docker.io");
assert_eq!(registry_host("GHCR.io"), "ghcr.io");
}
#[test]
fn settings_round_trip_and_passwords() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
assert_eq!(load_settings(dir).connections[0].endpoint, DockerEndpoint::Default);
let settings = DockerSettings {
connections: vec![DockerConnection {
id: "c1".into(),
name: "Build box".into(),
endpoint: DockerEndpoint::Tcp { host: "build:2376".into(), tls_cert_dir: None },
}],
registries: vec![DockerRegistry {
id: "r1".into(),
name: "GHCR".into(),
address: "ghcr.io".into(),
username: "me".into(),
}],
};
save_settings(dir, &settings).unwrap();
assert_eq!(load_settings(dir), settings);
set_registry_password(dir, "r1", Some("s3cret")).unwrap();
let auth = auth_for(dir, "ghcr.io/org/app").unwrap();
assert_eq!((auth.username.as_str(), auth.password.as_str()), ("me", "s3cret"));
assert!(auth_for(dir, "postgres").is_none());
save_settings(dir, &DockerSettings { registries: Vec::new(), ..settings }).unwrap();
assert_eq!(registry_password(dir, "r1"), None);
// An empty list stays empty (the default connection is only for a missing file).
save_settings(dir, &DockerSettings { connections: Vec::new(), registries: Vec::new() }).unwrap();
assert!(load_settings(dir).connections.is_empty());
}
}
+442
Переглянути файл
@@ -0,0 +1,442 @@
//! Docker for the Services tool window, driven through the `docker` CLI (like git
//! is driven through its CLI): listing with `--format '{{json .}}'`, container
//! lifecycle, logs and `docker events` streams, pulls and prunes.
//!
//! Every function blocks; callers run them off the UI / command threads.
mod connection;
mod model;
mod process;
pub use connection::*;
pub use model::*;
use ide_core::{CancellationToken, IdeError, IdeResult};
use process::Stream;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::OnceLock;
use std::time::Duration;
/// Listing and lifecycle commands normally answer in well under a second; a
/// wedged daemon must not keep a worker thread forever.
const QUICK: Duration = Duration::from_secs(20);
/// Stop / restart wait for the container's grace period (10 s by default).
const LIFECYCLE: Duration = Duration::from_secs(90);
/// Locates the docker executable once per process. GUI apps on macOS start with a
/// minimal `PATH`, so the usual install locations are probed after the lookup.
pub fn docker_executable() -> Option<PathBuf> {
static DOCKER: OnceLock<Option<PathBuf>> = OnceLock::new();
DOCKER
.get_or_init(|| {
if let Some(explicit) = std::env::var_os("OPENIDEA_DOCKER").map(PathBuf::from).filter(|p| p.is_file()) {
return Some(explicit);
}
if let Ok(found) = which::which("docker") {
return Some(found);
}
let home = std::env::var_os(if cfg!(windows) { "USERPROFILE" } else { "HOME" }).map(PathBuf::from);
let mut candidates: Vec<PathBuf> = if cfg!(windows) {
vec![PathBuf::from(r"C:\Program Files\Docker\Docker\resources\bin\docker.exe")]
} else {
[
"/usr/local/bin/docker",
"/opt/homebrew/bin/docker",
"/Applications/Docker.app/Contents/Resources/bin/docker",
"/usr/bin/docker",
"/snap/bin/docker",
]
.iter()
.map(PathBuf::from)
.collect()
};
if let Some(home) = home.filter(|_| !cfg!(windows)) {
candidates.push(home.join(".orbstack/bin/docker"));
candidates.push(home.join(".rd/bin/docker"));
candidates.push(home.join(".docker/bin/docker"));
}
candidates.into_iter().find(|p| p.is_file())
})
.clone()
}
/// Environment for docker command lines a run configuration launches: the endpoint's
/// variables plus a `PATH` that starts with docker's directory (GUI apps on macOS
/// lack the usual install locations), so `docker` / `docker compose` resolve.
pub fn run_env(ep: &DockerEndpoint) -> std::collections::BTreeMap<String, String> {
let mut env = ep.env_vars();
if let Some(dir) = docker_executable().as_deref().and_then(Path::parent) {
let current = std::env::var_os("PATH").unwrap_or_default();
let mut dirs: Vec<PathBuf> = vec![dir.to_path_buf()];
dirs.extend(std::env::split_paths(&current).filter(|d| d != dir));
if let Ok(path) = std::env::join_paths(dirs) {
env.insert("PATH".into(), path.to_string_lossy().into_owned());
}
}
env
}
fn binary() -> IdeResult<PathBuf> {
docker_executable()
.ok_or_else(|| IdeError::unavailable("docker executable not found; install Docker and make sure it is on PATH"))
}
fn command(ep: &DockerEndpoint, args: &[&str]) -> IdeResult<Command> {
let mut cmd = Command::new(binary()?);
ep.apply(&mut cmd);
cmd.args(args).env("DOCKER_CLI_HINTS", "false").env("NO_COLOR", "1");
tracing::debug!(?args, "docker");
Ok(cmd)
}
/// Runs a docker command, mapping a non-zero exit to an error with docker's message.
fn run(ep: &DockerEndpoint, args: &[&str], timeout: Duration) -> IdeResult<String> {
let out = process::run(command(ep, args)?, timeout, None)?;
if out.success { Ok(out.stdout) } else { Err(failure(args, &out.stderr, &out.stdout)) }
}
fn failure(args: &[&str], stderr: &str, stdout: &str) -> IdeError {
let msg = [stderr, stdout].iter().map(|s| s.trim()).find(|s| !s.is_empty()).unwrap_or_default();
let msg = msg.strip_prefix("Error response from daemon: ").unwrap_or(msg).to_owned();
let msg = if msg.is_empty() { format!("docker {} failed", args.first().copied().unwrap_or_default()) } else { msg };
let lower = msg.to_ascii_lowercase();
if lower.contains("no such") || lower.contains("not found") {
IdeError::not_found(msg)
} else if lower.contains("cannot connect to the docker daemon") || lower.contains("is the docker daemon running") {
IdeError::unavailable(msg)
} else {
IdeError::internal(msg)
}
}
/// Whether docker can be used right now.
pub fn availability(ep: &DockerEndpoint) -> DockerAvailability {
let Ok(cmd) = command(ep, &["version", "--format", "{{json .}}"]) else {
return DockerAvailability::NotInstalled;
};
match process::run(cmd, Duration::from_secs(15), None) {
Ok(out) => parse_version(&out.stdout, &out.stderr, out.success),
Err(e) => DockerAvailability::DaemonDown { message: e.message, context: None },
}
}
/// Availability plus containers, images, volumes and networks (listed in parallel).
pub fn snapshot(ep: &DockerEndpoint) -> DockerSnapshot {
let availability = availability(ep);
let executable = docker_executable().map(|p| ide_core::ipc_path::to_ipc(&p));
let mut snap = DockerSnapshot {
availability,
executable,
containers: Vec::new(),
images: Vec::new(),
volumes: Vec::new(),
networks: Vec::new(),
errors: Vec::new(),
global_args: ep.global_args(),
};
if !matches!(snap.availability, DockerAvailability::Ready { .. }) {
return snap;
}
const FMT: &str = "{{json .}}";
let (containers, images, volumes, networks) = std::thread::scope(|s| {
let c = s.spawn(|| run(ep, &["ps", "--all", "--no-trunc", "--format", FMT], QUICK));
let i = s.spawn(|| run(ep, &["image", "ls", "--no-trunc", "--format", FMT], QUICK));
let v = s.spawn(|| run(ep, &["volume", "ls", "--format", FMT], QUICK));
let n = s.spawn(|| run(ep, &["network", "ls", "--no-trunc", "--format", FMT], QUICK));
let join = |h: std::thread::ScopedJoinHandle<'_, IdeResult<String>>| {
h.join().unwrap_or_else(|_| Err(IdeError::internal("docker listing panicked")))
};
(join(c), join(i), join(v), join(n))
});
let mut take = |r: IdeResult<String>, what: &str| match r {
Ok(text) => Some(text),
Err(e) => {
snap.errors.push(format!("{what}: {}", e.message));
None
}
};
if let Some(t) = take(containers, "Containers") {
snap.containers = parse_containers(&t);
}
if let Some(t) = take(images, "Images") {
snap.images = parse_images(&t);
}
if let Some(t) = take(volumes, "Volumes") {
snap.volumes = parse_volumes(&t);
}
if let Some(t) = take(networks, "Networks") {
snap.networks = parse_networks(&t);
}
snap
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum ContainerOp {
Start,
Stop,
Restart,
Pause,
Unpause,
Kill,
}
impl ContainerOp {
fn verb(self) -> &'static str {
match self {
Self::Start => "start",
Self::Stop => "stop",
Self::Restart => "restart",
Self::Pause => "pause",
Self::Unpause => "unpause",
Self::Kill => "kill",
}
}
}
/// Starts / stops / … the containers `ids`.
pub fn container_op(ep: &DockerEndpoint, ids: &[String], op: ContainerOp) -> IdeResult<()> {
if ids.is_empty() {
return Ok(());
}
let mut args = vec!["container", op.verb()];
args.extend(ids.iter().map(String::as_str));
run(ep, &args, LIFECYCLE).map(drop)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum DockerObject {
Container,
Image,
Volume,
Network,
}
impl DockerObject {
fn noun(self) -> &'static str {
match self {
Self::Container => "container",
Self::Image => "image",
Self::Volume => "volume",
Self::Network => "network",
}
}
}
/// `docker <object> rm` (`force`: stop running containers / untag used images first).
pub fn remove(ep: &DockerEndpoint, object: DockerObject, ids: &[String], force: bool) -> IdeResult<()> {
if ids.is_empty() {
return Ok(());
}
let mut args = vec![object.noun(), "rm"];
if force && matches!(object, DockerObject::Container | DockerObject::Image | DockerObject::Volume) {
args.push("--force");
}
args.extend(ids.iter().map(String::as_str));
run(ep, &args, LIFECYCLE).map(drop)
}
/// `docker inspect` of one object, pretty-printed.
pub fn inspect(ep: &DockerEndpoint, object: DockerObject, id: &str) -> IdeResult<String> {
let out = run(ep, &["inspect", "--type", object.noun(), id], QUICK)?;
match serde_json::from_str::<serde_json::Value>(&out) {
// A one-element array for a single id: show the object itself.
Ok(serde_json::Value::Array(mut items)) if items.len() == 1 => {
Ok(serde_json::to_string_pretty(&items.remove(0)).unwrap_or(out))
}
Ok(v) => Ok(serde_json::to_string_pretty(&v).unwrap_or(out)),
Err(_) => Ok(out),
}
}
/// Streams a container's logs (`tail` last lines, then follows while `follow`)
/// until it ends or `cancel` fires.
pub fn logs(
ep: &DockerEndpoint,
id: &str,
tail: u32,
follow: bool,
timestamps: bool,
cancel: &CancellationToken,
on_line: &dyn Fn(DockerLogLine),
) -> IdeResult<()> {
let tail = tail.to_string();
let mut args = vec!["logs", "--tail", &tail];
if follow {
args.push("--follow");
}
if timestamps {
args.push("--timestamps");
}
args.push(id);
let (ok, stderr) = process::stream(command(ep, &args)?, cancel, &mut |stream, text| {
on_line(DockerLogLine { stderr: stream == Stream::Stderr, text })
})?;
if ok { Ok(()) } else { Err(failure(&args, &stderr.join("\n"), "")) }
}
/// Follows `docker events` until it ends (daemon gone) or `cancel` fires.
pub fn watch_events(ep: &DockerEndpoint, cancel: &CancellationToken, on_event: &dyn Fn(DockerEvent)) -> IdeResult<()> {
let args = ["events", "--format", "{{json .}}"];
let (ok, stderr) = process::stream(command(ep, &args)?, cancel, &mut |stream, line| {
if stream == Stream::Stdout
&& let Some(e) = parse_event(&line)
{
on_event(e);
}
})?;
if ok { Ok(()) } else { Err(failure(&args, &stderr.join("\n"), "")) }
}
/// Runs a long docker command (pull, prune, compose…), streaming its output lines.
fn run_streaming(
ep: &DockerEndpoint,
config: Option<&Path>,
args: &[&str],
cancel: &CancellationToken,
on_output: &dyn Fn(String),
) -> IdeResult<()> {
on_output(format!("docker {}", args.join(" ")));
let mut cmd = command(ep, args)?;
if let Some(dir) = config {
cmd.env("DOCKER_CONFIG", dir);
}
let (ok, stderr) = process::stream(cmd, cancel, &mut |_, line| {
if !line.trim().is_empty() {
on_output(line);
}
})?;
if ok { Ok(()) } else { Err(failure(args, &stderr.join("\n"), "")) }
}
/// `docker pull <reference>`; with `auth` it logs in to the registry first, in a
/// throwaway config directory (the user's docker config stays untouched).
pub fn pull(
ep: &DockerEndpoint,
reference: &str,
auth: Option<&RegistryAuth>,
cancel: &CancellationToken,
on_output: &dyn Fn(String),
) -> IdeResult<()> {
let reference = reference.trim();
if reference.is_empty() || reference.starts_with('-') {
return Err(IdeError::invalid("enter an image name, e.g. postgres:16"));
}
let Some(auth) = auth else { return run_streaming(ep, None, &["pull", reference], cancel, on_output) };
let config = TempConfig::new()?;
on_output(format!("Logging in to {} as {}", auth.address, auth.username));
login_in(ep, &config.dir, auth, Some(cancel))?;
run_streaming(ep, Some(&config.dir), &["pull", reference], cancel, on_output)
}
/// `docker login` with `auth`; returns docker's answer (`Login Succeeded`). Nothing is
/// stored in the user's docker config.
pub fn login(ep: &DockerEndpoint, auth: &RegistryAuth) -> IdeResult<String> {
let config = TempConfig::new()?;
login_in(ep, &config.dir, auth, None)
}
fn login_in(
ep: &DockerEndpoint,
config: &Path,
auth: &RegistryAuth,
cancel: Option<&CancellationToken>,
) -> IdeResult<String> {
let host = registry_host(&auth.address);
let user = auth.username.trim();
if host.is_empty() || host.starts_with('-') || user.is_empty() || user.starts_with('-') {
return Err(IdeError::invalid("the registry needs an address and a username"));
}
let mut args = vec!["login", "--username", user, "--password-stdin"];
// Docker Hub is the CLI's default server.
if host != "docker.io" {
args.push(&host);
}
let mut cmd = command(ep, &args)?;
cmd.env("DOCKER_CONFIG", config);
let out = process::run_with_input(cmd, QUICK, cancel, Some(&auth.password))?;
if out.success {
Ok(out.stdout.lines().map(str::trim).rfind(|l| !l.is_empty()).unwrap_or("Login Succeeded").to_owned())
} else {
Err(failure(&args, &out.stderr, &out.stdout))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum PruneTarget {
Containers,
/// Dangling images only.
Images,
/// Every image no container uses.
AllImages,
Volumes,
Networks,
/// Stopped containers, unused networks, dangling images and build cache.
System,
}
/// `docker <object> prune --force`.
pub fn prune(
ep: &DockerEndpoint,
target: PruneTarget,
cancel: &CancellationToken,
on_output: &dyn Fn(String),
) -> IdeResult<()> {
let args: &[&str] = match target {
PruneTarget::Containers => &["container", "prune", "--force"],
PruneTarget::Images => &["image", "prune", "--force"],
PruneTarget::AllImages => &["image", "prune", "--all", "--force"],
PruneTarget::Volumes => &["volume", "prune", "--force"],
PruneTarget::Networks => &["network", "prune", "--force"],
PruneTarget::System => &["system", "prune", "--force"],
};
run_streaming(ep, None, args, cancel, on_output)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum ComposeOp {
Start,
Stop,
Restart,
/// Stops and removes the project's containers and networks.
Down,
}
/// Runs `docker compose -p <project> <op>` for a project found through container
/// labels. `files` / `working_dir` (from the labels too) are passed when they still
/// exist, so commands that need the model keep working.
pub fn compose_op(
ep: &DockerEndpoint,
project: &str,
files: &[String],
working_dir: Option<&str>,
op: ComposeOp,
cancel: &CancellationToken,
on_output: &dyn Fn(String),
) -> IdeResult<()> {
let mut args: Vec<&str> = vec!["compose", "--project-name", project];
let existing: Vec<&String> = files.iter().filter(|f| Path::new(f).is_file()).collect();
if !existing.is_empty() && existing.len() == files.len() {
for f in existing {
args.push("--file");
args.push(f);
}
if let Some(dir) = working_dir.filter(|d| Path::new(d).is_dir()) {
args.push("--project-directory");
args.push(dir);
}
}
args.push(match op {
ComposeOp::Start => "start",
ComposeOp::Stop => "stop",
ComposeOp::Restart => "restart",
ComposeOp::Down => "down",
});
run_streaming(ep, None, &args, cancel, on_output)
}
+464
Переглянути файл
@@ -0,0 +1,464 @@
//! IPC types and parsers for the docker CLI's `--format '{{json .}}'` output
//! (one JSON object per line).
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::BTreeMap;
/// Whether docker can be used, and why not.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum DockerAvailability {
Ready {
client_version: String,
server_version: String,
/// Active docker context (`default`, `orbstack`, `desktop-linux`…).
context: Option<String>,
},
/// No `docker` executable was found.
NotInstalled,
/// The CLI is there but cannot reach a daemon (not started, wrong context…).
DaemonDown { message: String, context: Option<String> },
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerContainer {
pub id: String,
pub name: String,
pub image: String,
pub command: String,
/// `running`, `exited`, `paused`, `created`, `restarting`, `removing`, `dead`.
pub state: String,
/// Human status, e.g. `Up 2 hours (healthy)`.
pub status: String,
pub ports: String,
pub created_at: String,
/// `com.docker.compose.project` label.
pub compose_project: Option<String>,
pub compose_service: Option<String>,
/// `com.docker.compose.project.working_dir` label.
pub compose_working_dir: Option<String>,
/// `com.docker.compose.project.config_files` label (comma-separated in docker).
pub compose_files: Vec<String>,
}
impl DockerContainer {
pub fn running(&self) -> bool {
matches!(self.state.as_str(), "running" | "restarting" | "paused")
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerImage {
pub id: String,
/// `<none>` for dangling images.
pub repository: String,
pub tag: String,
pub size: String,
pub created_since: String,
}
impl DockerImage {
/// `repo:tag`, or the id for untagged images.
pub fn reference(&self) -> String {
if self.repository == "<none>" || self.repository.is_empty() {
short_id(&self.id).to_owned()
} else if self.tag == "<none>" || self.tag.is_empty() {
self.repository.clone()
} else {
format!("{}:{}", self.repository, self.tag)
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerVolume {
pub name: String,
pub driver: String,
pub mountpoint: String,
pub compose_project: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerNetwork {
pub id: String,
pub name: String,
pub driver: String,
pub scope: String,
pub compose_project: Option<String>,
}
/// Everything the Services tree shows, fetched in one go.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerSnapshot {
pub availability: DockerAvailability,
/// The docker executable in use, e.g. for `docker exec -it` in a terminal.
pub executable: Option<String>,
pub containers: Vec<DockerContainer>,
pub images: Vec<DockerImage>,
pub volumes: Vec<DockerVolume>,
pub networks: Vec<DockerNetwork>,
/// Lists that failed although the daemon answered (shown, not fatal).
pub errors: Vec<String>,
/// Global CLI options selecting the connection's daemon (for `docker exec` in a terminal).
#[serde(default)]
pub global_args: Vec<String>,
}
/// One relevant `docker events` entry.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerEvent {
/// `container`, `image`, `volume`, `network`.
pub object: String,
pub action: String,
pub id: String,
pub name: Option<String>,
}
/// A line of `docker logs` output.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DockerLogLine {
pub stderr: bool,
pub text: String,
}
/// The first 12 hex digits of an id (`sha256:` prefix dropped), as docker prints them.
pub fn short_id(id: &str) -> &str {
let id = id.strip_prefix("sha256:").unwrap_or(id);
&id[..id.len().min(12)]
}
/// Parses docker's `Labels` column (`a=b,c=d`). Values may themselves contain
/// commas (compose's `config_files`), so a segment without `=` continues the
/// previous value.
pub fn parse_labels(raw: &str) -> BTreeMap<String, String> {
let mut out: BTreeMap<String, String> = BTreeMap::new();
let mut last: Option<String> = None;
for seg in raw.split(',') {
match seg.split_once('=') {
Some((k, v)) if !k.is_empty() && !k.contains(['/', ' ']) => {
out.insert(k.to_owned(), v.to_owned());
last = Some(k.to_owned());
}
_ => {
if let Some(v) = last.as_ref().and_then(|k| out.get_mut(k)) {
v.push(',');
v.push_str(seg);
}
}
}
}
out
}
const COMPOSE_PROJECT: &str = "com.docker.compose.project";
const COMPOSE_SERVICE: &str = "com.docker.compose.service";
const COMPOSE_DIR: &str = "com.docker.compose.project.working_dir";
const COMPOSE_FILES: &str = "com.docker.compose.project.config_files";
fn lines<T: for<'de> Deserialize<'de>>(text: &str) -> impl Iterator<Item = T> + '_ {
text.lines().filter(|l| l.trim_start().starts_with('{')).filter_map(|l| match serde_json::from_str::<T>(l) {
Ok(v) => Some(v),
Err(e) => {
tracing::debug!("skipping unparsable docker line: {e}");
None
}
})
}
#[derive(Deserialize, Default)]
#[serde(rename_all = "PascalCase", default)]
struct RawContainer {
#[serde(rename = "ID")]
id: String,
names: String,
image: String,
command: String,
state: String,
status: String,
ports: String,
created_at: String,
labels: String,
}
/// `docker ps -a --no-trunc --format '{{json .}}'`.
pub fn parse_containers(text: &str) -> Vec<DockerContainer> {
let mut out: Vec<DockerContainer> = lines::<RawContainer>(text)
.map(|r| {
let mut labels = parse_labels(&r.labels);
let files = labels
.remove(COMPOSE_FILES)
.map(|f| f.split(',').map(str::trim).filter(|s| !s.is_empty()).map(str::to_owned).collect())
.unwrap_or_default();
// Older docker versions derive `State` from nothing: fall back on the status text.
let state = if r.state.is_empty() {
if r.status.starts_with("Up") { "running".into() } else { "exited".into() }
} else {
r.state.to_ascii_lowercase()
};
DockerContainer {
name: r.names.split(',').next().unwrap_or_default().trim_start_matches('/').to_owned(),
id: r.id,
image: r.image,
command: r.command.trim_matches('"').to_owned(),
state,
status: r.status,
ports: r.ports,
created_at: r.created_at,
compose_project: labels.remove(COMPOSE_PROJECT).filter(|s| !s.is_empty()),
compose_service: labels.remove(COMPOSE_SERVICE).filter(|s| !s.is_empty()),
compose_working_dir: labels.remove(COMPOSE_DIR).filter(|s| !s.is_empty()),
compose_files: files,
}
})
.collect();
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
#[derive(Deserialize, Default)]
#[serde(rename_all = "PascalCase", default)]
struct RawImage {
#[serde(rename = "ID")]
id: String,
repository: String,
tag: String,
size: String,
created_since: String,
}
/// `docker image ls --no-trunc --format '{{json .}}'`.
pub fn parse_images(text: &str) -> Vec<DockerImage> {
let mut out: Vec<DockerImage> = lines::<RawImage>(text)
.map(|r| DockerImage {
id: r.id,
repository: r.repository,
tag: r.tag,
size: r.size,
created_since: r.created_since,
})
.collect();
// Tagged images by name, dangling ones last.
out.sort_by(|a, b| {
(a.repository == "<none>", &a.repository, &a.tag).cmp(&(b.repository == "<none>", &b.repository, &b.tag))
});
out
}
#[derive(Deserialize, Default)]
#[serde(rename_all = "PascalCase", default)]
struct RawVolume {
name: String,
driver: String,
mountpoint: String,
labels: String,
}
/// `docker volume ls --format '{{json .}}'`.
pub fn parse_volumes(text: &str) -> Vec<DockerVolume> {
let mut out: Vec<DockerVolume> = lines::<RawVolume>(text)
.map(|r| DockerVolume {
compose_project: parse_labels(&r.labels).remove(COMPOSE_PROJECT).filter(|s| !s.is_empty()),
name: r.name,
driver: r.driver,
mountpoint: r.mountpoint,
})
.collect();
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
#[derive(Deserialize, Default)]
#[serde(rename_all = "PascalCase", default)]
struct RawNetwork {
#[serde(rename = "ID")]
id: String,
name: String,
driver: String,
scope: String,
labels: String,
}
/// `docker network ls --no-trunc --format '{{json .}}'`.
pub fn parse_networks(text: &str) -> Vec<DockerNetwork> {
let mut out: Vec<DockerNetwork> = lines::<RawNetwork>(text)
.map(|r| DockerNetwork {
compose_project: parse_labels(&r.labels).remove(COMPOSE_PROJECT).filter(|s| !s.is_empty()),
id: r.id,
name: r.name,
driver: r.driver,
scope: r.scope,
})
.collect();
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
#[derive(Deserialize, Default)]
#[serde(default)]
struct RawVersion {
#[serde(rename = "Client")]
client: Option<RawVersionPart>,
#[serde(rename = "Server")]
server: Option<RawVersionPart>,
}
#[derive(Deserialize, Default)]
#[serde(rename_all = "PascalCase", default)]
struct RawVersionPart {
version: String,
context: Option<String>,
}
/// Availability from `docker version --format '{{json .}}'` (stdout, stderr, exit status).
/// Without a daemon docker still prints the client half (with `"Server": null`)
/// and explains the problem on stderr.
pub fn parse_version(stdout: &str, stderr: &str, success: bool) -> DockerAvailability {
let parsed: RawVersion = stdout
.lines()
.find(|l| l.trim_start().starts_with('{'))
.and_then(|l| serde_json::from_str(l).ok())
.unwrap_or_default();
let context = parsed.client.as_ref().and_then(|c| c.context.clone()).filter(|c| !c.is_empty());
match (&parsed.server, success) {
(Some(server), true) if !server.version.is_empty() => DockerAvailability::Ready {
client_version: parsed.client.map(|c| c.version).unwrap_or_default(),
server_version: server.version.clone(),
context,
},
_ => {
let message = stderr
.lines()
.map(str::trim)
.find(|l| !l.is_empty())
.unwrap_or("Cannot connect to the Docker daemon")
.to_owned();
DockerAvailability::DaemonDown { message, context }
}
}
}
#[derive(Deserialize, Default)]
#[serde(default)]
struct RawEvent {
#[serde(rename = "Type")]
kind: String,
#[serde(rename = "Action")]
action: String,
#[serde(rename = "Actor")]
actor: RawActor,
/// Old-style field (`docker events` before API 1.22 had only these).
id: String,
status: String,
}
#[derive(Deserialize, Default)]
#[serde(rename_all = "PascalCase", default)]
struct RawActor {
#[serde(rename = "ID")]
id: String,
attributes: BTreeMap<String, String>,
}
/// One `docker events --format '{{json .}}'` line, or `None` for events that do
/// not change what the Services view shows (exec sessions, attach, resize…).
pub fn parse_event(line: &str) -> Option<DockerEvent> {
let raw: RawEvent = serde_json::from_str(line.trim()).ok()?;
let action = if raw.action.is_empty() { raw.status } else { raw.action };
let object = if raw.kind.is_empty() { "container".to_owned() } else { raw.kind };
if !matches!(object.as_str(), "container" | "image" | "volume" | "network") {
return None;
}
let verb = action.split(':').next().unwrap_or_default().trim();
let noisy = verb.starts_with("exec_")
|| matches!(
verb,
"attach" | "detach" | "resize" | "top" | "commit" | "copy" | "archive-path" | "extract-to-dir" | "export"
);
if noisy || verb.is_empty() {
return None;
}
let id = if raw.actor.id.is_empty() { raw.id } else { raw.actor.id };
Some(DockerEvent { name: raw.actor.attributes.get("name").cloned(), object, action: verb.to_owned(), id })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn labels_keep_commas_inside_values() {
let l = parse_labels(
"com.docker.compose.project=demo,com.docker.compose.project.config_files=/a/x.yml,/a/y.yml,k=v",
);
assert_eq!(l["com.docker.compose.project"], "demo");
assert_eq!(l["com.docker.compose.project.config_files"], "/a/x.yml,/a/y.yml");
assert_eq!(l["k"], "v");
assert!(parse_labels("").is_empty());
}
#[test]
fn short_ids() {
assert_eq!(short_id("sha256:0123456789abcdef"), "0123456789ab");
assert_eq!(short_id("abc"), "abc");
}
#[test]
fn image_reference() {
let img = |repo: &str, tag: &str| DockerImage {
id: "sha256:feedfacecafe0000".into(),
repository: repo.into(),
tag: tag.into(),
size: String::new(),
created_since: String::new(),
};
assert_eq!(img("postgres", "16").reference(), "postgres:16");
assert_eq!(img("<none>", "<none>").reference(), "feedfacecafe");
assert_eq!(img("local/app", "<none>").reference(), "local/app");
}
#[test]
fn version_without_daemon() {
let out = r#"{"Client":{"Version":"24.0.2","Context":"orbstack"},"Server":null}"#;
let err = "Cannot connect to the Docker daemon at unix:///x.sock. Is the docker daemon running?\n";
assert_eq!(
parse_version(out, err, false),
DockerAvailability::DaemonDown {
message: "Cannot connect to the Docker daemon at unix:///x.sock. Is the docker daemon running?".into(),
context: Some("orbstack".into())
}
);
let ok = r#"{"Client":{"Version":"27.1.1","Context":"default"},"Server":{"Version":"27.1.1"}}"#;
assert!(
matches!(parse_version(ok, "", true), DockerAvailability::Ready { ref server_version, .. } if server_version == "27.1.1")
);
}
#[test]
fn events_skip_exec_noise() {
let start = r#"{"status":"start","id":"c1","from":"nginx","Type":"container","Action":"start","Actor":{"ID":"c1","Attributes":{"name":"web"}},"scope":"local","time":1}"#;
assert_eq!(
parse_event(start),
Some(DockerEvent {
object: "container".into(),
action: "start".into(),
id: "c1".into(),
name: Some("web".into())
})
);
let exec = r#"{"Type":"container","Action":"exec_start: sh","Actor":{"ID":"c1","Attributes":{}}}"#;
assert_eq!(parse_event(exec), None);
let health = r#"{"Type":"container","Action":"health_status: healthy","Actor":{"ID":"c1","Attributes":{}}}"#;
assert_eq!(parse_event(health).map(|e| e.action), Some("health_status".into()));
let daemon = r#"{"Type":"daemon","Action":"reload","Actor":{"ID":"d","Attributes":{}}}"#;
assert_eq!(parse_event(daemon), None);
assert_eq!(parse_event("not json"), None);
}
}
+198
Переглянути файл
@@ -0,0 +1,198 @@
//! Child processes of the docker CLI: bounded runs and cancellable line streams.
use ide_core::{CancellationToken, IdeError, IdeResult};
use std::io::Read;
use std::process::{Child, Command, Stdio};
use std::sync::mpsc;
use std::time::{Duration, Instant};
pub(crate) struct Output {
pub stdout: String,
pub stderr: String,
pub success: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Stream {
Stdout,
Stderr,
}
fn prepare(cmd: &mut Command) {
cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped());
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
cmd.process_group(0);
}
ide_core::process::no_window(cmd);
}
fn spawn(cmd: &mut Command) -> IdeResult<Child> {
cmd.spawn().map_err(|e| IdeError::unavailable(format!("could not run docker: {e}")))
}
/// Kills the child and, on Unix, its whole process group (CLI plugins such as compose).
pub(crate) fn kill(child: &mut Child) {
#[cfg(unix)]
{
let pid = child.id() as libc::pid_t;
// SAFETY: plain syscall; the child leads its own process group (`prepare`).
unsafe {
libc::kill(-pid, libc::SIGTERM);
}
std::thread::sleep(Duration::from_millis(20));
unsafe {
libc::kill(-pid, libc::SIGKILL);
}
}
let _ = child.kill();
let _ = child.wait();
}
fn read_all(mut pipe: impl Read + Send + 'static) -> std::thread::JoinHandle<String> {
std::thread::spawn(move || {
let mut buf = Vec::new();
let _ = pipe.read_to_end(&mut buf);
String::from_utf8_lossy(&buf).into_owned()
})
}
/// Runs `cmd` to completion. Fails when it outlives `timeout` or `cancel` fires;
/// a non-zero exit is reported in [`Output::success`], not as an error.
pub(crate) fn run(cmd: Command, timeout: Duration, cancel: Option<&CancellationToken>) -> IdeResult<Output> {
run_with_input(cmd, timeout, cancel, None)
}
/// [`run`] with `input` written to the child's stdin (e.g. `--password-stdin`).
pub(crate) fn run_with_input(
mut cmd: Command,
timeout: Duration,
cancel: Option<&CancellationToken>,
input: Option<&str>,
) -> IdeResult<Output> {
prepare(&mut cmd);
if input.is_some() {
cmd.stdin(Stdio::piped());
}
let mut child = spawn(&mut cmd)?;
if let (Some(text), Some(mut stdin)) = (input, child.stdin.take()) {
// Dropping `stdin` afterwards closes it, so the CLI sees EOF.
let _ = std::io::Write::write_all(&mut stdin, text.as_bytes());
}
let out = child.stdout.take().map(read_all);
let err = child.stderr.take().map(read_all);
let deadline = Instant::now() + timeout;
let status = loop {
if let Some(status) = child.try_wait()? {
break status;
}
if cancel.is_some_and(CancellationToken::is_cancelled) {
kill(&mut child);
return Err(IdeError::cancelled());
}
if Instant::now() >= deadline {
kill(&mut child);
return Err(IdeError::unavailable("docker did not respond in time"));
}
std::thread::sleep(Duration::from_millis(10));
};
Ok(Output {
stdout: out.map(|t| t.join().unwrap_or_default()).unwrap_or_default(),
stderr: err.map(|t| t.join().unwrap_or_default()).unwrap_or_default(),
success: status.success(),
})
}
enum Event {
Line(Stream, String),
Closed,
}
fn spawn_reader(mut pipe: impl Read + Send + 'static, tx: mpsc::Sender<Event>, stream: Stream) {
std::thread::spawn(move || {
let mut buf = [0u8; 8192];
let mut pending: Vec<u8> = Vec::new();
let flush = |pending: &mut Vec<u8>| {
if !pending.is_empty() {
let line = String::from_utf8_lossy(pending).trim_end().to_string();
pending.clear();
let _ = tx.send(Event::Line(stream, line));
}
};
loop {
match pipe.read(&mut buf) {
Ok(0) | Err(_) => break,
Ok(n) => {
for &b in &buf[..n] {
// `\r` too: pull / build progress rewrites its line in place.
if b == b'\n' || b == b'\r' {
flush(&mut pending);
} else {
pending.push(b);
}
}
}
}
}
flush(&mut pending);
let _ = tx.send(Event::Closed);
});
}
/// Runs `cmd`, handing every output line to `on_line` until it exits or `cancel`
/// fires. Returns whether it exited successfully plus the last stderr lines.
pub(crate) fn stream(
mut cmd: Command,
cancel: &CancellationToken,
on_line: &mut dyn FnMut(Stream, String),
) -> IdeResult<(bool, Vec<String>)> {
if cancel.is_cancelled() {
return Err(IdeError::cancelled());
}
prepare(&mut cmd);
let mut child = spawn(&mut cmd)?;
let (tx, rx) = mpsc::channel::<Event>();
let mut open = 0;
if let Some(out) = child.stdout.take() {
spawn_reader(out, tx.clone(), Stream::Stdout);
open += 1;
}
if let Some(err) = child.stderr.take() {
spawn_reader(err, tx.clone(), Stream::Stderr);
open += 1;
}
drop(tx);
let mut stderr_tail: Vec<String> = Vec::new();
while open > 0 {
if cancel.is_cancelled() {
kill(&mut child);
return Err(IdeError::cancelled());
}
match rx.recv_timeout(Duration::from_millis(50)) {
Ok(Event::Line(stream, line)) => {
if stream == Stream::Stderr && !line.trim().is_empty() {
stderr_tail.push(line.clone());
if stderr_tail.len() > 20 {
stderr_tail.remove(0);
}
}
on_line(stream, line);
}
Ok(Event::Closed) => open -= 1,
Err(mpsc::RecvTimeoutError::Timeout) => {}
Err(mpsc::RecvTimeoutError::Disconnected) => break,
}
}
let status = loop {
if let Some(status) = child.try_wait()? {
break status;
}
if cancel.is_cancelled() {
kill(&mut child);
return Err(IdeError::cancelled());
}
std::thread::sleep(Duration::from_millis(10));
};
Ok((status.success(), stderr_tail))
}
+3
Переглянути файл
@@ -0,0 +1,3 @@
{"Containers":"N/A","CreatedAt":"2026-09-20 08:00:00 +0200 CEST","CreatedSince":"4 weeks ago","Digest":"<none>","ID":"sha256:aaaaaaaaaaaabbbbbbbbbbbbccccccccccccddddddddddddeeeeeeeeeeeeffff","Repository":"<none>","SharedSize":"N/A","Size":"120MB","Tag":"<none>","UniqueSize":"N/A","VirtualSize":"120MB"}
{"Containers":"N/A","CreatedAt":"2026-09-01 08:00:00 +0200 CEST","CreatedSince":"7 weeks ago","Digest":"<none>","ID":"sha256:1111111111112222222222223333333333334444444444445555555555556666","Repository":"postgres","SharedSize":"N/A","Size":"431MB","Tag":"16","UniqueSize":"N/A","VirtualSize":"431.4MB"}
{"Containers":"N/A","CreatedAt":"2026-09-02 08:00:00 +0200 CEST","CreatedSince":"7 weeks ago","Digest":"<none>","ID":"sha256:7777777777778888888888889999999999990000000000001111111111112222","Repository":"nginx","SharedSize":"N/A","Size":"192MB","Tag":"1.27","UniqueSize":"N/A","VirtualSize":"192MB"}
+3
Переглянути файл
@@ -0,0 +1,3 @@
{"CreatedAt":"2026-10-01 09:12:40.1 +0200 CEST","Driver":"bridge","ID":"f00dbabe0000111122223333444455556666777788889999aaaabbbbccccdddd","IPv6":"false","Internal":"false","Labels":"com.docker.compose.network=default,com.docker.compose.project=shop","Name":"shop_default","Scope":"local"}
{"CreatedAt":"2026-09-01 08:00:00 +0200 CEST","Driver":"bridge","ID":"0123012301230123012301230123012301230123012301230123012301230123","IPv6":"false","Internal":"false","Labels":"","Name":"bridge","Scope":"local"}
{"CreatedAt":"2026-09-01 08:00:00 +0200 CEST","Driver":"host","ID":"4567456745674567456745674567456745674567456745674567456745674567","IPv6":"false","Internal":"false","Labels":"","Name":"host","Scope":"local"}
+3
Переглянути файл
@@ -0,0 +1,3 @@
{"Command":"\"docker-entrypoint.sh postgres\"","CreatedAt":"2026-10-01 09:12:44 +0200 CEST","ID":"4f2b8c1d9e0a7b6c5d4e3f2a1b0c9d8e7f6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c","Image":"postgres:16","Labels":"com.docker.compose.config-hash=abc,com.docker.compose.project=shop,com.docker.compose.project.config_files=/work/shop/compose.yaml,/work/shop/compose.override.yaml,com.docker.compose.project.working_dir=/work/shop,com.docker.compose.service=db","LocalVolumes":"1","Mounts":"shop_pgdata","Names":"shop-db-1","Networks":"shop_default","Ports":"0.0.0.0:5432->5432/tcp","RunningFor":"3 weeks ago","Size":"0B","State":"running","Status":"Up 2 hours (healthy)"}
{"Command":"\"/docker-entrypoint.sh nginx -g 'daemon off;'\"","CreatedAt":"2026-10-02 10:00:00 +0200 CEST","ID":"9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b","Image":"nginx:1.27","Labels":"maintainer=NGINX Docker Maintainers <[email protected]>","LocalVolumes":"0","Mounts":"","Names":"/web","Networks":"bridge","Ports":"","RunningFor":"2 weeks ago","Size":"0B","State":"exited","Status":"Exited (0) 3 days ago"}
not json: a warning line docker sometimes prints
+2
Переглянути файл
@@ -0,0 +1,2 @@
{"Availability":"N/A","Driver":"local","Group":"N/A","Labels":"com.docker.compose.project=shop,com.docker.compose.version=2.29.1,com.docker.compose.volume=pgdata","Links":"N/A","Mountpoint":"/var/lib/docker/volumes/shop_pgdata/_data","Name":"shop_pgdata","Scope":"local","Size":"N/A","Status":"N/A"}
{"Availability":"N/A","Driver":"local","Group":"N/A","Labels":"","Links":"N/A","Mountpoint":"/var/lib/docker/volumes/0a1b/_data","Name":"0a1b","Scope":"local","Size":"N/A","Status":"N/A"}
+52
Переглянути файл
@@ -0,0 +1,52 @@
//! Parsing of recorded `docker … --format '{{json .}}'` output.
use ide_docker::{parse_containers, parse_images, parse_networks, parse_volumes};
const PS: &str = include_str!("fixtures/ps.jsonl");
const IMAGES: &str = include_str!("fixtures/images.jsonl");
const VOLUMES: &str = include_str!("fixtures/volumes.jsonl");
const NETWORKS: &str = include_str!("fixtures/networks.jsonl");
#[test]
fn containers_with_compose_labels() {
let c = parse_containers(PS);
assert_eq!(c.len(), 2, "non-JSON lines are skipped");
let db = &c[0];
assert_eq!(db.name, "shop-db-1");
assert_eq!(db.state, "running");
assert!(db.running());
assert_eq!(db.status, "Up 2 hours (healthy)");
assert_eq!(db.command, "docker-entrypoint.sh postgres");
assert_eq!(db.compose_project.as_deref(), Some("shop"));
assert_eq!(db.compose_service.as_deref(), Some("db"));
assert_eq!(db.compose_working_dir.as_deref(), Some("/work/shop"));
assert_eq!(db.compose_files, ["/work/shop/compose.yaml", "/work/shop/compose.override.yaml"]);
assert_eq!(db.ports, "0.0.0.0:5432->5432/tcp");
let web = &c[1];
assert_eq!(web.name, "web", "leading slash of legacy names is dropped");
assert!(!web.running());
assert_eq!(web.compose_project, None);
}
#[test]
fn images_sorted_with_dangling_last() {
let i = parse_images(IMAGES);
let refs: Vec<String> = i.iter().map(|i| i.reference()).collect();
assert_eq!(refs, ["nginx:1.27", "postgres:16", "aaaaaaaaaaaa"]);
assert_eq!(i[1].size, "431MB");
assert_eq!(i[1].created_since, "7 weeks ago");
}
#[test]
fn volumes_and_networks() {
let v = parse_volumes(VOLUMES);
assert_eq!(v.iter().map(|v| v.name.as_str()).collect::<Vec<_>>(), ["0a1b", "shop_pgdata"]);
assert_eq!(v[1].compose_project.as_deref(), Some("shop"));
assert_eq!(v[1].mountpoint, "/var/lib/docker/volumes/shop_pgdata/_data");
let n = parse_networks(NETWORKS);
assert_eq!(n.iter().map(|n| n.name.as_str()).collect::<Vec<_>>(), ["bridge", "host", "shop_default"]);
assert_eq!(n[2].compose_project.as_deref(), Some("shop"));
assert_eq!(n[1].driver, "host");
}
+18
Переглянути файл
@@ -0,0 +1,18 @@
[package]
name = "ide-github"
description = "GitHub pull requests through the gh CLI"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
ide-core.workspace = true
ide-vcs.workspace = true
serde.workspace = true
serde_json.workspace = true
specta.workspace = true
tracing.workspace = true
which = "8"
[target.'cfg(unix)'.dependencies]
libc = "0.2"
Loaded 100 of 2587 files, more files were not shown because too many files have changed in this diff. Show more