From 9f6ec4a20d640439e238305702b4a3635fb1fa15 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Fri, 22 May 2026 15:53:25 +0200 Subject: [PATCH 1/6] Working LSP error checking --- Cargo.lock | 1 + analyzer.bat | 4 +- crates/analyzer/Cargo.toml | 1 + crates/analyzer/src/builder.rs | 171 ++++++++++++++++++++++++++++++ crates/analyzer/src/lib.rs | 152 +++++++++++++++++++------- crates/analyzer/src/transpiler.rs | 126 +++++++++++----------- 6 files changed, 353 insertions(+), 102 deletions(-) create mode 100644 crates/analyzer/src/builder.rs diff --git a/Cargo.lock b/Cargo.lock index 954ab28..6d7a24e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,6 +444,7 @@ dependencies = [ name = "mist-analyzer" version = "0.0.2" dependencies = [ + "cargo_metadata", "dashmap 6.2.1", "mist-codegen", "mist-parser", diff --git a/analyzer.bat b/analyzer.bat index d69f716..8ffbbf8 100644 --- a/analyzer.bat +++ b/analyzer.bat @@ -4,4 +4,6 @@ cd D:\projects\mist\mist mist build >nul 2>&1 -target\debug\mist-analyzer.exe +cd D:\projects\mist\mist-lsp-test + +D:\projects\mist\mist\target\debug\mist-analyzer.exe diff --git a/crates/analyzer/Cargo.toml b/crates/analyzer/Cargo.toml index 35ced73..49b74be 100644 --- a/crates/analyzer/Cargo.toml +++ b/crates/analyzer/Cargo.toml @@ -15,3 +15,4 @@ mist-codegen = { workspace = true } dashmap = "6.2.1" tokio = { version = "1.52.3", features = ["full", "macros", "rt", "rt-multi-thread"] } tower-lsp = "0.20.0" +cargo_metadata = "0.23.1" diff --git a/crates/analyzer/src/builder.rs b/crates/analyzer/src/builder.rs new file mode 100644 index 0000000..138c8ea --- /dev/null +++ b/crates/analyzer/src/builder.rs @@ -0,0 +1,171 @@ +use std::{ + collections::HashMap, + env, fs, + path::{MAIN_SEPARATOR, PathBuf}, + process::{Command, Stdio}, +}; + +use cargo_metadata::{CompilerMessage, Message}; +use mist_parser::rev_mapper::{RustMap, find_mapping, get_mapping}; + +#[derive(Debug, Clone)] +pub struct MistDiagnosticMessage { + pub message: String, + pub file_path: PathBuf, + pub file_name: String, + pub line: usize, + pub column: usize, +} + +#[derive(Debug, Clone)] +pub enum MistDiagnostic { + Error(MistDiagnosticMessage), + Warning(MistDiagnosticMessage), + Rust(CompilerMessage), +} + +pub fn build(mut args: Vec, root: PathBuf) -> Vec { + args.insert(1, "--message-format=json".to_string()); + + let is_root = root == env::current_dir().expect("Failed getting env"); + + let mut command = Command::new("cargo") + .args(args) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to run cargo"); + + let mut reader = std::io::BufReader::new(command.stdout.take().expect("Failed to get reader")); + + let mut diagnostics = Vec::new(); + + let mut mapping = HashMap::new(); + + let mist_src = format!(".mist{MAIN_SEPARATOR}lsp"); + + for message in cargo_metadata::Message::parse_stream(&mut reader) { + match message { + Ok(Message::CompilerMessage(msg)) => { + for span in &msg.message.spans { + if span.is_primary { + let rust_path = root.join(&span.file_name); + + let mist_file = span + .file_name + .replacen(&mist_src, "src", 1) + .trim_end_matches(".rs") + .to_string() + + ".mist"; + + let mist_path = root.join(&mist_file); + + if !fs::exists(&mist_path).expect("Unable to check if mist file exists") { + diagnostics.push(MistDiagnostic::Rust(msg)); + break; + } + + let map = mapping.entry(rust_path.clone()).or_insert_with(|| { + get_mapping( + &fs::read_to_string(rust_path) + .expect("Failed to read file for mapping"), + ) + }); + + let mist_span = + find_mapping(&map, &RustMap(span.line_end, span.column_start)) + .expect("Unable to find mapping"); + + let mist_msg = MistDiagnosticMessage { + message: span.label.clone().unwrap_or(msg.message.message.clone()), + file_name: if is_root { + mist_file + } else { + mist_path.to_string_lossy().to_string() + }, + file_path: mist_path, + line: mist_span.1.0, + column: mist_span.1.1, + }; + + match msg.message.level { + cargo_metadata::diagnostic::DiagnosticLevel::Error => { + diagnostics.push(MistDiagnostic::Error(mist_msg)) + } + + cargo_metadata::diagnostic::DiagnosticLevel::Warning => { + diagnostics.push(MistDiagnostic::Warning(mist_msg)) + } + + _ => {} + } + } + } + } + + Ok(Message::BuildFinished(_)) => { + command.wait().unwrap(); + + return diagnostics; + } + + Ok(Message::TextLine(text)) => println!("{text}"), + _ => {} + } + } + + diagnostics +} + +// pub fn print_diagnostics(diagnostics: &Vec) { +// let mut files = HashMap::new(); + +// for diag in diagnostics { +// match diag { +// MistDiagnostic::Error(msg) => { +// let line = get_line(&mut files, &msg); + +// println!( +// "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}", +// msg.file_name, +// msg.line, +// msg.column, +// msg.message, +// line.unwrap_or_default(), +// ) +// } + +// MistDiagnostic::Warning(msg) => { +// let line = get_line(&mut files, &msg); + +// println!( +// "\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}", +// msg.file_name, +// msg.line, +// msg.column, +// msg.message, +// line.unwrap_or_default(), +// ) +// } + +// MistDiagnostic::Rust(rs) => println!("{rs}"), +// } +// } +// } + +pub fn get_line( + files: &mut HashMap>, + msg: &MistDiagnosticMessage, +) -> Option { + let src_path = msg.file_path.clone(); + + let lines = files.entry(src_path.clone()).or_insert_with(|| { + fs::read_to_string(src_path) + .expect("Unable to read mist file") + .lines() + .into_iter() + .map(String::from) + .collect() + }); + + lines.get(msg.line - 1).map(|v| v.trim().to_string()) +} diff --git a/crates/analyzer/src/lib.rs b/crates/analyzer/src/lib.rs index 8bc9f88..44bbc28 100644 --- a/crates/analyzer/src/lib.rs +++ b/crates/analyzer/src/lib.rs @@ -1,3 +1,4 @@ +pub mod builder; pub mod transpiler; use std::path::{Component, PathBuf}; @@ -8,6 +9,7 @@ use tower_lsp::jsonrpc::Result; use tower_lsp::lsp_types::*; use tower_lsp::{Client, LanguageServer, LspService, Server}; +use crate::builder::MistDiagnostic; use crate::transpiler::transpile_file; #[derive(Debug)] @@ -21,16 +23,6 @@ impl LanguageServer for Backend { async fn initialize(&self, params: InitializeParams) -> Result { let mut res = InitializeResult::default(); - // res.capabilities.completion_provider = Some(CompletionOptions { - // resolve_provider: None, - // trigger_characters: None, - // all_commit_characters: None, - // work_done_progress_options: WorkDoneProgressOptions { - // work_done_progress: None, - // }, - // completion_item: None, - // }); - res.capabilities.text_document_sync = Some(TextDocumentSyncCapability::Options( TextDocumentSyncOptions { open_close: Some(true), @@ -41,23 +33,21 @@ impl LanguageServer for Backend { }, )); - *self.workspace_folder.lock().await = params + // Safely extract workspace root without deep nested matching panics + let folder_path = params .workspace_folders - .iter() - .next() - .map(|folders| { - folders - .iter() - .next() - .map(|v| v.uri.to_file_path().ok()) - .flatten() - }) - .flatten(); + .as_ref() + .and_then(|folders| folders.first()) + .and_then(|folder| folder.uri.to_file_path().ok()); + + if let Some(ref path) = folder_path { + *self.workspace_folder.lock().await = Some(path.clone()); + } let workspace_folder = self.workspace_folder.clone(); - tokio::spawn(async move { if let Some(root) = &*workspace_folder.lock().await { + // Consideration: Ensure transpiler::build is panic-safe internally transpiler::build(root); } }); @@ -73,22 +63,115 @@ impl LanguageServer for Backend { async fn did_save(&self, params: DidSaveTextDocumentParams) { self.client - .log_message(MessageType::INFO, "getting completion") + .log_message(MessageType::INFO, "Processing did_save event") .await; - let input_path = params - .text_document - .uri - .to_file_path() - .expect("Invalid document path"); + // 1. Safe URI parsing fallback + let input_path = match params.text_document.uri.to_file_path() { + Ok(path) => path, + Err(_) => { + self.client + .log_message( + MessageType::WARNING, + "Skipping: Document URI is not a valid local file path", + ) + .await; + return; + } + }; let output_path = from_mist_to_rust(input_path.clone()); self.client - .log_message(MessageType::INFO, output_path.display()) + .log_message( + MessageType::INFO, + format!("Transpiling: {}", input_path.display()), + ) .await; - transpile_file(&input_path, &output_path); + // 2. Safe call to the updated transpile_file function + if let Err(err_msg) = transpile_file(&input_path, &output_path) { + self.client + .log_message( + MessageType::ERROR, + format!("Transpilation error: {err_msg}"), + ) + .await; + // Optimization choice: You can return early here, or keep moving forward + // to collect compiler diagnostics anyway. + } + + // 3. Safe workspace root retrieval fallback + let workspace_root = match self.workspace_folder.lock().await.clone() { + Some(root) => root, + None => { + self.client + .log_message( + MessageType::WARNING, + "Skipping diagnostics: No active workspace folder found", + ) + .await; + return; + } + }; + + // 4. Run the project builder stage + let diagnostics_raw = builder::build( + vec![ + "check".to_string(), + "--bin".to_string(), + "mist-lsp".to_string(), + ], + workspace_root, + ); + + let mut diagnostics = Vec::new(); + + for diag in diagnostics_raw.iter() { + let (msg, severity) = match diag { + MistDiagnostic::Error(msg) => (msg, DiagnosticSeverity::ERROR), + MistDiagnostic::Warning(msg) => (msg, DiagnosticSeverity::WARNING), + MistDiagnostic::Rust(d) => { + self.client + .log_message( + MessageType::LOG, + format!("Unhandled underlying Rust diagnostic: {d:?}"), + ) + .await; + continue; + } + }; + + // 5. Safe indexing checks via saturating_sub + let line = (msg.line as u32).saturating_sub(1); + let column = (msg.column as u32).saturating_sub(1); + + diagnostics.push(Diagnostic { + range: Range { + start: Position { + line, + character: column, + }, + end: Position { + line, + character: u32::MAX, // Highlights to the end of the line safely + }, + }, + severity: Some(severity), + code: None, + source: Some("mist-analyzer".to_string()), + message: msg.message.clone(), + related_information: None, + tags: None, + data: None, + code_description: None, + }); + } + + // 6. Send the generated diagnostics back to the IDE client interface + self.client + .publish_diagnostics(params.text_document.uri, diagnostics, None) + .await; } async fn shutdown(&self) -> Result<()> { @@ -111,22 +194,13 @@ pub async fn start() { fn from_mist_to_rust(mut path: PathBuf) -> PathBuf { path.set_extension("rs"); - // 1. Convert path to a vector of components let mut comps: Vec = path.components().collect(); - // 2. Find the index of the *last* component matching "src" if let Some(pos) = comps.iter().rposition(|c| c.as_os_str() == "src") { - // Define your replacement path let replacement = std::path::Path::new(".mist/lsp"); - - // 3. Splice the replacement components into the original vector, - // replacing the single "src" component at `pos` comps.splice(pos..=pos, replacement.components()); - - // 4. Rebuild the PathBuf from the modified components comps.iter().collect() } else { - // Return path with just the extension changed if "src" wasn't found path } } diff --git a/crates/analyzer/src/transpiler.rs b/crates/analyzer/src/transpiler.rs index 37a5473..36f5049 100644 --- a/crates/analyzer/src/transpiler.rs +++ b/crates/analyzer/src/transpiler.rs @@ -1,7 +1,6 @@ use std::{ fs, path::{Path, PathBuf}, - process, }; use mist_parser::error::ParseError; @@ -10,42 +9,50 @@ pub fn build(root: &PathBuf) { let src_dir = root.join("src"); let out_dir = root.join(".mist/lsp"); - build_dir(&root, &src_dir, &src_dir, &out_dir); + if let Err(e) = build_dir(root, &src_dir, &src_dir, &out_dir) { + eprintln!("Warning: Build directory run aborted safely: {e}"); + } } -fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) { - let entries = match fs::read_dir(current_dir) { - Ok(entries) => entries, - Err(e) => { - eprintln!( - "error: failed to read directory {}\n {}", - current_dir.display(), - e - ); - - process::exit(1); - } - }; +fn build_dir( + root: &Path, + base_src: &Path, + current_dir: &Path, + out_dir: &Path, +) -> Result<(), String> { + let entries = fs::read_dir(current_dir) + .map_err(|e| format!("failed to read directory {}: {}", current_dir.display(), e))?; for entry in entries { let entry = match entry { Ok(entry) => entry, Err(e) => { - eprintln!("error: failed to read directory entry\n {}", e); - - process::exit(1); + eprintln!("Warning: Skipping invalid directory entry: {e}"); + continue; // Skip corrupted entry instead of crashing } }; let path = entry.path(); - // recurse into nested directories + // Recurse into nested directories if path.is_dir() { - build_dir(root, base_src, &path, out_dir); + if let Err(e) = build_dir(root, base_src, &path, out_dir) { + eprintln!("Warning: Nested build directory failed: {e}"); + } continue; } - let relative = path.strip_prefix(base_src).unwrap(); + // Safe prefix stripping fallback + let relative = match path.strip_prefix(base_src) { + Ok(p) => p, + Err(_) => { + eprintln!( + "Warning: Path {} is outside base source directory", + path.display() + ); + continue; + } + }; // Handle non-mist files with a cache check if path.extension().and_then(|e| e.to_str()) != Some("mist") { @@ -60,7 +67,13 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) { continue; } - fs::copy(&path, dest_path).expect("Failed to copy non-mist file"); + if let Err(e) = fs::copy(&path, &dest_path) { + eprintln!( + "Warning: Failed to copy non-mist file {}: {}", + path.display(), + e + ); + } continue; } @@ -71,75 +84,64 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) { continue; } - transpile_file(&path, &output_path); - } -} - -pub fn transpile_file(path: &Path, output_path: &Path) { - // create parent directories - if let Some(parent) = output_path.parent() { - if let Err(e) = fs::create_dir_all(parent) { + if let Err(e) = transpile_file(&path, &output_path) { eprintln!( - "error: failed to create output directory {}\n {}", - parent.display(), + "Warning: Transpilation failed for {}: {}", + path.display(), e ); - - process::exit(1); } } - // read source - let source = match fs::read_to_string(&path) { - Ok(s) => s, - Err(e) => { - eprintln!("error: failed to read file {}\n {}", path.display(), e); + Ok(()) +} - process::exit(1); - } - }; +pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> { + // Create parent directories + if let Some(parent) = output_path.parent() { + fs::create_dir_all(parent).map_err(|e| { + format!( + "failed to create output directory {}: {}", + parent.display(), + e + ) + })?; + } + + // Read source + let source = fs::read_to_string(path) + .map_err(|e| format!("failed to read file {}: {}", path.display(), e))?; let parser_result = mist_parser::parse(&source).map_err(|e| match e { ParseError::Ast(e) => { + // Using components carefully to prevent out-of-bounds or zero layout crashes let start_pos = e.span.start_pos().line_col(); - let span = e.span.as_str(); format!( - "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}", - path.as_os_str().display(), + "\n{}:{}:{}\n Error: {}\n\t{}{}\t{}", + path.display(), start_pos.0, start_pos.1, e.error_message, span, - if span.ends_with("\n") { "" } else { "\n" }, + if span.ends_with('\n') { "" } else { "\n" }, "^".repeat(span.trim().len()), ) } ParseError::PreAst(e) => format!("{e}"), }); - let ast = match parser_result { - Ok(ast) => ast, - Err(e) => { - eprintln!("error: parse failed in {}\n{}", path.display(), e); - - process::exit(1); - } - }; + // If parsing fails, return the error string back gracefully so the LSP can show it + let ast = parser_result.map_err(|e| format!("parse failed in {}:\n{}", path.display(), e))?; let mut gc = mist_codegen::RustCodegen::new(); let output = gc.generate(ast); - if let Err(e) = fs::write(&output_path, output) { - eprintln!( - "error: failed to write output {}\n {}", - output_path.display(), - e - ); + fs::write(output_path, output) + .map_err(|e| format!("failed to write output {}: {}", output_path.display(), e))?; - process::exit(1); - } + Ok(()) } fn should_skip(source: &Path, output: &Path) -> bool { From b96a6f154da72d354e917e80a3db1811062848ad Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Sat, 23 May 2026 04:36:56 +0200 Subject: [PATCH 2/6] errors --- crates/analyzer/src/builder.rs | 42 ++++------------------------------ 1 file changed, 5 insertions(+), 37 deletions(-) diff --git a/crates/analyzer/src/builder.rs b/crates/analyzer/src/builder.rs index 138c8ea..2522602 100644 --- a/crates/analyzer/src/builder.rs +++ b/crates/analyzer/src/builder.rs @@ -76,7 +76,11 @@ pub fn build(mut args: Vec, root: PathBuf) -> Vec { .expect("Unable to find mapping"); let mist_msg = MistDiagnosticMessage { - message: span.label.clone().unwrap_or(msg.message.message.clone()), + message: format!( + "{}: {}", + msg.message.message, + span.label.clone().unwrap_or_default() + ), file_name: if is_root { mist_file } else { @@ -116,42 +120,6 @@ pub fn build(mut args: Vec, root: PathBuf) -> Vec { diagnostics } -// pub fn print_diagnostics(diagnostics: &Vec) { -// let mut files = HashMap::new(); - -// for diag in diagnostics { -// match diag { -// MistDiagnostic::Error(msg) => { -// let line = get_line(&mut files, &msg); - -// println!( -// "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}", -// msg.file_name, -// msg.line, -// msg.column, -// msg.message, -// line.unwrap_or_default(), -// ) -// } - -// MistDiagnostic::Warning(msg) => { -// let line = get_line(&mut files, &msg); - -// println!( -// "\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}", -// msg.file_name, -// msg.line, -// msg.column, -// msg.message, -// line.unwrap_or_default(), -// ) -// } - -// MistDiagnostic::Rust(rs) => println!("{rs}"), -// } -// } -// } - pub fn get_line( files: &mut HashMap>, msg: &MistDiagnosticMessage, From 77b69912265152ed86eebeceb711cbe1d0b5dc15 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Sat, 23 May 2026 08:52:45 +0200 Subject: [PATCH 3/6] Improved error system LSp --- crates/analyzer/src/lib.rs | 116 ++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/crates/analyzer/src/lib.rs b/crates/analyzer/src/lib.rs index 44bbc28..c158aad 100644 --- a/crates/analyzer/src/lib.rs +++ b/crates/analyzer/src/lib.rs @@ -1,6 +1,7 @@ pub mod builder; pub mod transpiler; +use std::collections::HashMap; use std::path::{Component, PathBuf}; use std::sync::Arc; @@ -10,12 +11,33 @@ use tower_lsp::lsp_types::*; use tower_lsp::{Client, LanguageServer, LspService, Server}; use crate::builder::MistDiagnostic; -use crate::transpiler::transpile_file; #[derive(Debug)] struct Backend { client: Client, workspace_folder: Arc>>, + previous_diagnostics: Arc>>>, +} + +/// Helper function to force percent-encoding on Windows drive colons +/// so that the URLs exactly match what VS Code/LSP clients send. +fn clean_lsp_url(path: &std::path::Path) -> Option { + let mut url_str = Url::from_file_path(path).ok()?.to_string(); + + // Look for Windows patterns like file:///D: or file:///d: and switch to %3A + if url_str.starts_with("file:///I:") || url_str.starts_with("file:///i:") || // Catch-all or explicit check: + (url_str.len() > 10 && url_str.as_bytes()[11] == b':') + { + // Safely replace the first colon occurring after "file:///" + if let Some(pos) = url_str.find(':') { + if pos == 11 { + // Double check it's the drive letter colon + url_str.replace_range(pos..=pos, "%3A"); + } + } + } + + Url::parse(&url_str).ok() } #[tower_lsp::async_trait] @@ -33,7 +55,6 @@ impl LanguageServer for Backend { }, )); - // Safely extract workspace root without deep nested matching panics let folder_path = params .workspace_folders .as_ref() @@ -47,7 +68,6 @@ impl LanguageServer for Backend { let workspace_folder = self.workspace_folder.clone(); tokio::spawn(async move { if let Some(root) = &*workspace_folder.lock().await { - // Consideration: Ensure transpiler::build is panic-safe internally transpiler::build(root); } }); @@ -61,47 +81,15 @@ impl LanguageServer for Backend { .await; } - async fn did_save(&self, params: DidSaveTextDocumentParams) { + async fn did_save(&self, _: DidSaveTextDocumentParams) { self.client .log_message(MessageType::INFO, "Processing did_save event") .await; - // 1. Safe URI parsing fallback - let input_path = match params.text_document.uri.to_file_path() { - Ok(path) => path, - Err(_) => { - self.client - .log_message( - MessageType::WARNING, - "Skipping: Document URI is not a valid local file path", - ) - .await; - return; - } - }; - - let output_path = from_mist_to_rust(input_path.clone()); - - self.client - .log_message( - MessageType::INFO, - format!("Transpiling: {}", input_path.display()), - ) - .await; - - // 2. Safe call to the updated transpile_file function - if let Err(err_msg) = transpile_file(&input_path, &output_path) { - self.client - .log_message( - MessageType::ERROR, - format!("Transpilation error: {err_msg}"), - ) - .await; - // Optimization choice: You can return early here, or keep moving forward - // to collect compiler diagnostics anyway. + if let Some(root) = &*self.workspace_folder.lock().await { + transpiler::build(root); } - // 3. Safe workspace root retrieval fallback let workspace_root = match self.workspace_folder.lock().await.clone() { Some(root) => root, None => { @@ -115,38 +103,35 @@ impl LanguageServer for Backend { } }; - // 4. Run the project builder stage let diagnostics_raw = builder::build( vec![ "check".to_string(), - "--bin".to_string(), - "mist-lsp".to_string(), + "--workspace".to_string(), + "--all-targets".to_string(), ], workspace_root, ); - let mut diagnostics = Vec::new(); + let mut diagnostics = HashMap::new(); for diag in diagnostics_raw.iter() { let (msg, severity) = match diag { MistDiagnostic::Error(msg) => (msg, DiagnosticSeverity::ERROR), MistDiagnostic::Warning(msg) => (msg, DiagnosticSeverity::WARNING), - MistDiagnostic::Rust(d) => { - self.client - .log_message( - MessageType::LOG, - format!("Unhandled underlying Rust diagnostic: {d:?}"), - ) - .await; + MistDiagnostic::Rust(_) => { continue; } }; - // 5. Safe indexing checks via saturating_sub let line = (msg.line as u32).saturating_sub(1); let column = (msg.column as u32).saturating_sub(1); - diagnostics.push(Diagnostic { + let url = match clean_lsp_url(&msg.file_path) { + Some(u) => u, + None => continue, + }; + + let diagnostic_item = Diagnostic { range: Range { start: Position { line, @@ -154,7 +139,7 @@ impl LanguageServer for Backend { }, end: Position { line, - character: u32::MAX, // Highlights to the end of the line safely + character: column + 1, }, }, severity: Some(severity), @@ -165,13 +150,27 @@ impl LanguageServer for Backend { tags: None, data: None, code_description: None, - }); + }; + + diagnostics + .entry(url) + .or_insert_with(Vec::new) + .push(diagnostic_item); } - // 6. Send the generated diagnostics back to the IDE client interface - self.client - .publish_diagnostics(params.text_document.uri, diagnostics, None) - .await; + for (file, _) in self.previous_diagnostics.lock().await.iter() { + self.client + .publish_diagnostics(file.clone(), Vec::new(), None) + .await; + } + + for (file, diag) in &diagnostics { + self.client + .publish_diagnostics(file.clone(), diag.clone(), None) + .await; + } + + *self.previous_diagnostics.lock().await = diagnostics; } async fn shutdown(&self) -> Result<()> { @@ -187,11 +186,12 @@ pub async fn start() { let (service, socket) = LspService::new(|client| Backend { client, workspace_folder: Arc::new(Mutex::new(None)), + previous_diagnostics: Arc::new(Mutex::new(HashMap::new())), }); Server::new(stdin, stdout, socket).serve(service).await; } -fn from_mist_to_rust(mut path: PathBuf) -> PathBuf { +pub fn from_mist_to_rust(mut path: PathBuf) -> PathBuf { path.set_extension("rs"); let mut comps: Vec = path.components().collect(); From b149bcde4a485418a00043cfdb9c93ada5492e6f Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Sat, 23 May 2026 09:50:04 +0200 Subject: [PATCH 4/6] Refactored transpiler LSP --- src/transpiler.rs | 57 ++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/transpiler.rs b/src/transpiler.rs index 8c2a6ea..051e305 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -5,7 +5,7 @@ use std::{ time::Instant, }; -use mist_parser::error::ParseError; +use mist_parser::{ast::TopLevel, error::ParseError}; pub fn build(force: bool) -> PathBuf { let start = Instant::now(); @@ -110,40 +110,35 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path, f } }; - let parser_result = mist_parser::parse(&source).map_err(|e| match e { - ParseError::Ast(e) => { - let start_pos = e.span.start_pos().line_col(); - - let span = e.span.as_str(); - - format!( - "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}", - path.as_os_str().display(), - start_pos.0, - start_pos.1, - e.error_message, - span, - if span.ends_with("\n") { "" } else { "\n" }, - "^".repeat(span.trim().len()), - ) - } - ParseError::PreAst(e) => format!("{e}"), - }); - - let ast = match parser_result { + let output = match transpile_file(&source) { Ok(ast) => ast, Err(e) => { - eprintln!("error: parse failed in {}\n{}", path.display(), e); + match e { + ParseError::Ast(e) => { + let start_pos = e.span.start_pos().line_col(); + + let span = e.span.as_str(); + + eprintln!( + "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}", + path.as_os_str().display(), + start_pos.0, + start_pos.1, + e.error_message, + span, + if span.ends_with("\n") { "" } else { "\n" }, + "^".repeat(span.trim().len()), + ) + } + ParseError::PreAst(e) => { + eprintln!("error: parse failed in {}\n{}", path.display(), e) + } + } process::exit(1); } }; - // semantic::walk_ast(semantic::scope::Scope::from_top(root, &ast), &mut ast); - - let mut gc = mist_codegen::RustCodegen::new(); - let output = gc.generate(ast); - if let Err(e) = fs::write(&output_path, output) { eprintln!( "error: failed to write output {}\n {}", @@ -156,6 +151,12 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path, f } } +pub fn transpile_file<'a>(source: &'a str) -> Result>> { + let mut gc = mist_codegen::RustCodegen::new(); + + Ok(gc.generate(mist_parser::parse(&source)?)) +} + fn should_skip(source: &Path, output: &Path) -> bool { if let (Ok(src_meta), Ok(out_meta)) = (fs::metadata(source), fs::metadata(output)) { if let (Ok(src_time), Ok(out_time)) = (src_meta.modified(), out_meta.modified()) { From fa00b3a1990b7a83b10f635a4a3242ba0c9ed59e Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Sat, 23 May 2026 10:14:23 +0200 Subject: [PATCH 5/6] Position injection --- crates/analyzer/src/lib.rs | 67 ++++++++++++++++++++++++++++++- crates/analyzer/src/transpiler.rs | 8 +++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/crates/analyzer/src/lib.rs b/crates/analyzer/src/lib.rs index c158aad..886d771 100644 --- a/crates/analyzer/src/lib.rs +++ b/crates/analyzer/src/lib.rs @@ -2,6 +2,7 @@ pub mod builder; pub mod transpiler; use std::collections::HashMap; +use std::fs; use std::path::{Component, PathBuf}; use std::sync::Arc; @@ -55,6 +56,8 @@ impl LanguageServer for Backend { }, )); + res.capabilities.definition_provider = Some(OneOf::Left(true)); + let folder_path = params .workspace_folders .as_ref() @@ -81,6 +84,10 @@ impl LanguageServer for Backend { .await; } + async fn shutdown(&self) -> Result<()> { + Ok(()) + } + async fn did_save(&self, _: DidSaveTextDocumentParams) { self.client .log_message(MessageType::INFO, "Processing did_save event") @@ -173,8 +180,38 @@ impl LanguageServer for Backend { *self.previous_diagnostics.lock().await = diagnostics; } - async fn shutdown(&self) -> Result<()> { - Ok(()) + async fn goto_definition( + &self, + params: GotoDefinitionParams, + ) -> Result> { + let mut source = fs::read_to_string( + params + .text_document_position_params + .text_document + .uri + .to_file_path() + .unwrap(), + ) + .expect("Failed to read source"); + + let inject = "__mist_23"; + + source = insert_at_position( + &source, + params.text_document_position_params.position.line as usize, + params.text_document_position_params.position.character as usize, + &inject, + ); + + let output = transpiler::transpile_text(&source).expect("Failed to transpile"); + + let position = output.find(inject).expect("Didn't find injection").saturating_sub(1); + + self.client + .log_message(MessageType::INFO, format!("Found at {position}")) + .await; + + Ok(None) } } @@ -204,3 +241,29 @@ pub fn from_mist_to_rust(mut path: PathBuf) -> PathBuf { path } } + +fn insert_at_position(s: &str, line: usize, col: usize, insert: &str) -> String { + let mut lines: Vec = s.lines().map(|l| l.to_string()).collect(); + + // Convert to 0-index + let line_idx = line.saturating_sub(1); + let col_idx = col.saturating_sub(1); + + // Ensure line exists (optional behavior: extend with empty lines) + if line_idx >= lines.len() { + lines.resize(line_idx + 1, String::new()); + } + + let target = &mut lines[line_idx]; + + // Clamp column to valid char boundary + let char_idx = target + .char_indices() + .nth(col_idx) + .map(|(i, _)| i) + .unwrap_or(target.len()); // if past end, append + + target.insert_str(char_idx, insert); + + lines.join("\n") +} diff --git a/crates/analyzer/src/transpiler.rs b/crates/analyzer/src/transpiler.rs index 36f5049..0e51b31 100644 --- a/crates/analyzer/src/transpiler.rs +++ b/crates/analyzer/src/transpiler.rs @@ -3,7 +3,7 @@ use std::{ path::{Path, PathBuf}, }; -use mist_parser::error::ParseError; +use mist_parser::{ast::TopLevel, error::ParseError}; pub fn build(root: &PathBuf) { let src_dir = root.join("src"); @@ -144,6 +144,12 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> { Ok(()) } +pub fn transpile_text<'a>(source: &'a str) -> Result>> { + let mut gc = mist_codegen::RustCodegen::new(); + + Ok(gc.generate(mist_parser::parse(&source)?)) +} + fn should_skip(source: &Path, output: &Path) -> bool { if let (Ok(src_meta), Ok(out_meta)) = (fs::metadata(source), fs::metadata(output)) { if let (Ok(src_time), Ok(out_time)) = (src_meta.modified(), out_meta.modified()) { From eb247676172e4b06048a158b0fcedbf0b8b6a275 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Sat, 23 May 2026 11:27:05 +0200 Subject: [PATCH 6/6] idk --- Cargo.lock | 6 +++--- crates/analyzer/Cargo.toml | 2 +- crates/analyzer/src/lib.rs | 5 ++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d7a24e..17a2a50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,9 +130,9 @@ dependencies = [ [[package]] name = "dashmap" -version = "6.2.1" +version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6361d5c062261c78a176addb82d4c821ae42bed6089de0e12603cd25de2059c" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" dependencies = [ "cfg-if", "crossbeam-utils", @@ -445,7 +445,7 @@ name = "mist-analyzer" version = "0.0.2" dependencies = [ "cargo_metadata", - "dashmap 6.2.1", + "dashmap 6.1.0", "mist-codegen", "mist-parser", "tokio", diff --git a/crates/analyzer/Cargo.toml b/crates/analyzer/Cargo.toml index 49b74be..cc3c8c6 100644 --- a/crates/analyzer/Cargo.toml +++ b/crates/analyzer/Cargo.toml @@ -12,7 +12,7 @@ categories = ["compilers"] mist-parser = { workspace = true } mist-codegen = { workspace = true } -dashmap = "6.2.1" +dashmap = "6.1.0" tokio = { version = "1.52.3", features = ["full", "macros", "rt", "rt-multi-thread"] } tower-lsp = "0.20.0" cargo_metadata = "0.23.1" diff --git a/crates/analyzer/src/lib.rs b/crates/analyzer/src/lib.rs index 886d771..fbbffe0 100644 --- a/crates/analyzer/src/lib.rs +++ b/crates/analyzer/src/lib.rs @@ -205,7 +205,10 @@ impl LanguageServer for Backend { let output = transpiler::transpile_text(&source).expect("Failed to transpile"); - let position = output.find(inject).expect("Didn't find injection").saturating_sub(1); + let position = output + .find(inject) + .expect("Didn't find injection") + .saturating_sub(1); self.client .log_message(MessageType::INFO, format!("Found at {position}"))