Cleaned up lsp
This commit is contained in:
+1
-372
@@ -1,6 +1,4 @@
|
||||
pub mod builder;
|
||||
pub mod rust_analyzer;
|
||||
pub mod transpiler;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::{Component, PathBuf};
|
||||
@@ -10,11 +8,9 @@ use mist_parser::rev_mapper;
|
||||
use ropey::Rope;
|
||||
use tokio::sync::Mutex;
|
||||
use tower_lsp::jsonrpc::Result;
|
||||
use tower_lsp::lsp_types::notification::Notification;
|
||||
use tower_lsp::lsp_types::{self, *};
|
||||
use tower_lsp::{Client, LanguageServer, LspService, Server};
|
||||
|
||||
use crate::builder::MistDiagnostic;
|
||||
use crate::rust_analyzer::RustAnalyzer;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -143,8 +139,6 @@ impl LanguageServer for Backend {
|
||||
if let Some(root) = &*workspace_folder.lock().await {
|
||||
let src_root = root.join("src");
|
||||
|
||||
transpiler::build(root);
|
||||
|
||||
analyzer
|
||||
.lock()
|
||||
.await
|
||||
@@ -157,23 +151,7 @@ impl LanguageServer for Backend {
|
||||
collect_mist_files(&src_root, &mut files);
|
||||
|
||||
for file in files {
|
||||
if let Ok(text) = std::fs::read_to_string(&file) {
|
||||
if let Ok(transpiled) = transpiler::transpile_text(&text) {
|
||||
let rust_path = from_mist_to_rust(file.clone());
|
||||
|
||||
// store document
|
||||
documents
|
||||
.lock()
|
||||
.await
|
||||
.insert(file.clone(), Rope::from_str(&text));
|
||||
|
||||
// store mapping
|
||||
// mapping
|
||||
// .lock()
|
||||
// .await
|
||||
// .insert(rust_path, rev_mapper::get_mapping(&transpiled));
|
||||
}
|
||||
}
|
||||
if let Ok(text) = std::fs::read_to_string(&file) {}
|
||||
}
|
||||
|
||||
eprintln!("Ready to use");
|
||||
@@ -199,355 +177,6 @@ impl LanguageServer for Backend {
|
||||
async fn shutdown(&self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn did_save(&self, _: DidSaveTextDocumentParams) {
|
||||
self.client
|
||||
.log_message(MessageType::INFO, "Processing did_save event")
|
||||
.await;
|
||||
|
||||
if let Some(root) = &*self.workspace_folder.lock().await {
|
||||
transpiler::build(root);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
let diagnostics_raw = builder::build(
|
||||
vec![
|
||||
"check".to_string(),
|
||||
"--workspace".to_string(),
|
||||
"--all-targets".to_string(),
|
||||
],
|
||||
workspace_root,
|
||||
);
|
||||
|
||||
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(_) => {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let line = (msg.line as u32).saturating_sub(1);
|
||||
let column = (msg.column as u32).saturating_sub(1);
|
||||
|
||||
let url = match clean_lsp_url(&msg.file_path) {
|
||||
Some(u) => u,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let diagnostic_item = Diagnostic {
|
||||
range: Range {
|
||||
start: Position {
|
||||
line,
|
||||
character: column,
|
||||
},
|
||||
end: Position {
|
||||
line,
|
||||
character: u32::MAX,
|
||||
},
|
||||
},
|
||||
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,
|
||||
};
|
||||
|
||||
diagnostics
|
||||
.entry(url)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(diagnostic_item);
|
||||
}
|
||||
|
||||
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 did_open(&self, mut params: DidOpenTextDocumentParams) {
|
||||
if params.text_document.language_id == "mist" {
|
||||
let original_text = params.text_document.text.clone();
|
||||
|
||||
self.documents.lock().await.insert(
|
||||
params.text_document.uri.to_file_path().unwrap(),
|
||||
Rope::from_str(&original_text),
|
||||
);
|
||||
|
||||
params.text_document.language_id = "rust".to_string();
|
||||
|
||||
match transpiler::transpile_text(&original_text) {
|
||||
Ok(transpiled_text) => {
|
||||
params.text_document.text = transpiled_text;
|
||||
|
||||
let rust_path =
|
||||
from_mist_to_rust(params.text_document.uri.to_file_path().unwrap());
|
||||
|
||||
params.text_document.uri = Url::from_file_path(&rust_path).unwrap();
|
||||
|
||||
// self.mapping.lock().await.insert(
|
||||
// rust_path,
|
||||
// rev_mapper::get_mapping(¶ms.text_document.text),
|
||||
// );
|
||||
}
|
||||
Err(e) => {
|
||||
self.client
|
||||
.log_message(
|
||||
MessageType::WARNING,
|
||||
format!("MIST-LSP: Syntax invalid during open/change: {:?}", e),
|
||||
)
|
||||
.await;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(mut ra) = self.rust_analyzer.try_lock() {
|
||||
let _ = ra
|
||||
.notify(notification::DidOpenTextDocument::METHOD, params)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn did_change(&self, mut params: DidChangeTextDocumentParams) {
|
||||
let mist_path = params.text_document.uri.to_file_path().unwrap();
|
||||
|
||||
let rust_path = from_mist_to_rust(mist_path.clone());
|
||||
|
||||
let rust_uri = Url::from_file_path(&rust_path).unwrap();
|
||||
|
||||
if let Some(change) = params.content_changes.first_mut() {
|
||||
self.documents
|
||||
.lock()
|
||||
.await
|
||||
.insert(mist_path, Rope::from_str(&change.text));
|
||||
|
||||
match transpiler::transpile_text(&change.text) {
|
||||
Ok(transpiled_text) => {
|
||||
change.text = transpiled_text;
|
||||
|
||||
// self.mapping
|
||||
// .lock()
|
||||
// .await
|
||||
// .insert(rust_path, rev_mapper::get_mapping(&change.text));
|
||||
}
|
||||
Err(e) => {
|
||||
self.client
|
||||
.log_message(
|
||||
MessageType::WARNING,
|
||||
format!("MIST-LSP: Syntax invalid during change: {:?}", e),
|
||||
)
|
||||
.await;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
params.text_document.uri = rust_uri;
|
||||
|
||||
if let Ok(mut ra) = self.rust_analyzer.try_lock() {
|
||||
let _ = ra
|
||||
.notify(notification::DidChangeTextDocument::METHOD, params)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn did_close(&self, mut params: DidCloseTextDocumentParams) {
|
||||
self.client
|
||||
.log_message(MessageType::INFO, "MIST-LSP: Processing did_close event")
|
||||
.await;
|
||||
|
||||
let rust_path = from_mist_to_rust(params.text_document.uri.to_file_path().unwrap());
|
||||
|
||||
let mist_path = params.text_document.uri.to_file_path().unwrap();
|
||||
|
||||
self.documents.lock().await.remove(&mist_path);
|
||||
|
||||
self.mapping.lock().await.remove(&rust_path);
|
||||
|
||||
params.text_document.uri = Url::from_file_path(&rust_path).unwrap();
|
||||
|
||||
if let Ok(mut ra) = self.rust_analyzer.try_lock() {
|
||||
let _ = ra
|
||||
.notify(notification::DidCloseTextDocument::METHOD, params)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn goto_definition(
|
||||
&self,
|
||||
params: GotoDefinitionParams,
|
||||
) -> Result<Option<GotoDefinitionResponse>> {
|
||||
let file_path = params
|
||||
.text_document_position_params
|
||||
.text_document
|
||||
.uri
|
||||
.to_file_path()
|
||||
.unwrap();
|
||||
|
||||
let source = match self.documents.lock().await.get(&file_path) {
|
||||
Some(doc) => doc.clone(),
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let inject = "__mist_23";
|
||||
let injected_source = insert_at_position(
|
||||
&source,
|
||||
params.text_document_position_params.position.line as usize + 1,
|
||||
params.text_document_position_params.position.character as usize,
|
||||
&inject,
|
||||
);
|
||||
|
||||
let output = Rope::from_str(
|
||||
&match transpiler::transpile_text(&injected_source.to_string()) {
|
||||
Ok(out) => out,
|
||||
Err(_) => return Ok(None),
|
||||
},
|
||||
);
|
||||
|
||||
let (line, character) = match find_row_col(&output, inject) {
|
||||
Some(coords) => coords,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let uri =
|
||||
Url::from_file_path(from_mist_to_rust(file_path)).expect("failed to generate rs url");
|
||||
|
||||
let rs_res = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.request::<request::GotoDefinition>(lsp_types::GotoDefinitionParams {
|
||||
text_document_position_params: lsp_types::TextDocumentPositionParams {
|
||||
position: lsp_types::Position {
|
||||
line: line as u32,
|
||||
character: character as u32,
|
||||
},
|
||||
text_document: lsp_types::TextDocumentIdentifier { uri },
|
||||
},
|
||||
partial_result_params: lsp_types::PartialResultParams::default(),
|
||||
work_done_progress_params: lsp_types::WorkDoneProgressParams::default(),
|
||||
})
|
||||
.await
|
||||
.expect("Failed to send to rust");
|
||||
|
||||
Ok(match rs_res {
|
||||
Some(GotoDefinitionResponse::Array(arr)) => {
|
||||
let mut mapped_arr = Vec::new();
|
||||
for rs_loc in arr {
|
||||
let mapped_loc = self.get_mist_location(rs_loc).await;
|
||||
mapped_arr.push(mapped_loc);
|
||||
}
|
||||
Some(GotoDefinitionResponse::Array(mapped_arr))
|
||||
}
|
||||
_ => rs_res,
|
||||
})
|
||||
}
|
||||
|
||||
async fn completion(&self, mut params: CompletionParams) -> Result<Option<CompletionResponse>> {
|
||||
self.client
|
||||
.log_message(MessageType::INFO, "COMPLETEING!")
|
||||
.await;
|
||||
|
||||
let file_path = params
|
||||
.text_document_position
|
||||
.text_document
|
||||
.uri
|
||||
.to_file_path()
|
||||
.unwrap();
|
||||
|
||||
let source = match self.documents.lock().await.get(&file_path) {
|
||||
Some(doc) => doc.clone(),
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let inject = "__mist_23";
|
||||
let injected_source = insert_at_position(
|
||||
&source,
|
||||
params.text_document_position.position.line as usize + 1,
|
||||
params.text_document_position.position.character as usize,
|
||||
&inject,
|
||||
);
|
||||
|
||||
let output = Rope::from_str(
|
||||
&match transpiler::transpile_text(&injected_source.to_string()) {
|
||||
Ok(out) => out,
|
||||
Err(_) => return Ok(None),
|
||||
},
|
||||
);
|
||||
|
||||
let (line, character) = match find_row_col(&output, inject) {
|
||||
Some(coords) => coords,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let uri =
|
||||
Url::from_file_path(from_mist_to_rust(file_path)).expect("failed to generate rs url");
|
||||
|
||||
params.text_document_position.text_document.uri = uri;
|
||||
params.text_document_position.position.line = line as u32;
|
||||
params.text_document_position.position.character = character as u32;
|
||||
|
||||
let rs_res = self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.request::<request::Completion>(params)
|
||||
.await
|
||||
.expect("Failed to send to rust");
|
||||
|
||||
Ok(rs_res.map(|rs_res| match rs_res {
|
||||
CompletionResponse::Array(items) => {
|
||||
CompletionResponse::Array(items.into_iter().map(simplify_item).collect())
|
||||
}
|
||||
|
||||
CompletionResponse::List(list) => {
|
||||
CompletionResponse::Array(list.items.into_iter().map(simplify_item).collect())
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
async fn completion_resolve(&self, params: CompletionItem) -> Result<CompletionItem> {
|
||||
match self
|
||||
.rust_analyzer
|
||||
.lock()
|
||||
.await
|
||||
.request::<request::ResolveCompletionItem>(params.clone())
|
||||
.await
|
||||
{
|
||||
Ok(o) => Ok(o),
|
||||
_ => Ok(params),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
Reference in New Issue
Block a user