Working formatter #90

This commit is contained in:
2026-06-26 01:02:01 +02:00
parent fda193b8ff
commit e5509e89a4
3 changed files with 72 additions and 27 deletions
+53 -4
View File
@@ -16,14 +16,33 @@ use tower_lsp::lsp_types::{self, *};
use tower_lsp::{Client, LanguageServer, LspService, Server};
use crate::rust_analyzer::RustAnalyzer;
use crate::transpiler::{TranspileError, transpile_mist, transpile_mist_no_sem};
use crate::transpiler::{TranspileError, format_mist, transpile_mist, transpile_mist_no_sem};
static MARKER_COUNTER: AtomicU64 = AtomicU64::new(0);
const KEYWORDS: [&'static str; 22] = [
"if", "else", "for", "while", "match", "return", "break", "continue ", "struct ", "enum ",
"class ", "trait ", "impl ", "pub ", "mut ", "let ", "dyn ", "loop", "unsafe",
"override", "module ", "void ",
"if",
"else",
"for",
"while",
"match",
"return",
"break",
"continue ",
"struct ",
"enum ",
"class ",
"trait ",
"impl ",
"pub ",
"mut ",
"let ",
"dyn ",
"loop",
"unsafe",
"override",
"module ",
"void ",
];
fn keyword_completion_items() -> impl Iterator<Item = CompletionItem> {
@@ -666,6 +685,8 @@ impl LanguageServer for Backend {
},
));
res.capabilities.document_formatting_provider = Some(OneOf::Left(true));
res.capabilities.completion_provider = Some(CompletionOptions {
resolve_provider: Some(false),
trigger_characters: Some(vec![
@@ -1256,6 +1277,34 @@ impl LanguageServer for Backend {
}
}
}
async fn formatting(
&self,
params: DocumentFormattingParams,
) -> tower_lsp::jsonrpc::Result<Option<Vec<TextEdit>>> {
let mist_path = params
.text_document
.uri
.to_file_path()
.map_err(|_| tower_lsp::jsonrpc::Error::internal_error())?;
let docs = self.documents.lock().await;
let old_text = docs
.get(&mist_path)
.map_or_else(|| Err(tower_lsp::jsonrpc::Error::internal_error()), Ok)?;
let new_text = format_mist(&old_text.to_string())
.map_err(|_| tower_lsp::jsonrpc::Error::internal_error())?;
Ok(Some(vec![TextEdit {
new_text,
range: Range {
start: Position::new(0, 0),
end: Position::new(u32::MAX, u32::MAX),
},
}]))
}
}
fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<CompletionItem>) {
+11
View File
@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};
use mist_codegen::fmt::MistCodegen;
use mist_codegen::RustCodegen;
use mist_parser::parse;
use mist_parser::rev_mapper::Mapping;
@@ -83,3 +84,13 @@ pub fn transpile_mist_no_sem(
mapping: codegen.mapping,
})
}
pub fn format_mist(source: &str) -> Result<String, String> {
let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?;
let mut codegen = MistCodegen::new();
codegen.generate(parsed.mod_attributes);
let output = codegen.generate(parsed.items);
Ok(output)
}