From 4021bdd0d18df470807d4443233b09babc072f98 Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Sun, 6 Dec 2026 03:45:10 +0800 Subject: [PATCH] fix(plugins): ask line markers again after a save of a file their plugin covers (#8272) A plugin's gutter markers can be built from another file (MyBatis arrows in Mapper.xml come from the Java mapper), but the editor asked line-markers only after its own document changed. A save now asks again, debounced, in the visible editors of the same project root whose language a plugin declares along with the saved file's ("*" covers all); hidden kept tabs ask once shown. plugin_line_marker_languages(path) returns those languages from the usable plugins' declarations without loading plugin code. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- crates/ide-host/src/ops/lsp.rs | 18 ++ crates/ide-host/src/plugins/providers.rs | 52 +++++- crates/ide-host/tests/plugins.rs | 49 ++++++ plugins/README.md | 6 +- src-tauri/tests/commands.snap | 1 + src/bindings.ts | 6 + src/features/plugins/lineMarkerGutter.test.ts | 158 +++++++++++++++++- src/features/plugins/lineMarkerGutter.ts | 65 ++++++- 8 files changed, 341 insertions(+), 14 deletions(-) diff --git a/crates/ide-host/src/ops/lsp.rs b/crates/ide-host/src/ops/lsp.rs index 027d19517c..9d548e363f 100644 --- a/crates/ide-host/src/ops/lsp.rs +++ b/crates/ide-host/src/ops/lsp.rs @@ -715,6 +715,23 @@ pub async fn plugin_line_markers(host: &Host, window: &str, doc: DocId) -> IdeRe Ok(crate::plugins::LineMarkers { markers, late }) } +/// The languages whose plugin line markers a save of `path` can change: those a plugin declares for +/// `line-markers` along with the file's language (`"*"` for every language). Does not load plugin +/// code. By path, not document: closing a tab saves it and then closes its document. +#[command(trust = trusted)] +pub async fn plugin_line_marker_languages( + host: &Host, + window: &str, + path: std::path::PathBuf, +) -> IdeResult> { + let Some(plugins) = editor_providers(ProviderKind::LineMarkers) else { + return Ok(Vec::new()); + }; + let project = host.project(window)?; + let language = blocking(move || Ok(project.documents.language_of(&path, None).language.0)).await?; + Ok(plugins.line_marker_languages_with(&language)) +} + /// Where other files import the document's file (Find Usages on a file, a Vue component's /// usages). Cancel with `cancel_request(request)`. #[command(trust = trusted)] @@ -3832,6 +3849,7 @@ ide_command::domain!( plugin_color_presentations, plugin_formatters, plugin_language_visuals, + plugin_line_marker_languages, plugin_line_markers, prepare_workspace_edit, redo_workspace_edit, diff --git a/crates/ide-host/src/plugins/providers.rs b/crates/ide-host/src/plugins/providers.rs index 3cb0df608d..c6357926c4 100644 --- a/crates/ide-host/src/plugins/providers.rs +++ b/crates/ide-host/src/plugins/providers.rs @@ -128,6 +128,29 @@ impl Providers { out } + /// Each plugin's languages declared for `kind`, in declaration order. + fn languages_by_plugin(&self, kind: ProviderKind) -> Vec<(String, Vec)> { + let mut out: Vec<(String, Vec)> = Vec::new(); + for (_, plugin, kinds, languages) in self.items.read().iter() { + if !kinds.contains(&kind) { + continue; + } + let at = match out.iter().position(|(id, _)| id == plugin) { + Some(at) => at, + None => { + out.push((plugin.clone(), Vec::new())); + out.len() - 1 + } + }; + for language in languages { + if !out[at].1.contains(language) { + out[at].1.push(language.clone()); + } + } + } + out + } + /// One coherent declaration snapshot, preserving the first match per plugin. fn matching>(&self, kind: ProviderKind, language: &str) -> T { let items = self.items.read(); @@ -301,6 +324,11 @@ use std::collections::BTreeSet; use std::sync::Weak; use std::time::{Duration, Instant}; +fn usable_provider(p: &LoadedPlugin) -> bool { + let m = p.meta.lock(); + matches!(m.status, PluginStatus::Active | PluginStatus::Inactive) && m.pending.is_empty() +} + /// What the plugins answered for completion in the editor. #[derive(Debug, Default)] pub(crate) struct EditorCompletion { @@ -342,13 +370,29 @@ impl PluginHost { .plugins_for(kind, language) .into_iter() .filter_map(|id| self.get(&id).ok()) - .filter(|p| { - let m = p.meta.lock(); - matches!(m.status, PluginStatus::Active | PluginStatus::Inactive) && m.pending.is_empty() - }) + .filter(|p| usable_provider(p)) .collect() } + /// The languages a usable plugin declares for `line-markers` along with `language` (`"*"` for + /// every language), without loading any plugin: a saved file of `language` can change the + /// markers of the files of these languages. + pub fn line_marker_languages_with(&self, language: &str) -> Vec { + let mut out: Vec = Vec::new(); + for (id, languages) in self.providers.languages_by_plugin(ProviderKind::LineMarkers) { + let covers = languages.iter().any(|l| l == "*" || l == language); + if !covers || !self.get(&id).is_ok_and(|p| usable_provider(&p)) { + continue; + } + for l in languages { + if !out.contains(&l) { + out.push(l); + } + } + } + out + } + /// Edit-path calls (soft / hard deadline) to every usable plugin that /// declares `kind` for `language`, in plugin order, leaving out those that /// failed or were late. Each plugin runs on its own thread, so the calls diff --git a/crates/ide-host/tests/plugins.rs b/crates/ide-host/tests/plugins.rs index 648d15183b..c787511fac 100644 --- a/crates/ide-host/tests/plugins.rs +++ b/crates/ide-host/tests/plugins.rs @@ -3465,7 +3465,13 @@ fn official_license_header_reports_fixes_and_scans() { #[test] fn official_mybatis_navigates_between_interface_and_xml() { let f = fixture(Limits::default()); + assert!(f.host.line_marker_languages_with("java").is_empty()); f.install(&official("mybatis")); + // One plugin's two declarations: a saved Kotlin file can change the XML's markers too. + let mut languages = f.host.line_marker_languages_with("kotlin"); + languages.sort(); + assert_eq!(languages, ["java", "kotlin", "xml"]); + assert!(f.host.line_marker_languages_with("markdown").is_empty()); let java_path = f.project.join("src/main/java/com/acme/UserMapper.java"); let xml_path = f.project.join("src/main/resources/mapper/UserMapper.xml"); std::fs::create_dir_all(java_path.parent().unwrap()).unwrap(); @@ -8903,3 +8909,46 @@ fn line_markers_too_late_from_one_provider_keep_the_others() { assert!(markers.iter().all(|(id, _)| id == "on-time")); }); } + +/// A disabled plugin's languages don't make a save ask for line markers again. +#[test] +fn line_marker_languages_leave_out_disabled_plugins() { + let f = fixture(Limits::default()); + f.install(&official("mybatis")); + assert_eq!(f.host.line_marker_languages_with("xml").len(), 3); + f.host.set_enabled("openidea.mybatis", false).unwrap(); + assert!(f.host.line_marker_languages_with("xml").is_empty()); +} + +/// Another plugin's languages don't count: only one plugin declaring both links two files. +#[test] +fn line_marker_languages_come_from_the_plugins_covering_the_saved_language() { + let f = fixture(Limits::default()); + let tmp = tempfile::tempdir().unwrap(); + let install = |id: &str, languages: &str| { + let pkg = tmp.path().join(id); + copy_dir(&misbehave_pkg(), &pkg); + std::fs::write( + pkg.join("plugin.toml"), + format!( + "id = \"{id}\"\nname = \"{id}\"\nversion = \"0.0.1\"\nengines.openidea = \"^1.0\"\n\ + wasm = \"dist/plugin.wasm\"\nactivation = [\"on-language:text\"]\n\ + [[contributes.providers]]\nkinds = [\"line-markers\"]\nlanguages = {languages}\n" + ), + ) + .unwrap(); + f.install(&pkg); + }; + install("text-markers", r#"["text"]"#); + install("md-markers", r#"["markdown"]"#); + // Another plugin's languages don't come along. + assert_eq!(f.host.line_marker_languages_with("text"), ["text"]); + assert_eq!(f.host.line_marker_languages_with("markdown"), ["markdown"]); + assert!(f.host.line_marker_languages_with("java").is_empty()); + install("any-markers", r#"["*"]"#); + // `any-markers` covers every language, so every save reaches its editors. + let mut languages = f.host.line_marker_languages_with("text"); + languages.sort(); + assert_eq!(languages, ["*", "text"]); + assert_eq!(f.host.line_marker_languages_with("java"), ["*"]); +} diff --git a/plugins/README.md b/plugins/README.md index be77129b64..2f75bb62b0 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -1936,7 +1936,11 @@ Editor providers run under the edit-path budget: 50 ms, after which the result is dropped, and an interruption at 500 ms. Every plugin providing a kind is asked at the same time, so each has the whole budget. When a plugin's `line-markers` call is dropped, the editor keeps the markers it -shows from that plugin until the next edit or plugin event asks it again. +shows from that plugin until the next edit or plugin event asks it again. Saving +a file also asks `line-markers` again in the visible editors of the same +project root whose language a plugin declares along with the saved file's +(hidden tabs ask once shown), so markers built from other files follow their +saved changes. Providers run in the plugin's one instance, with its commands, events and views: they see the same memory (Checkstyle answers quick fixes from the diff --git a/src-tauri/tests/commands.snap b/src-tauri/tests/commands.snap index cb0420e919..307eaefc55 100644 --- a/src-tauri/tests/commands.snap +++ b/src-tauri/tests/commands.snap @@ -589,6 +589,7 @@ plugin_grant outside_restricted desktop bin plugin_install_dir outside_restricted desktop bindings async plugin_installed_intellij_themes always desktop bindings async plugin_language_visuals trusted project_host bindings async +plugin_line_marker_languages trusted project_host bindings async plugin_line_markers trusted project_host bindings async plugin_link trusted desktop bindings async plugin_list always desktop bindings async diff --git a/src/bindings.ts b/src/bindings.ts index df85711ec6..884b56b757 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -1653,6 +1653,12 @@ export const commands = { /** Theme plugins installed in the JetBrains IDEs of this user, the ones in use first. */ pluginInstalledIntellijThemes: () => typedError(__TAURI_INVOKE("plugin_installed_intellij_themes")), pluginLanguageVisuals: (doc: DocId) => typedError(__TAURI_INVOKE("plugin_language_visuals", { doc })), + /** + * The languages whose plugin line markers a save of `path` can change: those a plugin declares for + * `line-markers` along with the file's language (`"*"` for every language). Does not load plugin + * code. By path, not document: closing a tab saves it and then closes its document. + */ + pluginLineMarkerLanguages: (path: string) => typedError(__TAURI_INVOKE("plugin_line_marker_languages", { path })), /** Gutter markers of plugin `line-markers` providers for an open document. */ pluginLineMarkers: (doc: DocId) => typedError(__TAURI_INVOKE("plugin_line_markers", { doc })), /** Links a development plugin folder (`openidea-plugin link` does the same from a terminal). */ diff --git a/src/features/plugins/lineMarkerGutter.test.ts b/src/features/plugins/lineMarkerGutter.test.ts index 47aeb0c45a..c796f7a3ec 100644 --- a/src/features/plugins/lineMarkerGutter.test.ts +++ b/src/features/plugins/lineMarkerGutter.test.ts @@ -1,7 +1,8 @@ -import { afterEach, expect, it, vi } from "vitest"; +import { afterEach, expect, it, onTestFinished, vi } from "vitest"; import { EditorState } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; -import { viewShown, type EditorDocContext } from "@/editor/api"; +import { _emitEditorEvent, viewShown, type EditorDocContext } from "@/editor/api"; +import { useProject } from "@/platform/project"; import type { LineMarkerInfo, PluginEvent } from "@/bindings"; import { iconAreaExtension } from "@/editor/core/cm/iconArea"; import { pluginLineMarkerGutter } from "./lineMarkerGutter"; @@ -9,13 +10,17 @@ import { pluginIconName } from "./pluginIcon"; import { projectMarkerIcon } from "./markerIcons"; const mock = vi.hoisted(() => ({ markers: vi.fn(), + languages: vi.fn(), assets: vi.fn(), event: undefined as ((event: PluginEvent) => void) | undefined, off: vi.fn(), })); vi.mock(import("@/platform/ipc"), async (original) => { const actual = await original(); - return { ...actual, commands: { ...actual.commands, pluginLineMarkers: mock.markers } }; + return { + ...actual, + commands: { ...actual.commands, pluginLineMarkers: mock.markers, pluginLineMarkerLanguages: mock.languages }, + }; }); vi.mock("./backend", () => ({ pluginsBackend: () => ({ @@ -298,3 +303,150 @@ it("does not show an older answer over a newer one asked while the tab was hidde await vi.advanceTimersByTimeAsync(0); expect(labels(view)).toEqual([]); }); + +// MyBatis: a statement's markers in the XML come from the Java mapper's methods. +const covered = (languages: string[]) => mock.languages.mockResolvedValue({ status: "ok", data: languages }); +const save = async (path = "/w/a/Mapper.java") => { + _emitEditorEvent({ type: "docSaved", doc: 1, path }); + await vi.advanceTimersByTimeAsync(450); +}; +function xmlEditor(path = "/w/a/Mapper.xml", doc = 2) { + return new EditorView({ + parent: document.body, + state: EditorState.create({ + doc: "