Make NodeRuntime non-static for prettier runner

This commit is contained in:
Kirill Bulatov 2023-09-06 18:49:56 +03:00
parent a8387b8b19
commit ce6b31d938
6 changed files with 39 additions and 20 deletions

2
Cargo.lock generated
View file

@ -1501,6 +1501,7 @@ dependencies = [
"log", "log",
"lsp", "lsp",
"nanoid", "nanoid",
"node_runtime",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"pretty_assertions", "pretty_assertions",
"project", "project",
@ -10003,6 +10004,7 @@ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
"menu", "menu",
"node_runtime",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"postage", "postage",
"project", "project",

View file

@ -72,6 +72,7 @@ fs = { path = "../fs", features = ["test-support"] }
git = { path = "../git", features = ["test-support"] } git = { path = "../git", features = ["test-support"] }
live_kit_client = { path = "../live_kit_client", features = ["test-support"] } live_kit_client = { path = "../live_kit_client", features = ["test-support"] }
lsp = { path = "../lsp", features = ["test-support"] } lsp = { path = "../lsp", features = ["test-support"] }
node_runtime = { path = "../node_runtime" }
project = { path = "../project", features = ["test-support"] } project = { path = "../project", features = ["test-support"] }
rpc = { path = "../rpc", features = ["test-support"] } rpc = { path = "../rpc", features = ["test-support"] }
settings = { path = "../settings", features = ["test-support"] } settings = { path = "../settings", features = ["test-support"] }

View file

@ -72,7 +72,7 @@ use std::{
str, str,
sync::{ sync::{
atomic::{AtomicUsize, Ordering::SeqCst}, atomic::{AtomicUsize, Ordering::SeqCst},
Arc, OnceLock, Arc,
}, },
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -154,6 +154,7 @@ pub struct Project {
copilot_lsp_subscription: Option<gpui::Subscription>, copilot_lsp_subscription: Option<gpui::Subscription>,
copilot_log_subscription: Option<lsp::Subscription>, copilot_log_subscription: Option<lsp::Subscription>,
current_lsp_settings: HashMap<Arc<str>, LspSettings>, current_lsp_settings: HashMap<Arc<str>, LspSettings>,
node_runtime: Option<Arc<dyn NodeRuntime>>,
prettier_instances: HashMap< prettier_instances: HashMap<
(Option<WorktreeId>, PathBuf), (Option<WorktreeId>, PathBuf),
Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>, Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>,
@ -554,27 +555,14 @@ impl SearchMatchCandidate {
} }
} }
static NODE_RUNTIME: OnceLock<Arc<dyn NodeRuntime>> = OnceLock::new();
impl Project { impl Project {
pub fn init_settings(cx: &mut AppContext) { pub fn init_settings(cx: &mut AppContext) {
settings::register::<ProjectSettings>(cx); settings::register::<ProjectSettings>(cx);
} }
pub fn init( pub fn init(client: &Arc<Client>, cx: &mut AppContext) {
client: &Arc<Client>,
node_runtime: Option<Arc<dyn NodeRuntime>>,
cx: &mut AppContext,
) {
Self::init_settings(cx); Self::init_settings(cx);
// TODO kb move it to Project::local and other constructors?
if let Some(node_runtime) = node_runtime {
NODE_RUNTIME
.set(node_runtime)
.unwrap_or_else(|_| panic!("multiple init calls tried to set node runtime"));
}
client.add_model_message_handler(Self::handle_add_collaborator); client.add_model_message_handler(Self::handle_add_collaborator);
client.add_model_message_handler(Self::handle_update_project_collaborator); client.add_model_message_handler(Self::handle_update_project_collaborator);
client.add_model_message_handler(Self::handle_remove_collaborator); client.add_model_message_handler(Self::handle_remove_collaborator);
@ -624,6 +612,7 @@ impl Project {
pub fn local( pub fn local(
client: Arc<Client>, client: Arc<Client>,
node_runtime: Arc<dyn NodeRuntime>,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
languages: Arc<LanguageRegistry>, languages: Arc<LanguageRegistry>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
@ -679,6 +668,7 @@ impl Project {
copilot_lsp_subscription, copilot_lsp_subscription,
copilot_log_subscription: None, copilot_log_subscription: None,
current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(), current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(),
node_runtime: Some(node_runtime),
prettier_instances: HashMap::default(), prettier_instances: HashMap::default(),
} }
}) })
@ -777,6 +767,7 @@ impl Project {
copilot_lsp_subscription, copilot_lsp_subscription,
copilot_log_subscription: None, copilot_log_subscription: None,
current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(), current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(),
node_runtime: None,
prettier_instances: HashMap::default(), prettier_instances: HashMap::default(),
}; };
for worktree in worktrees { for worktree in worktrees {
@ -811,13 +802,23 @@ impl Project {
root_paths: impl IntoIterator<Item = &Path>, root_paths: impl IntoIterator<Item = &Path>,
cx: &mut gpui::TestAppContext, cx: &mut gpui::TestAppContext,
) -> ModelHandle<Project> { ) -> ModelHandle<Project> {
use node_runtime::FakeNodeRuntime;
let mut languages = LanguageRegistry::test(); let mut languages = LanguageRegistry::test();
languages.set_executor(cx.background()); languages.set_executor(cx.background());
let http_client = util::http::FakeHttpClient::with_404_response(); let http_client = util::http::FakeHttpClient::with_404_response();
let client = cx.update(|cx| client::Client::new(http_client.clone(), cx)); let client = cx.update(|cx| client::Client::new(http_client.clone(), cx));
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx)); let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
let project = let project = cx.update(|cx| {
cx.update(|cx| Project::local(client, user_store, Arc::new(languages), fs, cx)); Project::local(
client,
FakeNodeRuntime::new(),
user_store,
Arc::new(languages),
fs,
cx,
)
});
for path in root_paths { for path in root_paths {
let (tree, _) = project let (tree, _) = project
.update(cx, |project, cx| { .update(cx, |project, cx| {
@ -8201,7 +8202,7 @@ impl Project {
buffer: &ModelHandle<Buffer>, buffer: &ModelHandle<Buffer>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Option<Task<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>> { ) -> Option<Task<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>> {
let node_runtime = Arc::clone(NODE_RUNTIME.get()?); let node_runtime = Arc::clone(self.node_runtime.as_ref()?);
let buffer_file = File::from_dyn(buffer.read(cx).file()); let buffer_file = File::from_dyn(buffer.read(cx).file());
let buffer_path = buffer_file.map(|file| Arc::clone(file.path())); let buffer_path = buffer_file.map(|file| Arc::clone(file.path()));
let worktree_path = buffer_file let worktree_path = buffer_file

View file

@ -30,6 +30,7 @@ gpui = { path = "../gpui" }
install_cli = { path = "../install_cli" } install_cli = { path = "../install_cli" }
language = { path = "../language" } language = { path = "../language" }
menu = { path = "../menu" } menu = { path = "../menu" }
node_runtime = { path = "../node_runtime" }
project = { path = "../project" } project = { path = "../project" }
settings = { path = "../settings" } settings = { path = "../settings" }
terminal = { path = "../terminal" } terminal = { path = "../terminal" }

View file

@ -42,6 +42,7 @@ use gpui::{
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem}; use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem};
use itertools::Itertools; use itertools::Itertools;
use language::{LanguageRegistry, Rope}; use language::{LanguageRegistry, Rope};
use node_runtime::NodeRuntime;
use std::{ use std::{
any::TypeId, any::TypeId,
borrow::Cow, borrow::Cow,
@ -456,6 +457,7 @@ pub struct AppState {
pub initialize_workspace: pub initialize_workspace:
fn(WeakViewHandle<Workspace>, bool, Arc<AppState>, AsyncAppContext) -> Task<Result<()>>, fn(WeakViewHandle<Workspace>, bool, Arc<AppState>, AsyncAppContext) -> Task<Result<()>>,
pub background_actions: BackgroundActions, pub background_actions: BackgroundActions,
pub node_runtime: Arc<dyn NodeRuntime>,
} }
pub struct WorkspaceStore { pub struct WorkspaceStore {
@ -474,6 +476,7 @@ struct Follower {
impl AppState { impl AppState {
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub fn test(cx: &mut AppContext) -> Arc<Self> { pub fn test(cx: &mut AppContext) -> Arc<Self> {
use node_runtime::FakeNodeRuntime;
use settings::SettingsStore; use settings::SettingsStore;
if !cx.has_global::<SettingsStore>() { if !cx.has_global::<SettingsStore>() {
@ -498,6 +501,7 @@ impl AppState {
user_store, user_store,
// channel_store, // channel_store,
workspace_store, workspace_store,
node_runtime: FakeNodeRuntime::new(),
initialize_workspace: |_, _, _, _| Task::ready(Ok(())), initialize_workspace: |_, _, _, _| Task::ready(Ok(())),
build_window_options: |_, _, _| Default::default(), build_window_options: |_, _, _| Default::default(),
background_actions: || &[], background_actions: || &[],
@ -816,6 +820,7 @@ impl Workspace {
)> { )> {
let project_handle = Project::local( let project_handle = Project::local(
app_state.client.clone(), app_state.client.clone(),
app_state.node_runtime.clone(),
app_state.user_store.clone(), app_state.user_store.clone(),
app_state.languages.clone(), app_state.languages.clone(),
app_state.fs.clone(), app_state.fs.clone(),
@ -3517,6 +3522,8 @@ impl Workspace {
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub fn test_new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self { pub fn test_new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
use node_runtime::FakeNodeRuntime;
let client = project.read(cx).client(); let client = project.read(cx).client();
let user_store = project.read(cx).user_store(); let user_store = project.read(cx).user_store();
@ -3530,6 +3537,7 @@ impl Workspace {
build_window_options: |_, _, _| Default::default(), build_window_options: |_, _, _| Default::default(),
initialize_workspace: |_, _, _, _| Task::ready(Ok(())), initialize_workspace: |_, _, _, _| Task::ready(Ok(())),
background_actions: || &[], background_actions: || &[],
node_runtime: FakeNodeRuntime::new(),
}); });
Self::new(0, project, app_state, cx) Self::new(0, project, app_state, cx)
} }

View file

@ -138,7 +138,7 @@ fn main() {
theme::init(Assets, cx); theme::init(Assets, cx);
context_menu::init(cx); context_menu::init(cx);
project::Project::init(&client, Some(Arc::clone(&node_runtime)), cx); project::Project::init(&client, cx);
client::init(&client, cx); client::init(&client, cx);
command_palette::init(cx); command_palette::init(cx);
language::init(cx); language::init(cx);
@ -154,7 +154,12 @@ fn main() {
semantic_index::init(fs.clone(), http.clone(), languages.clone(), cx); semantic_index::init(fs.clone(), http.clone(), languages.clone(), cx);
vim::init(cx); vim::init(cx);
terminal_view::init(cx); terminal_view::init(cx);
copilot::init(copilot_language_server_id, http.clone(), node_runtime, cx); copilot::init(
copilot_language_server_id,
http.clone(),
node_runtime.clone(),
cx,
);
assistant::init(cx); assistant::init(cx);
component_test::init(cx); component_test::init(cx);
@ -181,6 +186,7 @@ fn main() {
initialize_workspace, initialize_workspace,
background_actions, background_actions,
workspace_store, workspace_store,
node_runtime,
}); });
cx.set_global(Arc::downgrade(&app_state)); cx.set_global(Arc::downgrade(&app_state));