Skip to content

Commit

Permalink
handle document open and change in lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Sep 10, 2024
1 parent bd46a3d commit 2caae8b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 5 deletions.
33 changes: 33 additions & 0 deletions crates/son_of_anton/src/document_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::collections::HashMap;

use lsp_types::Uri;

pub struct DocumentStore {
store: HashMap<Uri, String>,
}

use anyhow::{anyhow, Result};

impl DocumentStore {
pub fn new() -> DocumentStore {
DocumentStore {
store: HashMap::new(),
}
}

pub fn put_document(&mut self, uri: Uri, document: String) {
self.store.insert(uri, document);
}

pub fn get_document(&self, uri: &Uri) -> Result<&String> {
self.store
.get(uri)
.ok_or_else(|| anyhow!("Document not found for URI: {:?}", uri))
}
}

impl Default for DocumentStore {
fn default() -> Self {
Self::new()
}
}
1 change: 1 addition & 0 deletions crates/son_of_anton/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod document_store;
pub mod logging;
pub mod lsp;
pub mod rpc;
23 changes: 20 additions & 3 deletions crates/son_of_anton/src/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
pub mod document_change;
mod document_open;
pub mod initialize;

use std::io::{Stdout, Write};
use std::process::exit;

use anyhow::anyhow;
use anyhow::Result;
use lsp_types::{InitializeParams, InitializedParams};
use lsp_types::{
DidChangeTextDocumentParams, DidOpenTextDocumentParams, InitializeParams, InitializedParams,
};
use serde::{de::DeserializeOwned, Serialize};
use serde_json::from_str;
use tracing::{event, Level};

use crate::document_store::DocumentStore;
use crate::rpc::{self, LspRequest, Request, Response};

pub struct SonOfAnton {
writer: Stdout,
document_store: DocumentStore,
}

impl SonOfAnton {
pub fn from(writer: Stdout) -> Self {
Self { writer }
Self {
writer,
document_store: DocumentStore::new(),
}
}

pub fn handle_message(&mut self, lsp_request: LspRequest) -> Result<()> {
Expand All @@ -36,6 +45,14 @@ impl SonOfAnton {
let resp = initialize::handle_initialize();
self.send_response(lsp_request, resp)?;
}
"textDocument/didOpen" => {
let params: DidOpenTextDocumentParams = deserialize_request(&lsp_request)?;
document_open::handle_open(params, self);
}
"textDocument/didChange" => {
let params: DidChangeTextDocumentParams = deserialize_request(&lsp_request)?;
document_change::handle_change(params, self)?;
}
"initialized" => {
let _ = deserialize_request::<InitializedParams>(&lsp_request)?;
event!(Level::INFO, "Server was initialized");
Expand Down Expand Up @@ -84,7 +101,7 @@ where
let deserialized_request: Request<T> = from_str(&lsp_request.content)?;
event!(
Level::DEBUG,
"Received request from server: {:?}",
"Received request from server: {:#?}",
deserialized_request
);
deserialized_request
Expand Down
17 changes: 17 additions & 0 deletions crates/son_of_anton/src/lsp/document_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use anyhow::anyhow;
use anyhow::Result;
use lsp_types::DidChangeTextDocumentParams;

use super::SonOfAnton;

pub fn handle_change(params: DidChangeTextDocumentParams, lsp: &mut SonOfAnton) -> Result<()> {
let document_change = params
.content_changes
.first()
.ok_or_else(|| anyhow!("Change event contained no changes"))?;

lsp.document_store
.put_document(params.text_document.uri, document_change.text.clone());

Ok(())
}
12 changes: 12 additions & 0 deletions crates/son_of_anton/src/lsp/document_open.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use lsp_types::DidOpenTextDocumentParams;
use tracing::{event, Level};

use super::SonOfAnton;

pub fn handle_open(params: DidOpenTextDocumentParams, lsp: &mut SonOfAnton) {
let uri = params.text_document.uri;
event!(Level::DEBUG, "Got open: {:?}", uri);

lsp.document_store
.put_document(uri, params.text_document.text);
}
6 changes: 5 additions & 1 deletion crates/son_of_anton/src/lsp/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use lsp_types::{HoverProviderCapability, InitializeResult, ServerCapabilities, ServerInfo};
use lsp_types::{
HoverProviderCapability, InitializeResult, ServerCapabilities, ServerInfo,
TextDocumentSyncCapability, TextDocumentSyncKind,
};

pub fn handle_initialize() -> InitializeResult {
InitializeResult {
capabilities: ServerCapabilities {
hover_provider: Some(HoverProviderCapability::Simple(true)),
text_document_sync: Some(TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL)),
..Default::default()
},
server_info: Some(ServerInfo {
Expand Down
1 change: 0 additions & 1 deletion crates/son_of_anton/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use tracing::{event, Level};

#[derive(Serialize, Deserialize, Debug)]
pub struct BaseMessage {
Expand Down

0 comments on commit 2caae8b

Please sign in to comment.