дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +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]>
3243 рядки
137 KiB
Rust
3243 рядки
137 KiB
Rust
//! Wasmtime glue for the `openidea:[email protected]` world (`wit/`): one shared
|
||
//! engine, one `Store` per plugin. Everything a call may do outside the
|
||
//! sandbox goes through [`HostApi`], implemented by the host; the imports
|
||
//! themselves are in `wit_host`.
|
||
|
||
use crate::types::{
|
||
CodeAction, CommitDraft, CommitProblem, CompletionItem, FileOperation, InlineSuggestion, LineMarker, Location,
|
||
PluginHover, PluginInlayHint, ProviderDocument, SemanticToken,
|
||
};
|
||
use crate::types::{
|
||
EventKind, HostError, HttpRequest, HttpResponse, HttpStreamHead, Invocation, Limits, NotificationLevel,
|
||
ProcessChunk, ProcessCommand, ProcessOutput, Prompt, RepositoryStatus, StatusItem, SyntaxFold, SyntaxSymbol,
|
||
SyntaxToken, WorkbenchEvent,
|
||
};
|
||
use crate::types::{
|
||
FieldValue, ServiceLog, ServiceLogToggle, ServicePane, ServiceTab, TableSort, View, ViewBody, ViewButton,
|
||
ViewEvent, ViewField, ViewListItem, ViewTable, ViewTreeItem, Viewport,
|
||
};
|
||
use crate::wit_host::completion_list_bindings::exports::openidea::plugin::completion_list_provider as wit_cl;
|
||
use crate::wit_host::hover_actions_bindings::exports::openidea::plugin::hover_actions as wit_hia;
|
||
use crate::wit_host::injections_bindings::exports::openidea::plugin::injections as wit_inj;
|
||
use crate::wit_host::language_features_bindings::exports::openidea::plugin::language_features as wit_lf;
|
||
use crate::wit_host::run_anything_bindings::exports::openidea::plugin::run_anything_provider as wit_ra;
|
||
use crate::wit_host::run_config_completion_bindings::exports::openidea::plugin::run_configuration_completion as wit_rcc;
|
||
use crate::wit_host::run_target_validation_bindings::exports::openidea::plugin::run_target_validation as wit_rtv;
|
||
use crate::wit_host::search_bindings::exports::openidea::plugin::search_provider as wit_search;
|
||
use crate::wit_host::{
|
||
bindings, commit_checks_bindings, details_bindings, endpoints_bindings, file_actions_bindings, hover_bindings,
|
||
indexing_bindings, inlay_bindings, providers_bindings, semantic_bindings, syntax_bindings,
|
||
syntax_decoration_bindings, views_bindings,
|
||
};
|
||
use ide_core::CancellationToken;
|
||
use ide_core::ipc_path::to_ipc;
|
||
use ide_extension::Capability;
|
||
use parking_lot::Mutex;
|
||
use std::path::{Path, PathBuf};
|
||
use std::sync::Arc;
|
||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||
use std::time::{Duration, Instant};
|
||
use wasmtime::component::{Component, HasSelf, Linker, ResourceTable};
|
||
use wasmtime::{Config, Engine, Store, StoreLimits, StoreLimitsBuilder, UpdateDeadline};
|
||
use wasmtime_wasi::p2::pipe::MemoryOutputPipe;
|
||
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
|
||
|
||
use crate::wit_host::execution_configurations_bindings::exports::openidea::plugin::execution_configurations as wit_exec;
|
||
use crate::wit_host::execution_configurations_bindings::openidea::plugin::launch_configurations as wit_exec_launch;
|
||
use crate::wit_host::launch_configurations_bindings::exports::openidea::plugin::launch_configurations as wit_launch;
|
||
use crate::wit_host::run_configurations_bindings::exports::openidea::plugin::run_configurations as wit_configs;
|
||
use crate::wit_host::test_results_bindings::exports::openidea::plugin::test_results as wit_tests;
|
||
use bindings::openidea::plugin::events as wit_events;
|
||
use bindings::openidea::plugin::run as wit_run;
|
||
use bindings::openidea::plugin::types as wit_types;
|
||
use commit_checks_bindings::exports::openidea::plugin::commit_checker as wit_cc;
|
||
use details_bindings::exports::openidea::plugin::detail_provider as wit_detail;
|
||
use details_bindings::openidea::plugin::detail_types as wit_dt;
|
||
use endpoints_bindings::exports::openidea::plugin::endpoints_provider as wit_endpoints;
|
||
use file_actions_bindings::exports::openidea::plugin::file_actions as wit_fa;
|
||
use hover_bindings::exports::openidea::plugin::hover_provider as wit_hover;
|
||
use indexing_bindings::exports::openidea::plugin::document_indexer as wit_index;
|
||
use inlay_bindings::exports::openidea::plugin::inlay_hints_provider as wit_inlay;
|
||
use providers_bindings::exports::openidea::plugin::providers as wit_prov;
|
||
use semantic_bindings::exports::openidea::plugin::semantic_tokens_provider as wit_sem;
|
||
use syntax_bindings::exports::openidea::plugin::syntax_provider as wit_syntax;
|
||
use syntax_decoration_bindings::exports::openidea::plugin::syntax_decorations_provider as wit_syn;
|
||
use views_bindings::exports::openidea::plugin::view_provider as wit_view;
|
||
use views_bindings::openidea::plugin::view_types as wit_vt;
|
||
|
||
/// The API version of the world above, `major.minor` of [`host_api`].
|
||
pub const API_VERSION: &str = "1.20";
|
||
const EPOCH_TICK: Duration = Duration::from_millis(10);
|
||
pub(crate) const MAX_TEXT: usize = 1 << 20;
|
||
/// The most text, and the most PNG bytes, `ui.read-clipboard` returns.
|
||
pub const MAX_CLIPBOARD_BYTES: usize = 16 << 20;
|
||
pub(crate) const MAX_TITLE: usize = 256;
|
||
const STDERR_CAPACITY: usize = 256 << 10;
|
||
/// Message of a call interrupted through [`Runtime::cancel_handle`].
|
||
pub const CANCELLED: &str = "was cancelled";
|
||
|
||
/// What plugin code may ask of the IDE, per plugin. Implemented by the host,
|
||
/// which routes notifications and status items to windows and edits to open
|
||
/// documents. Capabilities are already checked when these are called.
|
||
/// Where `settings.open` / `settings.open-permissions` take the user.
|
||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||
pub enum SettingsTarget {
|
||
/// The plugin's settings node, or the declared sub-page with this id.
|
||
Page(Option<String>),
|
||
/// Settings › Plugins with the plugin's permissions in view.
|
||
Permissions,
|
||
}
|
||
|
||
pub trait HostApi: Send + Sync {
|
||
/// Read only the schema of a connected project table; database.schema was checked.
|
||
fn database_table_schema(
|
||
&self,
|
||
_root: &Path,
|
||
_client: Option<&str>,
|
||
_table: &crate::PluginDatabaseTableRef,
|
||
_cancel: &CancellationToken,
|
||
_timeout: Duration,
|
||
) -> Result<crate::PluginDatabaseTableSchema, HostError> {
|
||
Err(HostError::Invalid("Database schema access is unavailable on this host".into()))
|
||
}
|
||
/// Creates/replaces this plugin's non-secret project connection settings.
|
||
fn database_upsert(&self, root: &Path, source: crate::PluginDataSource) -> Result<(), HostError>;
|
||
/// Removes only an owned data source and its credentials.
|
||
fn database_remove(&self, root: &Path, id: &str) -> Result<(), HostError>;
|
||
/// Private ids of only this plugin's sources.
|
||
fn database_sources(&self, root: &Path) -> Result<Vec<String>, HostError>;
|
||
/// Writes a password to the Database credential store, without a read path.
|
||
fn database_credential(&self, root: &Path, id: &str, password: String) -> Result<(), HostError>;
|
||
/// Requests normal connection/introspection in the invoking project's window.
|
||
fn database_connect(
|
||
&self,
|
||
root: &Path,
|
||
client: &str,
|
||
id: &str,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError>;
|
||
/// Sends `method` to the running language server named `server` of the project at
|
||
/// `root`: a request (the result as JSON), or with `notify` a notification (`null`).
|
||
/// `params` is JSON. Never starts a server.
|
||
#[allow(clippy::too_many_arguments)]
|
||
fn lsp_request(
|
||
&self,
|
||
root: &Path,
|
||
server: &str,
|
||
method: &str,
|
||
params: Option<String>,
|
||
notify: bool,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<String, HostError>;
|
||
/// The flat dependency graph of the Maven / Gradle builds of the project at `root`
|
||
/// (of the module `module` only, when given), as the IDE last resolved it; never runs
|
||
/// a build tool.
|
||
fn build_dependencies(&self, root: &Path, module: Option<&str>) -> Result<Vec<crate::DependencyNode>, HostError>;
|
||
/// Stdio argv only; no URL or bearer token.
|
||
fn mcp_stdio_command(&self, client: &str) -> Option<Vec<String>>;
|
||
/// What other plugins contributed to `point`, as (plugin id, JSON). Only the
|
||
/// plugin that declares the point may read it.
|
||
fn extension_contributions(&self, point: &str) -> Result<Vec<(String, String)>, HostError>;
|
||
|
||
/// Registers a project-scoped wrapper after validating its process grant and pin.
|
||
fn register_run_target(
|
||
&self,
|
||
root: &Path,
|
||
target: crate::PluginRunTarget,
|
||
generation: Option<u64>,
|
||
) -> Result<(), HostError>;
|
||
/// Withdraws this plugin's target, preserving the user's remembered prompt answer.
|
||
fn unregister_run_target(&self, root: &Path, id: &str);
|
||
|
||
/// Starts one of this plugin's configurations in a window.
|
||
fn start_run(
|
||
&self,
|
||
client: &str,
|
||
config: &str,
|
||
args: Vec<String>,
|
||
options: crate::PluginRunStartOptions,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<String, HostError>;
|
||
/// Opens/reuses a keyed terminal, retained across idle instance reclamation; `cwd`
|
||
/// (API 1.18) is a directory relative to the window's project root.
|
||
#[allow(clippy::too_many_arguments)]
|
||
fn open_terminal_session(
|
||
&self,
|
||
client: &str,
|
||
key: String,
|
||
title: String,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<String, HostError>;
|
||
/// Writes only to an owned, live terminal.
|
||
fn write_terminal(
|
||
&self,
|
||
client: &str,
|
||
key: &str,
|
||
text: String,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError>;
|
||
/// Opens a content tab through the shared editor implementation.
|
||
fn open_content_tab(
|
||
&self,
|
||
client: &str,
|
||
tab: crate::ContentTab,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError>;
|
||
/// Opens a diff of two texts in the invoking window; a `None` side doesn't exist.
|
||
fn open_diff(&self, client: &str, diff: crate::FileDiff, file_name: Option<String>) -> Result<(), HostError>;
|
||
/// Answers a content action in the invoking window.
|
||
fn reply_content_tab(&self, client: &str, request: &str, reply: crate::ContentTabReply) -> Result<(), HostError>;
|
||
/// The invoking window's editors.
|
||
fn open_editors(&self, client: &str) -> Result<Vec<crate::OpenEditor>, HostError>;
|
||
/// Opens a validated file and optionally selects a range; `as_text` skips custom editors.
|
||
fn editor_open(
|
||
&self,
|
||
client: &str,
|
||
path: &Path,
|
||
selection: Option<ide_core::Range>,
|
||
as_text: bool,
|
||
) -> Result<(), HostError>;
|
||
/// Changes the selections, or reveals a range, only in an editor of this window.
|
||
fn editor_ranges(
|
||
&self,
|
||
client: &str,
|
||
path: &Path,
|
||
ranges: Vec<ide_core::Range>,
|
||
reveal: bool,
|
||
) -> Result<(), HostError>;
|
||
/// Find in Files with the project's document overlay.
|
||
fn search_text(
|
||
&self,
|
||
client: Option<&str>,
|
||
root: &Path,
|
||
query: crate::TextQuery,
|
||
limit: u32,
|
||
cancel: &CancellationToken,
|
||
deadline: Instant,
|
||
) -> Result<crate::TextSearch, HostError>;
|
||
/// Window-reported Problems view snapshot.
|
||
fn problems(&self, client: &str, path: Option<&Path>) -> Result<Vec<crate::FileProblems>, HostError>;
|
||
/// Replace or clear an editor decoration set.
|
||
fn decorations(&self, path: Option<&Path>, set: &str, items: Vec<crate::Decoration>) -> Result<(), HostError>;
|
||
/// Replace or clear a tree decoration set.
|
||
fn file_decorations(
|
||
&self,
|
||
path: Option<&Path>,
|
||
set: &str,
|
||
item: Option<crate::FileDecoration>,
|
||
) -> Result<(), HostError>;
|
||
/// Replace all `(path, icon)` entries of a tree decoration set at once (API 1.8).
|
||
fn file_icon_decorations(&self, set: &str, items: Vec<(PathBuf, String)>) -> Result<(), HostError>;
|
||
/// Fork a recorded native agent session into a new task in the invoking
|
||
/// window's project, returning the new task id (API 1.18).
|
||
fn fork_agent_session(&self, client: &str, agent: &str, session_id: &str, cwd: &Path) -> Result<String, HostError>;
|
||
/// Trusted repository history.
|
||
fn vcs_log(
|
||
&self,
|
||
root: &Path,
|
||
path: Option<&str>,
|
||
limit: u32,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Vec<crate::CommitInfo>, HostError>;
|
||
/// Trusted repository attribution, including unsaved documents.
|
||
fn vcs_blame(&self, client: Option<&str>, root: &Path, path: &str) -> Result<Vec<crate::BlameLine>, HostError>;
|
||
/// Trusted repository working-tree patch, bounded to 4 MiB.
|
||
fn vcs_diff(
|
||
&self,
|
||
root: &Path,
|
||
path: Option<&str>,
|
||
against: Option<&str>,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<String>, HostError>;
|
||
|
||
/// Shows a notification; `actions` name commands the plugin declares.
|
||
fn notify(
|
||
&self,
|
||
client: Option<&str>,
|
||
level: NotificationLevel,
|
||
title: String,
|
||
body: String,
|
||
actions: Vec<crate::NotificationAction>,
|
||
) -> Result<(), HostError>;
|
||
/// [`HostApi::notify`] in the most recently focused window of the open project at
|
||
/// `root`; not-found (and nothing shown) once it has closed.
|
||
fn notify_project(
|
||
&self,
|
||
root: &Path,
|
||
level: NotificationLevel,
|
||
title: String,
|
||
body: String,
|
||
actions: Vec<crate::NotificationAction>,
|
||
) -> Result<(), HostError>;
|
||
fn copy_to_clipboard(&self, client: Option<&str>, text: String) -> Result<(), HostError>;
|
||
/// The clipboard of the machine `client`'s window runs on, as that window reads it;
|
||
/// waits for the window until `cancel` fires.
|
||
fn read_clipboard(&self, client: &str, cancel: &CancellationToken) -> Result<crate::ClipboardContent, HostError>;
|
||
fn open_url(&self, client: Option<&str>, url: String) -> Result<(), HostError>;
|
||
fn open_project(&self, client: Option<&str>, path: String, new_window: bool) -> Result<(), HostError>;
|
||
/// Shows `prompt` in `client`'s window and waits for the answer until the
|
||
/// user gives one or `cancel` fires: the chosen keys (`QuickPick`), the
|
||
/// text (`Input`, one element) or any value (`Confirm`: OK); `None` when
|
||
/// dismissed.
|
||
fn ask(
|
||
&self,
|
||
client: Option<&str>,
|
||
prompt: Prompt,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<Vec<String>>, HostError>;
|
||
/// Brings the plugin's declared view to the front in `client`'s window.
|
||
fn reveal_view(&self, client: Option<&str>, view: &str) -> Result<(), HostError>;
|
||
/// Opens Settings in `client`'s window at one of the plugin's own pages
|
||
/// (`settings.open`, `settings.open-permissions`).
|
||
fn open_settings(&self, client: Option<&str>, target: SettingsTarget) -> Result<(), HostError>;
|
||
/// Whether this stable id is a command declared by this plugin.
|
||
fn owns_command(&self, id: &str) -> bool;
|
||
/// Executes a stable command in the invoking window and waits for its result.
|
||
fn execute_command(
|
||
&self,
|
||
invocation: &Invocation,
|
||
id: &str,
|
||
arg: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<Option<String>, HostError>;
|
||
/// Appends text, or clears (`None`), in a plugin-owned output channel.
|
||
fn output_write(&self, channel: &str, text: Option<&str>) -> Result<(), HostError>;
|
||
/// Reveals an output channel in the invoking window.
|
||
fn output_reveal(&self, client: &str, channel: &str) -> Result<(), HostError>;
|
||
/// Checks manifest minimum API requirements for new declarations or subscriptions.
|
||
fn require_api(&self, what: &str, minor: u64) -> Result<(), HostError>;
|
||
/// Sets (`Some`) or removes the plugin's status bar item `id`.
|
||
fn status_item(&self, id: &str, item: Option<StatusItem>);
|
||
/// Checks a status item menu before it is set: its actions name commands the plugin declares.
|
||
fn check_status_menu(&self, menu: &[crate::StatusMenuEntry]) -> Result<(), HostError>;
|
||
/// Sets or removes the status override in the invoking window.
|
||
fn window_status_item(&self, client: &str, id: &str, item: Option<StatusItem>) -> Result<(), HostError>;
|
||
/// Replaces the plugin's console highlight rules (API 1.12); empty removes them.
|
||
fn console_rules(&self, rules: Vec<crate::ConsoleRule>) -> Result<(), HostError>;
|
||
/// Sets (`Some`, validated JSON text) or removes the plugin's context key,
|
||
/// for window `client` (every window if `None`).
|
||
fn set_context(&self, client: Option<&str>, key: &str, value: Option<String>) -> Result<(), HostError>;
|
||
/// Roots of the open projects.
|
||
fn projects(&self) -> Vec<std::path::PathBuf>;
|
||
/// Roots of the workspace members attached to the project at `root` (a
|
||
/// project window group's folders, as the editor and LSP resolve them).
|
||
/// Empty for a plain project.
|
||
fn member_roots(&self, root: &Path) -> Vec<std::path::PathBuf>;
|
||
/// The most specific open project containing a document (including a new file).
|
||
fn project_of(&self, path: &Path) -> Option<std::path::PathBuf> {
|
||
crate::sandbox::innermost_root(self.projects(), path)
|
||
}
|
||
|
||
/// Searches the current in-memory index of an open project.
|
||
fn symbols(
|
||
&self,
|
||
root: &Path,
|
||
query: &str,
|
||
limit: usize,
|
||
cancel: &CancellationToken,
|
||
deadline: Instant,
|
||
) -> Result<Vec<crate::SymbolInfo>, HostError>;
|
||
/// Unsaved text of an open editor's file.
|
||
fn buffer_text(&self, path: &Path) -> Option<String>;
|
||
/// Version and text of the open document at `path`.
|
||
fn document(&self, path: &Path) -> Option<(u64, String)>;
|
||
/// Applies edits to the open document at `path` if it is still at `version`.
|
||
fn apply_edits(
|
||
&self,
|
||
client: Option<&str>,
|
||
path: &Path,
|
||
version: u64,
|
||
edits: Vec<ide_core::TextEdit>,
|
||
) -> Result<u64, HostError>;
|
||
fn storage_get(&self, key: &str) -> Result<Option<String>, HostError>;
|
||
/// Sets, or (`None`) deletes.
|
||
fn storage_set(&self, key: &str, value: Option<String>) -> Result<(), HostError>;
|
||
fn storage_keys(&self, prefix: &str) -> Result<Vec<String>, HostError>;
|
||
fn vcs_status(&self, root: &Path) -> Result<Option<RepositoryStatus>, HostError>;
|
||
fn vcs_remotes(&self, root: &Path) -> Result<Vec<(String, String)>, HostError>;
|
||
fn vcs_roots(&self, root: &Path) -> Result<Vec<String>, HostError>;
|
||
fn vcs_repository_at(&self, root: &Path, path: &str) -> Result<Option<crate::RepositoryInfo>, HostError>;
|
||
fn vcs_fetch(&self, root: &Path, cancel: &CancellationToken) -> Result<(), HostError>;
|
||
/// The commit being prepared in `client`'s window (`None`: no repository),
|
||
/// waiting for the window until `cancel` fires.
|
||
fn pending_commit(
|
||
&self,
|
||
client: Option<&str>,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<CommitDraft>, HostError>;
|
||
/// Replaces the commit message of `client`'s window if its draft is still at
|
||
/// `revision` (else [`HostError::Stale`]).
|
||
fn set_commit_message(&self, client: Option<&str>, revision: u64, message: String) -> Result<(), HostError>;
|
||
/// Subscribes to (or unsubscribes from) a kind of workbench event.
|
||
/// Project files matching `glob` (root-relative), at most `limit`.
|
||
fn find_files(&self, root: &Path, glob: &str, limit: usize) -> Result<Vec<String>, HostError>;
|
||
/// Replaces the plugin's diagnostics for `path`.
|
||
fn publish_diagnostics(&self, path: &Path, items: Vec<ide_core::Diagnostic>) -> Result<(), HostError>;
|
||
/// Performs `request` (its host is granted), filling in secret placeholders.
|
||
fn fetch(&self, request: HttpRequest, cancel: &CancellationToken) -> Result<HttpResponse, HostError>;
|
||
/// As [`HostApi::fetch`], returning once the headers arrive (`request.timeout`
|
||
/// bounds only that wait); the body is read with [`HostApi::http_stream_read`].
|
||
fn fetch_stream(&self, request: HttpRequest, cancel: &CancellationToken) -> Result<HttpStreamHead, HostError>;
|
||
/// The next body bytes of an owned stream, at most `max`: empty and not done
|
||
/// when nothing arrived within `timeout`; `true` once the body has ended.
|
||
fn http_stream_read(
|
||
&self,
|
||
id: u64,
|
||
max: usize,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<(Vec<u8>, bool), HostError>;
|
||
/// Closes an owned stream, including when its resource is dropped.
|
||
fn http_stream_close(&self, id: u64);
|
||
|
||
fn websocket_connect(
|
||
&self,
|
||
root: &Path,
|
||
request: crate::WebSocketRequest,
|
||
timeout: Duration,
|
||
) -> Result<u64, HostError>;
|
||
fn websocket_send(&self, connection: u64, bytes: Vec<u8>) -> Result<(), HostError>;
|
||
fn websocket_drain(&self, connection: u64, limit: usize) -> Result<crate::WebSocketBatch, HostError>;
|
||
fn websocket_close(&self, connection: u64);
|
||
/// The `net:ask` answer for a request to HTTPS host `host` of this plugin
|
||
/// (`subject`): `Some(true)` allowed and remembered (a host the user
|
||
/// already allowed connects without asking again, also in the background),
|
||
/// `Some(false)` allowed for this request only, `None` refused — also when
|
||
/// nobody could be asked (`client` is `None` outside commands and native view actions).
|
||
fn net_ask(
|
||
&self,
|
||
subject: &ide_extension::ledger::Subject,
|
||
client: Option<&str>,
|
||
host: &str,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<bool>, HostError>;
|
||
fn secret_is_set(&self, name: &str) -> bool;
|
||
|
||
/// Stores or deletes a secret declared by this plugin; never returns its value.
|
||
fn secret_set(&self, name: &str, value: Option<&str>) -> Result<(), HostError>;
|
||
/// Creates a desktop loopback listener and returns its handle and redirect URI.
|
||
fn begin_loopback(&self, client: &str, path: &str) -> Result<(u64, String), HostError>;
|
||
/// Awaits a browser redirect, excluded from the caller's execution budget.
|
||
fn wait_loopback(
|
||
&self,
|
||
id: u64,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<String>, HostError>;
|
||
/// Releases a desktop listener when its resource or plugin stops.
|
||
fn close_loopback(&self, id: u64);
|
||
/// Connects to an approved host-local socket; owned by this plugin instance.
|
||
fn ipc_connect(&self, name: &str, cancel: &CancellationToken, timeout: Duration) -> Result<u64, HostError>;
|
||
/// Writes to an owned IPC connection.
|
||
fn ipc_write(
|
||
&self,
|
||
id: u64,
|
||
bytes: Vec<u8>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError>;
|
||
/// Reads from an owned IPC connection, empty on timeout or EOF.
|
||
fn ipc_read(
|
||
&self,
|
||
id: u64,
|
||
max: usize,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Vec<u8>, HostError>;
|
||
/// Closes an owned IPC connection, including when its resource is dropped.
|
||
fn ipc_close(&self, id: u64);
|
||
/// Runs the approved file of `process:<program>` with `cmd.env` added to
|
||
/// the inherited environment, and waits.
|
||
fn run_process(
|
||
&self,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<&Path>,
|
||
stdin: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<ProcessOutput, HostError>;
|
||
/// Runs `cmd` with configurable capture options (API 1.15).
|
||
fn run_process_with_options(
|
||
&self,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<&Path>,
|
||
stdin: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
options: Option<crate::types::ProcessOptions>,
|
||
) -> Result<ProcessOutput, HostError>;
|
||
/// Delivers every event of `kind`, dropping its path filter.
|
||
fn subscribe(&self, kind: EventKind);
|
||
/// Delivers `kind` (`file-opened`, `file-saved`, `files-changed`) only for matching paths.
|
||
fn subscribe_paths(&self, kind: &str, globs: Vec<String>) -> Result<(), HostError> {
|
||
self.subscribe_paths_with_options(kind, globs, None)
|
||
}
|
||
/// Delivers `kind` with optional debounce window in milliseconds (API 1.18).
|
||
fn subscribe_paths_with_options(
|
||
&self,
|
||
kind: &str,
|
||
globs: Vec<String>,
|
||
debounce_ms: Option<u32>,
|
||
) -> Result<(), HostError>;
|
||
/// Monotonically increasing project generation (API 1.18).
|
||
fn project_generation(&self, root: &Path) -> Result<u64, HostError>;
|
||
/// Replaces what this plugin watches outside `project` for the window `client`
|
||
/// (API 1.18): `paths` are real paths an `fs.read` grant covers, already checked.
|
||
/// Empty stops watching.
|
||
fn watch_paths(&self, _client: &str, _project: &Path, _paths: Vec<PathBuf>) -> Result<(), HostError> {
|
||
Err(HostError::Failed("watching paths is unavailable here".into()))
|
||
}
|
||
fn unsubscribe(&self, kind: &str);
|
||
/// Starts `cmd` and returns the handle to it without waiting.
|
||
fn start_process(&self, cmd: ProcessCommand, cwd: Option<&Path>, client: Option<&str>) -> Result<u64, HostError>;
|
||
/// Starts `cmd` with streaming options (API 1.15).
|
||
fn start_process_stream(
|
||
&self,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<&Path>,
|
||
client: Option<&str>,
|
||
options: Option<crate::types::ProcessOptions>,
|
||
) -> Result<u64, HostError>;
|
||
/// What process `id` printed since the last call, each stream at most
|
||
/// `max` bytes (the rest waits for the next call), and its status once it
|
||
/// ended.
|
||
fn poll_process(&self, id: u64, max: usize) -> Result<ProcessChunk, HostError>;
|
||
/// Writes `text` to the stdin of process `id`.
|
||
fn write_process(
|
||
&self,
|
||
id: u64,
|
||
text: &str,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError>;
|
||
/// Closes the stdin of process `id`.
|
||
fn close_process_stdin(&self, id: u64) -> Result<(), HostError>;
|
||
/// Ends process `id`; not an error if it already ended.
|
||
fn kill_process(&self, id: u64) -> Result<(), HostError>;
|
||
/// Forgets the handle and ends its process if it still runs.
|
||
fn release_process(&self, id: u64);
|
||
/// Waits up to `timeout` for process `id` to end (`ZERO` only checks) and
|
||
/// returns what it printed meanwhile with its status once it ended.
|
||
fn wait_process(
|
||
&self,
|
||
id: u64,
|
||
max: usize,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<ProcessChunk, HostError>;
|
||
/// Starts a progress indicator; cancelling it from the UI cancels `cancel`.
|
||
fn task_begin(&self, client: Option<&str>, title: String, cancellable: bool, cancel: CancellationToken) -> u64;
|
||
fn task_report(&self, id: u64, done: u32, total: Option<u32>, message: Option<String>);
|
||
fn task_cancelled(&self, id: u64) -> bool;
|
||
fn task_end(&self, id: u64);
|
||
/// The plugin's view (or custom editors) `view` must be rendered again.
|
||
fn refresh_view(&self, view: &str);
|
||
/// A message for the script of the `html` body of `view`.
|
||
fn post_view_message(&self, view: &str, message: String) -> Result<(), HostError>;
|
||
/// Opens a terminal in `client`'s window running the approved file of
|
||
/// `process:<program>` in `cwd`.
|
||
fn open_terminal(
|
||
&self,
|
||
client: Option<&str>,
|
||
title: String,
|
||
program: &str,
|
||
args: Vec<String>,
|
||
cwd: Option<&Path>,
|
||
) -> Result<(), HostError>;
|
||
fn open_location(&self, client: Option<&str>, path: &Path, line: u32, column: u32) -> Result<(), HostError>;
|
||
/// Opens a log tab `key` in `client`'s window following `log` (already
|
||
/// checked against the plugin's grants and limits).
|
||
fn open_log_tab(&self, client: &str, key: String, title: String, log: ServiceLog) -> Result<(), HostError>;
|
||
/// The IDE's UI language as a BCP 47 tag.
|
||
fn locale(&self) -> String;
|
||
/// A setting of the plugin as JSON (project over global over the schema default).
|
||
fn setting(&self, root: Option<&Path>, key: &str) -> Option<String>;
|
||
/// Reads and decodes a text file outside the project, whose real path an
|
||
/// `fs.read` grant already covers: the unsaved buffer wins, then the disk.
|
||
/// `max` bounds the bytes read.
|
||
fn read_text_file(&self, real: &Path, max: usize) -> Result<String, HostError>;
|
||
/// The plugin's private directory, independent of the current invocation.
|
||
fn plugin_dir(&self) -> Result<PathBuf, HostError>;
|
||
/// The plugin's install / package directory (read-only for the guest).
|
||
fn install_dir(&self) -> Result<PathBuf, HostError>;
|
||
/// Replaces the text of a file whose real path the caller already checked:
|
||
/// in one step, keeping its encoding and BOM (a new file is UTF-8). Refused
|
||
/// while an editor holds unsaved changes for it.
|
||
fn write_text_file(&self, real: &Path, text: &str) -> Result<(), HostError>;
|
||
/// Replaces the bytes of a file whose real path the caller already checked,
|
||
/// in one step. Refused while an editor holds unsaved changes for it.
|
||
fn write_bytes_file(&self, real: &Path, bytes: &[u8]) -> Result<(), HostError>;
|
||
}
|
||
|
||
/// Shared compilation engine plus an epoch ticker thread for wall-clock limits.
|
||
pub struct PluginEngine {
|
||
engine: Engine,
|
||
linker: Linker<StoreData>,
|
||
}
|
||
|
||
impl PluginEngine {
|
||
pub fn new() -> Result<Self, String> {
|
||
let mut config = Config::new();
|
||
config.wasm_component_model(true).consume_fuel(true).epoch_interruption(true);
|
||
let engine = Engine::new(&config).map_err(|e| format!("wasm engine: {e}"))?;
|
||
let mut linker = Linker::new(&engine);
|
||
wasmtime_wasi::p2::add_to_linker_sync(&mut linker).map_err(|e| e.to_string())?;
|
||
bindings::Plugin::add_to_linker::<_, HasSelf<_>>(&mut linker, |s| s).map_err(|e| e.to_string())?;
|
||
// Cranelift caches fuel in a JIT local across `new_epoch` yields and only
|
||
// reloads `VMStoreContext::fuel_consumed` across host import calls, so
|
||
// `tasks` host calls refill `StoreContextMut::set_fuel` directly on return.
|
||
bind_task_fuel_refill(&mut linker).map_err(|e| e.to_string())?;
|
||
|
||
let weak = engine.weak();
|
||
std::thread::Builder::new()
|
||
.name("plugin-epoch".into())
|
||
.spawn(move || {
|
||
while let Some(engine) = weak.upgrade() {
|
||
engine.increment_epoch();
|
||
drop(engine);
|
||
std::thread::sleep(EPOCH_TICK);
|
||
}
|
||
})
|
||
.map_err(|e| e.to_string())?;
|
||
Ok(Self { engine, linker })
|
||
}
|
||
|
||
pub fn compile(&self, wasm: &Path) -> Result<Component, String> {
|
||
let bytes = std::fs::read(wasm).map_err(|e| format!("{}: {e}", wasm.display()))?;
|
||
self.compile_bytes(&bytes).map_err(|e| format!("{}: {e}", wasm.display()))
|
||
}
|
||
|
||
pub fn compile_bytes(&self, bytes: &[u8]) -> Result<Component, String> {
|
||
Component::new(&self.engine, bytes).map_err(|e| format!("not a valid plugin component: {e}"))
|
||
}
|
||
|
||
/// Compiles `wasm`, or loads it precompiled from `cache_dir`: the key is the module's hash plus the engine's compatibility hash,
|
||
/// so an engine upgrade simply misses and recompiles. Cache failures only
|
||
/// cost the compilation.
|
||
pub fn compile_cached(&self, wasm: &Path, cache_dir: &Path) -> Result<Component, String> {
|
||
let bytes = std::fs::read(wasm).map_err(|e| format!("{}: {e}", wasm.display()))?;
|
||
let file = cache_dir.join(format!("{}.cwasm", self.cache_key(&bytes)));
|
||
// Read into memory rather than `deserialize_file`: that maps the file and makes its pages
|
||
// executable, which the hardened runtime of the signed macOS app refuses ("failed to make
|
||
// memory executable"), so the cache never hit and every activation compiled again.
|
||
if let Ok(precompiled) = std::fs::read(&file) {
|
||
// SAFETY: the cache directory belongs to the IDE (inside its plugins
|
||
// directory) and is written only by `store_precompiled` below from
|
||
// components this engine compiled; a corrupt file fails validation.
|
||
match unsafe { Component::deserialize(&self.engine, &precompiled) } {
|
||
Ok(component) => return Ok(component),
|
||
Err(e) => {
|
||
tracing::info!("discarding precompiled {}: {e}", file.display());
|
||
let _ = std::fs::remove_file(&file);
|
||
}
|
||
}
|
||
}
|
||
let component = self.compile_bytes(&bytes).map_err(|e| format!("{}: {e}", wasm.display()))?;
|
||
store_precompiled(&component, &file);
|
||
Ok(component)
|
||
}
|
||
|
||
/// Fills the cache for `wasm` if needed (background warm-up). Returns the
|
||
/// cache file name, to keep it when pruning.
|
||
pub fn precompile(&self, wasm: &Path, cache_dir: &Path) -> Result<String, String> {
|
||
let bytes = std::fs::read(wasm).map_err(|e| format!("{}: {e}", wasm.display()))?;
|
||
let name = format!("{}.cwasm", self.cache_key(&bytes));
|
||
let file = cache_dir.join(&name);
|
||
if !file.is_file() {
|
||
let component = self.compile_bytes(&bytes)?;
|
||
store_precompiled(&component, &file);
|
||
}
|
||
Ok(name)
|
||
}
|
||
|
||
fn cache_key(&self, bytes: &[u8]) -> String {
|
||
use std::hash::{Hash, Hasher};
|
||
let mut engine = std::collections::hash_map::DefaultHasher::new();
|
||
self.engine.precompile_compatibility_hash().hash(&mut engine);
|
||
let module = blake3::hash(bytes).to_hex();
|
||
format!("{}-{:016x}", &module[..32], engine.finish())
|
||
}
|
||
|
||
pub fn engine(&self) -> &Engine {
|
||
&self.engine
|
||
}
|
||
|
||
/// Rejects a component this host can't run, with a message that says what
|
||
/// to do: one importing interfaces outside the plugin API and WASI, or one
|
||
/// built for a plugin API this host doesn't implement (another major, or a
|
||
/// newer minor), or one that doesn't link: an import the host lacks or
|
||
/// declares with another type (same API version, different WIT), or a
|
||
/// missing plugin export. Returns the API version the component was built
|
||
/// for (`None`: it uses no plugin interface).
|
||
pub fn check_component(&self, component: &Component) -> Result<Option<semver::Version>, String> {
|
||
let ty = component.component_type();
|
||
let imports: Vec<String> = ty.imports(&self.engine).map(|(name, _)| name.to_owned()).collect();
|
||
let foreign: Vec<&str> = imports
|
||
.iter()
|
||
.map(String::as_str)
|
||
.filter(|i| !(i.starts_with(PLUGIN_PACKAGE_PREFIX) || i.starts_with("wasi:")))
|
||
.collect();
|
||
if !foreign.is_empty() {
|
||
return Err(format!("the component imports interfaces the IDE does not provide: {}", foreign.join(", ")));
|
||
}
|
||
let exports = ty.exports(&self.engine).map(|(name, _)| name.to_owned()).collect::<Vec<_>>();
|
||
let built_for = component_api(imports.iter().chain(&exports).map(String::as_str))?;
|
||
if let Some(v) = &built_for {
|
||
check_api(v, &host_api())?;
|
||
}
|
||
let pre = self.linker.instantiate_pre(component).map_err(|e| link_error(&format!("{e:#}")))?;
|
||
bindings::PluginIndices::new(&pre).map_err(|e| link_error(&format!("{e:#}")))?;
|
||
Ok(built_for)
|
||
}
|
||
}
|
||
|
||
/// Explains a component that doesn't link against this host's plugin API.
|
||
fn link_error(detail: &str) -> String {
|
||
format!(
|
||
"the component doesn't match the plugin API of this IDE ({API_VERSION}): {detail}. \
|
||
If it was built for a newer OpenIDEA, update OpenIDEA; otherwise rebuild it with the {API_VERSION} SDK"
|
||
)
|
||
}
|
||
|
||
/// Interface names of the plugin API start with this (`openidea:plugin/[email protected]`).
|
||
const PLUGIN_PACKAGE_PREFIX: &str = "openidea:plugin/";
|
||
|
||
/// The plugin API this host implements: the version of the `wit/` package.
|
||
pub fn host_api() -> semver::Version {
|
||
semver::Version::parse(ide_extension::ENGINE_VERSION).expect("ENGINE_VERSION is a semantic version")
|
||
}
|
||
|
||
/// The plugin API version a component was built for: the highest version on
|
||
/// its `openidea:plugin/*` imports and exports. An unversioned plugin
|
||
/// interface is an error, since nothing could tell compatible hosts apart.
|
||
pub fn component_api<'a>(names: impl IntoIterator<Item = &'a str>) -> Result<Option<semver::Version>, String> {
|
||
let mut highest: Option<semver::Version> = None;
|
||
for name in names {
|
||
let Some(rest) = name.strip_prefix(PLUGIN_PACKAGE_PREFIX) else { continue };
|
||
let Some((_, version)) = rest.split_once('@') else {
|
||
return Err(format!("the component uses the unversioned plugin interface {name:?}"));
|
||
};
|
||
let version =
|
||
semver::Version::parse(version).map_err(|e| format!("bad plugin API version in {name:?}: {e}"))?;
|
||
if highest.as_ref().is_none_or(|h| version > *h) {
|
||
highest = Some(version);
|
||
}
|
||
}
|
||
Ok(highest)
|
||
}
|
||
|
||
/// A component built for `built_for` runs on a host implementing `host` when
|
||
/// the major versions match and the host's minor is at least as new (1.x only
|
||
/// adds; patches change no interface; 0.x makes no promise, so there every
|
||
/// minor is its own API).
|
||
pub fn check_api(built_for: &semver::Version, host: &semver::Version) -> Result<(), String> {
|
||
let (b, h) = ((built_for.major, built_for.minor), (host.major, host.minor));
|
||
let same_line = b.0 == h.0 && (h.0 > 0 || b.1 == h.1);
|
||
if same_line && b <= h {
|
||
return Ok(());
|
||
}
|
||
let newer = b > h;
|
||
let (b, h) = (format!("{}.{}", b.0, b.1), format!("{}.{}", h.0, h.1));
|
||
Err(if newer {
|
||
format!("the component was built for plugin API {b}, newer than this IDE's {h}: update OpenIDEA")
|
||
} else {
|
||
format!("the component was built for plugin API {b}; this IDE runs {h}: rebuild it with the {h} SDK")
|
||
})
|
||
}
|
||
|
||
fn store_precompiled(component: &Component, file: &Path) {
|
||
let Ok(bytes) = component.serialize() else { return };
|
||
if let Some(dir) = file.parent() {
|
||
let _ = std::fs::create_dir_all(dir);
|
||
}
|
||
// Unique per write: two threads may store the same component at once, and a
|
||
// shared temporary file could be renamed into place half written.
|
||
static WRITES: AtomicU64 = AtomicU64::new(0);
|
||
let tmp = file.with_extension(format!("tmp{}-{}", std::process::id(), WRITES.fetch_add(1, Ordering::Relaxed)));
|
||
if std::fs::write(&tmp, bytes).and_then(|()| std::fs::rename(&tmp, file)).is_err() {
|
||
let _ = std::fs::remove_file(&tmp);
|
||
}
|
||
}
|
||
|
||
/// Interrupts the running call: the epoch callback traps on `flag`, host
|
||
/// operations (a fetch) watch `token`. Reset when a call starts.
|
||
#[derive(Default)]
|
||
pub struct CallCancel {
|
||
flag: AtomicBool,
|
||
token: Mutex<CancellationToken>,
|
||
}
|
||
|
||
impl CallCancel {
|
||
pub fn cancel(&self) {
|
||
self.flag.store(true, Ordering::Release);
|
||
self.token.lock().cancel();
|
||
}
|
||
|
||
/// Cooperative cancellation: host operations (`net`, `process`, fetch)
|
||
/// return `cancelled` and tasks report it; the guest keeps its instance.
|
||
pub fn cancel_soft(&self) {
|
||
self.token.lock().cancel();
|
||
}
|
||
|
||
pub(crate) fn token(&self) -> CancellationToken {
|
||
self.token.lock().clone()
|
||
}
|
||
|
||
pub(crate) fn is_set(&self) -> bool {
|
||
self.flag.load(Ordering::Acquire)
|
||
}
|
||
|
||
fn reset(&self) {
|
||
self.flag.store(false, Ordering::Release);
|
||
*self.token.lock() = CancellationToken::new();
|
||
}
|
||
}
|
||
|
||
pub struct StoreData {
|
||
/// The running call is interrupted at the next epoch tick past this.
|
||
pub(crate) deadline: Instant,
|
||
/// Wall-clock limit of the running call (for the message).
|
||
pub(crate) timeout: Duration,
|
||
pub(crate) call_started: Instant,
|
||
pub(crate) cancel: Arc<CallCancel>,
|
||
pub(crate) limits: Limits,
|
||
wasi: WasiCtx,
|
||
pub(crate) table: ResourceTable,
|
||
store_limits: StoreLimits,
|
||
stderr: MemoryOutputPipe,
|
||
stderr_seen: usize,
|
||
pub(crate) plugin_id: String,
|
||
pub(crate) granted: Vec<Capability>,
|
||
pub(crate) subject: ide_extension::ledger::Subject,
|
||
pub(crate) host: Arc<dyn HostApi>,
|
||
pub(crate) invocation: Invocation,
|
||
/// The running call is a command or an event.
|
||
pub(crate) interactive: bool,
|
||
/// The user ran it: a command, or a view action other than a web view message.
|
||
/// Gates what must not happen unattended (`net:ask` prompts, reading the clipboard).
|
||
pub(crate) user_invoked: bool,
|
||
/// Editor window for pick, pick-many, input and confirm during action resolution.
|
||
pub(crate) action_prompt_client: Option<String>,
|
||
/// Tasks begun by the running call and not ended yet.
|
||
pub(crate) open_tasks: Vec<u64>,
|
||
pub(crate) last_report: Option<Instant>,
|
||
/// One-shot unwind window after a blocking host I/O call times out.
|
||
pub(crate) host_io_grace_used: bool,
|
||
/// Fuel budget active for the running call (scales when a background task begins).
|
||
pub(crate) active_fuel: u64,
|
||
}
|
||
|
||
impl WasiView for StoreData {
|
||
fn ctx(&mut self) -> WasiCtxView<'_> {
|
||
WasiCtxView { ctx: &mut self.wasi, table: &mut self.table }
|
||
}
|
||
}
|
||
|
||
impl StoreData {
|
||
/// New guest stderr output since the last call (panic messages, eprintln!).
|
||
fn drain_stderr(&mut self) -> String {
|
||
let all = self.stderr.contents();
|
||
let new = String::from_utf8_lossy(&all[self.stderr_seen.min(all.len())..]).into_owned();
|
||
self.stderr_seen = all.len();
|
||
new
|
||
}
|
||
}
|
||
|
||
pub(crate) fn clip(mut s: String, max: usize) -> String {
|
||
if s.len() > max {
|
||
let mut end = max;
|
||
while !s.is_char_boundary(end) {
|
||
end -= 1;
|
||
}
|
||
s.truncate(end);
|
||
}
|
||
s
|
||
}
|
||
|
||
/// A call failed: either the plugin returned an error (it stays usable) or it
|
||
/// trapped / ran out of budget (the instance is poisoned and must be dropped).
|
||
#[derive(Debug)]
|
||
pub enum CallError {
|
||
/// The plugin returned an error.
|
||
Guest(GuestError),
|
||
/// The instance trapped (panic, fuel, memory, timeout) and is unusable.
|
||
Trap(String),
|
||
}
|
||
|
||
impl CallError {
|
||
fn missing_export(interface: &str) -> Self {
|
||
Self::Guest(GuestError::Failed(format!("the plugin does not export {interface}")))
|
||
}
|
||
}
|
||
|
||
impl std::fmt::Display for CallError {
|
||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
match self {
|
||
Self::Guest(e) => e.fmt(f),
|
||
Self::Trap(e) => f.write_str(e),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// An error a plugin returned (the API's `error` variant).
|
||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||
pub enum GuestError {
|
||
/// A capability it needs is not granted.
|
||
Denied(String),
|
||
Stale,
|
||
NotFound(String),
|
||
Invalid(String),
|
||
/// The user cancelled, or the plugin is being stopped.
|
||
Cancelled,
|
||
Failed(String),
|
||
}
|
||
|
||
impl GuestError {
|
||
/// Whether this is the plugin failing, as opposed to an outcome the
|
||
/// user or the host caused (cancelling, a missing grant, bad input).
|
||
pub fn is_failure(&self) -> bool {
|
||
matches!(self, Self::Failed(_))
|
||
}
|
||
}
|
||
|
||
impl std::fmt::Display for GuestError {
|
||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
match self {
|
||
Self::Denied(c) => write!(f, "permission {c} was not granted"),
|
||
Self::Stale => f.write_str("the document changed meanwhile"),
|
||
Self::NotFound(m) => write!(f, "not found: {m}"),
|
||
Self::Invalid(m) => write!(f, "invalid: {m}"),
|
||
Self::Cancelled => f.write_str("cancelled"),
|
||
Self::Failed(m) => f.write_str(m),
|
||
}
|
||
}
|
||
}
|
||
|
||
impl From<GuestError> for ide_core::IdeError {
|
||
fn from(e: GuestError) -> Self {
|
||
use ide_core::error::ErrorKind;
|
||
let kind = match &e {
|
||
GuestError::Denied(_) => ErrorKind::PermissionDenied,
|
||
GuestError::Stale => ErrorKind::OutOfSync,
|
||
GuestError::NotFound(_) => ErrorKind::NotFound,
|
||
GuestError::Invalid(_) => ErrorKind::InvalidInput,
|
||
GuestError::Cancelled => ErrorKind::Cancelled,
|
||
GuestError::Failed(_) => ErrorKind::Internal,
|
||
};
|
||
ide_core::IdeError::new(kind, e.to_string())
|
||
}
|
||
}
|
||
|
||
pub struct RuntimeSetup {
|
||
pub plugin_id: String,
|
||
pub version: String,
|
||
/// Capabilities granted to the plugin.
|
||
pub granted: Vec<Capability>,
|
||
/// Whose grant this is (installed id, or the package directory of a
|
||
/// linked or project plugin): what `net:ask` answers are remembered for.
|
||
pub subject: ide_extension::ledger::Subject,
|
||
pub host: Arc<dyn HostApi>,
|
||
pub limits: Limits,
|
||
}
|
||
|
||
pub struct Runtime {
|
||
store: Store<StoreData>,
|
||
exports: Exports,
|
||
limits: Limits,
|
||
version: String,
|
||
}
|
||
|
||
/// What the component exports: the `plugin` world, plus optional interfaces.
|
||
struct Exports {
|
||
run_target_validation: Option<wit_rtv::Guest>,
|
||
run_anything: Option<wit_ra::Guest>,
|
||
run_config_completion: Option<wit_rcc::Guest>,
|
||
language_features: Option<wit_lf::Guest>,
|
||
injections: Option<wit_inj::Guest>,
|
||
plugin: bindings::Plugin,
|
||
syntax: Option<wit_syntax::Guest>,
|
||
providers: Option<wit_prov::Guest>,
|
||
indexer: Option<wit_index::Guest>,
|
||
file_actions: Option<wit_fa::Guest>,
|
||
views: Option<wit_view::Guest>,
|
||
semantic: Option<wit_sem::Guest>,
|
||
details: Option<wit_detail::Guest>,
|
||
diagnostics: Option<crate::wit_host::diagnostics_bindings::exports::openidea::plugin::diagnostics_provider::Guest>,
|
||
hover: Option<wit_hover::Guest>,
|
||
commit_checks: Option<wit_cc::Guest>,
|
||
inlay: Option<wit_inlay::Guest>,
|
||
syntax_decorations: Option<wit_syn::Guest>,
|
||
endpoints: Option<wit_endpoints::Guest>,
|
||
search: Option<wit_search::Guest>,
|
||
hover_actions: Option<wit_hia::Guest>,
|
||
completion_list: Option<wit_cl::Guest>,
|
||
run_configurations: Option<wit_configs::Guest>,
|
||
launch_configurations: Option<wit_launch::Guest>,
|
||
execution_configurations: Option<wit_exec::Guest>,
|
||
test_results: Option<wit_tests::Guest>,
|
||
mcp: Option<crate::wit_host::mcp_bindings::exports::openidea::plugin::mcp_provider::Guest>,
|
||
}
|
||
|
||
/// Most results one syntax call may return (more are dropped).
|
||
const MAX_SYNTAX_ITEMS: usize = 200_000;
|
||
/// Most events per delivered batch.
|
||
const MAX_BATCH: usize = 1000;
|
||
|
||
fn event_to_wit(e: WorkbenchEvent) -> wit_events::Event {
|
||
match e {
|
||
WorkbenchEvent::DocumentChanged(value) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "document-changed".into(),
|
||
data: serde_json::to_string(&value).expect("document change serializes"),
|
||
}),
|
||
WorkbenchEvent::WebsocketReady(value) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "websocket-ready".into(),
|
||
data: serde_json::to_string(&value).expect("WebSocket ready event serializes"),
|
||
}),
|
||
WorkbenchEvent::RunExited(value) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "run-exited".into(),
|
||
data: serde_json::to_string(&value).unwrap(),
|
||
}),
|
||
WorkbenchEvent::TerminalExited(value) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "terminal-exited".into(),
|
||
data: serde_json::to_string(&value).unwrap(),
|
||
}),
|
||
WorkbenchEvent::ContentTabAction(value) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "content-tab-action".into(),
|
||
data: serde_json::to_string(&value).unwrap(),
|
||
}),
|
||
WorkbenchEvent::FileClosed(p) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "file-closed".into(),
|
||
data: serde_json::to_string(&p).expect("path serializes"),
|
||
}),
|
||
WorkbenchEvent::SettingsChanged(keys) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "settings-changed".into(),
|
||
data: serde_json::to_string(&keys).expect("keys serialize"),
|
||
}),
|
||
WorkbenchEvent::ExtensionsChanged(points) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "extensions-changed".into(),
|
||
data: serde_json::to_string(&points).expect("points serialize"),
|
||
}),
|
||
WorkbenchEvent::InputModeChanged(mode) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "input-mode-changed".into(),
|
||
data: serde_json::to_string(&mode).expect("mode serializes"),
|
||
}),
|
||
WorkbenchEvent::CaretMoved(caret) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "caret-moved".into(),
|
||
data: serde_json::json!({
|
||
"path": caret.path,
|
||
"line": caret.line,
|
||
"col": caret.col,
|
||
"settled": caret.settled,
|
||
})
|
||
.to_string(),
|
||
}),
|
||
WorkbenchEvent::FileOpened(p) => wit_events::Event::FileOpened(p),
|
||
WorkbenchEvent::FileSaved(p) => wit_events::Event::FileSaved(p),
|
||
WorkbenchEvent::FilesChanged(batch) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "files-changed".into(),
|
||
data: serde_json::to_string(&batch).expect("file paths serialize"),
|
||
}),
|
||
WorkbenchEvent::WatchedFilesChanged(batch) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "watched-files-changed".into(),
|
||
data: serde_json::to_string(&batch).expect("file paths serialize"),
|
||
}),
|
||
WorkbenchEvent::EditorFocused(p) => wit_events::Event::EditorFocused(p),
|
||
WorkbenchEvent::VcsChanged(p) => wit_events::Event::VcsChanged(p),
|
||
WorkbenchEvent::ProblemsChanged => {
|
||
wit_events::Event::Other(wit_types::Extension { kind: "problems-changed".into(), data: "{}".into() })
|
||
}
|
||
WorkbenchEvent::ProjectClosed(p) => wit_events::Event::Other(wit_types::Extension {
|
||
kind: "project-closed".into(),
|
||
data: serde_json::to_string(&p).expect("path serializes"),
|
||
}),
|
||
WorkbenchEvent::Scheduled => wit_events::Event::Scheduled,
|
||
WorkbenchEvent::ActionPerformed(a) => wit_events::Event::ActionPerformed(wit_events::ActionPerformed {
|
||
action: a.action,
|
||
title: a.title,
|
||
source: a.source.name().to_owned(),
|
||
shortcut: a.shortcut,
|
||
}),
|
||
}
|
||
}
|
||
|
||
impl Runtime {
|
||
pub fn instantiate(engine: &PluginEngine, component: &Component, setup: RuntimeSetup) -> Result<Self, String> {
|
||
let stderr = MemoryOutputPipe::new(STDERR_CAPACITY);
|
||
// No preopened dirs, env, args or network: host imports are the only way out.
|
||
let wasi = WasiCtx::builder().stderr(stderr.clone()).build();
|
||
let store_limits = StoreLimitsBuilder::new()
|
||
.memory_size(setup.limits.memory_bytes)
|
||
.instances(32)
|
||
.tables(64)
|
||
.memories(8)
|
||
.trap_on_grow_failure(true)
|
||
.build();
|
||
let now = Instant::now();
|
||
let data = StoreData {
|
||
deadline: now + setup.limits.timeout,
|
||
timeout: setup.limits.timeout,
|
||
call_started: now,
|
||
cancel: Arc::default(),
|
||
limits: setup.limits,
|
||
wasi,
|
||
table: ResourceTable::new(),
|
||
store_limits,
|
||
stderr,
|
||
stderr_seen: 0,
|
||
plugin_id: setup.plugin_id,
|
||
granted: setup.granted,
|
||
subject: setup.subject,
|
||
host: setup.host,
|
||
invocation: Invocation::default(),
|
||
interactive: false,
|
||
user_invoked: false,
|
||
action_prompt_client: None,
|
||
open_tasks: Vec::new(),
|
||
last_report: None,
|
||
host_io_grace_used: false,
|
||
active_fuel: setup.limits.fuel,
|
||
};
|
||
let mut store = Store::new(&engine.engine, data);
|
||
store.limiter(|d| &mut d.store_limits);
|
||
// Checked every epoch tick: the per-call deadline and cancellation.
|
||
store.epoch_deadline_callback(|ctx| {
|
||
let data = ctx.data();
|
||
if data.cancel.is_set() {
|
||
Err(wasmtime::Error::msg(CANCELLED))
|
||
} else if Instant::now() >= data.deadline {
|
||
Err(wasmtime::Error::msg(format!("timed out after {:?}", data.timeout)))
|
||
} else {
|
||
Ok(UpdateDeadline::Continue(1))
|
||
}
|
||
});
|
||
arm(&mut store, &setup.limits, setup.limits.timeout).map_err(|e| e.to_string())?;
|
||
let api_hint = |e: wasmtime::Error| {
|
||
let msg = describe_trap(&e, &setup.limits, setup.limits.fuel);
|
||
if msg.contains("openidea:plugin/") { link_error(&msg) } else { msg }
|
||
};
|
||
let pre = engine.linker.instantiate_pre(component).map_err(api_hint)?;
|
||
let plugin_indices = bindings::PluginIndices::new(&pre).map_err(api_hint)?;
|
||
let syntax_indices = wit_syntax::GuestIndices::new(&pre).ok();
|
||
let index_indices = wit_index::GuestIndices::new(&pre).ok();
|
||
let file_action_indices = wit_fa::GuestIndices::new(&pre).ok();
|
||
let provider_indices = wit_prov::GuestIndices::new(&pre).ok();
|
||
let view_indices = wit_view::GuestIndices::new(&pre).ok();
|
||
let semantic_indices = wit_sem::GuestIndices::new(&pre).ok();
|
||
let detail_indices = wit_detail::GuestIndices::new(&pre).ok();
|
||
use crate::wit_host::diagnostics_bindings::exports::openidea::plugin::diagnostics_provider as wit_diag;
|
||
let diagnostics_indices = wit_diag::GuestIndices::new(&pre).ok();
|
||
let hover_indices = wit_hover::GuestIndices::new(&pre).ok();
|
||
let commit_check_indices = wit_cc::GuestIndices::new(&pre).ok();
|
||
let inlay_indices = wit_inlay::GuestIndices::new(&pre).ok();
|
||
let syntax_decoration_indices = wit_syn::GuestIndices::new(&pre).ok();
|
||
let endpoint_indices = wit_endpoints::GuestIndices::new(&pre).ok();
|
||
let search_indices = wit_search::GuestIndices::new(&pre).ok();
|
||
let hover_actions_indices = wit_hia::GuestIndices::new(&pre).ok();
|
||
let completion_list_indices = wit_cl::GuestIndices::new(&pre).ok();
|
||
let configs_indices = wit_configs::GuestIndices::new(&pre).ok();
|
||
let launch_indices = wit_launch::GuestIndices::new(&pre).ok();
|
||
let exec_indices = wit_exec::GuestIndices::new(&pre).ok();
|
||
let tests_indices = wit_tests::GuestIndices::new(&pre).ok();
|
||
let validation_indices = wit_rtv::GuestIndices::new(&pre).ok();
|
||
let run_anything_indices = wit_ra::GuestIndices::new(&pre).ok();
|
||
let run_config_completion_indices = wit_rcc::GuestIndices::new(&pre).ok();
|
||
let lf_indices = wit_lf::GuestIndices::new(&pre).ok();
|
||
let inj_indices = wit_inj::GuestIndices::new(&pre).ok();
|
||
let mcp_indices =
|
||
crate::wit_host::mcp_bindings::exports::openidea::plugin::mcp_provider::GuestIndices::new(&pre).ok();
|
||
let instance = pre.instantiate(&mut store).map_err(api_hint)?;
|
||
let mcp = mcp_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let run_configurations =
|
||
configs_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let launch_configurations =
|
||
launch_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let execution_configurations =
|
||
exec_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let test_results = tests_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let language_features = lf_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let injections = inj_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let run_target_validation =
|
||
validation_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let run_anything = run_anything_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let run_config_completion =
|
||
run_config_completion_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let plugin = plugin_indices.load(&mut store, &instance).map_err(api_hint)?;
|
||
let syntax = match syntax_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let providers = match provider_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let indexer = match index_indices {
|
||
Some(i) => Some(i.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let file_actions = match file_action_indices {
|
||
Some(i) => Some(i.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let views = match view_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let semantic = match semantic_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let details = match detail_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let diagnostics = match diagnostics_indices {
|
||
Some(i) => Some(i.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let hover = match hover_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let commit_checks = match commit_check_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let inlay = match inlay_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let syntax_decorations = match syntax_decoration_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
let search = search_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let hover_actions =
|
||
hover_actions_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let completion_list =
|
||
completion_list_indices.map(|i| i.load(&mut store, &instance)).transpose().map_err(api_hint)?;
|
||
let endpoints = match endpoint_indices {
|
||
Some(indices) => Some(indices.load(&mut store, &instance).map_err(api_hint)?),
|
||
None => None,
|
||
};
|
||
Ok(Self {
|
||
store,
|
||
exports: Exports {
|
||
run_target_validation,
|
||
run_anything,
|
||
run_config_completion,
|
||
language_features,
|
||
injections,
|
||
plugin,
|
||
syntax,
|
||
providers,
|
||
views,
|
||
indexer,
|
||
semantic,
|
||
details,
|
||
diagnostics,
|
||
hover,
|
||
commit_checks,
|
||
inlay,
|
||
syntax_decorations,
|
||
endpoints,
|
||
search,
|
||
hover_actions,
|
||
completion_list,
|
||
run_configurations,
|
||
launch_configurations,
|
||
execution_configurations,
|
||
test_results,
|
||
mcp,
|
||
file_actions,
|
||
},
|
||
limits: setup.limits,
|
||
version: setup.version,
|
||
})
|
||
}
|
||
|
||
pub fn has_run_anything(&self) -> bool {
|
||
self.exports.run_anything.is_some()
|
||
}
|
||
|
||
/// Run Anything entries for `text` (API 1.18); none without the export.
|
||
pub fn run_anything_suggest(
|
||
&mut self,
|
||
prefix: &str,
|
||
text: &str,
|
||
invocation: Invocation,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::RunAnythingItem>, CallError> {
|
||
if self.exports.run_anything.is_none() {
|
||
return Ok(Vec::new());
|
||
}
|
||
let root = invocation.workspace_root.as_deref().map(to_ipc).unwrap_or_default();
|
||
let items = self.call(invocation, timeout, |e, store| {
|
||
e.run_anything.as_ref().unwrap().call_suggest(store, &root, prefix, text)
|
||
})?;
|
||
Ok(items
|
||
.into_iter()
|
||
.take(crate::MAX_RUN_ANYTHING_ITEMS)
|
||
.map(|i| crate::RunAnythingItem {
|
||
label: clip(i.label, MAX_TITLE),
|
||
detail: i.detail.map(|d| clip(d, MAX_TITLE)),
|
||
line: i.line.map(|l| clip(l, 4096)),
|
||
})
|
||
.collect())
|
||
}
|
||
|
||
/// Runs a Run Anything line (API 1.18): whether the plugin claimed it.
|
||
pub fn run_anything_run(
|
||
&mut self,
|
||
prefix: &str,
|
||
line: &str,
|
||
invocation: Invocation,
|
||
timeout: Duration,
|
||
) -> Result<bool, CallError> {
|
||
if self.exports.run_anything.is_none() {
|
||
return Ok(false);
|
||
}
|
||
let root = invocation.workspace_root.as_deref().map(to_ipc).unwrap_or_default();
|
||
self.call(invocation, timeout, |e, store| e.run_anything.as_ref().unwrap().call_run(store, &root, prefix, line))
|
||
}
|
||
|
||
/// Completions for `field` of this plugin's configuration `config` (API 1.18); none
|
||
/// without the export. Items whose range falls outside `text` are dropped.
|
||
pub fn run_configuration_complete(
|
||
&mut self,
|
||
config: &str,
|
||
field: &str,
|
||
text: &str,
|
||
caret: u32,
|
||
invocation: Invocation,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::RunConfigCompletion>, CallError> {
|
||
if self.exports.run_config_completion.is_none() {
|
||
return Ok(Vec::new());
|
||
}
|
||
let root = invocation.workspace_root.as_deref().map(to_ipc).unwrap_or_default();
|
||
let items = self.call(invocation, timeout, |e, store| {
|
||
e.run_config_completion.as_ref().unwrap().call_complete(store, &root, config, field, text, caret)
|
||
})?;
|
||
let len = text.encode_utf16().count() as u32;
|
||
Ok(items
|
||
.into_iter()
|
||
.filter(|i| i.replace_start <= i.replace_end && i.replace_end <= len)
|
||
.take(crate::MAX_RUN_CONFIG_COMPLETIONS)
|
||
.map(|i| crate::RunConfigCompletion {
|
||
insert: clip(i.insert.unwrap_or_else(|| i.label.clone()), 4096),
|
||
label: clip(i.label, MAX_TITLE),
|
||
detail: i.detail.map(|d| clip(d, MAX_TITLE)),
|
||
replace_start: i.replace_start,
|
||
replace_end: i.replace_end,
|
||
})
|
||
.collect())
|
||
}
|
||
|
||
pub fn has_run_target_validation(&self) -> bool {
|
||
self.exports.run_target_validation.is_some()
|
||
}
|
||
|
||
/// Missing optional validators allow the launch without a guest call.
|
||
pub fn validate_run_target(
|
||
&mut self,
|
||
root: String,
|
||
target: String,
|
||
launch: crate::RunTargetLaunch,
|
||
timeout: Duration,
|
||
) -> Result<Result<(), String>, CallError> {
|
||
if self.exports.run_target_validation.is_none() {
|
||
return Ok(Ok(()));
|
||
}
|
||
let invocation = Invocation { workspace_root: Some(root.clone().into()), ..Default::default() };
|
||
let launch = wit_rtv::LaunchInfo {
|
||
operation: match launch.operation {
|
||
crate::RunTargetOperation::Run => wit_rtv::Operation::Run,
|
||
crate::RunTargetOperation::Test => wit_rtv::Operation::Test,
|
||
},
|
||
program: launch.program,
|
||
cwd: launch.cwd,
|
||
};
|
||
self.call(invocation, timeout, |e, store| {
|
||
e.run_target_validation.as_ref().unwrap().call_validate(store, &root, &target, &launch)
|
||
})
|
||
.map(|r| r.map_err(|message| clip(message, 4096)))
|
||
}
|
||
|
||
/// Complete configurations from the optional API 1.18 export, else API 1.9, else API 1.4.
|
||
pub fn run_configurations(
|
||
&mut self,
|
||
root: String,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::PluginRunConfiguration>, CallError> {
|
||
let invocation = Invocation { workspace_root: Some(root.clone().into()), ..Default::default() };
|
||
let items: Vec<crate::PluginRunConfiguration> = if self.exports.execution_configurations.is_some() {
|
||
let items = self.call(invocation, timeout, |e, store| {
|
||
e.execution_configurations.as_ref().unwrap().call_configurations(store, &root)
|
||
})?;
|
||
if items.len() > 1000 {
|
||
return Err(CallError::Guest(GuestError::Invalid("too many run configurations".into())));
|
||
}
|
||
items
|
||
.into_iter()
|
||
.map(|c| crate::PluginRunConfiguration {
|
||
id: c.id,
|
||
name: c.name,
|
||
program: c.program,
|
||
args: c.args,
|
||
cwd: c.cwd,
|
||
test: c.test,
|
||
framework: c.framework,
|
||
env: c.env,
|
||
debugger: match c.debugger {
|
||
Some(wit_exec_launch::Debugger::Launch(language)) => {
|
||
Some(crate::PluginDebugger::Launch { language })
|
||
}
|
||
Some(wit_exec_launch::Debugger::AttachChild(a)) => Some(crate::PluginDebugger::AttachChild {
|
||
kind: a.kind,
|
||
command_contains: a.command_contains,
|
||
}),
|
||
Some(wit_exec_launch::Debugger::Other(_)) | None => None,
|
||
},
|
||
output_mode: match c.output_mode {
|
||
wit_run::OutputMode::Pipes => crate::PluginOutputMode::Pipes,
|
||
wit_run::OutputMode::Pty => crate::PluginOutputMode::Pty,
|
||
},
|
||
})
|
||
.collect()
|
||
} else if self.exports.launch_configurations.is_some() {
|
||
let items = self.call(invocation, timeout, |e, store| {
|
||
e.launch_configurations.as_ref().unwrap().call_configurations(store, &root)
|
||
})?;
|
||
if items.len() > 1000 {
|
||
return Err(CallError::Guest(GuestError::Invalid("too many run configurations".into())));
|
||
}
|
||
items
|
||
.into_iter()
|
||
.map(|c| crate::PluginRunConfiguration {
|
||
id: c.id,
|
||
name: c.name,
|
||
program: c.program,
|
||
args: c.args,
|
||
cwd: c.cwd,
|
||
test: c.test,
|
||
framework: c.framework,
|
||
env: c.env,
|
||
debugger: match c.debugger {
|
||
Some(wit_launch::Debugger::Launch(language)) => {
|
||
Some(crate::PluginDebugger::Launch { language })
|
||
}
|
||
Some(wit_launch::Debugger::AttachChild(a)) => Some(crate::PluginDebugger::AttachChild {
|
||
kind: a.kind,
|
||
command_contains: a.command_contains,
|
||
}),
|
||
Some(wit_launch::Debugger::Other(_)) | None => None,
|
||
},
|
||
output_mode: crate::PluginOutputMode::Pipes,
|
||
})
|
||
.collect()
|
||
} else if self.exports.run_configurations.is_some() {
|
||
let items = self.call(invocation, timeout, |e, store| {
|
||
e.run_configurations.as_ref().unwrap().call_configurations(store, &root)
|
||
})?;
|
||
if items.len() > 1000 {
|
||
return Err(CallError::Guest(GuestError::Invalid("too many run configurations".into())));
|
||
}
|
||
items
|
||
.into_iter()
|
||
.map(|c| crate::PluginRunConfiguration {
|
||
id: c.id,
|
||
name: c.name,
|
||
program: c.program,
|
||
args: c.args,
|
||
cwd: c.cwd,
|
||
test: c.test,
|
||
framework: c.framework,
|
||
..Default::default()
|
||
})
|
||
.collect()
|
||
} else {
|
||
return Err(CallError::missing_export("run-configurations"));
|
||
};
|
||
Ok(items)
|
||
}
|
||
/// Selects tests under the same budget as the test parser.
|
||
pub fn select_tests(
|
||
&mut self,
|
||
config: String,
|
||
tests: Vec<Vec<String>>,
|
||
invocation: Invocation,
|
||
timeout: Duration,
|
||
) -> Result<Option<Vec<String>>, CallError> {
|
||
if self.exports.test_results.is_none() {
|
||
return Err(CallError::missing_export("test-results"));
|
||
}
|
||
self.call(invocation, timeout, |e, store| {
|
||
e.test_results.as_ref().unwrap().call_select_tests(store, &config, &tests)
|
||
})
|
||
}
|
||
|
||
/// One editor-budget batch of a plugin test parser.
|
||
pub fn parse_test_results(
|
||
&mut self,
|
||
config: String,
|
||
chunk: String,
|
||
invocation: Invocation,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::PluginTestEvent>, CallError> {
|
||
if self.exports.test_results.is_none() {
|
||
return Err(CallError::missing_export("test-results"));
|
||
}
|
||
let items = self.call(invocation, timeout, |e, store| {
|
||
e.test_results.as_ref().unwrap().call_parse(store, &config, &chunk)
|
||
})?;
|
||
if items.len() > 10000 {
|
||
return Err(CallError::Guest(GuestError::Invalid("too many test events".into())));
|
||
}
|
||
Ok(items.into_iter().map(test_event_from_wit).collect())
|
||
}
|
||
|
||
/// The component exports `syntax-provider`.
|
||
pub fn has_syntax(&self) -> bool {
|
||
self.exports.syntax.is_some()
|
||
}
|
||
|
||
pub fn syntax_highlights(
|
||
&mut self,
|
||
language: &str,
|
||
text: &str,
|
||
timeout: Duration,
|
||
) -> Result<Vec<SyntaxToken>, CallError> {
|
||
let tokens = self.syntax_call(timeout, |s, store| s.call_highlights(store, language, text))?;
|
||
Ok(tokens
|
||
.into_iter()
|
||
.take(MAX_SYNTAX_ITEMS)
|
||
.map(|t| SyntaxToken { start: t.start, end: t.end, kind: clip(t.kind, 32) })
|
||
.collect())
|
||
}
|
||
|
||
pub fn syntax_outline(
|
||
&mut self,
|
||
language: &str,
|
||
text: &str,
|
||
timeout: Duration,
|
||
) -> Result<Vec<SyntaxSymbol>, CallError> {
|
||
let symbols = self.syntax_call(timeout, |s, store| s.call_outline(store, language, text))?;
|
||
Ok(symbols
|
||
.into_iter()
|
||
.take(MAX_SYNTAX_ITEMS)
|
||
.map(|s| SyntaxSymbol {
|
||
name: clip(s.name, MAX_TITLE),
|
||
detail: s.detail.map(|d| clip(d, MAX_TITLE)),
|
||
kind: clip(s.kind, 32),
|
||
start: s.start,
|
||
end: s.end,
|
||
name_start: s.name_start,
|
||
name_end: s.name_end,
|
||
parent: s.parent,
|
||
})
|
||
.collect())
|
||
}
|
||
|
||
pub fn syntax_folding(
|
||
&mut self,
|
||
language: &str,
|
||
text: &str,
|
||
timeout: Duration,
|
||
) -> Result<Vec<SyntaxFold>, CallError> {
|
||
let folds = self.syntax_call(timeout, |s, store| s.call_folding(store, language, text))?;
|
||
Ok(folds
|
||
.into_iter()
|
||
.take(MAX_SYNTAX_ITEMS)
|
||
.map(|f| SyntaxFold { start_line: f.start_line, end_line: f.end_line, kind: clip(f.kind, 32) })
|
||
.collect())
|
||
}
|
||
|
||
fn syntax_call<R>(
|
||
&mut self,
|
||
timeout: Duration,
|
||
f: impl FnOnce(&wit_syntax::Guest, &mut Store<StoreData>) -> wasmtime::Result<R>,
|
||
) -> Result<R, CallError> {
|
||
if self.exports.syntax.is_none() {
|
||
return Err(CallError::missing_export("syntax-provider"));
|
||
}
|
||
self.call(Invocation::default(), timeout, |exports, store| f(exports.syntax.as_ref().expect("checked"), store))
|
||
}
|
||
|
||
pub fn index_document(
|
||
&mut self,
|
||
path: &str,
|
||
text: &str,
|
||
symbols: &[crate::indexing::IndexSymbol],
|
||
invocation: Invocation,
|
||
) -> Result<crate::indexing::Edges, CallError> {
|
||
if self.exports.indexer.is_none() {
|
||
return Err(CallError::missing_export("document-indexer"));
|
||
}
|
||
let doc = wit_index::Document {
|
||
path: path.into(),
|
||
text: text.into(),
|
||
symbols: symbols
|
||
.iter()
|
||
.map(|s| wit_index::Symbol {
|
||
name: s.name.clone(),
|
||
kind: s.kind.clone(),
|
||
container: s.container.clone(),
|
||
range: range_to_wit(s.range),
|
||
})
|
||
.collect(),
|
||
};
|
||
let edges = self.call(invocation, self.limits.timeout, |e, store| {
|
||
e.indexer.as_ref().expect("checked").call_index(store, &doc)
|
||
})?;
|
||
fn anchor(a: wit_index::Anchor) -> crate::indexing::Anchor {
|
||
crate::indexing::Anchor { key: a.key, range: range_from_wit(a.range) }
|
||
}
|
||
Ok(crate::indexing::Edges {
|
||
definitions: edges.definitions.into_iter().map(anchor).collect(),
|
||
references: edges.references.into_iter().map(anchor).collect(),
|
||
aliases: edges
|
||
.aliases
|
||
.into_iter()
|
||
.map(|a| crate::indexing::Alias { prefix: a.prefix, target: a.target })
|
||
.collect(),
|
||
markers: edges.markers.into_iter().map(anchor).collect(),
|
||
})
|
||
}
|
||
|
||
fn provider_call<R>(
|
||
&mut self,
|
||
timeout: Duration,
|
||
f: impl FnOnce(&wit_prov::Guest, &mut Store<StoreData>) -> wasmtime::Result<R>,
|
||
) -> Result<R, CallError> {
|
||
if self.exports.providers.is_none() {
|
||
return Err(CallError::missing_export("providers"));
|
||
}
|
||
self.call(Invocation::default(), timeout, |e, store| f(e.providers.as_ref().expect("checked"), store))
|
||
}
|
||
|
||
pub fn provide_completion(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
trigger: Option<&str>,
|
||
timeout: Duration,
|
||
) -> Result<Vec<CompletionItem>, CallError> {
|
||
Ok(self.provide_completion_list(doc, at, trigger, timeout)?.0)
|
||
}
|
||
|
||
/// Completion items and whether the plugin marked them incomplete: from the
|
||
/// `completion-list-provider` export (API 1.16) when the plugin has it, else
|
||
/// from `providers.completion`, which is never incomplete.
|
||
pub fn provide_completion_list(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
trigger: Option<&str>,
|
||
timeout: Duration,
|
||
) -> Result<(Vec<CompletionItem>, bool), CallError> {
|
||
macro_rules! item {
|
||
($i:expr) => {{
|
||
let i = $i;
|
||
CompletionItem {
|
||
label: clip(i.label, MAX_TITLE),
|
||
detail: i.detail.map(|d| clip(d, MAX_TITLE)),
|
||
kind: i.kind.map(|k| clip(k, 32)),
|
||
insert_text: clip(i.insert_text, 64 << 10),
|
||
snippet: i.snippet,
|
||
replace: i.replace.map(range_from_wit),
|
||
sort_text: i.sort_text.map(|t| clip(t, 64)),
|
||
filter_text: i.filter_text.map(|t| clip(t, MAX_TITLE)),
|
||
documentation: i.documentation.map(|d| clip(d, 16 << 10)),
|
||
additional_edits: i
|
||
.additional_edits
|
||
.into_iter()
|
||
.take(MAX_ADDITIONAL_EDITS)
|
||
.map(edit_from_wit)
|
||
.collect(),
|
||
}
|
||
}};
|
||
}
|
||
if self.exports.completion_list.is_some() {
|
||
let d = wit_cl::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let list = self.call(Invocation::default(), timeout, |exports, store| {
|
||
exports.completion_list.as_ref().expect("checked").call_completion(store, &d, pos_to_wit(at), trigger)
|
||
})?;
|
||
let items = list.items.into_iter().take(MAX_PROVIDER_ITEMS).map(|i| item!(i)).collect();
|
||
return Ok((items, list.incomplete));
|
||
}
|
||
let d = doc_to_wit(doc);
|
||
let items = self.provider_call(timeout, |p, s| p.call_completion(s, &d, pos_to_wit(at), trigger))?;
|
||
Ok((items.into_iter().take(MAX_PROVIDER_ITEMS).map(|i| item!(i)).collect(), false))
|
||
}
|
||
|
||
pub fn provide_inline_completion(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Option<InlineSuggestion>, CallError> {
|
||
let d = doc_to_wit(doc);
|
||
let s = self.provider_call(timeout, |p, s| p.call_inline_completion(s, &d, pos_to_wit(at)))?;
|
||
Ok(s.map(|s| InlineSuggestion {
|
||
insert_text: clip(s.insert_text, 64 << 10),
|
||
replace: s.replace.map(range_from_wit),
|
||
}))
|
||
}
|
||
|
||
pub fn provide_code_actions(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
selection: ide_core::Range,
|
||
timeout: Duration,
|
||
) -> Result<Vec<CodeAction>, CallError> {
|
||
let mut out = Vec::new();
|
||
if self.exports.providers.is_some() {
|
||
let d = doc_to_wit(doc);
|
||
let actions = self.provider_call(timeout, |p, s| p.call_code_actions(s, &d, range_to_wit(selection)))?;
|
||
out.extend(actions.into_iter().take(MAX_PROVIDER_ITEMS).map(code_action_from_wit));
|
||
}
|
||
if self.exports.file_actions.is_some() {
|
||
let d = file_action_doc(doc);
|
||
let actions = self.call(Invocation::default(), timeout, |e, store| {
|
||
e.file_actions.as_ref().expect("checked").call_code_actions(store, &d, range_to_wit(selection))
|
||
})?;
|
||
out.extend(actions.into_iter().take(MAX_PROVIDER_ITEMS).map(file_action_from_wit));
|
||
}
|
||
if out.is_empty() && self.exports.providers.is_none() {
|
||
return Err(CallError::missing_export("providers"));
|
||
}
|
||
Ok(out)
|
||
}
|
||
|
||
/// The edits of an action `provide_code_actions` returned with `data`.
|
||
pub fn resolve_code_action(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
action: &CodeAction,
|
||
invocation: Invocation,
|
||
timeout: Duration,
|
||
) -> Result<CodeAction, CallError> {
|
||
if action.file_action {
|
||
if self.exports.file_actions.is_none() {
|
||
return Err(CallError::missing_export("file-actions"));
|
||
}
|
||
let d = file_action_doc(doc);
|
||
let a = file_action_to_wit(action);
|
||
let resolved = self.call(Invocation::default(), timeout, |e, store| {
|
||
store.data_mut().action_prompt_client = invocation.client;
|
||
e.file_actions.as_ref().expect("checked").call_resolve(store, &d, &a)
|
||
})?;
|
||
return resolved.map(file_action_from_wit).map_err(|e| CallError::Guest(e.into()));
|
||
}
|
||
let d = doc_to_wit(doc);
|
||
let a = code_action_to_wit(action);
|
||
if self.exports.providers.is_none() {
|
||
return Err(CallError::missing_export("providers"));
|
||
}
|
||
let resolved = self.call(Invocation::default(), timeout, |e, store| {
|
||
store.data_mut().action_prompt_client = invocation.client;
|
||
e.providers.as_ref().expect("checked").call_resolve_code_action(store, &d, &a)
|
||
})?;
|
||
resolved.map(code_action_from_wit).map_err(|e| CallError::Guest(e.into()))
|
||
}
|
||
|
||
pub fn provide_definition(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Vec<Location>, CallError> {
|
||
let d = doc_to_wit(doc);
|
||
let locations = self.provider_call(timeout, |p, s| p.call_definition(s, &d, pos_to_wit(at)))?;
|
||
Ok(locations.into_iter().take(MAX_PROVIDER_ITEMS).map(location_from_wit).collect())
|
||
}
|
||
|
||
pub fn provide_line_markers(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<LineMarker>, CallError> {
|
||
let d = doc_to_wit(doc);
|
||
let markers = self.provider_call(timeout, |p, s| p.call_line_markers(s, &d))?;
|
||
Ok(markers
|
||
.into_iter()
|
||
.take(MAX_PROVIDER_ITEMS)
|
||
.map(|m| LineMarker {
|
||
line: m.line,
|
||
icon: m.icon.map(|i| clip(i, 256)),
|
||
tooltip: clip(m.tooltip, 1024),
|
||
target: m.target.map(location_from_wit),
|
||
})
|
||
.collect())
|
||
}
|
||
|
||
pub fn provide_semantic_tokens(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<SemanticToken>, CallError> {
|
||
if self.exports.semantic.is_none() {
|
||
return Err(CallError::missing_export("semantic-tokens-provider"));
|
||
}
|
||
let d = wit_sem::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let tokens = self.call(Invocation::default(), timeout, |e, store| {
|
||
e.semantic.as_ref().expect("checked").call_semantic_tokens(store, &d)
|
||
})?;
|
||
Ok(tokens
|
||
.into_iter()
|
||
.take(MAX_SEMANTIC_TOKENS)
|
||
.map(|t| SemanticToken {
|
||
range: range_from_wit(t.range),
|
||
kind: clip(t.kind, 32),
|
||
modifiers: t.modifiers.into_iter().take(MAX_TOKEN_MODIFIERS).map(|m| clip(m, 32)).collect(),
|
||
})
|
||
.collect())
|
||
}
|
||
|
||
pub fn provide_diagnostics(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<ide_core::Diagnostic>, CallError> {
|
||
use crate::wit_host::diagnostics_bindings::exports::openidea::plugin::diagnostics_provider as w;
|
||
if self.exports.diagnostics.is_none() {
|
||
return Err(CallError::missing_export("diagnostics-provider"));
|
||
}
|
||
let d = w::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let items = self.call(Invocation::default(), timeout, |e, store| {
|
||
e.diagnostics.as_ref().expect("checked").call_diagnostics(store, &d)
|
||
})?;
|
||
Ok(crate::wit_host::diagnostics_from_wit(items, &self.store.data().plugin_id))
|
||
}
|
||
|
||
pub fn provide_hover(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Option<PluginHover>, CallError> {
|
||
if self.exports.hover.is_none() {
|
||
return Err(CallError::missing_export("hover-provider"));
|
||
}
|
||
let d = wit_hover::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let hover = self.call(Invocation::default(), timeout, |e, store| {
|
||
e.hover.as_ref().expect("checked").call_hover(store, &d, pos_to_wit(at))
|
||
})?;
|
||
Ok(hover
|
||
.filter(|h| !h.markdown.trim().is_empty())
|
||
.map(|h| PluginHover { markdown: clip(h.markdown, MAX_HOVER_BYTES), range: h.range.map(range_from_wit) }))
|
||
}
|
||
|
||
pub fn provide_inlay_hints(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
range: ide_core::Range,
|
||
timeout: Duration,
|
||
) -> Result<Vec<PluginInlayHint>, CallError> {
|
||
if self.exports.inlay.is_none() {
|
||
return Err(CallError::missing_export("inlay-hints-provider"));
|
||
}
|
||
let d = wit_inlay::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let r = wit_types::Range { start: pos_to_wit(range.start), end: pos_to_wit(range.end) };
|
||
let hints = self.call(Invocation::default(), timeout, |e, store| {
|
||
e.inlay.as_ref().expect("checked").call_inlay_hints(store, &d, r)
|
||
})?;
|
||
Ok(hints
|
||
.into_iter()
|
||
.filter(|h| !h.label.trim().is_empty())
|
||
.take(MAX_PROVIDER_ITEMS)
|
||
.map(|h| PluginInlayHint {
|
||
position: ide_core::Pos::new(h.position.line, h.position.character),
|
||
label: clip(h.label, 256),
|
||
kind: clip(h.kind, 32),
|
||
tooltip: h.tooltip.map(|t| clip(t, 1024)),
|
||
padding_left: h.padding_left,
|
||
padding_right: h.padding_right,
|
||
})
|
||
.collect())
|
||
}
|
||
|
||
pub fn provide_syntax_decorations(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
ranges: &[ide_core::Range],
|
||
nodes: &[crate::SyntaxNodeSnapshot],
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::PluginSyntaxMark>, CallError> {
|
||
if self.exports.syntax_decorations.is_none() {
|
||
return Err(CallError::missing_export("syntax-decorations-provider"));
|
||
}
|
||
let request = wit_syn::DecorationRequest {
|
||
document: wit_syn::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
},
|
||
ranges: ranges.iter().copied().map(range_to_wit).collect(),
|
||
nodes: nodes
|
||
.iter()
|
||
.map(|n| wit_syn::SyntaxNode { name: n.name.clone(), range: range_to_wit(n.range), parent: n.parent })
|
||
.collect(),
|
||
};
|
||
let marks = self.call(Invocation::default(), timeout, |e, store| {
|
||
e.syntax_decorations.as_ref().expect("checked").call_syntax_decorations(store, &request)
|
||
})?;
|
||
Ok(marks
|
||
.into_iter()
|
||
.filter_map(|m| {
|
||
let foreground = match &m.foreground {
|
||
Some(f) => Some(crate::syntax_foreground(&f.dark, &f.light)?),
|
||
None if crate::syntax_token_allowed(&m.token) => None,
|
||
None => return None,
|
||
};
|
||
Some(crate::PluginSyntaxMark { range: range_from_wit(m.range), token: m.token, foreground })
|
||
})
|
||
.take(MAX_PROVIDER_ITEMS)
|
||
.collect())
|
||
}
|
||
|
||
pub fn activate(&mut self) -> Result<(), CallError> {
|
||
let data = self.store.data();
|
||
let ctx = wit_types::Context {
|
||
plugin_id: data.plugin_id.clone(),
|
||
plugin_version: self.version.clone(),
|
||
api_version: API_VERSION.into(),
|
||
capabilities: data.granted.iter().map(|c| c.to_string()).collect(),
|
||
};
|
||
self.call(Invocation::default(), self.limits.timeout, |e, store| {
|
||
e.plugin.openidea_plugin_lifecycle().call_activate(store, &ctx)
|
||
})
|
||
.and_then(|r| r.map_err(|e| CallError::Guest(e.into())))
|
||
}
|
||
|
||
pub fn run_command(
|
||
&mut self,
|
||
id: &str,
|
||
arg: Option<&str>,
|
||
invocation: Invocation,
|
||
) -> Result<Option<String>, CallError> {
|
||
let result = self.call(invocation, self.limits.timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
store.data_mut().interactive = true;
|
||
store.data_mut().user_invoked = true;
|
||
let r = e.plugin.openidea_plugin_commands().call_run(
|
||
&mut *store,
|
||
id,
|
||
wasmtime::component::Resource::new_borrow(ctx.rep()),
|
||
arg,
|
||
);
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})?;
|
||
result.map_err(|e| CallError::Guest(e.into()))
|
||
}
|
||
|
||
/// Content of a view or custom editor (`invocation.editor`: its document).
|
||
pub fn render_view(
|
||
&mut self,
|
||
view: &str,
|
||
viewport: Option<Viewport>,
|
||
invocation: Invocation,
|
||
) -> Result<View, CallError> {
|
||
if self.exports.views.is_none() {
|
||
return Err(CallError::missing_export("view-provider"));
|
||
}
|
||
let viewport = viewport.map(|v| wit_vt::Viewport { first_row: v.first_row, rows: v.rows.min(MAX_TABLE_ROWS) });
|
||
let result = self.call(invocation, self.limits.timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
let views = e.views.as_ref().expect("checked");
|
||
let r =
|
||
views.call_render(&mut *store, view, viewport, wasmtime::component::Resource::new_borrow(ctx.rep()));
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})?;
|
||
match result {
|
||
Ok(v) => Ok(view_from_wit(v)),
|
||
Err(e) => Err(CallError::Guest(e.into())),
|
||
}
|
||
}
|
||
|
||
/// An on-demand category query, always non-interactive and bounded.
|
||
pub fn search(
|
||
&mut self,
|
||
category: &str,
|
||
query: &str,
|
||
limit: u32,
|
||
invocation: Invocation,
|
||
) -> Result<crate::search::SearchPage, CallError> {
|
||
if self.exports.search.is_none() {
|
||
return Err(CallError::missing_export("search-provider"));
|
||
}
|
||
let result = self.call(invocation, Duration::from_secs(2), |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
let r = e.search.as_ref().expect("checked").call_search(
|
||
&mut *store,
|
||
category,
|
||
query,
|
||
limit,
|
||
wasmtime::component::Resource::new_borrow(ctx.rep()),
|
||
);
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})?;
|
||
let page = result.map_err(|e| CallError::Guest(e.into()))?;
|
||
if page.items.len() > limit as usize {
|
||
return Err(CallError::Guest(GuestError::Invalid("too many search results".into())));
|
||
}
|
||
Ok(crate::search::SearchPage {
|
||
has_more: page.has_more,
|
||
items: page
|
||
.items
|
||
.into_iter()
|
||
.map(|i| crate::search::SearchItem {
|
||
id: i.id,
|
||
title: i.title,
|
||
description: i.description,
|
||
target: match i.target {
|
||
wit_search::SearchTarget::Location(l) => {
|
||
crate::search::SearchTarget::Location(location_from_wit(l))
|
||
}
|
||
wit_search::SearchTarget::Command(c) => {
|
||
crate::search::SearchTarget::Command { id: c.id, arg: c.arg }
|
||
}
|
||
},
|
||
})
|
||
.collect(),
|
||
})
|
||
}
|
||
|
||
/// Project routes, with an explicit project invocation and background budget.
|
||
pub fn endpoints(&mut self, invocation: Invocation, timeout: Duration) -> Result<Vec<crate::Endpoint>, CallError> {
|
||
if self.exports.endpoints.is_none() {
|
||
return Err(CallError::missing_export("endpoints-provider"));
|
||
}
|
||
let result = self.call(invocation, timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
let r = e
|
||
.endpoints
|
||
.as_ref()
|
||
.expect("checked")
|
||
.call_endpoints(&mut *store, wasmtime::component::Resource::new_borrow(ctx.rep()));
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})?;
|
||
let entries = result.map_err(|e| CallError::Guest(e.into()))?;
|
||
if entries.len() > MAX_PROVIDER_ITEMS {
|
||
return Err(CallError::Guest(GuestError::Invalid("too many endpoints".into())));
|
||
}
|
||
Ok(entries
|
||
.into_iter()
|
||
.map(|e| crate::Endpoint {
|
||
method: e.method,
|
||
path: e.path,
|
||
framework: e.framework,
|
||
handler: e.handler,
|
||
location: location_from_wit(e.location),
|
||
})
|
||
.collect())
|
||
}
|
||
|
||
/// The Services pane of node `key` of the plugin's services view `view`.
|
||
pub fn service_detail(
|
||
&mut self,
|
||
view: &str,
|
||
key: &str,
|
||
invocation: Invocation,
|
||
) -> Result<Vec<ServiceTab>, CallError> {
|
||
if self.exports.details.is_none() {
|
||
return Err(CallError::missing_export("detail-provider"));
|
||
}
|
||
let result = self.call(invocation, self.limits.timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
let details = e.details.as_ref().expect("checked");
|
||
let r = details.call_detail(&mut *store, view, key, wasmtime::component::Resource::new_borrow(ctx.rep()));
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})?;
|
||
match result {
|
||
Ok(tabs) => Ok(tabs.into_iter().take(MAX_SERVICE_TABS).map(service_tab_from_wit).collect()),
|
||
Err(e) => Err(CallError::Guest(e.into())),
|
||
}
|
||
}
|
||
|
||
/// Runs the plugin's before-commit check `check` on `draft`.
|
||
pub fn commit_check(
|
||
&mut self,
|
||
check: &str,
|
||
draft: CommitDraft,
|
||
invocation: Invocation,
|
||
) -> Result<Vec<CommitProblem>, CallError> {
|
||
if self.exports.commit_checks.is_none() {
|
||
return Err(CallError::missing_export("commit-checker"));
|
||
}
|
||
let draft = crate::wit_host::commit_draft_to_wit(draft);
|
||
let result = self.call(invocation, self.limits.timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
let checks = e.commit_checks.as_ref().expect("checked");
|
||
let r = checks.call_check(&mut *store, check, &draft, wasmtime::component::Resource::new_borrow(ctx.rep()));
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})?;
|
||
match result {
|
||
Ok(problems) => Ok(problems.into_iter().take(MAX_COMMIT_PROBLEMS).map(commit_problem_from_wit).collect()),
|
||
Err(e) => Err(CallError::Guest(e.into())),
|
||
}
|
||
}
|
||
|
||
/// A user action in a view or custom editor.
|
||
pub fn view_event(&mut self, view: &str, event: ViewEvent, invocation: Invocation) -> Result<(), CallError> {
|
||
if self.exports.views.is_none() {
|
||
return Err(CallError::missing_export("view-provider"));
|
||
}
|
||
let user_invoked = !matches!(&event, ViewEvent::Message { .. });
|
||
let event = event_to_wit_view(event);
|
||
let result = self.call(invocation, self.limits.timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
store.data_mut().interactive = true;
|
||
store.data_mut().user_invoked = user_invoked;
|
||
let views = e.views.as_ref().expect("checked");
|
||
let r = views.call_handle(&mut *store, view, &event, wasmtime::component::Resource::new_borrow(ctx.rep()));
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})?;
|
||
result.map_err(|e| CallError::Guest(e.into()))
|
||
}
|
||
|
||
/// Delivers a batch of events (from the window in `invocation`).
|
||
pub fn handle_events(&mut self, events: Vec<WorkbenchEvent>, invocation: Invocation) -> Result<(), CallError> {
|
||
let events: Vec<wit_events::Event> = events.into_iter().take(MAX_BATCH).map(event_to_wit).collect();
|
||
self.call(invocation, self.limits.timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
store.data_mut().interactive = true;
|
||
let r = e.plugin.openidea_plugin_event_handler().call_handle(
|
||
&mut *store,
|
||
&events,
|
||
wasmtime::component::Resource::new_borrow(ctx.rep()),
|
||
);
|
||
store.data_mut().drop_invocation(ctx);
|
||
r
|
||
})
|
||
}
|
||
|
||
/// Best effort; errors are logged.
|
||
pub fn deactivate(&mut self) {
|
||
if let Err(e) = self.call(Invocation::default(), self.limits.timeout, |e, store| {
|
||
e.plugin.openidea_plugin_lifecycle().call_deactivate(store)
|
||
}) {
|
||
tracing::warn!(plugin = %self.store.data().plugin_id, "deactivate failed: {e}");
|
||
}
|
||
}
|
||
|
||
/// Interrupts the running call when cancelled (the instance is then
|
||
/// poisoned, like after a timeout). Reset when the next call starts.
|
||
pub fn cancel_handle(&self) -> Arc<CallCancel> {
|
||
self.store.data().cancel.clone()
|
||
}
|
||
|
||
fn call<R>(
|
||
&mut self,
|
||
invocation: Invocation,
|
||
timeout: Duration,
|
||
f: impl FnOnce(&Exports, &mut Store<StoreData>) -> wasmtime::Result<R>,
|
||
) -> Result<R, CallError> {
|
||
arm(&mut self.store, &self.limits, timeout).map_err(|e| CallError::Trap(e.to_string()))?;
|
||
self.store.data_mut().invocation = invocation;
|
||
let result = f(&self.exports, &mut self.store);
|
||
let data = self.store.data_mut();
|
||
data.invocation = Invocation::default();
|
||
data.interactive = false;
|
||
data.user_invoked = false;
|
||
data.action_prompt_client = None;
|
||
// Tasks don't outlive the call that began them.
|
||
for id in std::mem::take(&mut data.open_tasks) {
|
||
data.host.task_end(id);
|
||
}
|
||
let stderr = data.drain_stderr();
|
||
if !stderr.trim().is_empty() {
|
||
tracing::info!(plugin = %data.plugin_id, "stderr: {}", stderr.trim_end());
|
||
}
|
||
let active_fuel = data.active_fuel;
|
||
match result {
|
||
Ok(r) => Ok(r),
|
||
Err(e) => {
|
||
let mut msg = describe_trap(&e, &self.limits, active_fuel);
|
||
// Typically the panic message: "panicked at src/lib.rs:1:2:\nboom".
|
||
let detail: Vec<&str> =
|
||
stderr.lines().filter(|l| !l.trim().is_empty() && !l.starts_with("note:")).collect();
|
||
if !detail.is_empty() {
|
||
let tail = detail[detail.len().saturating_sub(4)..].join("\n");
|
||
msg = format!("{msg}\n{}", clip(tail, 1024));
|
||
}
|
||
Err(CallError::Trap(msg))
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Most items one provider call may return (more are dropped).
|
||
const MAX_PROVIDER_ITEMS: usize = 10_000;
|
||
const MAX_SEMANTIC_TOKENS: usize = 1 << 16;
|
||
const MAX_TOKEN_MODIFIERS: usize = 8;
|
||
const MAX_HOVER_BYTES: usize = 64 * 1024;
|
||
|
||
fn pos_to_wit(p: ide_core::Pos) -> wit_types::Position {
|
||
wit_types::Position { line: p.line, character: p.col }
|
||
}
|
||
|
||
fn range_to_wit(r: ide_core::Range) -> wit_types::Range {
|
||
wit_types::Range { start: pos_to_wit(r.start), end: pos_to_wit(r.end) }
|
||
}
|
||
|
||
fn range_from_wit(r: wit_types::Range) -> ide_core::Range {
|
||
ide_core::Range::new(
|
||
ide_core::Pos::new(r.start.line, r.start.character),
|
||
ide_core::Pos::new(r.end.line, r.end.character),
|
||
)
|
||
}
|
||
|
||
fn location_from_wit(l: wit_types::Location) -> Location {
|
||
Location { path: std::path::PathBuf::from(l.path), range: range_from_wit(l.range) }
|
||
}
|
||
|
||
fn doc_to_wit(d: &ProviderDocument) -> wit_prov::Document {
|
||
wit_prov::Document { path: to_ipc(&d.path), language: d.language.clone(), version: d.version, text: d.text.clone() }
|
||
}
|
||
|
||
/// Resets the budgets for the next call: fuel, deadline, cancellation.
|
||
fn arm(store: &mut Store<StoreData>, limits: &Limits, timeout: Duration) -> wasmtime::Result<()> {
|
||
store.set_fuel(limits.fuel)?;
|
||
let data = store.data_mut();
|
||
let now = Instant::now();
|
||
data.call_started = now;
|
||
data.deadline = now + timeout;
|
||
data.timeout = timeout;
|
||
data.last_report = None;
|
||
data.host_io_grace_used = false;
|
||
data.active_fuel = limits.fuel;
|
||
data.cancel.reset();
|
||
// The callback above runs every tick and decides.
|
||
store.set_epoch_deadline(1);
|
||
Ok(())
|
||
}
|
||
|
||
fn bind_task_fuel_refill(linker: &mut Linker<StoreData>) -> wasmtime::Result<()> {
|
||
use crate::wit_host::TaskRes;
|
||
use crate::wit_host::bindings::openidea::plugin::tasks::{Host, HostTask};
|
||
use wasmtime::StoreContextMut;
|
||
use wasmtime::component::Resource;
|
||
|
||
linker.allow_shadowing(true);
|
||
let tasks_iface = format!("openidea:plugin/tasks@{}", ide_extension::ENGINE_VERSION);
|
||
let mut inst = linker.instance(&tasks_iface)?;
|
||
inst.func_wrap("begin", |mut caller: StoreContextMut<'_, StoreData>, (title, cancellable): (String, bool)| {
|
||
let task = Host::begin(caller.data_mut(), title, cancellable)?;
|
||
let fuel = caller.data().active_fuel;
|
||
caller.set_fuel(fuel)?;
|
||
Ok((task,))
|
||
})?;
|
||
inst.func_wrap(
|
||
"[method]task.report",
|
||
|mut caller: StoreContextMut<'_, StoreData>,
|
||
(this, done, total, message): (Resource<TaskRes>, u32, Option<u32>, Option<String>)| {
|
||
HostTask::report(caller.data_mut(), this, done, total, message)?;
|
||
let fuel = caller.data().active_fuel;
|
||
caller.set_fuel(fuel)?;
|
||
Ok(())
|
||
},
|
||
)?;
|
||
inst.func_wrap(
|
||
"[method]task.is-cancelled",
|
||
|mut caller: StoreContextMut<'_, StoreData>, (this,): (Resource<TaskRes>,)| {
|
||
let cancelled = HostTask::is_cancelled(caller.data_mut(), this)?;
|
||
let fuel = caller.data().active_fuel;
|
||
caller.set_fuel(fuel)?;
|
||
Ok((cancelled,))
|
||
},
|
||
)?;
|
||
linker.allow_shadowing(false);
|
||
Ok(())
|
||
}
|
||
|
||
fn describe_trap(e: &wasmtime::Error, limits: &Limits, active_fuel: u64) -> String {
|
||
match e.downcast_ref::<wasmtime::Trap>() {
|
||
Some(wasmtime::Trap::OutOfFuel) => format!("exceeded its CPU budget ({active_fuel} fuel per call)"),
|
||
Some(wasmtime::Trap::Interrupt) => CANCELLED.to_owned(),
|
||
Some(wasmtime::Trap::UnreachableCodeReached) => "crashed (panic / unreachable)".to_owned(),
|
||
Some(trap) => format!("trapped: {trap}"),
|
||
None => {
|
||
let root = e.root_cause().to_string();
|
||
if root.contains("memory") {
|
||
format!("{root} (memory limit {} MiB)", limits.memory_bytes >> 20)
|
||
} else {
|
||
root
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Most table rows one render returns (and a viewport asks for).
|
||
const MAX_TABLE_ROWS: u32 = 2_000;
|
||
/// Most tree / list items and form fields per view.
|
||
const MAX_VIEW_ITEMS: usize = 20_000;
|
||
const MAX_CELL: usize = 64 << 10;
|
||
const MAX_HTML: usize = 4 << 20;
|
||
|
||
/// Most additional edits of a completion item.
|
||
const MAX_ADDITIONAL_EDITS: usize = 100;
|
||
|
||
fn edit_from_wit(e: wit_types::TextEdit) -> ide_core::TextEdit {
|
||
ide_core::TextEdit { range: range_from_wit(e.range), new_text: e.text }
|
||
}
|
||
|
||
fn code_action_from_wit(a: wit_prov::CodeAction) -> CodeAction {
|
||
CodeAction {
|
||
title: clip(a.title, MAX_TITLE),
|
||
kind: a.kind.map(|k| clip(k, 64)),
|
||
preferred: a.preferred,
|
||
edits: a
|
||
.edits
|
||
.into_iter()
|
||
.map(|f| (std::path::PathBuf::from(f.path), f.edits.into_iter().map(edit_from_wit).collect()))
|
||
.collect(),
|
||
operations: Vec::new(),
|
||
open: None,
|
||
data: a.data.map(|d| clip(d, 64 << 10)),
|
||
file_action: false,
|
||
}
|
||
}
|
||
|
||
fn code_action_to_wit(a: &CodeAction) -> wit_prov::CodeAction {
|
||
wit_prov::CodeAction {
|
||
title: a.title.clone(),
|
||
kind: a.kind.clone(),
|
||
preferred: a.preferred,
|
||
edits: a
|
||
.edits
|
||
.iter()
|
||
.map(|(path, edits)| wit_prov::FileEdit {
|
||
path: to_ipc(path),
|
||
edits: edits
|
||
.iter()
|
||
.map(|e| wit_types::TextEdit { range: range_to_wit(e.range), text: e.new_text.clone() })
|
||
.collect(),
|
||
})
|
||
.collect(),
|
||
data: a.data.clone(),
|
||
}
|
||
}
|
||
|
||
const MAX_FILE_OPERATIONS: usize = 1000;
|
||
|
||
fn file_action_doc(doc: &ProviderDocument) -> wit_fa::Document {
|
||
wit_fa::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
}
|
||
}
|
||
|
||
fn file_action_from_wit(a: wit_fa::FileAction) -> CodeAction {
|
||
CodeAction {
|
||
title: clip(a.title, MAX_TITLE),
|
||
kind: a.kind.map(|k| clip(k, 64)),
|
||
preferred: a.preferred,
|
||
edits: a
|
||
.edits
|
||
.into_iter()
|
||
.map(|f| (std::path::PathBuf::from(f.path), f.edits.into_iter().map(edit_from_wit).collect()))
|
||
.collect(),
|
||
operations: a.operations.into_iter().take(MAX_FILE_OPERATIONS).map(operation_from_wit).collect(),
|
||
open: a.open.map(std::path::PathBuf::from),
|
||
data: a.data.map(|d| clip(d, 64 << 10)),
|
||
file_action: true,
|
||
}
|
||
}
|
||
|
||
fn file_action_to_wit(a: &CodeAction) -> wit_fa::FileAction {
|
||
wit_fa::FileAction {
|
||
title: a.title.clone(),
|
||
kind: a.kind.clone(),
|
||
preferred: a.preferred,
|
||
operations: a
|
||
.operations
|
||
.iter()
|
||
.map(|op| match op {
|
||
FileOperation::Create { path, text, overwrite } => wit_fa::FileOperation::Create(wit_fa::CreateFile {
|
||
path: to_ipc(path),
|
||
text: text.clone(),
|
||
overwrite: *overwrite,
|
||
}),
|
||
FileOperation::Rename { from, to } => {
|
||
wit_fa::FileOperation::Rename(wit_fa::RenameFile { path: to_ipc(from), new_path: to_ipc(to) })
|
||
}
|
||
FileOperation::Delete { path } => {
|
||
wit_fa::FileOperation::Delete(wit_fa::DeleteFile { path: to_ipc(path) })
|
||
}
|
||
})
|
||
.collect(),
|
||
edits: a
|
||
.edits
|
||
.iter()
|
||
.map(|(path, edits)| wit_fa::FileEdit {
|
||
path: to_ipc(path),
|
||
edits: edits
|
||
.iter()
|
||
.map(|e| wit_types::TextEdit { range: range_to_wit(e.range), text: e.new_text.clone() })
|
||
.collect(),
|
||
})
|
||
.collect(),
|
||
open: a.open.as_deref().map(to_ipc),
|
||
data: a.data.clone(),
|
||
}
|
||
}
|
||
|
||
fn operation_from_wit(op: wit_fa::FileOperation) -> FileOperation {
|
||
match op {
|
||
wit_fa::FileOperation::Create(c) => {
|
||
FileOperation::Create { path: c.path.into(), text: c.text, overwrite: c.overwrite }
|
||
}
|
||
wit_fa::FileOperation::Rename(r) => FileOperation::Rename { from: r.path.into(), to: r.new_path.into() },
|
||
wit_fa::FileOperation::Delete(d) => FileOperation::Delete { path: d.path.into() },
|
||
}
|
||
}
|
||
|
||
fn opt_clip(s: Option<String>, max: usize) -> Option<String> {
|
||
s.map(|s| clip(s, max))
|
||
}
|
||
|
||
const MAX_COMMIT_PROBLEMS: usize = 50;
|
||
|
||
fn commit_problem_from_wit(p: wit_cc::CommitProblem) -> CommitProblem {
|
||
let (path, range) = match p.location {
|
||
Some(l) => (Some(clip(l.path, 4096)), Some(range_from_wit(l.range))),
|
||
None => (None, None),
|
||
};
|
||
CommitProblem { message: clip(p.message, 4096), path, range }
|
||
}
|
||
|
||
const MAX_SERVICE_TABS: usize = 8;
|
||
const MAX_LOG_TOGGLES: usize = 8;
|
||
const MAX_LOG_ARGS: usize = 256;
|
||
const MAX_LOG_ARG: usize = 4096;
|
||
|
||
fn service_tab_from_wit(t: wit_dt::Tab) -> ServiceTab {
|
||
let body = match t.body {
|
||
wit_dt::Pane::Message(m) => ServicePane::Message(clip(m, 16 << 10)),
|
||
wit_dt::Pane::Text(text) => ServicePane::Text(clip(text, MAX_TEXT)),
|
||
wit_dt::Pane::Log(log) => {
|
||
let args_fit = |args: &[String]| args.len() <= MAX_LOG_ARGS && args.iter().all(|a| a.len() <= MAX_LOG_ARG);
|
||
if args_fit(&log.args)
|
||
&& log.toggles.len() <= MAX_LOG_TOGGLES
|
||
&& log.toggles.iter().all(|t| args_fit(&t.args))
|
||
{
|
||
ServicePane::Log(ServiceLog {
|
||
program: clip(log.program, 256),
|
||
args: log.args,
|
||
toggles: log
|
||
.toggles
|
||
.into_iter()
|
||
.map(|t| ServiceLogToggle { id: clip(t.id, 64), label: clip(t.label, MAX_TITLE), args: t.args })
|
||
.collect(),
|
||
})
|
||
} else {
|
||
ServicePane::Message(format!(
|
||
"This log asks for more than {MAX_LOG_ARGS} arguments of {MAX_LOG_ARG} bytes or {MAX_LOG_TOGGLES} toggles."
|
||
))
|
||
}
|
||
}
|
||
};
|
||
ServiceTab { id: clip(t.id, 64), label: clip(t.label, MAX_TITLE), body }
|
||
}
|
||
|
||
/// A `table` body carried by the `other` case (the sortable variant, plugins/
|
||
/// README.md §10): the usual table fields as JSON plus `sort`, the column the
|
||
/// plugin orders by and the direction, or null. Anything unparsable degrades
|
||
/// like a body this host doesn't know.
|
||
fn sortable_table_from_json(data: &str) -> Option<ViewTable> {
|
||
#[derive(serde::Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
struct Json {
|
||
#[serde(default)]
|
||
columns: Vec<String>,
|
||
#[serde(default)]
|
||
total_rows: u32,
|
||
#[serde(default)]
|
||
first_row: u32,
|
||
#[serde(default)]
|
||
rows: Vec<Vec<String>>,
|
||
#[serde(default)]
|
||
editable: bool,
|
||
#[serde(default)]
|
||
sort: Option<TableSort>,
|
||
}
|
||
let json: Json = serde_json::from_str(data).ok()?;
|
||
Some(ViewTable {
|
||
columns: json.columns.into_iter().take(1000).map(|c| clip(c, MAX_TITLE)).collect(),
|
||
total_rows: json.total_rows,
|
||
first_row: json.first_row,
|
||
rows: json
|
||
.rows
|
||
.into_iter()
|
||
.take(MAX_TABLE_ROWS as usize)
|
||
.map(|r| r.into_iter().take(1000).map(|c| clip(c, MAX_CELL)).collect())
|
||
.collect(),
|
||
editable: json.editable,
|
||
sortable: true,
|
||
sort: json.sort,
|
||
})
|
||
}
|
||
|
||
fn view_from_wit(v: wit_vt::View) -> View {
|
||
let body = match v.body {
|
||
wit_vt::Body::Message(m) => ViewBody::Message(clip(m, 16 << 10)),
|
||
wit_vt::Body::Other(e) => match e.kind.as_str() {
|
||
"table" => match sortable_table_from_json(&e.data) {
|
||
Some(t) => ViewBody::Table(t),
|
||
None => ViewBody::Message("This view needs a newer version of OpenIDEA (table).".into()),
|
||
},
|
||
_ => ViewBody::Message(format!("This view needs a newer version of OpenIDEA ({}).", clip(e.kind, 64))),
|
||
},
|
||
wit_vt::Body::Tree(items) => ViewBody::Tree(
|
||
items
|
||
.into_iter()
|
||
.take(MAX_VIEW_ITEMS)
|
||
.map(|i| ViewTreeItem {
|
||
key: clip(i.key, 1024),
|
||
parent: opt_clip(i.parent, 1024),
|
||
label: clip(i.label, MAX_TITLE),
|
||
icon: opt_clip(i.icon, 64),
|
||
detail: opt_clip(i.detail, MAX_TITLE),
|
||
expandable: i.expandable,
|
||
expanded: i.expanded,
|
||
})
|
||
.collect(),
|
||
),
|
||
wit_vt::Body::List(items) => ViewBody::List(
|
||
items
|
||
.into_iter()
|
||
.take(MAX_VIEW_ITEMS)
|
||
.map(|i| ViewListItem {
|
||
key: clip(i.key, 1024),
|
||
label: clip(i.label, MAX_TITLE),
|
||
icon: opt_clip(i.icon, 64),
|
||
detail: opt_clip(i.detail, MAX_TITLE),
|
||
})
|
||
.collect(),
|
||
),
|
||
wit_vt::Body::Form(fields) => ViewBody::Form(
|
||
fields
|
||
.into_iter()
|
||
.take(MAX_VIEW_ITEMS)
|
||
.filter_map(|f| {
|
||
let value = match f.value {
|
||
wit_vt::FieldValue::Text(t) => FieldValue::Text(clip(t, MAX_CELL)),
|
||
wit_vt::FieldValue::Multiline(t) => FieldValue::Multiline(clip(t, 1 << 20)),
|
||
wit_vt::FieldValue::Checkbox(b) => FieldValue::Checkbox(b),
|
||
wit_vt::FieldValue::Choice((options, selected)) => FieldValue::Choice {
|
||
options: options.into_iter().take(1000).map(|o| clip(o, MAX_TITLE)).collect(),
|
||
selected: clip(selected, MAX_TITLE),
|
||
},
|
||
wit_vt::FieldValue::Other(e) => match e.kind.as_str() {
|
||
"label" => FieldValue::Label(clip(serde_json::from_str(&e.data).ok()?, MAX_CELL)),
|
||
"readonly-text" => {
|
||
#[derive(serde::Deserialize)]
|
||
struct ReadonlyText {
|
||
text: String,
|
||
#[serde(default)]
|
||
multiline: bool,
|
||
}
|
||
let value: ReadonlyText = serde_json::from_str(&e.data).ok()?;
|
||
FieldValue::ReadonlyText {
|
||
text: clip(value.text, if value.multiline { 1 << 20 } else { MAX_CELL }),
|
||
multiline: value.multiline,
|
||
}
|
||
}
|
||
_ => return None,
|
||
},
|
||
};
|
||
Some(ViewField { key: clip(f.key, 128), label: clip(f.label, MAX_TITLE), value })
|
||
})
|
||
.collect(),
|
||
),
|
||
wit_vt::Body::Table(t) => ViewBody::Table(ViewTable {
|
||
columns: t.columns.into_iter().take(1000).map(|c| clip(c, MAX_TITLE)).collect(),
|
||
total_rows: t.total_rows,
|
||
first_row: t.first_row,
|
||
rows: t
|
||
.rows
|
||
.into_iter()
|
||
.take(MAX_TABLE_ROWS as usize)
|
||
.map(|r| r.into_iter().take(1000).map(|c| clip(c, MAX_CELL)).collect())
|
||
.collect(),
|
||
editable: t.editable,
|
||
// The plain body predates sortable headers; `other` kind `table` opts in.
|
||
sortable: false,
|
||
sort: None,
|
||
}),
|
||
wit_vt::Body::Html(h) => ViewBody::Html(clip(h, MAX_HTML)),
|
||
};
|
||
View {
|
||
toolbar: v
|
||
.toolbar
|
||
.into_iter()
|
||
.take(32)
|
||
.map(|b| ViewButton {
|
||
id: clip(b.id, 64),
|
||
label: clip(b.label, MAX_TITLE),
|
||
icon: opt_clip(b.icon, 64),
|
||
enabled: b.enabled,
|
||
})
|
||
.collect(),
|
||
body,
|
||
status: opt_clip(v.status, 1024),
|
||
}
|
||
}
|
||
|
||
fn event_to_wit_view(e: ViewEvent) -> wit_vt::ViewEvent {
|
||
use wit_vt::ViewEvent as W;
|
||
match e {
|
||
ViewEvent::Button { id } => W::Button(id),
|
||
ViewEvent::Activate { key } => W::Activate(key),
|
||
ViewEvent::Select { key } => W::Select(key),
|
||
ViewEvent::Expand { key } => W::Expand(key),
|
||
ViewEvent::Collapse { key } => W::Collapse(key),
|
||
ViewEvent::Field { key, value } => W::Field((key, value)),
|
||
ViewEvent::Cell { row, column, value } => W::Cell(wit_vt::CellEdit { row, column, value }),
|
||
ViewEvent::InsertRow { before } => W::InsertRow(before),
|
||
ViewEvent::DeleteRows { rows } => W::DeleteRows(rows),
|
||
ViewEvent::Message { text } => W::Message(text),
|
||
ViewEvent::TableSelect { row } => {
|
||
W::Other(wit_types::Extension { kind: "table-select".into(), data: format!(r#"{{"row":{row}}}"#) })
|
||
}
|
||
ViewEvent::TableActivate { row } => {
|
||
W::Other(wit_types::Extension { kind: "table-activate".into(), data: format!(r#"{{"row":{row}}}"#) })
|
||
}
|
||
// A sortable table's header click, as an `other` view-event (plugins/
|
||
// README.md §10): the SDKs decode the kinds they know.
|
||
ViewEvent::Sort { column, descending } => W::Other(wit_types::Extension {
|
||
kind: "sort".into(),
|
||
data: format!(r#"{{"column":{column},"descending":{descending}}}"#),
|
||
}),
|
||
ViewEvent::Closed => W::Other(wit_types::Extension { kind: "closed".into(), data: "{}".into() }),
|
||
}
|
||
}
|
||
|
||
fn test_event_from_wit(event: wit_tests::TestEvent) -> crate::PluginTestEvent {
|
||
use crate::{PluginTestEvent as E, TestFailed, TestOutput, TestPassed, TestSkipped, TestStart};
|
||
let location = |l: wit_types::Location| crate::Location {
|
||
path: l.path.into(),
|
||
range: ide_core::Range::new(
|
||
ide_core::Pos::new(l.range.start.line, l.range.start.character),
|
||
ide_core::Pos::new(l.range.end.line, l.range.end.character),
|
||
),
|
||
};
|
||
let start = |v: wit_tests::TestStart| TestStart {
|
||
id: v.id,
|
||
parent: v.parent,
|
||
name: v.name,
|
||
location: v.location.map(location),
|
||
};
|
||
match event {
|
||
wit_tests::TestEvent::SuiteStarted(v) => E::SuiteStarted(start(v)),
|
||
wit_tests::TestEvent::TestStarted(v) => E::TestStarted(start(v)),
|
||
wit_tests::TestEvent::Passed(v) => E::Passed(TestPassed { id: v.id, duration_ms: v.duration_ms }),
|
||
wit_tests::TestEvent::Failed(v) => E::Failed(TestFailed {
|
||
id: v.id,
|
||
duration_ms: v.duration_ms,
|
||
message: v.message,
|
||
expected: v.expected,
|
||
actual: v.actual,
|
||
location: v.location.map(location),
|
||
}),
|
||
wit_tests::TestEvent::Skipped(v) => E::Skipped(TestSkipped { id: v.id, reason: v.reason }),
|
||
wit_tests::TestEvent::Output(v) => E::Output(TestOutput { id: v.id, text: v.text }),
|
||
wit_tests::TestEvent::Other(v) => E::Other { kind: v.kind, data: v.data },
|
||
}
|
||
}
|
||
|
||
fn parameter_from(v: wit_lf::Parameter) -> crate::language_features::Parameter {
|
||
crate::language_features::Parameter {
|
||
label: parameter_label_from(v.label),
|
||
documentation: v.documentation.map(|v| crate::runtime::clip(v, 65536)),
|
||
}
|
||
}
|
||
fn signature_from(v: wit_lf::Signature) -> crate::language_features::Signature {
|
||
crate::language_features::Signature {
|
||
label: crate::runtime::clip(v.label, 65536),
|
||
documentation: v.documentation.map(|v| crate::runtime::clip(v, 65536)),
|
||
parameters: v.parameters.into_iter().take(10000).map(parameter_from).collect(),
|
||
}
|
||
}
|
||
fn signature_help_from(v: wit_lf::SignatureHelpResult) -> crate::language_features::SignatureHelp {
|
||
crate::language_features::SignatureHelp {
|
||
signatures: v.signatures.into_iter().take(10000).map(signature_from).collect(),
|
||
active_signature: v.active_signature,
|
||
active_parameter: v.active_parameter,
|
||
}
|
||
}
|
||
fn highlight_from(v: wit_lf::Highlight) -> crate::language_features::Highlight {
|
||
crate::language_features::Highlight { range: range_from_wit(v.range), kind: crate::runtime::clip(v.kind, 65536) }
|
||
}
|
||
fn document_link_from(v: wit_lf::DocumentLink) -> crate::language_features::DocumentLink {
|
||
crate::language_features::DocumentLink {
|
||
range: range_from_wit(v.range),
|
||
target: crate::runtime::clip(v.target, 65536),
|
||
}
|
||
}
|
||
fn code_lens_from(v: wit_lf::CodeLens) -> crate::language_features::CodeLens {
|
||
crate::language_features::CodeLens {
|
||
range: range_from_wit(v.range),
|
||
title: crate::runtime::clip(v.title, 65536),
|
||
command: crate::runtime::clip(v.command, 65536),
|
||
argument: v.argument.map(|v| crate::runtime::clip(v, 65536)),
|
||
}
|
||
}
|
||
fn fold_range_from(v: wit_lf::FoldRange) -> crate::language_features::FoldRange {
|
||
crate::language_features::FoldRange {
|
||
start_line: v.start_line,
|
||
end_line: v.end_line,
|
||
kind: crate::runtime::clip(v.kind, 65536),
|
||
}
|
||
}
|
||
fn color_info_from(v: wit_lf::ColorInfo) -> crate::language_features::ColorInfo {
|
||
crate::language_features::ColorInfo {
|
||
range: range_from_wit(v.range),
|
||
red: v.red,
|
||
green: v.green,
|
||
blue: v.blue,
|
||
alpha: v.alpha,
|
||
}
|
||
}
|
||
fn document_symbol_from(v: wit_lf::DocumentSymbol) -> crate::language_features::DocumentSymbol {
|
||
crate::language_features::DocumentSymbol {
|
||
name: crate::runtime::clip(v.name, 65536),
|
||
detail: crate::runtime::clip(v.detail, 65536),
|
||
kind: crate::runtime::clip(v.kind, 65536),
|
||
range: range_from_wit(v.range),
|
||
selection_range: range_from_wit(v.selection_range),
|
||
parent: v.parent,
|
||
}
|
||
}
|
||
fn gutter_action_from(v: wit_lf::GutterAction) -> crate::language_features::GutterAction {
|
||
crate::language_features::GutterAction {
|
||
line: v.line,
|
||
icon: v.icon.map(|v| crate::runtime::clip(v, 65536)),
|
||
tooltip: crate::runtime::clip(v.tooltip, 65536),
|
||
command: crate::runtime::clip(v.command, 65536),
|
||
argument: v.argument.map(|v| crate::runtime::clip(v, 65536)),
|
||
}
|
||
}
|
||
fn hover_action_from(v: wit_hia::HoverAction) -> crate::language_features::HoverAction {
|
||
crate::language_features::HoverAction {
|
||
line: v.line,
|
||
start_line: v.start_line,
|
||
end_line: v.end_line,
|
||
icon: v.icon.map(|v| crate::runtime::clip(v, 65536)),
|
||
label: crate::runtime::clip(v.label, 65536),
|
||
tooltip: v.tooltip.map(|v| crate::runtime::clip(v, 65536)),
|
||
command: crate::runtime::clip(v.command, 65536),
|
||
argument: v.argument.map(|v| crate::runtime::clip(v, 65536)),
|
||
}
|
||
}
|
||
|
||
fn parameter_label_from(v: wit_lf::ParameterLabel) -> crate::language_features::ParameterLabel {
|
||
use crate::language_features::ParameterLabel as P;
|
||
match v {
|
||
wit_lf::ParameterLabel::Text(t) => P::Text(clip(t, 4096)),
|
||
wit_lf::ParameterLabel::Offsets(t) => P::Offsets(t),
|
||
}
|
||
}
|
||
impl Runtime {
|
||
fn language_feature_call<R>(
|
||
&mut self,
|
||
timeout: Duration,
|
||
f: impl FnOnce(&wit_lf::Guest, &mut Store<StoreData>) -> wasmtime::Result<R>,
|
||
) -> Result<R, CallError> {
|
||
self.language_feature_call_in(None, timeout, f)
|
||
}
|
||
/// [`Self::language_feature_call`] for an editor of the project at `project`, which the
|
||
/// call has as its invocation's project (`workspace.project-of` prefers it).
|
||
fn language_feature_call_in<R>(
|
||
&mut self,
|
||
project: Option<&Path>,
|
||
timeout: Duration,
|
||
f: impl FnOnce(&wit_lf::Guest, &mut Store<StoreData>) -> wasmtime::Result<R>,
|
||
) -> Result<R, CallError> {
|
||
if self.exports.language_features.is_none() {
|
||
return Err(CallError::missing_export("language-features"));
|
||
}
|
||
let invocation = Invocation { workspace_root: project.map(Path::to_path_buf), ..Default::default() };
|
||
self.call(invocation, timeout, |e, s| f(e.language_features.as_ref().expect("checked"), s))
|
||
}
|
||
pub fn language_format(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
range: Option<ide_core::Range>,
|
||
options: crate::language_features::FormatOptions,
|
||
timeout: Duration,
|
||
) -> Result<Vec<ide_core::TextEdit>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| {
|
||
p.call_format(
|
||
s,
|
||
&d,
|
||
range.map(range_to_wit),
|
||
wit_lf::FormatOptions { tab_size: options.tab_size, insert_spaces: options.insert_spaces },
|
||
)
|
||
})?;
|
||
let result = result.map_err(|e| CallError::Guest(e.into()))?;
|
||
Ok(result.into_iter().take(10000).map(edit_from_wit).collect())
|
||
}
|
||
pub fn language_signature_help(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Option<crate::language_features::SignatureHelp>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_signature_help(s, &d, pos_to_wit(at)))?;
|
||
Ok(result.map(signature_help_from))
|
||
}
|
||
pub fn language_document_highlights(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::Highlight>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_document_highlights(s, &d, pos_to_wit(at)))?;
|
||
Ok(result.into_iter().take(10000).map(highlight_from).collect())
|
||
}
|
||
pub fn language_document_links(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::DocumentLink>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_document_links(s, &d))?;
|
||
Ok(result.into_iter().take(10000).map(document_link_from).collect())
|
||
}
|
||
pub fn language_code_lenses(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::CodeLens>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_code_lenses(s, &d))?;
|
||
Ok(result.into_iter().take(10000).map(code_lens_from).collect())
|
||
}
|
||
pub fn language_selection_ranges(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Vec<ide_core::Range>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_selection_ranges(s, &d, pos_to_wit(at)))?;
|
||
Ok(result.into_iter().take(10000).map(range_from_wit).collect())
|
||
}
|
||
pub fn language_folding(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::FoldRange>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_folding(s, &d))?;
|
||
Ok(result.into_iter().take(10000).map(fold_range_from).collect())
|
||
}
|
||
pub fn language_colors(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::ColorInfo>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_colors(s, &d))?;
|
||
Ok(result.into_iter().take(10000).map(color_info_from).collect())
|
||
}
|
||
pub fn language_color_presentations(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
color: crate::language_features::ColorInfo,
|
||
timeout: Duration,
|
||
) -> Result<Vec<String>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| {
|
||
p.call_color_presentations(
|
||
s,
|
||
&d,
|
||
wit_lf::ColorInfo {
|
||
range: range_to_wit(color.range),
|
||
red: color.red,
|
||
green: color.green,
|
||
blue: color.blue,
|
||
alpha: color.alpha,
|
||
},
|
||
)
|
||
})?;
|
||
Ok(result.into_iter().take(10000).map(|v| crate::runtime::clip(v, 65536)).collect())
|
||
}
|
||
pub fn language_document_symbols(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::DocumentSymbol>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_document_symbols(s, &d))?;
|
||
Ok(result.into_iter().take(10000).map(document_symbol_from).collect())
|
||
}
|
||
pub fn language_implementation(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::Location>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_implementation(s, &d, pos_to_wit(at)))?;
|
||
Ok(result.into_iter().take(10000).map(location_from_wit).collect())
|
||
}
|
||
pub fn language_type_definition(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
at: ide_core::Pos,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::Location>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call(timeout, |p, s| p.call_type_definition(s, &d, pos_to_wit(at)))?;
|
||
Ok(result.into_iter().take(10000).map(location_from_wit).collect())
|
||
}
|
||
/// Gutter actions of `doc`, open in an editor of the project at `project`.
|
||
pub fn language_gutter_actions(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
project: Option<&Path>,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::GutterAction>, CallError> {
|
||
let d = wit_lf::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.language_feature_call_in(project, timeout, |p, s| p.call_gutter_actions(s, &d))?;
|
||
Ok(result.into_iter().take(10000).map(gutter_action_from).collect())
|
||
}
|
||
|
||
/// Hover actions of `doc`, open in an editor of the project at `project`.
|
||
pub fn language_hover_actions(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
project: Option<&Path>,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::HoverAction>, CallError> {
|
||
if self.exports.hover_actions.is_none() {
|
||
return Err(CallError::missing_export("hover-actions"));
|
||
}
|
||
let d = wit_hia::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let invocation = Invocation { workspace_root: project.map(Path::to_path_buf), ..Default::default() };
|
||
let result = self.call(invocation, timeout, |exports, store| {
|
||
exports.hover_actions.as_ref().expect("checked").call_hover_actions(store, &d)
|
||
})?;
|
||
Ok(result.into_iter().take(10_000).map(hover_action_from).collect())
|
||
}
|
||
|
||
pub fn provide_injections(
|
||
&mut self,
|
||
doc: &ProviderDocument,
|
||
timeout: Duration,
|
||
) -> Result<Vec<crate::language_features::Injection>, CallError> {
|
||
if self.exports.injections.is_none() {
|
||
return Err(CallError::missing_export("injections"));
|
||
}
|
||
let d = wit_inj::Document {
|
||
path: to_ipc(&doc.path),
|
||
language: doc.language.clone(),
|
||
version: doc.version,
|
||
text: doc.text.clone(),
|
||
};
|
||
let result = self.call(Invocation::default(), timeout, |e, s| {
|
||
e.injections.as_ref().expect("checked").call_injections(s, &d)
|
||
})?;
|
||
Ok(result
|
||
.into_iter()
|
||
.take(10000)
|
||
.map(|i| crate::language_features::Injection {
|
||
range: range_from_wit(i.range),
|
||
language: clip(i.language, 64),
|
||
})
|
||
.collect())
|
||
}
|
||
}
|
||
|
||
impl Runtime {
|
||
/// Optional MCP export, with the same wall-clock and fuel budgets as commands.
|
||
pub fn mcp_call(&mut self, tool: &str, arguments: &str, invocation: Invocation) -> Result<String, CallError> {
|
||
if self.exports.mcp.is_none() {
|
||
return Err(CallError::missing_export("mcp-provider"));
|
||
}
|
||
let result = self.call(invocation, self.limits.timeout, |e, store| {
|
||
let ctx = store.data_mut().new_invocation()?;
|
||
let result = e.mcp.as_ref().unwrap().call_call(
|
||
&mut *store,
|
||
tool,
|
||
arguments,
|
||
wasmtime::component::Resource::new_borrow(ctx.rep()),
|
||
);
|
||
store.data_mut().drop_invocation(ctx);
|
||
result
|
||
})?;
|
||
let result = result.map_err(|e| CallError::Guest(e.into()))?;
|
||
if result.len() > 4 << 20 {
|
||
return Err(CallError::Guest(GuestError::Invalid("MCP result exceeds 4 MiB".into())));
|
||
}
|
||
Ok(result)
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
/// The second compile of the same component loads the cached one instead of compiling and
|
||
/// writing it again (a failed load deletes and rewrites the file).
|
||
#[test]
|
||
fn a_precompiled_component_is_loaded_from_the_cache() {
|
||
let engine = PluginEngine::new().unwrap();
|
||
let wasm = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/hello/dist/plugin.wasm");
|
||
let cache = tempfile::tempdir().unwrap();
|
||
engine.compile_cached(&wasm, cache.path()).unwrap();
|
||
let cached = || {
|
||
let entries: Vec<_> = std::fs::read_dir(cache.path()).unwrap().flatten().map(|e| e.path()).collect();
|
||
assert_eq!(entries.len(), 1, "{entries:?}");
|
||
let file = entries[0].clone();
|
||
(std::fs::metadata(&file).unwrap().modified().unwrap(), file)
|
||
};
|
||
let before = cached();
|
||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||
engine.compile_cached(&wasm, cache.path()).unwrap();
|
||
assert_eq!(cached(), before, "loaded, not compiled and stored again");
|
||
}
|
||
|
||
#[test]
|
||
fn readonly_field_extensions_are_parsed_and_unknown_or_malformed_fields_are_skipped() {
|
||
let fields = [
|
||
("label", r#""Title \"quoted\"""#),
|
||
("readonly-text", r#"{"text":"one\ntwo","multiline":true}"#),
|
||
("readonly-text", r#"{"text":"author"}"#),
|
||
("label", "{}"),
|
||
("readonly-text", r#"{"text":false}"#),
|
||
("future", "null"),
|
||
]
|
||
.into_iter()
|
||
.enumerate()
|
||
.map(|(i, (kind, data))| wit_vt::Field {
|
||
key: i.to_string(),
|
||
label: "Field".into(),
|
||
value: wit_vt::FieldValue::Other(wit_types::Extension { kind: kind.into(), data: data.into() }),
|
||
})
|
||
.collect();
|
||
let view = view_from_wit(wit_vt::View { toolbar: vec![], body: wit_vt::Body::Form(fields), status: None });
|
||
let ViewBody::Form(fields) = view.body else { panic!("form") };
|
||
assert_eq!(fields.len(), 3);
|
||
assert_eq!(fields[0].value, FieldValue::Label("Title \"quoted\"".into()));
|
||
assert_eq!(fields[1].value, FieldValue::ReadonlyText { text: "one\ntwo".into(), multiline: true });
|
||
assert_eq!(fields[2].value, FieldValue::ReadonlyText { text: "author".into(), multiline: false });
|
||
}
|
||
|
||
#[test]
|
||
fn a_closed_dialog_leaves_as_an_other_event() {
|
||
let wit_vt::ViewEvent::Other(extension) = event_to_wit_view(ViewEvent::Closed) else { panic!("other") };
|
||
assert_eq!((extension.kind.as_str(), extension.data.as_str()), ("closed", "{}"));
|
||
}
|
||
|
||
#[test]
|
||
fn table_row_events_leave_through_released_extension_case() {
|
||
for (event, kind) in [
|
||
(ViewEvent::TableSelect { row: 51 }, "table-select"),
|
||
(ViewEvent::TableActivate { row: 51 }, "table-activate"),
|
||
] {
|
||
let wit_vt::ViewEvent::Other(extension) = event_to_wit_view(event) else { panic!("other") };
|
||
assert_eq!(extension.kind, kind);
|
||
assert_eq!(extension.data, r#"{"row":51}"#);
|
||
}
|
||
}
|
||
|
||
#[cfg(windows)]
|
||
#[test]
|
||
fn provider_and_resolved_action_paths_match_ipc_events() {
|
||
for (path, expected) in [
|
||
(r"C:\project\Example.java", "C:/project/Example.java"),
|
||
(r"\\?\C:\project\Example.java", "C:/project/Example.java"),
|
||
(r"\\server\share\Example.java", "//server/share/Example.java"),
|
||
] {
|
||
let doc = ProviderDocument { path: path.into(), ..Default::default() };
|
||
assert_eq!(doc_to_wit(&doc).path, expected);
|
||
let action = CodeAction {
|
||
title: "Fix".into(),
|
||
kind: None,
|
||
preferred: false,
|
||
edits: vec![(path.into(), Vec::new())],
|
||
operations: Vec::new(),
|
||
open: None,
|
||
data: None,
|
||
file_action: false,
|
||
};
|
||
assert_eq!(code_action_to_wit(&action).edits[0].path, expected);
|
||
}
|
||
}
|
||
|
||
fn ext(kind: &str) -> wit_types::Extension {
|
||
wit_types::Extension { kind: kind.into(), data: "{}".into() }
|
||
}
|
||
|
||
#[test]
|
||
fn bodies_and_fields_of_a_newer_api_degrade() {
|
||
let view = |body| wit_vt::View { toolbar: Vec::new(), body, status: None };
|
||
let shown = view_from_wit(view(wit_vt::Body::Other(ext("markdown"))));
|
||
assert_eq!(shown.body, ViewBody::Message("This view needs a newer version of OpenIDEA (markdown).".into()));
|
||
let field = |key: &str, value| wit_vt::Field { key: key.into(), label: key.into(), value };
|
||
let form = view_from_wit(view(wit_vt::Body::Form(vec![
|
||
field("color", wit_vt::FieldValue::Other(ext("color"))),
|
||
field("name", wit_vt::FieldValue::Text("n".into())),
|
||
])));
|
||
let ViewBody::Form(fields) = form.body else { panic!("a form") };
|
||
assert_eq!(fields.iter().map(|f| f.key.as_str()).collect::<Vec<_>>(), ["name"], "unknown fields are left out");
|
||
}
|
||
|
||
#[test]
|
||
fn a_sortable_table_arrives_through_other() {
|
||
let view = |data: &str| wit_vt::View {
|
||
toolbar: Vec::new(),
|
||
body: wit_vt::Body::Other(wit_types::Extension { kind: "table".into(), data: data.into() }),
|
||
status: None,
|
||
};
|
||
let shown = view_from_wit(view(
|
||
r#"{"columns":["ext","files"],"totalRows":3,"firstRow":1,"rows":[["rs","2"],["md","1"]],
|
||
"editable":false,"sort":{"column":1,"descending":true}}"#,
|
||
));
|
||
let ViewBody::Table(t) = shown.body else { panic!("{shown:?}") };
|
||
assert_eq!(t.columns, ["ext".to_owned(), "files".into()]);
|
||
assert_eq!((t.total_rows, t.first_row, t.editable), (3, 1, false));
|
||
assert!(t.sortable, "a `table` through `other` opts the headers in");
|
||
assert_eq!(t.sort, Some(TableSort { column: 1, descending: true }));
|
||
|
||
// No sort yet: still clickable headers, just no arrow.
|
||
let fresh = view_from_wit(view(
|
||
r#"{"columns":["a"],"totalRows":0,"firstRow":0,"rows":[],"editable":false,"sort":null}"#,
|
||
));
|
||
let ViewBody::Table(t) = fresh.body else { panic!("{fresh:?}") };
|
||
assert!(t.sortable);
|
||
assert_eq!(t.sort, None);
|
||
|
||
// Broken JSON degrades like any body the host doesn't know.
|
||
let broken = view_from_wit(view("{not json"));
|
||
assert!(matches!(broken.body, ViewBody::Message(_)), "{broken:?}");
|
||
}
|
||
|
||
#[test]
|
||
fn a_sort_click_leaves_as_an_other_extension() {
|
||
let wit = event_to_wit_view(ViewEvent::Sort { column: 2, descending: true });
|
||
let wit_vt::ViewEvent::Other(e) = wit else { panic!("{wit:?}") };
|
||
assert_eq!((e.kind.as_str(), e.data.as_str()), ("sort", r#"{"column":2,"descending":true}"#));
|
||
}
|
||
}
|