Skip to content

Commit

Permalink
fix: apply server configuration at initialization (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribru17 authored Dec 30, 2024
1 parent dce5db4 commit 6ba444a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
12 changes: 2 additions & 10 deletions src/handlers/did_change_configuration.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use log::warn;
use tower_lsp::lsp_types::DidChangeConfigurationParams;

use crate::{Backend, Options};
use crate::{util::set_configuration_options, Backend};

pub async fn did_change_configuration(backend: &Backend, params: DidChangeConfigurationParams) {
let Ok(changed_options) = serde_json::from_value::<Options>(params.settings) else {
warn!("Unable to parse configuration settings!",);
return;
};
let mut options = backend.options.write().unwrap();
options.parser_install_directories = changed_options.parser_install_directories;
options.parser_aliases = changed_options.parser_aliases;
options.language_retrieval_patterns = changed_options.language_retrieval_patterns;
set_configuration_options(backend, params.settings);
}

#[cfg(test)]
Expand Down
7 changes: 6 additions & 1 deletion src/handlers/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use log::{error, info};
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::{InitializeParams, InitializeResult, ServerInfo};

use crate::util::set_configuration_options;
use crate::{Backend, SERVER_CAPABILITIES};

pub async fn initialize(_backend: &Backend, params: InitializeParams) -> Result<InitializeResult> {
pub async fn initialize(backend: &Backend, params: InitializeParams) -> Result<InitializeResult> {
info!("ts_query_ls initialize: {params:?}");
if let Some(root_uri) = params.root_uri {
let root = PathBuf::from(root_uri.path());
Expand All @@ -15,6 +16,10 @@ pub async fn initialize(_backend: &Backend, params: InitializeParams) -> Result<
};
}

if let Some(init_options) = params.initialization_options {
set_configuration_options(backend, init_options);
}

Ok(InitializeResult {
capabilities: SERVER_CAPABILITIES.clone(),
server_info: Some(ServerInfo {
Expand Down
15 changes: 14 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use std::{
};

use lazy_static::lazy_static;
use log::warn;
use regex::Regex;
use ropey::Rope;
use serde_json::Value;
use streaming_iterator::StreamingIterator;
use tower_lsp::lsp_types::{
Diagnostic, DiagnosticSeverity, Location, Position, Range, TextDocumentContentChangeEvent,
Expand All @@ -18,7 +20,7 @@ use tree_sitter::{
QueryPredicateArg, TextProvider, Tree, WasmStore,
};

use crate::{SymbolInfo, QUERY_LANGUAGE};
use crate::{Backend, Options, SymbolInfo, QUERY_LANGUAGE};

lazy_static! {
static ref LINE_START: Regex = Regex::new(r"^([^\S\r\n]*)").unwrap();
Expand Down Expand Up @@ -602,3 +604,14 @@ pub fn format_iter(
}
}
}

pub fn set_configuration_options(backend: &Backend, options: Value) {
let Ok(parsed_options) = serde_json::from_value::<Options>(options) else {
warn!("Unable to parse configuration settings!",);
return;
};
let mut options = backend.options.write().unwrap();
options.parser_install_directories = parsed_options.parser_install_directories;
options.parser_aliases = parsed_options.parser_aliases;
options.language_retrieval_patterns = parsed_options.language_retrieval_patterns;
}

0 comments on commit 6ba444a

Please sign in to comment.