diff --git a/gui/src/buffer.rs b/gui/src/buffer.rs index ecdae4c..943c4d6 100644 --- a/gui/src/buffer.rs +++ b/gui/src/buffer.rs @@ -8,15 +8,25 @@ pub enum Buffer { File(FileBuffer), } +// TODO remove public #[derive(Debug, Default)] pub struct FileBuffer { - pub path: Option, pub content: text_editor::Content, // This causes a bug because [`Content`] can create exact replicas of a text - // editor so scrolling and cursor locations are the same on each. Currently the only way I see - // this changing is making a custom text editor widget. + // editor so scrolling and cursor locations are the same on each. Currently the only way I see + // this changing is making a custom text editor widget. + dirty: bool, + pub path: Option, } impl FileBuffer { + pub fn new(content: text_editor::Content, path: Option) -> Self { + Self { + content, + path, + ..Default::default() + } + } + pub fn to_file(&self) -> File { (self.path.clone(), self.content.text().to_string()) } @@ -25,8 +35,9 @@ impl FileBuffer { impl From for FileBuffer { fn from((path, content): File) -> Self { Self { - path, content: text_editor::Content::with_text(&content), + path, + ..Default::default() } } } diff --git a/gui/src/lib.rs b/gui/src/lib.rs index aa45180..9e220ec 100644 --- a/gui/src/lib.rs +++ b/gui/src/lib.rs @@ -1,4 +1,5 @@ -use iced::{application, exit, font, widget::column, Element, Task, Theme}; +use iced::{application, exit, font, futures::FutureExt, widget::column, Element, Task, Theme}; +use kuiper_lsp::client::LSPClient; use slotmap::{DefaultKey, SlotMap}; use std::path::PathBuf; @@ -45,6 +46,9 @@ impl Kuiper { Task::batch(vec![ font::load(iced_aw::BOOTSTRAP_FONT_BYTES).map(Message::FontLoaded), font::load(iced_aw::NERD_FONT_BYTES).map(Message::FontLoaded), + // TODO lazy load lsp start up + Task::perform(LSPClient::initialize(), lsp::Message::Initalize) + .map(Message::LanguageServer), ]), ) } @@ -64,10 +68,21 @@ impl Kuiper { if let Some(action) = toolbar::update(message) { match action { toolbar::Action::InsertFileBuffer(buffer) => { + let path = buffer.path.clone().unwrap(); let key = self.data.insert(buffer.into()); self.panes .active_pane_mut() .map(|pane| pane.insert_buffer(key)); + + // Blindly tell lsp every file is opened we want to send to it + // if let Some(client) = &mut self.lsp_client { + // return Task::perform( + // client.did_open(path), + // lsp::Syncronize::DidOpen, + // ) + // .map(lsp::Message::Syncronize) + // .map(Message::LanguageServer); + // } } toolbar::Action::SetWorkspacePath(path) => { self.workspace_folder = Some(path); @@ -114,7 +129,7 @@ impl Kuiper { // Task::perform(shutdown(client.clone().socket.clone()), |_| { // Message::LanguageServer(LanguageServer::Shutdown()) // }); - tracing::info!("Shutting down lsp"); + // tracing::info!("Shutting down lsp"); // tasks.push(task); } diff --git a/gui/src/lsp.rs b/gui/src/lsp.rs index 00babcf..24d87d5 100644 --- a/gui/src/lsp.rs +++ b/gui/src/lsp.rs @@ -13,7 +13,7 @@ pub enum Message { pub enum Syncronize { DidClose, DidChange, - DidOpen(), + DidOpen(Result<(), kuiper_lsp::Error>), } pub fn update(lsp_client: &mut Option, message: Message) -> Task { @@ -29,8 +29,9 @@ pub fn update(lsp_client: &mut Option, message: Message) -> Task { if let Some(client) = lsp_client { + // TODO it doesn't currently work lmao + // this is a future, but we're in a non-async block client.shutdown(); - todo!("Implement LSP shutdown"); } } Message::Syncronize(sync) => { @@ -42,7 +43,7 @@ pub fn update(lsp_client: &mut Option, message: Message) -> Task { eprintln!("DidChange not implemented"); } - Syncronize::DidOpen() => { + Syncronize::DidOpen(_) => { eprintln!("DidOpen not implemented"); } } diff --git a/lsp/src/client.rs b/lsp/src/client.rs index 0724c19..0a83368 100644 --- a/lsp/src/client.rs +++ b/lsp/src/client.rs @@ -150,34 +150,23 @@ impl LSPClient { pub async fn did_change() -> Result { todo!() } - pub async fn did_open( - path: PathBuf, - mut server: ServerSocket, - ) -> Result { + pub async fn did_open(&mut self, path: PathBuf) -> Result<(), crate::Error> { let text = tokio::fs::read_to_string(path.clone()).await.unwrap(); // TODO We already have the text else where in the program, // so we shouldn't need to re-read it. let uri = Url::from_file_path(path).unwrap(); - match server.did_open(DidOpenTextDocumentParams { - text_document: TextDocumentItem { - uri, - language_id: "rust".into(), - version: 0, - text, - }, - }) { - Ok(_) => Ok(server.clone()), - Err(e) => Err(e).context(LspSnafu), - } + self.socket + .did_open(DidOpenTextDocumentParams { + text_document: TextDocumentItem { + uri, + language_id: "rust".into(), + version: 0, + text, + }, + }) + .context(LspSnafu)?; + + Ok(()) } } - -// impl Drop for LSPClient { -// fn drop(&mut self) { -// error!("we are dropping"); -// Runtime::new() -// .expect("Create tokio runtime") -// .block_on(shutdown(self.socket.clone())); -// } -// }