Skip to content

Commit

Permalink
init on process start
Browse files Browse the repository at this point in the history
  • Loading branch information
Redhawk18 committed Feb 4, 2025
1 parent 9d1dd44 commit c479534
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
19 changes: 15 additions & 4 deletions gui/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ pub enum Buffer {
File(FileBuffer),
}

// TODO remove public
#[derive(Debug, Default)]
pub struct FileBuffer {
pub path: Option<PathBuf>,
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<PathBuf>,
}

impl FileBuffer {
pub fn new(content: text_editor::Content, path: Option<PathBuf>) -> Self {
Self {
content,
path,
..Default::default()
}
}

pub fn to_file(&self) -> File {
(self.path.clone(), self.content.text().to_string())
}
Expand All @@ -25,8 +35,9 @@ impl FileBuffer {
impl From<File> for FileBuffer {
fn from((path, content): File) -> Self {
Self {
path,
content: text_editor::Content::with_text(&content),
path,
..Default::default()
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions gui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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),
]),
)
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 4 additions & 3 deletions gui/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LSPClient>, message: Message) -> Task<Message> {
Expand All @@ -29,8 +29,9 @@ pub fn update(lsp_client: &mut Option<LSPClient>, message: Message) -> Task<Mess
},
Message::Shutdown => {
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) => {
Expand All @@ -42,7 +43,7 @@ pub fn update(lsp_client: &mut Option<LSPClient>, message: Message) -> Task<Mess
Syncronize::DidChange => {
eprintln!("DidChange not implemented");
}
Syncronize::DidOpen() => {
Syncronize::DidOpen(_) => {
eprintln!("DidOpen not implemented");
}
}
Expand Down
37 changes: 13 additions & 24 deletions lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,23 @@ impl LSPClient {
pub async fn did_change() -> Result<ServerSocket, crate::Error> {
todo!()
}
pub async fn did_open(
path: PathBuf,
mut server: ServerSocket,
) -> Result<ServerSocket, crate::Error> {
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()));
// }
// }

0 comments on commit c479534

Please sign in to comment.