+140
-19
@@ -21,7 +21,8 @@ use tower_lsp::{Client, LanguageServer, LspService, Server};
|
||||
|
||||
use crate::rust_analyzer::RustAnalyzer;
|
||||
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);
|
||||
@@ -98,7 +99,7 @@ fn lsp_pos_to_rust_map(pos: &Position) -> RustMap {
|
||||
|
||||
fn mist_map_to_lsp_pos(map: &MistMap) -> Position {
|
||||
Position {
|
||||
line: map.0 as u32,
|
||||
line: map.0.saturating_sub(1) as u32,
|
||||
character: map.1 as u32,
|
||||
}
|
||||
}
|
||||
@@ -188,6 +189,10 @@ fn find_marker_position(content: &str, marker: &str) -> Option<Position> {
|
||||
Some(Position { line, character })
|
||||
}
|
||||
|
||||
fn is_mist_file(path: &Path) -> bool {
|
||||
path.extension().and_then(|e| e.to_str()) == Some("mist")
|
||||
}
|
||||
|
||||
fn mist_to_rust_path(mist_path: &Path) -> PathBuf {
|
||||
let mut path = mist_path.to_path_buf();
|
||||
path.set_extension("rs");
|
||||
@@ -392,10 +397,13 @@ impl Backend {
|
||||
// Use the last known working Rust content + mapping to inject the
|
||||
// marker at the nearest valid Rust position.
|
||||
let mist_target = MistMap(line as usize, character as usize);
|
||||
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf());
|
||||
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") {
|
||||
rust_path.set_file_name("mod.rs");
|
||||
}
|
||||
let source_for_path = self
|
||||
.documents
|
||||
.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 map_guard = self.mapping.lock().await;
|
||||
@@ -611,15 +619,12 @@ impl Backend {
|
||||
// mod.rs output files for directories with no remaining source children.
|
||||
{
|
||||
let mut docs = self.documents.lock().await;
|
||||
let stale: Vec<PathBuf> = docs
|
||||
.keys()
|
||||
.filter(|p| !p.exists())
|
||||
.cloned()
|
||||
.collect();
|
||||
let stale: Vec<PathBuf> = docs.keys().filter(|p| !p.exists()).cloned().collect();
|
||||
for path in &stale {
|
||||
let source_for_path = docs.get(path).map(|r| r.to_string());
|
||||
docs.remove(path);
|
||||
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 map_path = rust_path.with_extension("map.json");
|
||||
let _ = std::fs::remove_file(&map_path);
|
||||
@@ -631,8 +636,7 @@ impl Backend {
|
||||
let synthetic: Vec<PathBuf> = docs
|
||||
.keys()
|
||||
.filter(|p| {
|
||||
p.file_name().and_then(|n| n.to_str()) == Some("package.mist")
|
||||
&& !p.exists()
|
||||
p.file_name().and_then(|n| n.to_str()) == Some("package.mist") && !p.exists()
|
||||
})
|
||||
.cloned()
|
||||
.collect();
|
||||
@@ -1089,6 +1093,18 @@ impl LanguageServer for Backend {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Non-.mist files pass through directly to rust-analyzer.
|
||||
if !is_mist_file(&mist_path) {
|
||||
let _ = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.did_open(uri, ¶ms.text_document.text)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
|
||||
let source = params.text_document.text;
|
||||
|
||||
self.documents
|
||||
@@ -1111,6 +1127,18 @@ impl LanguageServer for Backend {
|
||||
}
|
||||
};
|
||||
|
||||
if !is_mist_file(&mist_path) {
|
||||
if let Some(change) = params.content_changes.into_iter().last() {
|
||||
let _ = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.did_change(uri, &change.text, params.text_document.version)
|
||||
.await;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(change) = params.content_changes.into_iter().last() {
|
||||
let source = change.text;
|
||||
|
||||
@@ -1143,6 +1171,11 @@ impl LanguageServer for Backend {
|
||||
.expect("Failed to read file")
|
||||
};
|
||||
|
||||
if !is_mist_file(&mist_path) {
|
||||
let _ = self.rust_analyzer.lock().await.did_save(uri, &text).await;
|
||||
return;
|
||||
}
|
||||
|
||||
self.documents
|
||||
.lock()
|
||||
.await
|
||||
@@ -1175,9 +1208,18 @@ impl LanguageServer for Backend {
|
||||
}
|
||||
};
|
||||
|
||||
if !is_mist_file(&mist_path) {
|
||||
let _ = self.rust_analyzer.lock().await.did_close(uri).await;
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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) {
|
||||
let _ = self.rust_analyzer.lock().await.did_close(rust_uri).await;
|
||||
@@ -1195,6 +1237,25 @@ impl LanguageServer for Backend {
|
||||
Ok(p) => p,
|
||||
Err(_) => return Ok(None),
|
||||
};
|
||||
|
||||
// Non-.mist files pass through directly to rust-analyzer.
|
||||
if !is_mist_file(&mist_path) {
|
||||
let result = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.request::<lsp_types::request::Completion>(params)
|
||||
.await;
|
||||
|
||||
return match result {
|
||||
Ok(r) => Ok(r),
|
||||
Err(e) => {
|
||||
eprintln!("completion error (passthrough): {e}");
|
||||
Ok(None)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let source = match self.documents.lock().await.get(&mist_path) {
|
||||
Some(r) => r.to_string(),
|
||||
None => return Ok(None),
|
||||
@@ -1272,7 +1333,7 @@ impl LanguageServer for Backend {
|
||||
&self,
|
||||
params: GotoDefinitionParams,
|
||||
) -> tower_lsp::jsonrpc::Result<Option<GotoDefinitionResponse>> {
|
||||
let mist_uri = params.text_document_position_params.text_document.uri;
|
||||
let mist_uri = ¶ms.text_document_position_params.text_document.uri;
|
||||
let mist_pos = params.text_document_position_params.position;
|
||||
eprintln!(
|
||||
"[goto_definition] entered at mist ({}, {})",
|
||||
@@ -1283,6 +1344,25 @@ impl LanguageServer for Backend {
|
||||
Ok(p) => p,
|
||||
Err(_) => return Ok(None),
|
||||
};
|
||||
|
||||
// Non-.mist files pass through directly to rust-analyzer.
|
||||
if !is_mist_file(&mist_path) {
|
||||
let result = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.request::<lsp_types::request::GotoDefinition>(params)
|
||||
.await;
|
||||
|
||||
return match result {
|
||||
Ok(r) => Ok(r),
|
||||
Err(e) => {
|
||||
eprintln!("goto_definition error (passthrough): {e}");
|
||||
Ok(None)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let source = match self.documents.lock().await.get(&mist_path) {
|
||||
Some(r) => r.to_string(),
|
||||
None => return Ok(None),
|
||||
@@ -1452,13 +1532,32 @@ impl LanguageServer for Backend {
|
||||
}
|
||||
|
||||
async fn hover(&self, params: HoverParams) -> tower_lsp::jsonrpc::Result<Option<Hover>> {
|
||||
let mist_uri = params.text_document_position_params.text_document.uri;
|
||||
let mist_uri = ¶ms.text_document_position_params.text_document.uri;
|
||||
let mist_pos = params.text_document_position_params.position;
|
||||
|
||||
let mist_path = match mist_uri.to_file_path() {
|
||||
Ok(p) => p,
|
||||
Err(_) => return Ok(None),
|
||||
};
|
||||
|
||||
// Non-.mist files pass through directly to rust-analyzer.
|
||||
if !is_mist_file(&mist_path) {
|
||||
let result = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.request::<lsp_types::request::HoverRequest>(params)
|
||||
.await;
|
||||
|
||||
return match result {
|
||||
Ok(r) => Ok(r),
|
||||
Err(e) => {
|
||||
eprintln!("hover error (passthrough): {e}");
|
||||
Ok(None)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let source = match self.documents.lock().await.get(&mist_path) {
|
||||
Some(r) => r.to_string(),
|
||||
None => return Ok(None),
|
||||
@@ -1522,6 +1621,24 @@ impl LanguageServer for Backend {
|
||||
.to_file_path()
|
||||
.map_err(|_| tower_lsp::jsonrpc::Error::internal_error())?;
|
||||
|
||||
// Non-.mist files pass through directly to rust-analyzer.
|
||||
if !is_mist_file(&mist_path) {
|
||||
let result = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.request::<lsp_types::request::Formatting>(params)
|
||||
.await;
|
||||
|
||||
return match result {
|
||||
Ok(r) => Ok(r),
|
||||
Err(e) => {
|
||||
eprintln!("formatting error (passthrough): {e}");
|
||||
Ok(None)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let docs = self.documents.lock().await;
|
||||
|
||||
let old_text = docs
|
||||
@@ -1551,7 +1668,12 @@ impl LanguageServer for Backend {
|
||||
}
|
||||
}
|
||||
|
||||
fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<CompletionItem>, trigger_char: Option<&str>) {
|
||||
fn mist_ify_completions(
|
||||
source: &str,
|
||||
pos: &Position,
|
||||
items: &mut Vec<CompletionItem>,
|
||||
trigger_char: Option<&str>,
|
||||
) {
|
||||
let is_scoping_trigger = matches!(trigger_char, Some("." | ":"));
|
||||
|
||||
for item in items.iter_mut() {
|
||||
@@ -1731,7 +1853,6 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if matches!(curr_scope, Scope::Class | Scope::Struct) {
|
||||
items.push(CompletionItem {
|
||||
label: "field".into(),
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use mist_codegen::RustCodegen;
|
||||
use mist_parser::rev_mapper::Mapping;
|
||||
use mist_parser::{MistFmtConfig, parse};
|
||||
use mist_parser::{MistFmtConfig, parse, parse_module};
|
||||
|
||||
pub struct TranspiledFile {
|
||||
pub mist_path: PathBuf,
|
||||
@@ -17,18 +17,43 @@ pub enum TranspileError<'a> {
|
||||
Semantic(Vec<mist_parser::semantics::SemanticError>),
|
||||
}
|
||||
|
||||
/// Resolve the output `.rs` path for a `.mist` source file.
|
||||
///
|
||||
/// Always checks for a `pub module x;` declaration via `parse_module` and
|
||||
/// uses the declared module name as the filename when present. Falls back
|
||||
/// 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());
|
||||
// Package files (package.mist) must output as <dir>/mod.rs so the Rust
|
||||
// module hierarchy resolves correctly.
|
||||
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") {
|
||||
rust_path.set_file_name("mod.rs");
|
||||
} else if let Some(src) = source {
|
||||
// Prioritize the module declaration from the source over the file stem.
|
||||
// Every caller re-parses so we always pick up the current declaration.
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
rust_path
|
||||
}
|
||||
|
||||
pub fn transpile_mist<'a>(
|
||||
mist_path: &Path,
|
||||
source: &'a str,
|
||||
extra_mod_decl: &str,
|
||||
) -> Result<TranspiledFile, TranspileError<'a>> {
|
||||
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
|
||||
// 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");
|
||||
}
|
||||
let rust_path = resolve_rust_path(mist_path, Some(source));
|
||||
|
||||
let parsed = parse(source).map_err(TranspileError::Parse)?;
|
||||
|
||||
@@ -58,13 +83,7 @@ pub fn transpile_mist_no_sem(
|
||||
source: &str,
|
||||
extra_mod_decl: &str,
|
||||
) -> Result<TranspiledFile, String> {
|
||||
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
|
||||
// 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");
|
||||
}
|
||||
let rust_path = resolve_rust_path(mist_path, Some(source));
|
||||
|
||||
let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?;
|
||||
|
||||
|
||||
@@ -593,7 +593,7 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC
|
||||
cg.addln(" = core::mem::transmute(func_ptr);");
|
||||
|
||||
if mutable_self {
|
||||
cg.add_indented("func(self as *mut Self as *const core::ffi::c_void");
|
||||
cg.add_indented("func(self as *mut Self as *mut core::ffi::c_void");
|
||||
} else {
|
||||
cg.add_indented("func(self as *const Self as *const core::ffi::c_void");
|
||||
}
|
||||
|
||||
@@ -266,6 +266,33 @@ impl GenRust for TopLevelKind {
|
||||
cg.indent -= 1;
|
||||
cg.addln("}\n");
|
||||
}
|
||||
Self::StructDeclTupleUnit {
|
||||
visibility,
|
||||
name,
|
||||
generics,
|
||||
unit,
|
||||
} => {
|
||||
cg.addln(&format!(
|
||||
"{}struct {}{}",
|
||||
visibility.get_rust(),
|
||||
name.get_rust(),
|
||||
generics.get_rust()
|
||||
));
|
||||
cg.indent += 1;
|
||||
|
||||
if let Some(unit) = unit {
|
||||
cg.add(" (");
|
||||
for unit in unit {
|
||||
cg.add_indented("");
|
||||
unit.gen_rust(ctx, cg);
|
||||
cg.add(", ");
|
||||
}
|
||||
cg.add(")");
|
||||
}
|
||||
|
||||
cg.indent -= 1;
|
||||
cg.addln(";\n");
|
||||
}
|
||||
Self::EnumDecl {
|
||||
visibility,
|
||||
name,
|
||||
|
||||
@@ -118,8 +118,8 @@ pub void print_diagnostics(Vec<MistDiagnostic>& diagnostics)
|
||||
let line = get_line(&mut files, &msg);
|
||||
println!("\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}",
|
||||
msg.file_name,
|
||||
msg.line + 1,
|
||||
msg.column + 1,
|
||||
msg.line,
|
||||
msg.column,
|
||||
msg.message,
|
||||
line.unwrap_or_default(),)
|
||||
}
|
||||
@@ -127,8 +127,8 @@ pub void print_diagnostics(Vec<MistDiagnostic>& diagnostics)
|
||||
let line = get_line(&mut files, &msg);
|
||||
println!("\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}",
|
||||
msg.file_name,
|
||||
msg.line + 1,
|
||||
msg.column + 1,
|
||||
msg.line,
|
||||
msg.column,
|
||||
msg.message,
|
||||
line.unwrap_or_default(),)
|
||||
}
|
||||
@@ -143,5 +143,5 @@ pub Option<String> get_line(HashMap<PathBuf, Vec<String>> mut& files, MistDiagno
|
||||
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).map((v) => v.trim().to_string())
|
||||
lines.get(msg.line.saturating_sub(1)).map((v) => v.trim().to_string())
|
||||
}
|
||||
|
||||
@@ -59,6 +59,12 @@ pub enum TopLevelKind {
|
||||
generics: GenericsDecl,
|
||||
fields: Vec<Spanned<FieldDecl>>,
|
||||
},
|
||||
StructDeclTupleUnit {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: GenericsDecl,
|
||||
unit: Option<Vec<Spanned<TypeExpr>>>,
|
||||
},
|
||||
FunctionDecl(FunctionDecl),
|
||||
ClassDecl {
|
||||
visibility: Visibility,
|
||||
|
||||
@@ -287,6 +287,10 @@ struct_decl = {
|
||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list ~ "}"
|
||||
}
|
||||
|
||||
tuple_struct_decl = {
|
||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ tuple_type? ~ ";"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// ENUMS
|
||||
// ======================================================
|
||||
@@ -399,7 +403,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" }
|
||||
|
||||
top_level = {
|
||||
mod_attribute
|
||||
| (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
||||
| (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | tuple_struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
|
||||
@@ -57,6 +57,21 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
||||
fields: collect_recovered(inner.next().unwrap().into_inner())?,
|
||||
}),
|
||||
|
||||
Rule::tuple_struct_decl => Ok(TopLevelKind::StructDeclTupleUnit {
|
||||
visibility: Visibility::try_from(&mut inner)?,
|
||||
|
||||
name: inner.next().unwrap().try_into()?,
|
||||
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default())?,
|
||||
|
||||
unit: consume_rule(&mut inner, Rule::tuple_type)
|
||||
.map(|v| collect_recovered(v.into_inner()))
|
||||
.transpose()?,
|
||||
}),
|
||||
|
||||
Rule::class_decl => Ok(TopLevelKind::ClassDecl {
|
||||
visibility: Visibility::try_from(&mut inner)?,
|
||||
|
||||
@@ -147,7 +162,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
)),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ pub class Animal
|
||||
"Unknown".to_string()
|
||||
}
|
||||
|
||||
pub virtual void speak_mut(&mut self){}
|
||||
|
||||
pub void test_speak<'a>(&'a self)
|
||||
{
|
||||
assert_eq!(self.speak(), "Woof!");
|
||||
|
||||
@@ -100,6 +100,9 @@ pub void test_struct_destructure_rename()
|
||||
assert_eq!(b, 15);
|
||||
}
|
||||
|
||||
struct TuplePoint(i32, i32);
|
||||
struct Unit;
|
||||
|
||||
pub void run_tests()
|
||||
{
|
||||
test_struct_create_access();
|
||||
|
||||
Reference in New Issue
Block a user