fix Analyzer file path #118

This commit is contained in:
2026-07-14 08:46:33 +02:00
parent df9e31e26d
commit 708b956750
2 changed files with 49 additions and 51 deletions
+16 -7
View File
@@ -21,7 +21,8 @@ use tower_lsp::{Client, LanguageServer, LspService, Server};
use crate::rust_analyzer::RustAnalyzer; use crate::rust_analyzer::RustAnalyzer;
use crate::transpiler::{ use crate::transpiler::{
TranspileError, TranspiledFile, format_mist, transpile_mist, transpile_mist_no_sem, TranspileError, TranspiledFile, format_mist, resolve_rust_path, transpile_mist,
transpile_mist_no_sem,
}; };
static MARKER_COUNTER: AtomicU64 = AtomicU64::new(0); static MARKER_COUNTER: AtomicU64 = AtomicU64::new(0);
@@ -392,10 +393,13 @@ impl Backend {
// Use the last known working Rust content + mapping to inject the // Use the last known working Rust content + mapping to inject the
// marker at the nearest valid Rust position. // marker at the nearest valid Rust position.
let mist_target = MistMap(line as usize, character as usize); let mist_target = MistMap(line as usize, character as usize);
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf()); let source_for_path = self
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") { .documents
rust_path.set_file_name("mod.rs"); .lock()
} .await
.get(mist_path)
.map(|r| r.to_string());
let rust_path = resolve_rust_path(mist_path, source_for_path.as_deref());
let (last_mapping, last_content, original_version) = { let (last_mapping, last_content, original_version) = {
let map_guard = self.mapping.lock().await; let map_guard = self.mapping.lock().await;
@@ -617,9 +621,10 @@ impl Backend {
.cloned() .cloned()
.collect(); .collect();
for path in &stale { for path in &stale {
let source_for_path = docs.get(path).map(|r| r.to_string());
docs.remove(path); docs.remove(path);
if path.extension().and_then(|e| e.to_str()) == Some("mist") { if path.extension().and_then(|e| e.to_str()) == Some("mist") {
let rust_path = mist_to_rust_path(path); let rust_path = resolve_rust_path(path, source_for_path.as_deref());
let _ = std::fs::remove_file(&rust_path); let _ = std::fs::remove_file(&rust_path);
let map_path = rust_path.with_extension("map.json"); let map_path = rust_path.with_extension("map.json");
let _ = std::fs::remove_file(&map_path); let _ = std::fs::remove_file(&map_path);
@@ -1175,9 +1180,13 @@ impl LanguageServer for Backend {
} }
}; };
let source_for_path = {
let docs = self.documents.lock().await;
docs.get(&mist_path).map(|r| r.to_string())
};
self.documents.lock().await.remove(&mist_path); self.documents.lock().await.remove(&mist_path);
let rust_path = mist_to_rust_path(&mist_path); let rust_path = resolve_rust_path(&mist_path, source_for_path.as_deref());
if let Some(rust_uri) = clean_lsp_url(&rust_path) { if let Some(rust_uri) = clean_lsp_url(&rust_path) {
let _ = self.rust_analyzer.lock().await.did_close(rust_uri).await; let _ = self.rust_analyzer.lock().await.did_close(rust_uri).await;
+23 -34
View File
@@ -17,22 +17,22 @@ pub enum TranspileError<'a> {
Semantic(Vec<mist_parser::semantics::SemanticError>), Semantic(Vec<mist_parser::semantics::SemanticError>),
} }
pub fn transpile_mist<'a>( /// Resolve the output `.rs` path for a `.mist` source file.
mist_path: &Path, ///
source: &'a str, /// Always checks for a `pub module x;` declaration via `parse_module` and
extra_mod_decl: &str, /// uses the declared module name as the filename when present. Falls back
) -> Result<TranspiledFile, TranspileError<'a>> { /// to the literal file stem when parsing fails or the file has no module
/// declaration.
pub fn resolve_rust_path(mist_path: &Path, source: Option<&str>) -> PathBuf {
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf()); let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf());
// Package files (package.mist) must output as <dir>/mod.rs so the Rust module // Package files (package.mist) must output as <dir>/mod.rs so the Rust
// hierarchy resolves correctly (pub mod <child>; declarations look for sibling // module hierarchy resolves correctly.
// .rs files, and the parent module declaration looks for <dir>/mod.rs).
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") { if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") {
rust_path.set_file_name("mod.rs"); rust_path.set_file_name("mod.rs");
} } else if let Some(src) = source {
// Prioritize the module declaration from the source over the file stem. // Prioritize the module declaration from the source over the file stem.
// Every transpile re-parses so we always pick up the current declaration. // Every caller re-parses so we always pick up the current declaration.
if let Ok(Some((_, ref name))) = parse_module(source) { if let Ok(Some((_, ref name))) = parse_module(src) {
if rust_path.file_name().map(|v| v.to_str()).unwrap_or_default() != Some("mod.rs") { if rust_path.file_name().map(|v| v.to_str()).unwrap_or_default() != Some("mod.rs") {
if let Some(ext) = rust_path.extension().map(|e| e.to_owned()) { if let Some(ext) = rust_path.extension().map(|e| e.to_owned()) {
let mut new_name = std::ffi::OsString::from(&name.0); let mut new_name = std::ffi::OsString::from(&name.0);
@@ -44,6 +44,16 @@ pub fn transpile_mist<'a>(
} }
} }
} }
}
rust_path
}
pub fn transpile_mist<'a>(
mist_path: &Path,
source: &'a str,
extra_mod_decl: &str,
) -> Result<TranspiledFile, TranspileError<'a>> {
let rust_path = resolve_rust_path(mist_path, Some(source));
let parsed = parse(source).map_err(TranspileError::Parse)?; let parsed = parse(source).map_err(TranspileError::Parse)?;
@@ -73,28 +83,7 @@ pub fn transpile_mist_no_sem(
source: &str, source: &str,
extra_mod_decl: &str, extra_mod_decl: &str,
) -> Result<TranspiledFile, String> { ) -> Result<TranspiledFile, String> {
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf()); let rust_path = resolve_rust_path(mist_path, Some(source));
// Package files (package.mist) must output as <dir>/mod.rs so the Rust module
// hierarchy resolves correctly (pub mod <child>; declarations look for sibling
// .rs files, and the parent module declaration looks for <dir>/mod.rs).
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") {
rust_path.set_file_name("mod.rs");
}
// Prioritize the module declaration from the source over the file stem.
// Every transpile re-parses so we always pick up the current declaration.
if let Ok(Some((_, ref name))) = parse_module(source) {
if rust_path.file_name().map(|v| v.to_str()).unwrap_or_default() != Some("mod.rs") {
if let Some(ext) = rust_path.extension().map(|e| e.to_owned()) {
let mut new_name = std::ffi::OsString::from(&name.0);
new_name.push(".");
new_name.push(ext);
rust_path.set_file_name(new_name);
} else {
rust_path.set_file_name(&name.0);
}
}
}
let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?; let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?;