дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
Adds an optional syntax-decorations-provider export on plugin API 1.20: a plugin paints foreground colours on ranges of an open text editor from a bounded slice of the editor's own syntax tree (no second parser). A mark is a theme token (rainbow-1 … rainbow-5, mapped onto --syn-rainbow-*) or its own dark and light colours (#rrggbb / #rrggbbaa, the set-styled format; SyntaxMark::colored in the SDK). The host validates the snapshot, drops marks with unknown tokens or invalid colours, debounces and cancels superseded requests, and the editor stylesheet picks the dark or light colour, so a theme change does not ask the plugin again. Sticky Scroll lines keep the marks. Co-authored-by: fsk <[email protected]>
358 рядки
13 KiB
Rust
358 рядки
13 KiB
Rust
mod common;
|
|
|
|
use ide_core::{CancellationToken, Pos, Range, RequestId};
|
|
use ide_host::plugins::{Consent, PluginHost};
|
|
use ide_host::{Host, plugins::commands::plugin_syntax_decorations};
|
|
use ide_plugin::{Limits, ProviderDocument, SyntaxNodeSnapshot};
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
fn fixture_dir() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ide-plugin/tests/fixtures/syntax-decorations")
|
|
}
|
|
|
|
fn host(dir: &Path) -> PluginHost {
|
|
let limits = ide_plugin::Limits {
|
|
edit_soft: Duration::from_secs(5),
|
|
edit_hard: Duration::from_secs(10),
|
|
..ide_plugin::Limits::default()
|
|
};
|
|
PluginHost::new(dir.join("plugins"), Arc::new(|_: Option<&str>, _| {}), limits).unwrap()
|
|
}
|
|
|
|
fn doc(dir: &Path, language: &str, version: u64) -> ProviderDocument {
|
|
ProviderDocument { path: dir.join("a.js"), language: language.into(), version, text: "const value = 1;\n".into() }
|
|
}
|
|
|
|
fn node(name: &str, parent: Option<u32>) -> SyntaxNodeSnapshot {
|
|
SyntaxNodeSnapshot { name: name.into(), range: Range::new(Pos::new(0, 6), Pos::new(0, 11)), parent }
|
|
}
|
|
|
|
fn pair() -> Vec<SyntaxNodeSnapshot> {
|
|
vec![node("Script", None), node("VariableName", Some(0))]
|
|
}
|
|
|
|
fn asked() -> Range {
|
|
Range::new(Pos::new(0, 0), Pos::new(0, 16))
|
|
}
|
|
|
|
#[test]
|
|
fn a_parented_node_is_marked_with_the_theme_token() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = host(tmp.path());
|
|
host.install_from_dir(&fixture_dir(), &Consent::all()).unwrap();
|
|
let marks = host.provide_syntax_decorations(
|
|
&doc(tmp.path(), "javascript", 4),
|
|
&[asked()],
|
|
&pair(),
|
|
&CancellationToken::new(),
|
|
);
|
|
assert_eq!(marks.len(), 1);
|
|
assert_eq!(marks[0].plugin, "test.syntax-decorations");
|
|
assert_eq!(marks[0].token, "rainbow-1");
|
|
assert_eq!(marks[0].range, Range::new(Pos::new(0, 6), Pos::new(0, 11)));
|
|
}
|
|
|
|
#[test]
|
|
fn colour_marks_are_normalised_and_invalid_ones_dropped() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = host(tmp.path());
|
|
host.install_from_dir(&fixture_dir(), &Consent::all()).unwrap();
|
|
let marks = host.provide_syntax_decorations(
|
|
&doc(tmp.path(), "javascript", 7),
|
|
&[asked()],
|
|
&pair(),
|
|
&CancellationToken::new(),
|
|
);
|
|
assert_eq!(marks.len(), 1, "{marks:?}");
|
|
assert_eq!(
|
|
marks[0].foreground,
|
|
Some(ide_plugin::SyntaxForeground { dark: "#80cbc4".into(), light: "#23766f".into() })
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_zero_version_and_another_language_produce_nothing() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = host(tmp.path());
|
|
host.install_from_dir(&fixture_dir(), &Consent::all()).unwrap();
|
|
assert!(
|
|
host.provide_syntax_decorations(
|
|
&doc(tmp.path(), "javascript", 0),
|
|
&[asked()],
|
|
&pair(),
|
|
&CancellationToken::new()
|
|
)
|
|
.is_empty()
|
|
);
|
|
assert!(
|
|
host.provide_syntax_decorations(&doc(tmp.path(), "yaml", 4), &[asked()], &pair(), &CancellationToken::new())
|
|
.is_empty()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_invalid_snapshot_does_not_call_the_plugin() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = host(tmp.path());
|
|
host.install_from_dir(&fixture_dir(), &Consent::all()).unwrap();
|
|
let marks = host.provide_syntax_decorations(
|
|
&doc(tmp.path(), "javascript", 4),
|
|
&[asked()],
|
|
&[node("A", Some(9))],
|
|
&CancellationToken::new(),
|
|
);
|
|
assert!(marks.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn declining_editor_read_disables_the_provider() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = host(tmp.path());
|
|
let pkg = tmp.path().join("optional");
|
|
std::fs::create_dir_all(pkg.join("dist")).unwrap();
|
|
std::fs::copy(fixture_dir().join("dist/plugin.wasm"), pkg.join("dist/plugin.wasm")).unwrap();
|
|
let manifest = std::fs::read_to_string(fixture_dir().join("plugin.toml")).unwrap();
|
|
std::fs::write(pkg.join("plugin.toml"), manifest.replace("required =", "optional =")).unwrap();
|
|
host.install_from_dir(&pkg, &Consent::default()).unwrap();
|
|
assert!(host.syntax_decoration_languages().is_empty());
|
|
assert!(
|
|
host.provide_syntax_decorations(
|
|
&doc(tmp.path(), "javascript", 4),
|
|
&[asked()],
|
|
&pair(),
|
|
&CancellationToken::new(),
|
|
)
|
|
.is_empty()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn uninstalling_the_plugin_drops_its_marks() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = host(tmp.path());
|
|
host.install_from_dir(&fixture_dir(), &Consent::all()).unwrap();
|
|
assert_eq!(
|
|
host.provide_syntax_decorations(
|
|
&doc(tmp.path(), "javascript", 4),
|
|
&[asked()],
|
|
&pair(),
|
|
&CancellationToken::new()
|
|
)
|
|
.len(),
|
|
1
|
|
);
|
|
host.uninstall("test.syntax-decorations").unwrap();
|
|
assert!(
|
|
host.provide_syntax_decorations(
|
|
&doc(tmp.path(), "javascript", 4),
|
|
&[asked()],
|
|
&pair(),
|
|
&CancellationToken::new()
|
|
)
|
|
.is_empty()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_kind_needs_plugin_api_1_20() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = host(tmp.path());
|
|
let pkg = tmp.path().join("old");
|
|
std::fs::create_dir_all(pkg.join("dist")).unwrap();
|
|
std::fs::copy(fixture_dir().join("dist/plugin.wasm"), pkg.join("dist/plugin.wasm")).unwrap();
|
|
let manifest = std::fs::read_to_string(fixture_dir().join("plugin.toml")).unwrap();
|
|
std::fs::write(pkg.join("plugin.toml"), manifest.replace("\"^1.20\"", "\"^1.0\"")).unwrap();
|
|
let err = host.install_from_dir(&pkg, &Consent::all()).unwrap_err();
|
|
assert!(err.message.contains("new in plugin API 1.20"), "{}", err.message);
|
|
}
|
|
|
|
fn misbehave_wasm() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ide-plugin/tests/fixtures/misbehave/dist/plugin.wasm")
|
|
}
|
|
|
|
/// The shared misbehave component, declared here as a syntax-decoration provider.
|
|
/// Its own manifest stays on `^1.0` and does not advertise this kind.
|
|
fn misbehave_provider(dir: &Path) -> PathBuf {
|
|
let pkg = dir.join("misbehave-syntax");
|
|
std::fs::create_dir_all(pkg.join("dist")).unwrap();
|
|
std::fs::copy(misbehave_wasm(), pkg.join("dist/plugin.wasm")).unwrap();
|
|
std::fs::write(
|
|
pkg.join("plugin.toml"),
|
|
"\
|
|
id = \"misbehave\"
|
|
name = \"Misbehave\"
|
|
version = \"0.0.2\"
|
|
engines.openidea = \"^1.20\"
|
|
wasm = \"dist/plugin.wasm\"
|
|
|
|
[capabilities]
|
|
required = [\"editor.read\"]
|
|
|
|
[[contributes.providers]]
|
|
kinds = [\"syntax-decorations\"]
|
|
languages = [\"javascript\"]
|
|
",
|
|
)
|
|
.unwrap();
|
|
pkg
|
|
}
|
|
|
|
fn tight(dir: &Path) -> PluginHost {
|
|
let limits = Limits {
|
|
edit_soft: Duration::from_millis(50),
|
|
edit_hard: Duration::from_millis(400),
|
|
fuel: u64::MAX,
|
|
restart_backoff: Duration::ZERO,
|
|
..Limits::default()
|
|
};
|
|
PluginHost::new(dir.join("plugins"), Arc::new(|_: Option<&str>, _| {}), limits).unwrap()
|
|
}
|
|
|
|
/// The first name is how misbehave fails. The second node is what the fixture marks.
|
|
fn signal(name: &str) -> Vec<SyntaxNodeSnapshot> {
|
|
vec![
|
|
SyntaxNodeSnapshot { name: name.into(), range: Range::new(Pos::new(0, 0), Pos::new(0, 5)), parent: None },
|
|
node("VariableName", Some(0)),
|
|
]
|
|
}
|
|
|
|
fn install_pair(host: &PluginHost, dir: &Path) {
|
|
host.install_from_dir(&fixture_dir(), &Consent::all()).unwrap();
|
|
host.install_from_dir(&misbehave_provider(dir), &Consent::all()).unwrap();
|
|
}
|
|
|
|
fn ready(host: &PluginHost, dir: &Path) {
|
|
let document = doc(dir, "javascript", 4);
|
|
let mut ok = false;
|
|
for _ in 0..30 {
|
|
let marks = host.provide_syntax_decorations(&document, &[asked()], &pair(), &CancellationToken::new());
|
|
let fixture = marks.iter().any(|m| m.plugin == "test.syntax-decorations");
|
|
let mis = host.info_of("misbehave").is_ok_and(|info| info.stats.activations >= 1);
|
|
if fixture && mis {
|
|
ok = true;
|
|
break;
|
|
}
|
|
std::thread::sleep(Duration::from_millis(40));
|
|
}
|
|
assert!(ok, "plugins did not finish activating");
|
|
}
|
|
|
|
fn keeps_the_other(marks: &[ide_plugin::SyntaxDecoration]) {
|
|
assert!(marks.iter().any(|m| m.plugin == "test.syntax-decorations" && m.token == "rainbow-1"), "{marks:?}");
|
|
assert!(marks.iter().all(|m| m.plugin != "misbehave"), "{marks:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_trapped_plugin_drops_its_marks_and_leaves_the_other() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = tight(tmp.path());
|
|
install_pair(&host, tmp.path());
|
|
ready(&host, tmp.path());
|
|
let document = doc(tmp.path(), "javascript", 4);
|
|
keeps_the_other(&host.provide_syntax_decorations(
|
|
&document,
|
|
&[asked()],
|
|
&signal("panic"),
|
|
&CancellationToken::new(),
|
|
));
|
|
assert_eq!(host.provide_syntax_decorations(&document, &[asked()], &pair(), &CancellationToken::new()).len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn a_slow_plugin_is_dropped_and_the_other_answers() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = tight(tmp.path());
|
|
install_pair(&host, tmp.path());
|
|
ready(&host, tmp.path());
|
|
let document = doc(tmp.path(), "javascript", 4);
|
|
keeps_the_other(&host.provide_syntax_decorations(
|
|
&document,
|
|
&[asked()],
|
|
&signal("sleep:150"),
|
|
&CancellationToken::new(),
|
|
));
|
|
let info = host.info_of("misbehave").unwrap();
|
|
assert!(info.stats.slow_calls >= 1, "{:?}", info.stats);
|
|
assert_eq!(info.stats.crashes, 0, "{:?}", info.stats);
|
|
assert_eq!(host.provide_syntax_decorations(&document, &[asked()], &pair(), &CancellationToken::new()).len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn uninstalling_a_spinning_plugin_keeps_the_other_answering() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let host = Arc::new(tight(tmp.path()));
|
|
install_pair(&host, tmp.path());
|
|
ready(&host, tmp.path());
|
|
let document = doc(tmp.path(), "javascript", 4);
|
|
let running = {
|
|
let host = host.clone();
|
|
let document = document.clone();
|
|
std::thread::spawn(move || {
|
|
host.provide_syntax_decorations(&document, &[asked()], &signal("spin"), &CancellationToken::new())
|
|
})
|
|
};
|
|
std::thread::sleep(Duration::from_millis(20));
|
|
host.uninstall("misbehave").unwrap();
|
|
keeps_the_other(&running.join().unwrap());
|
|
assert_eq!(host.provide_syntax_decorations(&document, &[asked()], &pair(), &CancellationToken::new()).len(), 1);
|
|
}
|
|
|
|
fn bump(project: &ide_host::Project, doc: ide_core::DocId) -> u32 {
|
|
project
|
|
.documents
|
|
.apply_edits(
|
|
doc,
|
|
&[ide_core::TextEdit { range: Range::new(Pos::new(0, 16), Pos::new(0, 16)), new_text: " ".into() }],
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn the_code_insight_limit_gates_the_provider_without_a_fixed_character_ceiling() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let root = dunce::canonicalize(tmp.path()).unwrap();
|
|
let file = root.join("a.js");
|
|
std::fs::write(&file, "const value = 1;\n").unwrap();
|
|
let plugins =
|
|
Arc::new(PluginHost::new(root.join("plugins"), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap());
|
|
plugins.install_from_dir(&fixture_dir(), &Consent::all()).unwrap();
|
|
|
|
let open_host = Host::new(common::host_config(&root.join("open")));
|
|
open_host.set_project_trust(&root, true, false).await.unwrap();
|
|
let open = open_host.open_project("main", &root).unwrap();
|
|
let small = open.documents.open(&file).unwrap();
|
|
assert!(!small.large);
|
|
assert!(bump(&open, small.id) >= 1);
|
|
let allowed =
|
|
plugin_syntax_decorations(&open_host, &plugins, "main", RequestId(1), small.id, vec![asked()], pair(), 2)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(allowed.marks.len(), 1, "{allowed:?}");
|
|
assert_eq!(allowed.marks[0].token, "rainbow-1");
|
|
|
|
let cancelled = RequestId(2);
|
|
open_host.cancel_request(cancelled);
|
|
let error = plugin_syntax_decorations(&open_host, &plugins, "main", cancelled, small.id, vec![asked()], pair(), 3)
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(error.kind, ide_core::error::ErrorKind::Cancelled);
|
|
|
|
let limited_host = Host::new(common::host_config(&root.join("limited")));
|
|
limited_host.set_project_trust(&root, true, false).await.unwrap();
|
|
let limited = limited_host.open_project("main", &root).unwrap();
|
|
let defaults = ide_workspace::FileDefaults { max_code_insight_bytes: 1, ..Default::default() };
|
|
limited.documents.set_file_defaults(defaults);
|
|
let large = limited.documents.open(&file).unwrap();
|
|
assert!(large.large, "a one-byte insight limit marks the file without allocating a huge buffer");
|
|
assert!(bump(&limited, large.id) >= 1, "the gate is only visible once the fixture would have answered");
|
|
let blocked =
|
|
plugin_syntax_decorations(&limited_host, &plugins, "main", RequestId(1), large.id, vec![asked()], pair(), 1)
|
|
.await
|
|
.unwrap();
|
|
assert!(blocked.marks.is_empty(), "{blocked:?}");
|
|
assert!(!blocked.stale);
|
|
assert!(blocked.version >= 1, "{blocked:?}");
|
|
}
|