On save
This commit is contained in:
+58
-13
@@ -1,6 +1,6 @@
|
|||||||
pub mod transpiler;
|
pub mod transpiler;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::{Component, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
@@ -8,6 +8,8 @@ use tower_lsp::jsonrpc::Result;
|
|||||||
use tower_lsp::lsp_types::*;
|
use tower_lsp::lsp_types::*;
|
||||||
use tower_lsp::{Client, LanguageServer, LspService, Server};
|
use tower_lsp::{Client, LanguageServer, LspService, Server};
|
||||||
|
|
||||||
|
use crate::transpiler::transpile_file;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Backend {
|
struct Backend {
|
||||||
client: Client,
|
client: Client,
|
||||||
@@ -19,15 +21,25 @@ impl LanguageServer for Backend {
|
|||||||
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
|
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
|
||||||
let mut res = InitializeResult::default();
|
let mut res = InitializeResult::default();
|
||||||
|
|
||||||
res.capabilities.completion_provider = Some(CompletionOptions {
|
// res.capabilities.completion_provider = Some(CompletionOptions {
|
||||||
resolve_provider: None,
|
// resolve_provider: None,
|
||||||
trigger_characters: None,
|
// trigger_characters: None,
|
||||||
all_commit_characters: None,
|
// all_commit_characters: None,
|
||||||
work_done_progress_options: WorkDoneProgressOptions {
|
// work_done_progress_options: WorkDoneProgressOptions {
|
||||||
work_done_progress: None,
|
// work_done_progress: None,
|
||||||
|
// },
|
||||||
|
// completion_item: None,
|
||||||
|
// });
|
||||||
|
|
||||||
|
res.capabilities.text_document_sync = Some(TextDocumentSyncCapability::Options(
|
||||||
|
TextDocumentSyncOptions {
|
||||||
|
open_close: Some(true),
|
||||||
|
change: Some(TextDocumentSyncKind::INCREMENTAL),
|
||||||
|
will_save: Some(false),
|
||||||
|
will_save_wait_until: Some(false),
|
||||||
|
save: Some(SaveOptions::default().into()),
|
||||||
},
|
},
|
||||||
completion_item: None,
|
));
|
||||||
});
|
|
||||||
|
|
||||||
*self.workspace_folder.lock().await = params
|
*self.workspace_folder.lock().await = params
|
||||||
.workspace_folders
|
.workspace_folders
|
||||||
@@ -59,14 +71,24 @@ impl LanguageServer for Backend {
|
|||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn completion(&self, _params: CompletionParams) -> Result<Option<CompletionResponse>> {
|
async fn did_save(&self, params: DidSaveTextDocumentParams) {
|
||||||
self.client
|
self.client
|
||||||
.log_message(MessageType::INFO, "getting completion")
|
.log_message(MessageType::INFO, "getting completion")
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
Ok(Some(CompletionResponse::Array(vec![
|
let input_path = params
|
||||||
CompletionItem::new_simple("new".to_string(), "The new keyword".to_string()),
|
.text_document
|
||||||
])))
|
.uri
|
||||||
|
.to_file_path()
|
||||||
|
.expect("Invalid document path");
|
||||||
|
|
||||||
|
let output_path = from_mist_to_rust(input_path.clone());
|
||||||
|
|
||||||
|
self.client
|
||||||
|
.log_message(MessageType::INFO, output_path.display())
|
||||||
|
.await;
|
||||||
|
|
||||||
|
transpile_file(&input_path, &output_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn shutdown(&self) -> Result<()> {
|
async fn shutdown(&self) -> Result<()> {
|
||||||
@@ -85,3 +107,26 @@ pub async fn start() {
|
|||||||
});
|
});
|
||||||
Server::new(stdin, stdout, socket).serve(service).await;
|
Server::new(stdin, stdout, socket).serve(service).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<Component> = 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_file(path: &Path, output_path: &Path) {
|
pub fn transpile_file(path: &Path, output_path: &Path) {
|
||||||
// create parent directories
|
// create parent directories
|
||||||
if let Some(parent) = output_path.parent() {
|
if let Some(parent) = output_path.parent() {
|
||||||
if let Err(e) = fs::create_dir_all(parent) {
|
if let Err(e) = fs::create_dir_all(parent) {
|
||||||
|
|||||||
Reference in New Issue
Block a user