Skip to content

Commit

Permalink
Move All Configuration Loading into initialize (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
InsertCreativityHere authored Jan 16, 2024
1 parent 1ce3bf2 commit e19a9a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
4 changes: 4 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export async function activate(context: ExtensionContext) {

const serverOptions: ServerOptions = { run, debug: run };

const config = workspace.getConfiguration("slice");
const configuration_sets = config.get<any[]>("configurations");

// Configure the language client options.
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: "file", language: "slice" }],
Expand All @@ -161,6 +164,7 @@ export async function activate(context: ExtensionContext) {
revealOutputChannelOn: RevealOutputChannelOn.Never,
initializationOptions: {
builtInSlicePath: builtInSlicePath,
configurations: configuration_sets,
},
};

Expand Down
6 changes: 1 addition & 5 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ impl LanguageServer for Backend {
}

async fn initialized(&self, _: InitializedParams) {
// Update the configuration sets by fetching the configurations from the client. This is performed after
// initialization because the client may not be ready to provide configurations before initialization.
self.session.fetch_configurations(&self.client).await;

// Publish the diagnostics for all files
// Now that the server and client are fully initialized, it's safe to publish any diagnostics we've found.
publish_diagnostics(&self.client, &self.session.configuration_sets).await;
}

Expand Down
52 changes: 15 additions & 37 deletions server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

use crate::configuration_set::ConfigurationSet;
use tokio::sync::{Mutex, RwLock};
use tower_lsp::{
lsp_types::{ConfigurationItem, DidChangeConfigurationParams, Url},
Client,
};
use tower_lsp::lsp_types::{DidChangeConfigurationParams, Url};

pub struct Session {
/// This vector contains all of the configuration sets for the language server. Each element is a tuple containing
Expand All @@ -32,14 +29,15 @@ impl Session {
&self,
params: tower_lsp::lsp_types::InitializeParams,
) {
let initialization_options = params.initialization_options;

// This is the path to the built-in Slice files that are included with the extension. It should always
// be present.
let built_in_slice_path = params
.initialization_options
.and_then(|opts| opts.get("builtInSlicePath").cloned())
let built_in_slice_path = initialization_options
.as_ref()
.and_then(|opts| opts.get("builtInSlicePath"))
.and_then(|v| v.as_str().map(str::to_owned))
.expect("builtInSlicePath not found in initialization options");
*self.built_in_slice_path.write().await = built_in_slice_path;

// Use the root_uri if it exists temporarily as we cannot access configuration until
// after initialization. Additionally, LSP may provide the windows path with escaping or a lowercase
Expand All @@ -49,38 +47,18 @@ impl Session {
.and_then(|uri| uri.to_file_path().ok())
.and_then(|path| Url::from_file_path(path).ok())
.expect("root_uri not found in initialization parameters");
*self.root_uri.write().await = Some(root_uri);
}

// Update the stored configuration sets by fetching them from the client.
pub async fn fetch_configurations(&self, client: &Client) {
let built_in_path = &self.built_in_slice_path.read().await;
let root_uri_guard = self.root_uri.read().await;
let root_uri = (*root_uri_guard).clone().expect("root_uri not set");

let params = vec![ConfigurationItem {
scope_uri: None,
section: Some("slice.configurations".to_string()),
}];

// Fetch the configurations from the client and parse them.
let configurations = client
.configuration(params)
.await
.ok()
.map(|response| {
let config_array = &response
.iter()
.filter_map(|config| config.as_array())
.flatten()
.cloned()
.collect::<Vec<_>>();
ConfigurationSet::parse_configuration_sets(config_array, &root_uri, built_in_path)
})
// Load any user configuration from the 'slice.configurations' option.
let configuration_sets = initialization_options
.as_ref()
.and_then(|opts| opts.get("configuration"))
.and_then(|v| v.as_array())
.map(|arr| ConfigurationSet::parse_configuration_sets(arr, &root_uri, &built_in_slice_path))
.unwrap_or_default();

// Update the configuration sets
self.update_configurations(configurations).await;
*self.built_in_slice_path.write().await = built_in_slice_path;
*self.root_uri.write().await = Some(root_uri);
self.update_configurations(configuration_sets).await;
}

// Update the configuration sets from the `DidChangeConfigurationParams` notification.
Expand Down

0 comments on commit e19a9a2

Please sign in to comment.