Skip to content

Commit

Permalink
refactor: merge compile and language state (#379)
Browse files Browse the repository at this point in the history
* refactor: remove dedicate servers

* refactor: merge compile state and language state

* refactor: initialize oneshot lsp by `super_init`

* refactor: remove compile_init

* dev: remove unused trait
  • Loading branch information
Myriad-Dreamin authored Jul 8, 2024
1 parent ad79b29 commit 83eef2f
Show file tree
Hide file tree
Showing 16 changed files with 826 additions and 971 deletions.
44 changes: 40 additions & 4 deletions crates/sync-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,22 @@ impl LspClient {
handler(service, response)
}

pub fn send_notification<N: lsp_types::notification::Notification>(&self, params: N::Params) {
let not = lsp_server::Notification::new(N::METHOD.to_owned(), params);

pub fn send_notification_(&self, notif: lsp_server::Notification) {
let sender = self.sender.read();
let Some(sender) = sender.as_ref() else {
log::warn!("failed to send notification: connection closed");
return;
};
let Err(res) = sender.send(not.into()) else {
let Err(res) = sender.send(notif.into()) else {
return;
};
log::warn!("failed to send notification: {res:?}");
}

pub fn send_notification<N: lsp_types::notification::Notification>(&self, params: N::Params) {
self.send_notification_(lsp_server::Notification::new(N::METHOD.to_owned(), params));
}

pub fn register_request(&self, request: &lsp_server::Request, request_received: Instant) {
let mut req_queue = self.req_queue.lock();
log::info!(
Expand Down Expand Up @@ -534,6 +536,39 @@ pub struct LspDriver<Args: Initializer> {
pub resources: ResourceMap<Args::S>,
}

impl<Args: Initializer> LspDriver<Args> {
pub fn state(&self) -> Option<&Args::S> {
match &self.state {
State::Ready(s) => Some(s),
_ => None,
}
}

pub fn state_mut(&mut self) -> Option<&mut Args::S> {
match &mut self.state {
State::Ready(s) => Some(s),
_ => None,
}
}

pub fn ready(&mut self, params: Args::I) -> AnySchedulableResponse {
let args = match &mut self.state {
State::Uninitialized(args) => args,
_ => {
return just_result!(Err(resp_err(
ErrorCode::InvalidRequest,
"Server is already initialized"
)))
}
};

let (s, res) = args.take().unwrap().initialize(params);
self.state = State::Ready(s);

res
}
}

impl<Args: Initializer> LspDriver<Args>
where
Args::S: 'static,
Expand All @@ -560,6 +595,7 @@ where

res
}

pub fn start_(&mut self, inbox: crossbeam_channel::Receiver<Message>) -> anyhow::Result<()> {
// todo: follow what rust analyzer does
// Windows scheduler implements priority boosts: if thread waits for an
Expand Down
26 changes: 11 additions & 15 deletions crates/tinymist/src/actor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@ use self::{
user_action::run_user_action_thread,
};
use crate::{
compile::CompileState,
world::{ImmutDict, LspWorldBuilder},
LanguageState,
};

impl CompileState {
impl LanguageState {
pub fn restart_server(&mut self, group: &str) {
let server = self.server(
group.to_owned(),
self.config
.determine_entry(self.config.determine_default_entry_path()),
self.config.determine_inputs(),
self.compile_config()
.determine_entry(self.compile_config().determine_default_entry_path()),
self.compile_config().determine_inputs(),
self.vfs_snapshot(),
);
if let Some(mut previous_server) = self.compiler.replace(server) {
if let Some(mut previous_server) = self.primary.replace(server) {
std::thread::spawn(move || previous_server.settle());
}
}
Expand All @@ -65,8 +64,8 @@ impl CompileState {
doc_rx,
entry: entry.clone(),
config: ExportConfig {
substitute_pattern: self.config.output_path.clone(),
mode: self.config.export_pdf,
substitute_pattern: self.compile_config().output_path.clone(),
mode: self.compile_config().export_pdf,
},
kind: ExportKind::Pdf,
count_words: self.config.notify_compile_status,
Expand All @@ -80,8 +79,8 @@ impl CompileState {

// Create the compile handler for client consuming results.
let position_encoding = self.const_config().position_encoding;
let enable_periscope = self.config.periscope_args.is_some();
let periscope_args = self.config.periscope_args.clone();
let enable_periscope = self.compile_config().periscope_args.is_some();
let periscope_args = self.compile_config().periscope_args.clone();
let handle = Arc::new(CompileHandler {
#[cfg(feature = "preview")]
inner: std::sync::Arc::new(RwLock::new(None)),
Expand All @@ -98,7 +97,7 @@ impl CompileState {
periscope: PeriscopeRenderer::new(periscope_args.unwrap_or_default()),
});

let font_resolver = self.config.determine_fonts();
let font_resolver = self.compile_config().determine_fonts();
let entry_ = entry.clone();
let handle_ = handle.clone();

Expand All @@ -114,17 +113,14 @@ impl CompileState {
});

// Create the client
let config = self.config.clone();
let config = self.compile_config().clone();
let client = CompileClientActor::new(handle, config, entry);
// We do send memory changes instead of initializing compiler with them.
// This is because there are state recorded inside of the compiler actor, and we
// must update them.
client.add_memory_changes(MemoryEvent::Update(snapshot));
client
}
}

impl LanguageState {
pub fn run_format_thread(&mut self) {
if self.format_thread.is_some() {
log::error!("formatting thread is already started");
Expand Down
2 changes: 1 addition & 1 deletion crates/tinymist/src/actor/typ_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ use super::{
};
use crate::{
actor::export::ExportRequest,
compile_init::CompileConfig,
tool::preview::CompileStatus,
utils::{self, threaded_receive},
world::{LspCompilerFeat, LspWorld},
CompileConfig,
};

type EditorSender = mpsc::UnboundedSender<EditorRequest>;
Expand Down
2 changes: 1 addition & 1 deletion crates/tinymist/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use once_cell::sync::Lazy;
use sync_lsp::transport::MirrorArgs;

use tinymist::compile_init::{CompileOnceArgs, FontArgs};
use tinymist::{CompileOnceArgs, FontArgs};

#[derive(Debug, Clone, clap::Parser)]
#[clap(name = "tinymist", author, version, about, long_version(LONG_VERSION.as_str()))]
Expand Down
2 changes: 0 additions & 2 deletions crates/tinymist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ mod world;

use lsp_server::ErrorCode;
use lsp_server::ResponseError;
pub use server::compile;
pub use server::compile_init;
pub use server::lsp::*;
pub use server::lsp_init::*;
#[cfg(feature = "preview")]
Expand Down
Loading

0 comments on commit 83eef2f

Please sign in to comment.