Skip to content

Commit 08d55ad

Browse files
connernilsenfacebook-github-bot
authored andcommitted
Refactor build system stuff to standalone LSP module
Summary: Kyle wanted a refactor of the build system functionality into a standalone file. Not fully sure if this is what he meant, but probably a good thing to do anyway. Reviewed By: kinto0 Differential Revision: D83537090 fbshipit-source-id: f7398b33dd884ff403e28474e2da83a42b56195c
1 parent 9b00999 commit 08d55ad

File tree

3 files changed

+96
-66
lines changed

3 files changed

+96
-66
lines changed

pyrefly/lib/lsp/build_system.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
use std::ffi::OsString;
9+
use std::sync::Arc;
10+
use std::sync::LazyLock;
11+
12+
use dupe::Dupe as _;
13+
use pyrefly_build::handle::Handle;
14+
use pyrefly_config::config::ConfigFile;
15+
use pyrefly_python::COMPILED_FILE_SUFFIXES;
16+
use pyrefly_python::PYTHON_EXTENSIONS;
17+
use pyrefly_python::module_path::ModulePath;
18+
use pyrefly_util::arc_id::ArcId;
19+
use pyrefly_util::events::CategorizedEvents;
20+
use pyrefly_util::lock::Mutex;
21+
use starlark_map::small_map::SmallMap;
22+
use starlark_map::small_set::SmallSet;
23+
24+
use crate::lsp::queue::HeavyTaskQueue;
25+
use crate::lsp::queue::LspEvent;
26+
use crate::lsp::queue::LspQueue;
27+
use crate::state::state::State;
28+
29+
pub fn should_requery_build_system(events: &CategorizedEvents) -> bool {
30+
static CONFIG_NAMES: LazyLock<SmallSet<OsString>> = LazyLock::new(|| {
31+
ConfigFile::CONFIG_FILE_NAMES
32+
.iter()
33+
.chain(ConfigFile::ADDITIONAL_ROOT_FILE_NAMES.iter())
34+
.map(OsString::from)
35+
.collect()
36+
});
37+
static PYTHON_SUFFIXES: LazyLock<SmallSet<OsString>> = LazyLock::new(|| {
38+
PYTHON_EXTENSIONS
39+
.iter()
40+
.chain(COMPILED_FILE_SUFFIXES.iter())
41+
.map(OsString::from)
42+
.collect()
43+
});
44+
45+
events.iter().any(|f| {
46+
!(f.file_name().is_some_and(|n| CONFIG_NAMES.contains(n))
47+
|| f.extension().is_some_and(|e| PYTHON_SUFFIXES.contains(e)))
48+
})
49+
}
50+
51+
/// Attempts to requery any open sourced_dbs for open files, and if there are changes,
52+
/// invalidate find and perform a recheck.
53+
pub fn queue_source_db_rebuild_and_recheck(
54+
state: &State,
55+
invalidated_configs: Arc<Mutex<SmallSet<ArcId<ConfigFile>>>>,
56+
sourcedb_queue: HeavyTaskQueue,
57+
lsp_queue: LspQueue,
58+
handles: &[Handle],
59+
) {
60+
let mut configs_to_paths: SmallMap<ArcId<ConfigFile>, SmallSet<ModulePath>> = SmallMap::new();
61+
let config_finder = state.config_finder();
62+
for handle in handles {
63+
let config = config_finder.python_file(handle.module(), handle.path());
64+
configs_to_paths
65+
.entry(config)
66+
.or_default()
67+
.insert(handle.path().dupe());
68+
}
69+
sourcedb_queue.queue_task(Box::new(move || {
70+
let new_invalidated_configs: SmallSet<ArcId<ConfigFile>> = configs_to_paths
71+
.into_iter()
72+
.filter(|(c, files)| match c.requery_source_db(files) {
73+
Ok(reloaded) => reloaded,
74+
Err(error) => {
75+
eprintln!("Error reloading source database for config: {error}");
76+
false
77+
}
78+
})
79+
.map(|(c, _)| c)
80+
.collect();
81+
if !new_invalidated_configs.is_empty() {
82+
let mut lock = invalidated_configs.lock();
83+
for c in new_invalidated_configs {
84+
lock.insert(c);
85+
}
86+
let _ = lsp_queue.send(LspEvent::InvalidateConfigFind);
87+
}
88+
}));
89+
}

pyrefly/lib/lsp/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
mod build_system;
89
pub mod features;
910
pub mod lsp;
1011
pub mod module_helpers;

pyrefly/lib/lsp/server.rs

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
use std::collections::HashMap;
99
use std::collections::HashSet;
10-
use std::ffi::OsString;
1110
use std::iter::once;
1211
use std::path::Path;
1312
use std::path::PathBuf;
1413
use std::sync::Arc;
15-
use std::sync::LazyLock;
1614
use std::sync::atomic::AtomicBool;
1715
use std::sync::atomic::AtomicI32;
1816
use std::sync::atomic::Ordering;
@@ -147,7 +145,6 @@ use lsp_types::request::WorkspaceConfiguration;
147145
use lsp_types::request::WorkspaceSymbolRequest;
148146
use pyrefly_build::handle::Handle;
149147
use pyrefly_config::config::ConfigSource;
150-
use pyrefly_python::COMPILED_FILE_SUFFIXES;
151148
use pyrefly_python::PYTHON_EXTENSIONS;
152149
use pyrefly_python::module::TextRangeWithModule;
153150
use pyrefly_python::module_name::ModuleName;
@@ -171,6 +168,8 @@ use starlark_map::small_set::SmallSet;
171168
use crate::commands::lsp::IndexingMode;
172169
use crate::config::config::ConfigFile;
173170
use crate::error::error::Error;
171+
use crate::lsp::build_system::queue_source_db_rebuild_and_recheck;
172+
use crate::lsp::build_system::should_requery_build_system;
174173
use crate::lsp::features::hover::get_hover;
175174
use crate::lsp::lsp::apply_change_events;
176175
use crate::lsp::lsp::as_notification;
@@ -1078,7 +1077,7 @@ impl Server {
10781077
ide_transaction_manager.save(transaction);
10791078
}
10801079
}
1081-
Self::queue_source_db_rebuild_and_recheck(
1080+
queue_source_db_rebuild_and_recheck(
10821081
&self.state,
10831082
self.invalidated_configs.dupe(),
10841083
self.sourcedb_queue.dupe(),
@@ -1087,47 +1086,6 @@ impl Server {
10871086
);
10881087
}
10891088

1090-
/// Attempts to requery any open sourced_dbs for open files, and if there are changes,
1091-
/// invalidate find and perform a recheck.
1092-
fn queue_source_db_rebuild_and_recheck(
1093-
state: &State,
1094-
invalidated_configs: Arc<Mutex<SmallSet<ArcId<ConfigFile>>>>,
1095-
sourcedb_queue: HeavyTaskQueue,
1096-
lsp_queue: LspQueue,
1097-
handles: &[Handle],
1098-
) {
1099-
let mut configs_to_paths: SmallMap<ArcId<ConfigFile>, SmallSet<ModulePath>> =
1100-
SmallMap::new();
1101-
let config_finder = state.config_finder();
1102-
for handle in handles {
1103-
let config = config_finder.python_file(handle.module(), handle.path());
1104-
configs_to_paths
1105-
.entry(config)
1106-
.or_default()
1107-
.insert(handle.path().dupe());
1108-
}
1109-
sourcedb_queue.queue_task(Box::new(move || {
1110-
let new_invalidated_configs: SmallSet<ArcId<ConfigFile>> = configs_to_paths
1111-
.into_iter()
1112-
.filter(|(c, files)| match c.requery_source_db(files) {
1113-
Ok(reloaded) => reloaded,
1114-
Err(error) => {
1115-
eprintln!("Error reloading source database for config: {error}");
1116-
false
1117-
}
1118-
})
1119-
.map(|(c, _)| c)
1120-
.collect();
1121-
if !new_invalidated_configs.is_empty() {
1122-
let mut lock = invalidated_configs.lock();
1123-
for c in new_invalidated_configs {
1124-
lock.insert(c);
1125-
}
1126-
let _ = lsp_queue.send(LspEvent::InvalidateConfigFind);
1127-
}
1128-
}));
1129-
}
1130-
11311089
fn invalidate_find_for_configs(&self, invalidated_configs: SmallSet<ArcId<ConfigFile>>) {
11321090
self.invalidate(|t| t.invalidate_find_for_configs(invalidated_configs));
11331091
}
@@ -1379,25 +1337,7 @@ impl Server {
13791337

13801338
let events = CategorizedEvents::new_lsp(params.changes);
13811339

1382-
static CONFIG_NAMES: LazyLock<SmallSet<OsString>> = LazyLock::new(|| {
1383-
ConfigFile::CONFIG_FILE_NAMES
1384-
.iter()
1385-
.chain(ConfigFile::ADDITIONAL_ROOT_FILE_NAMES.iter())
1386-
.map(OsString::from)
1387-
.collect()
1388-
});
1389-
static PYTHON_SUFFIXES: LazyLock<SmallSet<OsString>> = LazyLock::new(|| {
1390-
PYTHON_EXTENSIONS
1391-
.iter()
1392-
.chain(COMPILED_FILE_SUFFIXES.iter())
1393-
.map(OsString::from)
1394-
.collect()
1395-
});
1396-
1397-
let should_requery_build_system = events.iter().any(|f| {
1398-
!(f.file_name().is_some_and(|n| CONFIG_NAMES.contains(n))
1399-
|| f.extension().is_some_and(|e| PYTHON_SUFFIXES.contains(e)))
1400-
});
1340+
let should_requery_build_system = should_requery_build_system(&events);
14011341

14021342
self.invalidate(move |t| t.invalidate_events(&events));
14031343
// rewatch files in case we loaded or dropped any configs
@@ -1413,7 +1353,7 @@ impl Server {
14131353
.keys()
14141354
.map(|x| make_open_handle(&self.state, x))
14151355
.collect::<Vec<_>>();
1416-
Self::queue_source_db_rebuild_and_recheck(
1356+
queue_source_db_rebuild_and_recheck(
14171357
&self.state,
14181358
self.invalidated_configs.dupe(),
14191359
self.sourcedb_queue.dupe(),
@@ -1443,7 +1383,7 @@ impl Server {
14431383
let handles =
14441384
Self::validate_in_memory_for_transaction(&state, &open_files, transaction.as_mut());
14451385
state.commit_transaction(transaction);
1446-
Self::queue_source_db_rebuild_and_recheck(
1386+
queue_source_db_rebuild_and_recheck(
14471387
&state,
14481388
invalidated_configs,
14491389
sourcedb_queue,

0 commit comments

Comments
 (0)