Skip to content

Commit

Permalink
Store Tricks in a static container
Browse files Browse the repository at this point in the history
commit-id:a1e078b6
  • Loading branch information
integraledelebesgue committed Jan 21, 2025
1 parent db4158f commit 58c95c5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/lang/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use self::semantic::*;
pub use self::swapper::*;
pub use self::syntax::*;
use super::proc_macros::db::{ProcMacroDatabase, init_proc_macro_group};
use crate::Tricks;
use crate::TRICKS;

mod semantic;
mod swapper;
Expand All @@ -49,7 +49,7 @@ pub struct AnalysisDatabase {

impl AnalysisDatabase {
/// Creates a new instance of the database.
pub fn new(tricks: &Tricks) -> Self {
pub fn new() -> Self {
let mut db = Self { storage: Default::default() };

init_files_group(&mut db);
Expand All @@ -60,6 +60,8 @@ impl AnalysisDatabase {

db.set_cfg_set(Self::initial_cfg_set().into());

let tricks = TRICKS.get_or_init(Default::default);

let plugin_suite = [
get_default_plugin_suite(),
starknet_plugin_suite(),
Expand Down
8 changes: 3 additions & 5 deletions src/lang/db/swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use cairo_lang_utils::{Intern, LookupIntern};
use lsp_types::Url;
use tracing::{error, warn};

use crate::env_config;
use crate::lang::db::AnalysisDatabase;
use crate::lang::lsp::LsProtoGroup;
use crate::lang::proc_macros::db::ProcMacroGroup;
use crate::project::ProjectController;
use crate::{Tricks, env_config};

/// Swaps entire [`AnalysisDatabase`] with empty one periodically.
///
Expand Down Expand Up @@ -52,7 +52,6 @@ impl AnalysisDatabaseSwapper {
&mut self,
db: &mut AnalysisDatabase,
open_files: &HashSet<Url>,
tricks: &Tricks,
project_controller: &mut ProjectController,
) {
let Ok(elapsed) = self.last_replace.elapsed() else {
Expand All @@ -69,7 +68,7 @@ impl AnalysisDatabaseSwapper {
return;
}

self.swap(db, open_files, tricks, project_controller)
self.swap(db, open_files, project_controller)
}

/// Swaps the database.
Expand All @@ -78,13 +77,12 @@ impl AnalysisDatabaseSwapper {
&mut self,
db: &mut AnalysisDatabase,
open_files: &HashSet<Url>,
tricks: &Tricks,
project_controller: &mut ProjectController,
) {
let Ok(new_db) = catch_unwind(AssertUnwindSafe(|| {
project_controller.clear_loaded_workspaces();

let mut new_db = AnalysisDatabase::new(tricks);
let mut new_db = AnalysisDatabase::new();
self.migrate_proc_macro_state(&mut new_db, db);
self.migrate_file_overrides(&mut new_db, db, open_files);
self.update_project_for_open_files(open_files, project_controller);
Expand Down
2 changes: 1 addition & 1 deletion src/lang/proc_macros/plugins/downcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod unsafe_downcast_ref_tests {

#[test]
fn cast_succeed() {
let mut db = AnalysisDatabase::new(&Default::default());
let mut db = AnalysisDatabase::new();

let input = ExpandAttributeParams {
attr: "asd".to_string(),
Expand Down
24 changes: 15 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use std::num::NonZeroU32;
use std::panic::RefUnwindSafe;
use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::OnceLock;
use std::time::{Duration, SystemTime};
use std::{io, panic};

Expand Down Expand Up @@ -79,6 +80,9 @@ mod server;
mod state;
mod toolchain;

/// A container to store global customizations initialized upon launch.
pub(crate) static TRICKS: OnceLock<Tricks> = OnceLock::new();

/// Carries various customizations that can be applied to CairoLS.
///
/// See [the top-level documentation][lib] documentation for usage examples.
Expand Down Expand Up @@ -114,7 +118,9 @@ pub fn start_with_tricks(tricks: Tricks) -> ExitCode {
info!("language server starting");
env_config::report_to_logs();

let exit_code = match Backend::new(tricks) {
let _ = TRICKS.set(tricks);

let exit_code = match Backend::new() {
Ok(backend) => {
if let Err(err) = backend.run().map(|handle| handle.join()) {
error!("language server encountered an unrecoverable error: {err}");
Expand Down Expand Up @@ -233,9 +239,10 @@ impl BackendForTesting {
) -> (Box<dyn FnOnce() -> BackendForTesting + Send>, lsp_server::Connection) {
let (connection_initializer, client) = ConnectionInitializer::memory();

let init = Box::new(|| {
BackendForTesting(Backend::initialize(tricks, connection_initializer).unwrap())
});
let _ = TRICKS.set(tricks);

let init =
Box::new(|| BackendForTesting(Backend::initialize(connection_initializer).unwrap()));

(init, client)
}
Expand All @@ -246,23 +253,23 @@ impl BackendForTesting {
}

impl Backend {
fn new(tricks: Tricks) -> Result<Self> {
fn new() -> Result<Self> {
let connection_initializer = ConnectionInitializer::stdio();

Self::initialize(tricks, connection_initializer)
Self::initialize(connection_initializer)
}

/// Initializes the connection and crate a ready to run [`Backend`] instance.
///
/// As part of the initialization flow, this function exchanges client and server capabilities.
fn initialize(tricks: Tricks, connection_initializer: ConnectionInitializer) -> Result<Self> {
fn initialize(connection_initializer: ConnectionInitializer) -> Result<Self> {
let (id, init_params) = connection_initializer.initialize_start()?;

let client_capabilities = init_params.capabilities;
let server_capabilities = collect_server_capabilities(&client_capabilities);

let connection = connection_initializer.initialize_finish(id, server_capabilities)?;
let state = State::new(connection.make_sender(), client_capabilities, tricks);
let state = State::new(connection.make_sender(), client_capabilities);

Ok(Self { connection, state })
}
Expand Down Expand Up @@ -418,7 +425,6 @@ impl Backend {
state.db_swapper.maybe_swap(
&mut state.db,
&state.open_files,
&state.tricks,
&mut state.project_controller,
);
}
Expand Down
11 changes: 2 additions & 9 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::Arc;
use lsp_types::{ClientCapabilities, Url};
use salsa::ParallelDatabase;

use crate::Tricks;
use crate::config::Config;
use crate::lang::db::{AnalysisDatabase, AnalysisDatabaseSwapper};
use crate::lang::diagnostics::DiagnosticsController;
Expand All @@ -23,31 +22,25 @@ pub struct State {
pub client_capabilities: Owned<ClientCapabilities>,
pub scarb_toolchain: ScarbToolchain,
pub db_swapper: AnalysisDatabaseSwapper,
pub tricks: Owned<Tricks>,
pub diagnostics_controller: DiagnosticsController,
pub proc_macro_controller: ProcMacroClientController,
pub project_controller: ProjectController,
}

impl State {
pub fn new(
sender: ClientSender,
client_capabilities: ClientCapabilities,
tricks: Tricks,
) -> Self {
pub fn new(sender: ClientSender, client_capabilities: ClientCapabilities) -> Self {
let notifier = Client::new(sender).notifier();
let scarb_toolchain = ScarbToolchain::new(notifier.clone());
let proc_macro_controller =
ProcMacroClientController::new(scarb_toolchain.clone(), notifier.clone());

Self {
db: AnalysisDatabase::new(&tricks),
db: AnalysisDatabase::new(),
open_files: Default::default(),
config: Default::default(),
client_capabilities: Owned::new(client_capabilities.into()),
scarb_toolchain: scarb_toolchain.clone(),
db_swapper: AnalysisDatabaseSwapper::new(),
tricks: Owned::new(tricks.into()),
diagnostics_controller: DiagnosticsController::new(notifier.clone()),
proc_macro_controller,
project_controller: ProjectController::initialize(scarb_toolchain, notifier),
Expand Down

0 comments on commit 58c95c5

Please sign in to comment.