Skip to content

Commit

Permalink
reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust committed Feb 29, 2024
1 parent c1209e2 commit d91f05f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 75 deletions.
83 changes: 10 additions & 73 deletions crates/lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::path::PathBuf;
use dashmap::DashMap;
use tokio::sync::RwLock;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::notification::Notification;
use tower_lsp::lsp_types as lsp;
use tower_lsp::{Client, LanguageServer, LspService, Server};
use witcherscript::Script;
Expand Down Expand Up @@ -41,7 +40,6 @@ impl Backend {

content_graph: RwLock::new(ContentGraph::new()),
// source_trees: DashMap::new(),
//TODO a collection tracking errors for/in files, so error diagnostics don't dangle when these files get forgotten about

doc_buffers: DashMap::new(),
scripts: DashMap::new()
Expand All @@ -52,100 +50,39 @@ impl Backend {
#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, params: lsp::InitializeParams) -> Result<lsp::InitializeResult> {
if let Some(workspace_folders) = params.workspace_folders {
let mut workspace_roots = self.workspace_roots.write().await;
*workspace_roots = workspace_folders.into_iter()
.map(|f| f.uri.to_file_path().unwrap())
.collect();
}

Ok(lsp::InitializeResult {
server_info: Some(lsp::ServerInfo {
name: Backend::SERVER_NAME.into(),
version: Some(env!("CARGO_PKG_VERSION").into())
}),
capabilities: lsp::ServerCapabilities {
text_document_sync: Some(lsp::TextDocumentSyncCapability::Options(lsp::TextDocumentSyncOptions {
open_close: Some(true),
change: Some(lsp::TextDocumentSyncKind::INCREMENTAL),
will_save: Some(false),
will_save_wait_until: Some(false),
save: Some(lsp::TextDocumentSyncSaveOptions::SaveOptions(lsp::SaveOptions {
include_text: Some(false)
}))
})),
workspace: Some(lsp::WorkspaceServerCapabilities {
workspace_folders: Some(lsp::WorkspaceFoldersServerCapabilities {
supported: Some(true),
change_notifications: Some(lsp::OneOf::Left(true)),
}),
//TODO file operations notifiction config to know if content graph changed in some way
file_operations: None
}),
..lsp::ServerCapabilities::default()
}
})
providers::initialization::initialize(self, params).await
}

async fn initialized(&self, _: lsp::InitializedParams) {
self.log_info("Server initialized!").await;

self.client.register_capability(vec![
lsp::Registration {
id: lsp::notification::DidChangeConfiguration::METHOD.to_string(),
method: lsp::notification::DidChangeConfiguration::METHOD.to_string(),
register_options: None
}
]).await.unwrap();

self.fetch_config().await;

self.scan_content_repositories().await;
self.scan_workspace_projects().await;
self.build_content_graph().await;
async fn initialized(&self, params: lsp::InitializedParams) {
providers::initialization::initialized(self, params).await
}

async fn shutdown(&self) -> Result<()> {
Ok(())
}

async fn did_open(&self, params: lsp::DidOpenTextDocumentParams) {
providers::document_ops::did_open(self, params).await;
providers::document_ops::did_open(self, params).await
}

async fn did_change(&self, params: lsp::DidChangeTextDocumentParams) {
providers::document_ops::did_change(self, params).await;
providers::document_ops::did_change(self, params).await
}

async fn did_save(&self, params: lsp::DidSaveTextDocumentParams) {
providers::document_ops::did_save(self, params).await;
providers::document_ops::did_save(self, params).await
}

async fn did_close(&self, params: lsp::DidCloseTextDocumentParams) {
providers::document_ops::did_close(self, params).await;
providers::document_ops::did_close(self, params).await
}

async fn did_change_configuration(&self, _: lsp::DidChangeConfigurationParams) {
self.fetch_config().await;
self.scan_content_repositories().await;
self.build_content_graph().await;
async fn did_change_configuration(&self, params: lsp::DidChangeConfigurationParams) {
providers::configuration::did_change_configuration(self, params).await
}

async fn did_change_workspace_folders(&self, params: lsp::DidChangeWorkspaceFoldersParams) {
let added: Vec<_> = params.event.added.into_iter()
.map(|f| f.uri.to_file_path().unwrap())
.collect();

let removed: Vec<_> = params.event.removed.into_iter()
.map(|f| f.uri.to_file_path().unwrap())
.collect();

let mut workspace_roots = self.workspace_roots.write().await;
workspace_roots.retain(|p| !removed.contains(p));
workspace_roots.extend(added);

self.scan_workspace_projects().await;
self.build_content_graph().await;
providers::workspace::did_change_workspace_folders(self, params).await
}
}

Expand Down
11 changes: 11 additions & 0 deletions crates/lsp/src/providers/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use tower_lsp::lsp_types as lsp;
use crate::Backend;


pub async fn did_change_configuration(backend: &Backend, _: lsp::DidChangeConfigurationParams) {
backend.clear_all_diagnostics().await.unwrap();

backend.fetch_config().await;
backend.scan_content_repositories().await;
backend.build_content_graph().await;
}
59 changes: 59 additions & 0 deletions crates/lsp/src/providers/initialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use tower_lsp::lsp_types::notification::Notification;
use tower_lsp::lsp_types as lsp;
use tower_lsp::jsonrpc::Result;
use crate::Backend;


pub async fn initialize(backend: &Backend, params: lsp::InitializeParams) -> Result<lsp::InitializeResult> {
if let Some(workspace_folders) = params.workspace_folders {
let mut workspace_roots = backend.workspace_roots.write().await;
*workspace_roots = workspace_folders.into_iter()
.map(|f| f.uri.to_file_path().unwrap())
.collect();
}

Ok(lsp::InitializeResult {
server_info: Some(lsp::ServerInfo {
name: Backend::SERVER_NAME.into(),
version: Some(env!("CARGO_PKG_VERSION").into())
}),
capabilities: lsp::ServerCapabilities {
text_document_sync: Some(lsp::TextDocumentSyncCapability::Options(lsp::TextDocumentSyncOptions {
open_close: Some(true),
change: Some(lsp::TextDocumentSyncKind::INCREMENTAL),
will_save: Some(false),
will_save_wait_until: Some(false),
save: Some(lsp::TextDocumentSyncSaveOptions::SaveOptions(lsp::SaveOptions {
include_text: Some(false)
}))
})),
workspace: Some(lsp::WorkspaceServerCapabilities {
workspace_folders: Some(lsp::WorkspaceFoldersServerCapabilities {
supported: Some(true),
change_notifications: Some(lsp::OneOf::Left(true)),
}),
//TODO file operations notifiction config to know if content graph changed in some way
file_operations: None
}),
..lsp::ServerCapabilities::default()
}
})
}

pub async fn initialized(backend: &Backend, _: lsp::InitializedParams) {
backend.log_info("Server initialized!").await;

backend.client.register_capability(vec![
lsp::Registration {
id: lsp::notification::DidChangeConfiguration::METHOD.to_string(),
method: lsp::notification::DidChangeConfiguration::METHOD.to_string(),
register_options: None
}
]).await.unwrap();

backend.fetch_config().await;

backend.scan_content_repositories().await;
backend.scan_workspace_projects().await;
backend.build_content_graph().await;
}
8 changes: 7 additions & 1 deletion crates/lsp/src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
pub mod document_ops;
/// Modules providing implementations for [`Backend`]'s LanguageServer methods.
/// Made for better code organization purposes.
pub mod initialization;
pub mod document_ops;
pub mod configuration;
pub mod workspace;
24 changes: 24 additions & 0 deletions crates/lsp/src/providers/workspace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use tower_lsp::lsp_types as lsp;
use crate::Backend;


pub async fn did_change_workspace_folders(backend: &Backend, params: lsp::DidChangeWorkspaceFoldersParams) {
let added: Vec<_> = params.event.added.into_iter()
.map(|f| f.uri.to_file_path().unwrap())
.collect();

let removed: Vec<_> = params.event.removed.into_iter()
.map(|f| f.uri.to_file_path().unwrap())
.collect();

{
let mut workspace_roots = backend.workspace_roots.write().await;
workspace_roots.retain(|p| !removed.contains(p));
workspace_roots.extend(added);
}

backend.clear_all_diagnostics().await.unwrap();

backend.scan_workspace_projects().await;
backend.build_content_graph().await;
}
6 changes: 5 additions & 1 deletion crates/lsp/src/reporting/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::PathBuf;
use tower_lsp::lsp_types as lsp;
use tower_lsp::{jsonrpc, lsp_types as lsp};
use witcherscript_analysis::diagnostics::{Diagnostic, DiagnosticBody};
use witcherscript_project::{manifest::ManifestParseError, FileError};
use crate::Backend;
Expand Down Expand Up @@ -103,4 +103,8 @@ impl Backend {
self.client.publish_diagnostics(url, Vec::new(), None).await;
}
}

pub async fn clear_all_diagnostics(&self) -> jsonrpc::Result<()> {
self.client.workspace_diagnostic_refresh().await
}
}

0 comments on commit d91f05f

Please sign in to comment.