дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02: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 <[email protected]>
120 рядки
4.3 KiB
Rust
120 рядки
4.3 KiB
Rust
//! Proofreading › Typo: spell checking of open documents.
|
||
use crate::Host;
|
||
use crate::Project;
|
||
use ide_command::command;
|
||
|
||
use super::doc::blocking;
|
||
use ide_core::settings::SpellingSettings;
|
||
use ide_core::{DocId, IdeError, IdeResult, RequestId};
|
||
use ide_language::SpellingKind;
|
||
use ide_spell::{Region, RegionKind, SpellScope, Speller, Typo};
|
||
use std::path::{Path, PathBuf};
|
||
|
||
/// Languages checked as prose (all text but code spans).
|
||
const PROSE: &[&str] = &["plaintext", "markdown", "restructuredtext", "asciidoc", "latex"];
|
||
|
||
/// Custom dictionary paths: `~/…` from home, relative ones from the project root.
|
||
fn dictionary_paths(settings: &SpellingSettings, root: &Path) -> Vec<PathBuf> {
|
||
settings
|
||
.custom_dictionaries
|
||
.iter()
|
||
.map(|p| p.trim())
|
||
.filter(|p| !p.is_empty())
|
||
.map(|p| match p.strip_prefix("~/") {
|
||
Some(rest) => dirs::home_dir().unwrap_or_default().join(rest),
|
||
None => root.join(p),
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
fn speller(project: &Project) -> Speller {
|
||
let s = project.settings.effective().spelling;
|
||
let words = s.ide_words.iter().chain(&s.project_words);
|
||
Speller::new(words, &dictionary_paths(&s, &project.root))
|
||
}
|
||
|
||
/// Misspelt words in the comments, string literals and declared names of
|
||
/// `doc` that `scope` includes (all of its text for prose). Cancel with
|
||
/// `cancel_request(request)`.
|
||
#[command(trust = always)]
|
||
pub async fn spell_check(
|
||
host: &Host,
|
||
window: &str,
|
||
doc: DocId,
|
||
scope: SpellScope,
|
||
request: RequestId,
|
||
) -> IdeResult<Vec<Typo>> {
|
||
let project = host.project(window)?;
|
||
let syntax = host.syntax();
|
||
let cancel = host.begin_request(request);
|
||
let result = blocking(move || {
|
||
let path = project.documents.snapshot(doc)?.path.0.clone();
|
||
let snap = project.documents.text_snapshot(doc)?;
|
||
if snap.large {
|
||
return Ok((path, Vec::new()));
|
||
}
|
||
let regions = if PROSE.contains(&snap.language.as_str()) {
|
||
ide_spell::prose_regions(&snap.text)
|
||
} else {
|
||
let Some(found) = syntax.spelling_regions(&snap.language, &snap.text) else {
|
||
return Ok((path, Vec::new()));
|
||
};
|
||
found
|
||
.into_iter()
|
||
.map(|r| Region {
|
||
start: r.start,
|
||
end: r.end,
|
||
kind: match r.kind {
|
||
SpellingKind::Comment => RegionKind::Comment,
|
||
SpellingKind::String if r.raw => RegionKind::RawString,
|
||
SpellingKind::String => RegionKind::String,
|
||
SpellingKind::Name => RegionKind::Name,
|
||
},
|
||
})
|
||
.filter(|r| scope.includes(r.kind))
|
||
.collect()
|
||
};
|
||
if cancel.is_cancelled() {
|
||
return Err(IdeError::cancelled());
|
||
}
|
||
let typos =
|
||
speller(&project).check(&snap.text, ®ions, &|| cancel.is_cancelled()).ok_or_else(IdeError::cancelled)?;
|
||
Ok((path, typos))
|
||
})
|
||
.await;
|
||
host.end_request(request);
|
||
// Record the typos in the host's diagnostics snapshot so line-end annotations
|
||
// see them without waiting for a UI report; the editor still draws from its
|
||
// own call above, unchanged.
|
||
if let Ok((path, typos)) = &result {
|
||
let items: Vec<ide_core::Diagnostic> = typos
|
||
.iter()
|
||
.map(|t| ide_core::Diagnostic {
|
||
range: t.range,
|
||
severity: ide_core::Severity::Info,
|
||
message: format!("Typo: In word '{}'", t.word),
|
||
source: Some("Spelling".into()),
|
||
code: None,
|
||
unnecessary: false,
|
||
deprecated: false,
|
||
server: None,
|
||
})
|
||
.collect();
|
||
host.diagnostics.replace(path, "Spelling", items);
|
||
}
|
||
result.map(|(_, typos)| typos)
|
||
}
|
||
|
||
/// Replacements for a misspelt `word`, best first (Typo › Change to…).
|
||
#[command(trust = always)]
|
||
pub async fn spell_suggest(host: &Host, window: &str, word: String) -> IdeResult<Vec<String>> {
|
||
let project = host.project(window).ok();
|
||
blocking(move || {
|
||
let speller = project.as_deref().map(speller).unwrap_or_default();
|
||
Ok(speller.suggest(&word, 8))
|
||
})
|
||
.await
|
||
}
|
||
|
||
ide_command::domain!(spell: spell_check, spell_suggest);
|