mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
A command is now declared once, on the fn that implements it. A project-host command is an ide-host fn with #[command(trust = …)] listed in its module's domain!; its Tauri adapter, bindings entry, JSON-RPC handler, trust class and remote routing are derived from that declaration. Desktop-only commands keep their Tauri fn and add #[desktop_command(trust = …)]. This removes the hand-written pass-through adapters, collect_commands!, the per-domain register fns and the trust tables in trust.rs. The invoke handler looks up each command's declared trust class; a name nothing declares is denied. A remote window routes the project-host commands of the desktop's own build. Duplicate names are a compile error. CI runs ide-command's compile_fail doctests. Part of #3344. Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com>
169 lines
5.7 KiB
Rust
169 lines
5.7 KiB
Rust
//! Tree-sitter structure queries (`ide-syntax`): the fallback editor features
|
|
//! use when the language server is unavailable, not ready or failing.
|
|
//!
|
|
//! All positions are 0-based lines / UTF-16 columns. Document queries run on
|
|
//! the blocking pool against the document's cached, incrementally re-parsed
|
|
//! tree; an optional `request` id makes them cancellable via `cancel_request`.
|
|
//! Languages without a grammar (and documents too large to parse) reject with
|
|
//! `unavailable`.
|
|
use crate::Host;
|
|
use crate::Project;
|
|
use ide_command::command;
|
|
|
|
use super::doc::blocking;
|
|
use ide_core::{CancellationToken, DocId, IdeError, IdeResult, LanguageId, Pos, Range, RequestId};
|
|
use ide_syntax::{
|
|
ExpressionAt, FileIdentifiers, IdentifierOccurrence, Locals, NodeAt, Snapshot, StructuralRanges, StructureResult,
|
|
StructureService, StructureSupport, SyntaxErrors,
|
|
};
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
|
|
/// Runs `f` on the current snapshot of `doc` off the command thread.
|
|
async fn on_doc<T: Send + 'static>(
|
|
host: &Host,
|
|
window: &str,
|
|
doc: DocId,
|
|
request: Option<RequestId>,
|
|
f: impl FnOnce(&StructureService, &Snapshot, &CancellationToken) -> StructureResult<T> + Send + 'static,
|
|
) -> IdeResult<T> {
|
|
let project: Arc<Project> = host.project(window)?;
|
|
let cancel = request.map_or_else(CancellationToken::new, |id| host.begin_request(id));
|
|
let result = blocking(move || {
|
|
let snap = project.documents.text_snapshot(doc)?;
|
|
if snap.large {
|
|
return Err(IdeError::unavailable("file is too large for syntax analysis"));
|
|
}
|
|
let s = Snapshot::doc(doc, snap.version, &snap.language, &snap.text);
|
|
Ok(f(&project.structure, &s, &cancel)?)
|
|
})
|
|
.await;
|
|
if let Some(id) = request {
|
|
host.end_request(id);
|
|
}
|
|
result
|
|
}
|
|
|
|
/// What the structure service supports for a language (`tree`: every query,
|
|
/// `locals`: scope analysis).
|
|
#[command(trust = always)]
|
|
pub fn syntax_support(host: &Host, window: &str, language: LanguageId) -> IdeResult<StructureSupport> {
|
|
Ok(host.project(window)?.structure.support(&language))
|
|
}
|
|
|
|
/// The smallest named node covering `range`, with its ancestors (parent first).
|
|
#[command(trust = always)]
|
|
pub async fn syntax_node_at(
|
|
host: &Host,
|
|
window: &str,
|
|
doc: DocId,
|
|
range: Range,
|
|
request: Option<RequestId>,
|
|
) -> IdeResult<Option<NodeAt>> {
|
|
on_doc(host, window, doc, request, move |s, snap, c| s.node_at(snap, range, c)).await
|
|
}
|
|
|
|
/// The expression a selection denotes (exact or minimally enclosing), with its
|
|
/// statement, block and caret candidates.
|
|
#[command(trust = always)]
|
|
pub async fn syntax_expression_at(
|
|
host: &Host,
|
|
window: &str,
|
|
doc: DocId,
|
|
range: Range,
|
|
request: Option<RequestId>,
|
|
) -> IdeResult<ExpressionAt> {
|
|
on_doc(host, window, doc, request, move |s, snap, c| s.expression_at(snap, range, c)).await
|
|
}
|
|
|
|
/// `ERROR` / `MISSING` nodes (e.g. a missing `;`) in document order.
|
|
#[command(trust = always)]
|
|
pub async fn syntax_errors(
|
|
host: &Host,
|
|
window: &str,
|
|
doc: DocId,
|
|
request: Option<RequestId>,
|
|
) -> IdeResult<SyntaxErrors> {
|
|
on_doc(host, window, doc, request, |s, snap, c| s.syntax_errors(snap, c)).await
|
|
}
|
|
|
|
/// Scope analysis of the identifier at `pos`: its local definition and
|
|
/// references, or why it is not a local.
|
|
#[command(trust = always)]
|
|
pub async fn syntax_locals(
|
|
host: &Host,
|
|
window: &str,
|
|
doc: DocId,
|
|
pos: Pos,
|
|
request: Option<RequestId>,
|
|
) -> IdeResult<Locals> {
|
|
on_doc(host, window, doc, request, move |s, snap, c| s.locals(snap, pos, c)).await
|
|
}
|
|
|
|
/// Identifier tokens spelled `name` in a document, excluding comments and strings.
|
|
#[command(trust = always)]
|
|
pub async fn syntax_identifiers(
|
|
host: &Host,
|
|
window: &str,
|
|
doc: DocId,
|
|
name: String,
|
|
request: Option<RequestId>,
|
|
) -> IdeResult<Vec<IdentifierOccurrence>> {
|
|
on_doc(host, window, doc, request, move |s, snap, c| s.identifiers_named(snap, &name, c)).await
|
|
}
|
|
|
|
/// [`syntax_identifiers`] over files (open buffers win over disk); files
|
|
/// without occurrences or without a grammar are left out. Cancellable.
|
|
#[command(trust = always)]
|
|
pub async fn syntax_identifiers_in_files(
|
|
host: &Host,
|
|
window: &str,
|
|
paths: Vec<PathBuf>,
|
|
name: String,
|
|
request: RequestId,
|
|
) -> IdeResult<Vec<FileIdentifiers>> {
|
|
let project = host.project(window)?;
|
|
let languages = project.languages.clone();
|
|
let cancel = host.begin_request(request);
|
|
let result = blocking(move || {
|
|
let load = |path: &std::path::Path| {
|
|
if let Some(doc) = project.documents.by_path(path) {
|
|
let snap = project.documents.text_snapshot(doc).ok()?;
|
|
return (!snap.large).then_some((snap.language, Some((doc, snap.version)), snap.text));
|
|
}
|
|
let text = project.text_of(path).ok()?;
|
|
let first_line = text.lines().next();
|
|
Some((languages.detect(path, first_line), None, text))
|
|
};
|
|
Ok(project.structure.identifiers_in_files(&paths, &name, load, &cancel)?)
|
|
})
|
|
.await;
|
|
host.end_request(request);
|
|
result
|
|
}
|
|
|
|
/// Extend Selection chain plus statement / declaration ranges at `pos`
|
|
/// (Move Statement, Complete Statement for languages without a Lezer grammar).
|
|
#[command(trust = always)]
|
|
pub async fn syntax_structural_ranges(
|
|
host: &Host,
|
|
window: &str,
|
|
doc: DocId,
|
|
pos: Pos,
|
|
request: Option<RequestId>,
|
|
) -> IdeResult<StructuralRanges> {
|
|
on_doc(host, window, doc, request, move |s, snap, c| s.structural_ranges(snap, pos, c)).await
|
|
}
|
|
|
|
ide_command::domain!(
|
|
syntax:
|
|
syntax_errors,
|
|
syntax_expression_at,
|
|
syntax_identifiers,
|
|
syntax_identifiers_in_files,
|
|
syntax_locals,
|
|
syntax_node_at,
|
|
syntax_structural_ranges,
|
|
syntax_support,
|
|
);
|