2020-02-18 05:25:26 -06:00
|
|
|
//! The context or environment in which the language server functions. In our
|
|
|
|
//! server implementation this is know as the `WorldState`.
|
2019-12-21 14:27:38 -06:00
|
|
|
//!
|
|
|
|
//! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2020-06-26 04:21:21 -05:00
|
|
|
use std::{sync::Arc, time::Instant};
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2020-06-25 10:14:11 -05:00
|
|
|
use crossbeam_channel::{unbounded, Receiver, Sender};
|
2020-06-25 16:44:58 -05:00
|
|
|
use flycheck::FlycheckHandle;
|
|
|
|
use lsp_types::Url;
|
2019-07-04 15:05:17 -05:00
|
|
|
use parking_lot::RwLock;
|
2020-06-25 16:44:58 -05:00
|
|
|
use ra_db::{CrateId, VfsPath};
|
|
|
|
use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FileId};
|
|
|
|
use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target};
|
2020-03-28 05:08:19 -05:00
|
|
|
use stdx::format_to;
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2018-10-15 12:15:53 -05:00
|
|
|
use crate::{
|
2020-06-25 16:44:58 -05:00
|
|
|
config::Config,
|
2020-06-13 04:00:06 -05:00
|
|
|
diagnostics::{CheckFixes, DiagnosticCollection},
|
2020-06-11 04:04:09 -05:00
|
|
|
from_proto,
|
|
|
|
line_endings::LineEndings,
|
2020-06-26 04:21:21 -05:00
|
|
|
main_loop::Task,
|
2020-06-25 16:44:58 -05:00
|
|
|
reload::SourceRootConfig,
|
2020-06-24 11:57:30 -05:00
|
|
|
request_metrics::{LatestRequests, RequestMetrics},
|
2020-06-25 10:14:11 -05:00
|
|
|
show_message,
|
2020-06-25 08:35:42 -05:00
|
|
|
thread_pool::TaskPool,
|
2020-06-13 04:00:06 -05:00
|
|
|
to_proto::url_from_abs_path,
|
2020-06-11 04:04:09 -05:00
|
|
|
Result,
|
2018-08-17 11:54:08 -05:00
|
|
|
};
|
2020-06-24 17:17:11 -05:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2018-08-17 11:54:08 -05:00
|
|
|
|
2020-06-24 17:17:11 -05:00
|
|
|
#[derive(Eq, PartialEq)]
|
|
|
|
pub(crate) enum Status {
|
|
|
|
Loading,
|
|
|
|
Ready,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Status {
|
|
|
|
fn default() -> Self {
|
|
|
|
Status::Loading
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:27:39 -05:00
|
|
|
// Enforces drop order
|
|
|
|
pub(crate) struct Handle<H, C> {
|
|
|
|
pub(crate) handle: H,
|
|
|
|
pub(crate) receiver: C,
|
|
|
|
}
|
|
|
|
|
2020-06-26 04:21:21 -05:00
|
|
|
pub(crate) type ReqHandler = fn(&mut GlobalState, lsp_server::Response);
|
|
|
|
pub(crate) type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>;
|
|
|
|
|
2020-06-03 04:16:08 -05:00
|
|
|
/// `GlobalState` is the primary mutable state of the language server
|
2019-06-01 02:31:40 -05:00
|
|
|
///
|
|
|
|
/// The most interesting components are `vfs`, which stores a consistent
|
|
|
|
/// snapshot of the file systems, and `analysis_host`, which stores our
|
|
|
|
/// incremental salsa database.
|
2020-06-25 17:27:39 -05:00
|
|
|
///
|
|
|
|
/// Note that this struct has more than on impl in various modules!
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) struct GlobalState {
|
2020-06-25 10:14:11 -05:00
|
|
|
sender: Sender<lsp_server::Message>,
|
2020-06-25 17:27:39 -05:00
|
|
|
pub(crate) task_pool: Handle<TaskPool<Task>, Receiver<Task>>,
|
|
|
|
pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>,
|
|
|
|
pub(crate) flycheck: Option<Handle<FlycheckHandle, Receiver<flycheck::Message>>>,
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) config: Config,
|
|
|
|
pub(crate) analysis_host: AnalysisHost,
|
|
|
|
pub(crate) diagnostics: DiagnosticCollection,
|
2020-06-24 17:17:11 -05:00
|
|
|
pub(crate) mem_docs: FxHashSet<VfsPath>,
|
2020-06-11 04:04:09 -05:00
|
|
|
pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
|
2020-06-24 17:17:11 -05:00
|
|
|
pub(crate) status: Status,
|
|
|
|
pub(crate) req_queue: ReqQueue,
|
2020-06-25 16:44:58 -05:00
|
|
|
pub(crate) source_root_config: SourceRootConfig,
|
|
|
|
pub(crate) proc_macro_client: ProcMacroClient,
|
|
|
|
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
|
2020-06-24 17:41:08 -05:00
|
|
|
latest_requests: Arc<RwLock<LatestRequests>>,
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2019-06-01 02:31:40 -05:00
|
|
|
/// An immutable snapshot of the world's state at a point in time.
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) struct GlobalStateSnapshot {
|
|
|
|
pub(crate) config: Config,
|
|
|
|
pub(crate) analysis: Analysis,
|
|
|
|
pub(crate) check_fixes: CheckFixes,
|
2020-06-20 16:08:01 -05:00
|
|
|
pub(crate) latest_requests: Arc<RwLock<LatestRequests>>,
|
2020-06-11 04:04:09 -05:00
|
|
|
vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
|
2020-06-24 17:41:08 -05:00
|
|
|
workspaces: Arc<Vec<ProjectWorkspace>>,
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2020-06-03 04:16:08 -05:00
|
|
|
impl GlobalState {
|
2020-06-25 17:54:41 -05:00
|
|
|
pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
|
2020-06-25 16:11:59 -05:00
|
|
|
let loader = {
|
2020-06-25 17:27:39 -05:00
|
|
|
let (sender, receiver) = unbounded::<vfs::loader::Message>();
|
2020-06-26 04:39:27 -05:00
|
|
|
let handle: vfs_notify::NotifyHandle =
|
|
|
|
vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
|
2020-06-25 17:27:39 -05:00
|
|
|
let handle = Box::new(handle) as Box<dyn vfs::loader::Handle>;
|
|
|
|
Handle { handle, receiver }
|
2020-06-11 04:04:09 -05:00
|
|
|
};
|
2020-06-25 08:35:42 -05:00
|
|
|
|
|
|
|
let task_pool = {
|
|
|
|
let (sender, receiver) = unbounded();
|
2020-06-25 17:27:39 -05:00
|
|
|
let handle = TaskPool::new(sender);
|
|
|
|
Handle { handle, receiver }
|
2020-06-25 08:35:42 -05:00
|
|
|
};
|
|
|
|
|
2020-06-25 17:54:41 -05:00
|
|
|
let analysis_host = AnalysisHost::new(config.lru_capacity);
|
2020-06-25 16:26:21 -05:00
|
|
|
GlobalState {
|
2020-06-25 10:14:11 -05:00
|
|
|
sender,
|
2020-06-25 08:35:42 -05:00
|
|
|
task_pool,
|
2020-06-11 04:04:09 -05:00
|
|
|
loader,
|
2020-06-25 17:27:39 -05:00
|
|
|
config,
|
2020-06-25 17:54:41 -05:00
|
|
|
analysis_host,
|
2020-06-25 16:11:59 -05:00
|
|
|
flycheck: None,
|
2020-01-31 12:23:25 -06:00
|
|
|
diagnostics: Default::default(),
|
2020-06-24 17:17:11 -05:00
|
|
|
mem_docs: FxHashSet::default(),
|
2020-06-25 16:11:59 -05:00
|
|
|
vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))),
|
2020-06-24 17:17:11 -05:00
|
|
|
status: Status::default(),
|
2020-06-25 16:26:21 -05:00
|
|
|
req_queue: ReqQueue::default(),
|
2020-06-25 16:11:59 -05:00
|
|
|
source_root_config: SourceRootConfig::default(),
|
|
|
|
proc_macro_client: ProcMacroClient::dummy(),
|
|
|
|
workspaces: Arc::new(Vec::new()),
|
2020-06-25 17:27:39 -05:00
|
|
|
latest_requests: Default::default(),
|
2020-06-25 16:26:21 -05:00
|
|
|
}
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:35:22 -05:00
|
|
|
pub(crate) fn process_changes(&mut self) -> bool {
|
2020-06-11 04:04:09 -05:00
|
|
|
let change = {
|
|
|
|
let mut change = AnalysisChange::new();
|
|
|
|
let (vfs, line_endings_map) = &mut *self.vfs.write();
|
|
|
|
let changed_files = vfs.take_changes();
|
|
|
|
if changed_files.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fs_op = changed_files.iter().any(|it| it.is_created_or_deleted());
|
|
|
|
if fs_op {
|
|
|
|
let roots = self.source_root_config.partition(&vfs);
|
|
|
|
change.set_roots(roots)
|
|
|
|
}
|
|
|
|
|
|
|
|
for file in changed_files {
|
|
|
|
let text = if file.exists() {
|
|
|
|
let bytes = vfs.file_contents(file.file_id).to_vec();
|
|
|
|
match String::from_utf8(bytes).ok() {
|
|
|
|
Some(text) => {
|
|
|
|
let (text, line_endings) = LineEndings::normalize(text);
|
|
|
|
line_endings_map.insert(file.file_id, line_endings);
|
|
|
|
Some(Arc::new(text))
|
|
|
|
}
|
|
|
|
None => None,
|
2018-12-19 06:40:42 -06:00
|
|
|
}
|
2020-06-11 04:04:09 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
change.change_file(file.file_id, text);
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
2020-06-11 04:04:09 -05:00
|
|
|
change
|
|
|
|
};
|
|
|
|
|
2018-10-25 02:57:55 -05:00
|
|
|
self.analysis_host.apply_change(change);
|
2020-06-18 01:29:34 -05:00
|
|
|
true
|
2018-12-19 06:04:15 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:35:22 -05:00
|
|
|
pub(crate) fn snapshot(&self) -> GlobalStateSnapshot {
|
2020-06-03 04:16:08 -05:00
|
|
|
GlobalStateSnapshot {
|
2020-03-31 09:02:55 -05:00
|
|
|
config: self.config.clone(),
|
2018-09-02 06:46:15 -05:00
|
|
|
workspaces: Arc::clone(&self.workspaces),
|
2018-09-10 04:57:40 -05:00
|
|
|
analysis: self.analysis_host.analysis(),
|
2018-12-19 06:04:15 -06:00
|
|
|
vfs: Arc::clone(&self.vfs),
|
2019-05-31 12:14:54 -05:00
|
|
|
latest_requests: Arc::clone(&self.latest_requests),
|
2020-01-31 12:23:25 -06:00
|
|
|
check_fixes: Arc::clone(&self.diagnostics.check_fixes),
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 10:11:58 -06:00
|
|
|
|
2020-06-25 10:14:11 -05:00
|
|
|
pub(crate) fn send(&mut self, message: lsp_server::Message) {
|
|
|
|
self.sender.send(message).unwrap()
|
|
|
|
}
|
2020-06-25 12:23:52 -05:00
|
|
|
pub(crate) fn respond(&mut self, response: lsp_server::Response) {
|
|
|
|
if let Some((method, start)) = self.req_queue.incoming.complete(response.id.clone()) {
|
|
|
|
let duration = start.elapsed();
|
|
|
|
log::info!("handled req#{} in {:?}", response.id, duration);
|
|
|
|
let metrics =
|
|
|
|
RequestMetrics { id: response.id.clone(), method: method.to_string(), duration };
|
|
|
|
self.latest_requests.write().record(metrics);
|
|
|
|
self.send(response.into());
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 16:26:21 -05:00
|
|
|
pub(crate) fn show_message(&self, typ: lsp_types::MessageType, message: String) {
|
2020-06-25 10:14:11 -05:00
|
|
|
show_message(typ, message, &self.sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for GlobalState {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.analysis_host.request_cancellation()
|
|
|
|
}
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2020-06-03 04:16:08 -05:00
|
|
|
impl GlobalStateSnapshot {
|
2020-06-11 04:04:09 -05:00
|
|
|
pub(crate) fn url_to_file_id(&self, url: &Url) -> Result<FileId> {
|
|
|
|
let path = from_proto::abs_path(url)?;
|
|
|
|
let path = path.into();
|
|
|
|
let res =
|
|
|
|
self.vfs.read().0.file_id(&path).ok_or_else(|| format!("file not found: {}", path))?;
|
|
|
|
Ok(res)
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 04:04:09 -05:00
|
|
|
pub(crate) fn file_id_to_url(&self, id: FileId) -> Url {
|
|
|
|
file_id_to_url(&self.vfs.read().0, id)
|
2018-08-17 11:54:08 -05:00
|
|
|
}
|
2018-12-21 03:18:14 -06:00
|
|
|
|
2020-06-11 04:04:09 -05:00
|
|
|
pub(crate) fn file_line_endings(&self, id: FileId) -> LineEndings {
|
|
|
|
self.vfs.read().1[&id]
|
2019-08-20 10:53:59 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url {
|
2020-06-11 04:04:09 -05:00
|
|
|
let mut base = self.vfs.read().0.file_path(file_id);
|
2020-06-16 11:45:58 -05:00
|
|
|
base.pop();
|
|
|
|
let path = base.join(path);
|
2020-06-11 04:04:09 -05:00
|
|
|
let path = path.as_path().unwrap();
|
2020-06-13 04:00:06 -05:00
|
|
|
url_from_abs_path(&path)
|
2018-12-21 03:18:14 -06:00
|
|
|
}
|
2019-01-22 15:15:03 -06:00
|
|
|
|
2020-06-17 10:51:46 -05:00
|
|
|
pub(crate) fn cargo_target_for_crate_root(
|
|
|
|
&self,
|
|
|
|
crate_id: CrateId,
|
|
|
|
) -> Option<(&CargoWorkspace, Target)> {
|
2020-06-24 17:41:08 -05:00
|
|
|
let file_id = self.analysis.crate_root(crate_id).ok()?;
|
2020-06-11 04:04:09 -05:00
|
|
|
let path = self.vfs.read().0.file_path(file_id);
|
|
|
|
let path = path.as_path()?;
|
2020-06-17 10:51:46 -05:00
|
|
|
self.workspaces.iter().find_map(|ws| match ws {
|
|
|
|
ProjectWorkspace::Cargo { cargo, .. } => {
|
|
|
|
cargo.target_by_root(&path).map(|it| (cargo, it))
|
|
|
|
}
|
|
|
|
ProjectWorkspace::Json { .. } => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-24 11:54:05 -05:00
|
|
|
pub(crate) fn status(&self) -> String {
|
2020-03-28 05:08:19 -05:00
|
|
|
let mut buf = String::new();
|
2019-01-22 15:15:03 -06:00
|
|
|
if self.workspaces.is_empty() {
|
2020-03-28 05:08:19 -05:00
|
|
|
buf.push_str("no workspaces\n")
|
2019-01-22 15:15:03 -06:00
|
|
|
} else {
|
2020-03-28 05:08:19 -05:00
|
|
|
buf.push_str("workspaces:\n");
|
2019-01-22 15:15:03 -06:00
|
|
|
for w in self.workspaces.iter() {
|
2020-03-28 05:08:19 -05:00
|
|
|
format_to!(buf, "{} packages loaded\n", w.n_packages());
|
2019-01-22 15:15:03 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-28 05:08:19 -05:00
|
|
|
buf.push_str("\nanalysis:\n");
|
|
|
|
buf.push_str(
|
2019-07-25 12:22:41 -05:00
|
|
|
&self
|
|
|
|
.analysis
|
|
|
|
.status()
|
|
|
|
.unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()),
|
|
|
|
);
|
2020-03-28 05:08:19 -05:00
|
|
|
buf
|
2019-01-22 15:15:03 -06:00
|
|
|
}
|
2020-06-11 04:04:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {
|
|
|
|
let path = vfs.file_path(id);
|
|
|
|
let path = path.as_path().unwrap();
|
|
|
|
url_from_abs_path(&path)
|
|
|
|
}
|