дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-09 11:15:40 +02:00
A plugin system built on WebAssembly components (wasmtime, WASI p2), with a typed WIT API (`openidea:[email protected]`) and a Rust SDK, shipped as a preview. - Extension points declared in `plugin.toml` (commands, languages, themes, icon themes, menus, key bindings, status items, settings, views, custom editors, file icons and templates, run detectors, editor providers), validated before anything takes effect and projected onto the workbench's own registries: plugin themes and file icon themes are chosen in Settings › Appearance and Quick Switch, plugin shortcuts are edited in Settings › Keymap, plugin problems are grouped in Settings › Inspections. - Capabilities with risk levels and consent at install, pinned `process:` programs, secrets in the OS keychain. - Runtime: one thread per active plugin, fuel/time/memory budgets, soft and hard deadlines on the editing path, cancellation, crash backoff, idle reclaim, a precompiled-component cache; remote projects run code plugins on `openidea-server`. - Web views and custom editors with a sandboxed page, a document bridge joined to the text editor's undo history, and split or preview layouts. - Distribution: signed `.oiplugin` packages, a static marketplace index with revocation, versioned installs with rollback, install from a `.zip` or a folder. The release workflow signs and publishes the official plugins and their index, which released builds use as the default marketplace. - Official plugins: String Tools, Key Promoter, Git Toolbox, AI Completion, Checkstyle, MyBatis Navigator, CSV Editor, Solidity, Material Icons, Harbor Night, Excalidraw, Graphviz, Regex Tester, License Header. Closes #732 Co-authored-by: Deng Liu <[email protected]> Co-authored-by: florentvdry <[email protected]>
137 рядки
4.0 KiB
Rust
137 рядки
4.0 KiB
Rust
//! Syntax check for `when` clauses in UI declarations. The client evaluates
|
|
//! them (`src/platform/context.ts`); validating here keeps the client's
|
|
//! projection from failing. Grammar (same as the client):
|
|
//!
|
|
//! ```text
|
|
//! expr := or ; or := and ("||" and)* ; and := unary ("&&" unary)*
|
|
//! unary := "!" unary | "(" expr ")" | cmp ; cmp := key (("==" | "!=") value)?
|
|
//! ```
|
|
|
|
const MAX_LEN: usize = 512;
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
enum Tok<'a> {
|
|
Id(&'a str),
|
|
Str,
|
|
Op(&'a str),
|
|
}
|
|
|
|
fn tokenize(src: &str) -> Result<Vec<Tok<'_>>, String> {
|
|
let mut out = Vec::new();
|
|
let b = src.as_bytes();
|
|
let mut i = 0;
|
|
while i < b.len() {
|
|
let c = b[i] as char;
|
|
if c.is_whitespace() {
|
|
i += 1;
|
|
} else if ["&&", "||", "==", "!="].iter().any(|op| src[i..].starts_with(op)) {
|
|
out.push(Tok::Op(&src[i..i + 2]));
|
|
i += 2;
|
|
} else if matches!(c, '!' | '(' | ')') {
|
|
out.push(Tok::Op(&src[i..i + 1]));
|
|
i += 1;
|
|
} else if c == '\'' || c == '"' {
|
|
let end = src[i + 1..].find(c).ok_or_else(|| format!("unterminated string in when clause: {src}"))?;
|
|
out.push(Tok::Str);
|
|
i += end + 2;
|
|
} else {
|
|
let len = src[i..]
|
|
.find(|ch: char| !(ch.is_alphanumeric() || matches!(ch, '_' | '.' | '-' | ':' | '/')))
|
|
.unwrap_or(src.len() - i);
|
|
if len == 0 {
|
|
return Err(format!("unexpected '{c}' in when clause: {src}"));
|
|
}
|
|
out.push(Tok::Id(&src[i..i + len]));
|
|
i += len;
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// Checks a `when` clause; empty means "always".
|
|
pub fn validate_when(src: &str) -> Result<(), String> {
|
|
if src.len() > MAX_LEN {
|
|
return Err(format!("when clause longer than {MAX_LEN} bytes"));
|
|
}
|
|
if src.trim().is_empty() {
|
|
return Ok(());
|
|
}
|
|
let toks = tokenize(src)?;
|
|
let mut p = 0;
|
|
parse_or(&toks, &mut p, src)?;
|
|
if p != toks.len() {
|
|
return Err(format!("unexpected token in when clause: {src}"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn parse_or(t: &[Tok], p: &mut usize, src: &str) -> Result<(), String> {
|
|
parse_and(t, p, src)?;
|
|
while t.get(*p) == Some(&Tok::Op("||")) {
|
|
*p += 1;
|
|
parse_and(t, p, src)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn parse_and(t: &[Tok], p: &mut usize, src: &str) -> Result<(), String> {
|
|
parse_unary(t, p, src)?;
|
|
while t.get(*p) == Some(&Tok::Op("&&")) {
|
|
*p += 1;
|
|
parse_unary(t, p, src)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn parse_unary(t: &[Tok], p: &mut usize, src: &str) -> Result<(), String> {
|
|
match t.get(*p) {
|
|
Some(Tok::Op("!")) => {
|
|
*p += 1;
|
|
parse_unary(t, p, src)
|
|
}
|
|
Some(Tok::Op("(")) => {
|
|
*p += 1;
|
|
parse_or(t, p, src)?;
|
|
if t.get(*p) != Some(&Tok::Op(")")) {
|
|
return Err(format!("missing ')' in when clause: {src}"));
|
|
}
|
|
*p += 1;
|
|
Ok(())
|
|
}
|
|
Some(Tok::Id(_)) => {
|
|
*p += 1;
|
|
if matches!(t.get(*p), Some(Tok::Op("==" | "!="))) {
|
|
*p += 1;
|
|
match t.get(*p) {
|
|
Some(Tok::Id(_) | Tok::Str) => *p += 1,
|
|
_ => return Err(format!("missing value after comparison in when clause: {src}")),
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
_ => Err(format!("expected a context key in when clause: {src}")),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::validate_when;
|
|
|
|
#[test]
|
|
fn accepts_the_client_grammar() {
|
|
for ok in [
|
|
"",
|
|
"editorFocus",
|
|
"editorLang == rust",
|
|
"editorLang != 'rust' && !readOnly",
|
|
"(a || b) && toolWindow.project.active",
|
|
"resourceScheme == file:/x",
|
|
] {
|
|
assert!(validate_when(ok).is_ok(), "{ok}");
|
|
}
|
|
for bad in ["a &&", "(a", "a == ", "== a", "a b", "a $ b", "'x", "!()"] {
|
|
assert!(validate_when(bad).is_err(), "{bad}");
|
|
}
|
|
}
|
|
}
|