5044: Tweak visibility r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-25 00:16:32 +00:00 committed by GitHub
commit 78e94e4570
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 177 additions and 207 deletions

View file

@ -94,7 +94,7 @@ impl CargoTargetSpec {
global_state_snapshot: &GlobalStateSnapshot,
file_id: FileId,
) -> Result<Option<CargoTargetSpec>> {
let crate_id = match global_state_snapshot.analysis().crate_for(file_id)?.first() {
let crate_id = match global_state_snapshot.analysis.crate_for(file_id)?.first() {
Some(crate_id) => *crate_id,
None => return Ok(None),
};

View file

@ -37,7 +37,7 @@ pub(crate) fn file_position(
tdpp: lsp_types::TextDocumentPositionParams,
) -> Result<FilePosition> {
let file_id = file_id(world, &tdpp.text_document.uri)?;
let line_index = world.analysis().file_line_index(file_id)?;
let line_index = world.analysis.file_line_index(file_id)?;
let offset = offset(&*line_index, tdpp.position);
Ok(FilePosition { file_id, offset })
}
@ -48,7 +48,7 @@ pub(crate) fn file_range(
range: lsp_types::Range,
) -> Result<FileRange> {
let file_id = file_id(world, &text_document_identifier.uri)?;
let line_index = world.analysis().file_line_index(file_id)?;
let line_index = world.analysis.file_line_index(file_id)?;
let range = text_range(&line_index, range);
Ok(FileRange { file_id, range })
}

View file

@ -20,11 +20,12 @@ use crate::{
diagnostics::{CheckFixes, DiagnosticCollection},
from_proto,
line_endings::LineEndings,
main_loop::ReqQueue,
request_metrics::{LatestRequests, RequestMetrics},
to_proto::url_from_abs_path,
Result,
};
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};
fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> Option<Flycheck> {
// FIXME: Figure out the multi-workspace situation
@ -40,34 +41,48 @@ fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) ->
})
}
#[derive(Eq, PartialEq)]
pub(crate) enum Status {
Loading,
Ready,
}
impl Default for Status {
fn default() -> Self {
Status::Loading
}
}
/// `GlobalState` is the primary mutable state of the language server
///
/// The most interesting components are `vfs`, which stores a consistent
/// snapshot of the file systems, and `analysis_host`, which stores our
/// incremental salsa database.
#[derive(Debug)]
pub(crate) struct GlobalState {
pub(crate) config: Config,
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
pub(crate) analysis_host: AnalysisHost,
pub(crate) loader: Box<dyn vfs::loader::Handle>,
pub(crate) task_receiver: Receiver<vfs::loader::Message>,
pub(crate) flycheck: Option<Flycheck>,
pub(crate) diagnostics: DiagnosticCollection,
pub(crate) proc_macro_client: ProcMacroClient,
pub(crate) mem_docs: FxHashSet<VfsPath>,
pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
pub(crate) latest_requests: Arc<RwLock<LatestRequests>>,
pub(crate) status: Status,
pub(crate) req_queue: ReqQueue,
latest_requests: Arc<RwLock<LatestRequests>>,
source_root_config: SourceRootConfig,
_proc_macro_client: ProcMacroClient,
workspaces: Arc<Vec<ProjectWorkspace>>,
}
/// An immutable snapshot of the world's state at a point in time.
pub(crate) struct GlobalStateSnapshot {
pub(crate) config: Config,
pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
pub(crate) analysis: Analysis,
pub(crate) check_fixes: CheckFixes,
pub(crate) latest_requests: Arc<RwLock<LatestRequests>>,
vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
workspaces: Arc<Vec<ProjectWorkspace>>,
}
impl GlobalState {
@ -75,6 +90,7 @@ impl GlobalState {
workspaces: Vec<ProjectWorkspace>,
lru_capacity: Option<usize>,
config: Config,
req_queue: ReqQueue,
) -> GlobalState {
let mut change = AnalysisChange::new();
@ -133,22 +149,25 @@ impl GlobalState {
analysis_host.apply_change(change);
let mut res = GlobalState {
config,
workspaces: Arc::new(workspaces),
analysis_host,
loader,
vfs: Arc::new(RwLock::new((vfs, FxHashMap::default()))),
task_receiver,
latest_requests: Default::default(),
flycheck,
diagnostics: Default::default(),
proc_macro_client,
mem_docs: FxHashSet::default(),
vfs: Arc::new(RwLock::new((vfs, FxHashMap::default()))),
status: Status::default(),
req_queue,
latest_requests: Default::default(),
source_root_config: project_folders.source_root_config,
_proc_macro_client: proc_macro_client,
workspaces: Arc::new(workspaces),
};
res.process_changes();
res
}
pub fn update_configuration(&mut self, config: Config) {
pub(crate) fn update_configuration(&mut self, config: Config) {
self.analysis_host.update_lru_capacity(config.lru_capacity);
if config.check != self.config.check {
self.flycheck =
@ -158,7 +177,7 @@ impl GlobalState {
self.config = config;
}
pub fn process_changes(&mut self) -> bool {
pub(crate) fn process_changes(&mut self) -> bool {
let change = {
let mut change = AnalysisChange::new();
let (vfs, line_endings_map) = &mut *self.vfs.write();
@ -196,7 +215,7 @@ impl GlobalState {
true
}
pub fn snapshot(&self) -> GlobalStateSnapshot {
pub(crate) fn snapshot(&self) -> GlobalStateSnapshot {
GlobalStateSnapshot {
config: self.config.clone(),
workspaces: Arc::clone(&self.workspaces),
@ -207,11 +226,11 @@ impl GlobalState {
}
}
pub fn maybe_collect_garbage(&mut self) {
pub(crate) fn maybe_collect_garbage(&mut self) {
self.analysis_host.maybe_collect_garbage()
}
pub fn collect_garbage(&mut self) {
pub(crate) fn collect_garbage(&mut self) {
self.analysis_host.collect_garbage()
}
@ -221,10 +240,6 @@ impl GlobalState {
}
impl GlobalStateSnapshot {
pub(crate) fn analysis(&self) -> &Analysis {
&self.analysis
}
pub(crate) fn url_to_file_id(&self, url: &Url) -> Result<FileId> {
let path = from_proto::abs_path(url)?;
let path = path.into();
@ -253,7 +268,7 @@ impl GlobalStateSnapshot {
&self,
crate_id: CrateId,
) -> Option<(&CargoWorkspace, Target)> {
let file_id = self.analysis().crate_root(crate_id).ok()?;
let file_id = self.analysis.crate_root(crate_id).ok()?;
let path = self.vfs.read().0.file_path(file_id);
let path = path.as_path()?;
self.workspaces.iter().find_map(|ws| match ws {

View file

@ -56,9 +56,9 @@ pub(crate) fn handle_syntax_tree(
) -> Result<String> {
let _p = profile("handle_syntax_tree");
let id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(id)?;
let line_index = snap.analysis.file_line_index(id)?;
let text_range = params.range.map(|r| from_proto::text_range(&line_index, r));
let res = snap.analysis().syntax_tree(id, text_range)?;
let res = snap.analysis.syntax_tree(id, text_range)?;
Ok(res)
}
@ -68,10 +68,10 @@ pub(crate) fn handle_expand_macro(
) -> Result<Option<lsp_ext::ExpandedMacro>> {
let _p = profile("handle_expand_macro");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let offset = from_proto::offset(&line_index, params.position);
let res = snap.analysis().expand_macro(FilePosition { file_id, offset })?;
let res = snap.analysis.expand_macro(FilePosition { file_id, offset })?;
Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion }))
}
@ -81,7 +81,7 @@ pub(crate) fn handle_selection_range(
) -> Result<Option<Vec<lsp_types::SelectionRange>>> {
let _p = profile("handle_selection_range");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let res: Result<Vec<lsp_types::SelectionRange>> = params
.positions
.into_iter()
@ -93,7 +93,7 @@ pub(crate) fn handle_selection_range(
loop {
ranges.push(range);
let frange = FileRange { file_id, range };
let next = snap.analysis().extend_selection(frange)?;
let next = snap.analysis.extend_selection(frange)?;
if next == range {
break;
} else {
@ -124,13 +124,13 @@ pub(crate) fn handle_matching_brace(
) -> Result<Vec<Position>> {
let _p = profile("handle_matching_brace");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let res = params
.positions
.into_iter()
.map(|position| {
let offset = from_proto::offset(&line_index, position);
let offset = match snap.analysis().matching_brace(FilePosition { file_id, offset }) {
let offset = match snap.analysis.matching_brace(FilePosition { file_id, offset }) {
Ok(Some(matching_brace_offset)) => matching_brace_offset,
Err(_) | Ok(None) => offset,
};
@ -146,12 +146,12 @@ pub(crate) fn handle_join_lines(
) -> Result<Vec<lsp_types::TextEdit>> {
let _p = profile("handle_join_lines");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let line_endings = snap.file_line_endings(file_id);
let mut res = TextEdit::default();
for range in params.ranges {
let range = from_proto::text_range(&line_index, range);
let edit = snap.analysis().join_lines(FileRange { file_id, range })?;
let edit = snap.analysis.join_lines(FileRange { file_id, range })?;
match res.union(edit) {
Ok(()) => (),
Err(_edit) => {
@ -169,11 +169,11 @@ pub(crate) fn handle_on_enter(
) -> Result<Option<Vec<lsp_ext::SnippetTextEdit>>> {
let _p = profile("handle_on_enter");
let position = from_proto::file_position(&snap, params)?;
let edit = match snap.analysis().on_enter(position)? {
let edit = match snap.analysis.on_enter(position)? {
None => return Ok(None),
Some(it) => it,
};
let line_index = snap.analysis().file_line_index(position.file_id)?;
let line_index = snap.analysis.file_line_index(position.file_id)?;
let line_endings = snap.file_line_endings(position.file_id);
let edit = to_proto::snippet_text_edit_vec(&line_index, line_endings, true, edit);
Ok(Some(edit))
@ -186,7 +186,7 @@ pub(crate) fn handle_on_type_formatting(
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
let _p = profile("handle_on_type_formatting");
let mut position = from_proto::file_position(&snap, params.text_document_position)?;
let line_index = snap.analysis().file_line_index(position.file_id)?;
let line_index = snap.analysis.file_line_index(position.file_id)?;
let line_endings = snap.file_line_endings(position.file_id);
// in `ra_ide`, the `on_type` invariant is that
@ -194,7 +194,7 @@ pub(crate) fn handle_on_type_formatting(
position.offset -= TextSize::of('.');
let char_typed = params.ch.chars().next().unwrap_or('\0');
assert!({
let text = snap.analysis().file_text(position.file_id)?;
let text = snap.analysis.file_text(position.file_id)?;
text[usize::from(position.offset)..].starts_with(char_typed)
});
@ -206,7 +206,7 @@ pub(crate) fn handle_on_type_formatting(
return Ok(None);
}
let edit = snap.analysis().on_char_typed(position, char_typed)?;
let edit = snap.analysis.on_char_typed(position, char_typed)?;
let mut edit = match edit {
Some(it) => it,
None => return Ok(None),
@ -225,11 +225,11 @@ pub(crate) fn handle_document_symbol(
) -> Result<Option<lsp_types::DocumentSymbolResponse>> {
let _p = profile("handle_document_symbol");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let mut parents: Vec<(DocumentSymbol, Option<usize>)> = Vec::new();
for symbol in snap.analysis().file_structure(file_id)? {
for symbol in snap.analysis.file_structure(file_id)? {
let doc_symbol = DocumentSymbol {
name: symbol.label,
detail: symbol.detail,
@ -317,7 +317,7 @@ pub(crate) fn handle_workspace_symbol(
fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
let mut res = Vec::new();
for nav in snap.analysis().symbol_search(query)? {
for nav in snap.analysis.symbol_search(query)? {
let info = SymbolInformation {
name: nav.name().to_string(),
kind: to_proto::symbol_kind(nav.kind()),
@ -337,7 +337,7 @@ pub(crate) fn handle_goto_definition(
) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
let _p = profile("handle_goto_definition");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let nav_info = match snap.analysis().goto_definition(position)? {
let nav_info = match snap.analysis.goto_definition(position)? {
None => return Ok(None),
Some(it) => it,
};
@ -352,7 +352,7 @@ pub(crate) fn handle_goto_implementation(
) -> Result<Option<lsp_types::request::GotoImplementationResponse>> {
let _p = profile("handle_goto_implementation");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let nav_info = match snap.analysis().goto_implementation(position)? {
let nav_info = match snap.analysis.goto_implementation(position)? {
None => return Ok(None),
Some(it) => it,
};
@ -367,7 +367,7 @@ pub(crate) fn handle_goto_type_definition(
) -> Result<Option<lsp_types::request::GotoTypeDefinitionResponse>> {
let _p = profile("handle_goto_type_definition");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let nav_info = match snap.analysis().goto_type_definition(position)? {
let nav_info = match snap.analysis.goto_type_definition(position)? {
None => return Ok(None),
Some(it) => it,
};
@ -382,7 +382,7 @@ pub(crate) fn handle_parent_module(
) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
let _p = profile("handle_parent_module");
let position = from_proto::file_position(&snap, params)?;
let navs = snap.analysis().parent_module(position)?;
let navs = snap.analysis.parent_module(position)?;
let res = to_proto::goto_definition_response(&snap, None, navs)?;
Ok(Some(res))
}
@ -393,11 +393,11 @@ pub(crate) fn handle_runnables(
) -> Result<Vec<lsp_ext::Runnable>> {
let _p = profile("handle_runnables");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let offset = params.position.map(|it| from_proto::offset(&line_index, it));
let mut res = Vec::new();
let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
for runnable in snap.analysis().runnables(file_id)? {
for runnable in snap.analysis.runnables(file_id)? {
if let Some(offset) = offset {
if !runnable.nav.full_range().contains_inclusive(offset) {
continue;
@ -456,7 +456,7 @@ pub(crate) fn handle_completion(
let mut res = false;
if let Some(ctx) = params.context {
if ctx.trigger_character.unwrap_or_default() == ":" {
let source_file = snap.analysis().parse(position.file_id)?;
let source_file = snap.analysis.parse(position.file_id)?;
let syntax = source_file.syntax();
let text = syntax.text();
if let Some(next_char) = text.char_at(position.offset) {
@ -474,11 +474,11 @@ pub(crate) fn handle_completion(
return Ok(None);
}
let items = match snap.analysis().completions(&snap.config.completion, position)? {
let items = match snap.analysis.completions(&snap.config.completion, position)? {
None => return Ok(None),
Some(items) => items,
};
let line_index = snap.analysis().file_line_index(position.file_id)?;
let line_index = snap.analysis.file_line_index(position.file_id)?;
let line_endings = snap.file_line_endings(position.file_id);
let items: Vec<CompletionItem> = items
.into_iter()
@ -494,9 +494,9 @@ pub(crate) fn handle_folding_range(
) -> Result<Option<Vec<FoldingRange>>> {
let _p = profile("handle_folding_range");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let folds = snap.analysis().folding_ranges(file_id)?;
let text = snap.analysis().file_text(file_id)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let folds = snap.analysis.folding_ranges(file_id)?;
let text = snap.analysis.file_text(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let line_folding_only = snap.config.client_caps.line_folding_only;
let res = folds
.into_iter()
@ -511,7 +511,7 @@ pub(crate) fn handle_signature_help(
) -> Result<Option<lsp_types::SignatureHelp>> {
let _p = profile("handle_signature_help");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let call_info = match snap.analysis().call_info(position)? {
let call_info = match snap.analysis.call_info(position)? {
None => return Ok(None),
Some(it) => it,
};
@ -535,7 +535,7 @@ pub(crate) fn handle_hover(
) -> Result<Option<lsp_ext::Hover>> {
let _p = profile("handle_hover");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let info = match snap.analysis().hover(position)? {
let info = match snap.analysis.hover(position)? {
None => return Ok(None),
Some(info) => info,
};
@ -562,13 +562,13 @@ pub(crate) fn handle_prepare_rename(
let _p = profile("handle_prepare_rename");
let position = from_proto::file_position(&snap, params)?;
let optional_change = snap.analysis().rename(position, "dummy")?;
let optional_change = snap.analysis.rename(position, "dummy")?;
let range = match optional_change {
None => return Ok(None),
Some(it) => it.range,
};
let line_index = snap.analysis().file_line_index(position.file_id)?;
let line_index = snap.analysis.file_line_index(position.file_id)?;
let range = to_proto::range(&line_index, range);
Ok(Some(PrepareRenameResponse::Range(range)))
}
@ -588,7 +588,7 @@ pub(crate) fn handle_rename(
.into());
}
let optional_change = snap.analysis().rename(position, &*params.new_name)?;
let optional_change = snap.analysis.rename(position, &*params.new_name)?;
let source_change = match optional_change {
None => return Ok(None),
Some(it) => it.info,
@ -604,7 +604,7 @@ pub(crate) fn handle_references(
let _p = profile("handle_references");
let position = from_proto::file_position(&snap, params.text_document_position)?;
let refs = match snap.analysis().find_all_refs(position, None)? {
let refs = match snap.analysis.find_all_refs(position, None)? {
None => return Ok(None),
Some(refs) => refs,
};
@ -630,10 +630,10 @@ pub(crate) fn handle_formatting(
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
let _p = profile("handle_formatting");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let file = snap.analysis().file_text(file_id)?;
let crate_ids = snap.analysis().crate_for(file_id)?;
let file = snap.analysis.file_text(file_id)?;
let crate_ids = snap.analysis.crate_for(file_id)?;
let file_line_index = snap.analysis().file_line_index(file_id)?;
let file_line_index = snap.analysis.file_line_index(file_id)?;
let end_position = to_proto::position(&file_line_index, TextSize::of(file.as_str()));
let mut rustfmt = match &snap.config.rustfmt {
@ -642,7 +642,7 @@ pub(crate) fn handle_formatting(
cmd.args(extra_args);
if let Some(&crate_id) = crate_ids.first() {
// Assume all crates are in the same edition
let edition = snap.analysis().crate_edition(crate_id)?;
let edition = snap.analysis.crate_edition(crate_id)?;
cmd.arg("--edition");
cmd.arg(edition.to_string());
}
@ -706,9 +706,9 @@ fn handle_fixes(
res: &mut Vec<lsp_ext::CodeAction>,
) -> Result<()> {
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let range = from_proto::text_range(&line_index, params.range);
let diagnostics = snap.analysis().diagnostics(file_id)?;
let diagnostics = snap.analysis.diagnostics(file_id)?;
let fixes_from_diagnostics = diagnostics
.into_iter()
@ -752,7 +752,7 @@ pub(crate) fn handle_code_action(
}
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let range = from_proto::text_range(&line_index, params.range);
let frange = FileRange { file_id, range };
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
@ -761,12 +761,12 @@ pub(crate) fn handle_code_action(
if snap.config.client_caps.resolve_code_action {
for (index, assist) in
snap.analysis().unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate()
snap.analysis.unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate()
{
res.push(to_proto::unresolved_code_action(&snap, assist, index)?);
}
} else {
for assist in snap.analysis().resolved_assists(&snap.config.assist, frange)?.into_iter() {
for assist in snap.analysis.resolved_assists(&snap.config.assist, frange)?.into_iter() {
res.push(to_proto::resolved_code_action(&snap, assist)?);
}
}
@ -780,11 +780,11 @@ pub(crate) fn handle_resolve_code_action(
) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> {
let _p = profile("handle_resolve_code_action");
let file_id = from_proto::file_id(&snap, &params.code_action_params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let range = from_proto::text_range(&line_index, params.code_action_params.range);
let frange = FileRange { file_id, range };
let assists = snap.analysis().resolved_assists(&snap.config.assist, frange)?;
let assists = snap.analysis.resolved_assists(&snap.config.assist, frange)?;
let (id_string, index) = split_delim(&params.id, ':').unwrap();
let index = index.parse::<usize>().unwrap();
let assist = &assists[index];
@ -805,12 +805,12 @@ pub(crate) fn handle_code_lens(
}
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
if snap.config.lens.runnable() {
// Gather runnables
for runnable in snap.analysis().runnables(file_id)? {
for runnable in snap.analysis.runnables(file_id)? {
if should_skip_target(&runnable, cargo_spec.as_ref()) {
continue;
}
@ -838,7 +838,7 @@ pub(crate) fn handle_code_lens(
if snap.config.lens.impementations {
// Handle impls
lenses.extend(
snap.analysis()
snap.analysis
.file_structure(file_id)?
.into_iter()
.filter(|it| match it.kind {
@ -916,10 +916,10 @@ pub(crate) fn handle_document_highlight(
) -> Result<Option<Vec<DocumentHighlight>>> {
let _p = profile("handle_document_highlight");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let line_index = snap.analysis().file_line_index(position.file_id)?;
let line_index = snap.analysis.file_line_index(position.file_id)?;
let refs = match snap
.analysis()
.analysis
.find_all_refs(position, Some(SearchScope::single_file(position.file_id)))?
{
None => return Ok(None),
@ -943,7 +943,7 @@ pub(crate) fn handle_ssr(
) -> Result<lsp_types::WorkspaceEdit> {
let _p = profile("handle_ssr");
let source_change =
snap.analysis().structural_search_replace(&params.query, params.parse_only)??;
snap.analysis.structural_search_replace(&params.query, params.parse_only)??;
to_proto::workspace_edit(&snap, source_change)
}
@ -952,9 +952,9 @@ pub(crate) fn publish_diagnostics(
file_id: FileId,
) -> Result<DiagnosticTask> {
let _p = profile("publish_diagnostics");
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let diagnostics: Vec<Diagnostic> = snap
.analysis()
.analysis
.diagnostics(file_id)?
.into_iter()
.map(|d| Diagnostic {
@ -976,9 +976,9 @@ pub(crate) fn handle_inlay_hints(
) -> Result<Vec<InlayHint>> {
let _p = profile("handle_inlay_hints");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let analysis = snap.analysis();
let line_index = analysis.file_line_index(file_id)?;
Ok(analysis
let line_index = snap.analysis.file_line_index(file_id)?;
Ok(snap
.analysis
.inlay_hints(file_id, &snap.config.inlay_hints)?
.into_iter()
.map(|it| to_proto::inlay_int(&line_index, it))
@ -992,7 +992,7 @@ pub(crate) fn handle_call_hierarchy_prepare(
let _p = profile("handle_call_hierarchy_prepare");
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
let nav_info = match snap.analysis().call_hierarchy(position)? {
let nav_info = match snap.analysis.call_hierarchy(position)? {
None => return Ok(None),
Some(it) => it,
};
@ -1018,7 +1018,7 @@ pub(crate) fn handle_call_hierarchy_incoming(
let frange = from_proto::file_range(&snap, doc, item.range)?;
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
let call_items = match snap.analysis().incoming_calls(fpos)? {
let call_items = match snap.analysis.incoming_calls(fpos)? {
None => return Ok(None),
Some(it) => it,
};
@ -1027,7 +1027,7 @@ pub(crate) fn handle_call_hierarchy_incoming(
for call_item in call_items.into_iter() {
let file_id = call_item.target.file_id();
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
res.push(CallHierarchyIncomingCall {
from: item,
@ -1053,7 +1053,7 @@ pub(crate) fn handle_call_hierarchy_outgoing(
let frange = from_proto::file_range(&snap, doc, item.range)?;
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
let call_items = match snap.analysis().outgoing_calls(fpos)? {
let call_items = match snap.analysis.outgoing_calls(fpos)? {
None => return Ok(None),
Some(it) => it,
};
@ -1062,7 +1062,7 @@ pub(crate) fn handle_call_hierarchy_outgoing(
for call_item in call_items.into_iter() {
let file_id = call_item.target.file_id();
let line_index = snap.analysis().file_line_index(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let item = to_proto::call_hierarchy_item(&snap, call_item.target)?;
res.push(CallHierarchyOutgoingCall {
to: item,
@ -1084,10 +1084,10 @@ pub(crate) fn handle_semantic_tokens(
let _p = profile("handle_semantic_tokens");
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let text = snap.analysis().file_text(file_id)?;
let line_index = snap.analysis().file_line_index(file_id)?;
let text = snap.analysis.file_text(file_id)?;
let line_index = snap.analysis.file_line_index(file_id)?;
let highlights = snap.analysis().highlight(file_id)?;
let highlights = snap.analysis.highlight(file_id)?;
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
Ok(Some(semantic_tokens.into()))
}
@ -1099,10 +1099,10 @@ pub(crate) fn handle_semantic_tokens_range(
let _p = profile("handle_semantic_tokens_range");
let frange = from_proto::file_range(&snap, params.text_document, params.range)?;
let text = snap.analysis().file_text(frange.file_id)?;
let line_index = snap.analysis().file_line_index(frange.file_id)?;
let text = snap.analysis.file_text(frange.file_id)?;
let line_index = snap.analysis.file_line_index(frange.file_id)?;
let highlights = snap.analysis().highlight_range(frange)?;
let highlights = snap.analysis.highlight_range(frange)?;
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
Ok(Some(semantic_tokens.into()))
}
@ -1178,9 +1178,9 @@ fn show_impl_command_link(
position: &FilePosition,
) -> Option<lsp_ext::CommandLinkGroup> {
if snap.config.hover.implementations {
if let Some(nav_data) = snap.analysis().goto_implementation(*position).unwrap_or(None) {
if let Some(nav_data) = snap.analysis.goto_implementation(*position).unwrap_or(None) {
let uri = to_proto::url(snap, position.file_id);
let line_index = snap.analysis().file_line_index(position.file_id).ok()?;
let line_index = snap.analysis.file_line_index(position.file_id).ok()?;
let position = to_proto::position(&line_index, position.offset);
let locations: Vec<_> = nav_data
.info

View file

@ -37,12 +37,32 @@ use serde::de::DeserializeOwned;
pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
pub use crate::{
caps::server_capabilities,
main_loop::LspError,
main_loop::{main_loop, show_message},
};
use std::fmt;
pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
let res = T::deserialize(&json)
.map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?;
Ok(res)
}
#[derive(Debug)]
struct LspError {
code: i32,
message: String,
}
impl LspError {
fn new(code: i32, message: String) -> LspError {
LspError { code, message }
}
}
impl fmt::Display for LspError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
}
}
impl std::error::Error for LspError {}

View file

@ -1,9 +1,7 @@
//! The main loop of `rust-analyzer` responsible for dispatching LSP
//! requests/replies and notifications back to the client.
use std::{
env,
error::Error,
fmt,
env, fmt,
ops::Range,
panic,
sync::Arc,
@ -11,16 +9,13 @@ use std::{
};
use crossbeam_channel::{never, select, unbounded, RecvError, Sender};
use lsp_server::{
Connection, ErrorCode, Message, Notification, ReqQueue, Request, RequestId, Response,
};
use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response};
use lsp_types::{request::Request as _, NumberOrString, TextDocumentContentChangeEvent};
use ra_db::VfsPath;
use ra_flycheck::CheckTask;
use ra_ide::{Canceled, FileId, LineIndex};
use ra_prof::profile;
use ra_project_model::{PackageRoot, ProjectWorkspace};
use rustc_hash::FxHashSet;
use serde::{de::DeserializeOwned, Serialize};
use threadpool::ThreadPool;
@ -28,34 +23,12 @@ use crate::{
config::{Config, FilesWatcher, LinkedProject},
diagnostics::DiagnosticTask,
from_proto,
global_state::{file_id_to_url, GlobalState, GlobalStateSnapshot},
global_state::{file_id_to_url, GlobalState, GlobalStateSnapshot, Status},
handlers, lsp_ext,
request_metrics::RequestMetrics,
Result,
LspError, Result,
};
#[derive(Debug)]
pub struct LspError {
pub code: i32,
pub message: String,
}
impl LspError {
pub const UNKNOWN_FILE: i32 = -32900;
pub fn new(code: i32, message: String) -> LspError {
LspError { code, message }
}
}
impl fmt::Display for LspError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
}
}
impl Error for LspError {}
pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
log::info!("initial config: {:#?}", config);
@ -78,7 +51,6 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
SetThreadPriority(thread, thread_priority_above_normal);
}
let mut loop_state = LoopState::default();
let mut global_state = {
let workspaces = {
if config.linked_projects.is_empty() && config.notifications.cargo_toml_not_found {
@ -116,6 +88,8 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
.collect::<Vec<_>>()
};
let mut req_queue = ReqQueue::default();
if let FilesWatcher::Client = config.files.watcher {
let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
watchers: workspaces
@ -132,7 +106,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
register_options: Some(serde_json::to_value(registration_options).unwrap()),
};
let params = lsp_types::RegistrationParams { registrations: vec![registration] };
let request = loop_state.req_queue.outgoing.register(
let request = req_queue.outgoing.register(
lsp_types::request::RegisterCapability::METHOD.to_string(),
params,
DO_NOTHING,
@ -140,7 +114,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
connection.sender.send(request.into()).unwrap();
}
GlobalState::new(workspaces, config.lru_capacity, config)
GlobalState::new(workspaces, config.lru_capacity, config, req_queue)
};
let pool = ThreadPool::default();
@ -172,15 +146,13 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
};
}
assert!(!global_state.vfs.read().0.has_changes());
loop_turn(&pool, &task_sender, &connection, &mut global_state, &mut loop_state, event)?;
loop_turn(&pool, &task_sender, &connection, &mut global_state, event)?;
assert!(!global_state.vfs.read().0.has_changes());
}
}
global_state.analysis_host.request_cancellation();
log::info!("waiting for tasks to finish...");
task_receiver.into_iter().for_each(|task| {
on_task(task, &connection.sender, &mut loop_state.req_queue.incoming, &mut global_state)
});
task_receiver.into_iter().for_each(|task| on_task(task, &connection.sender, &mut global_state));
log::info!("...tasks have finished");
log::info!("joining threadpool...");
pool.join();
@ -244,35 +216,15 @@ impl fmt::Debug for Event {
}
}
type ReqHandler = fn(&mut GlobalState, Response);
pub(crate) type ReqHandler = fn(&mut GlobalState, Response);
pub(crate) type ReqQueue = lsp_server::ReqQueue<(&'static str, Instant), ReqHandler>;
const DO_NOTHING: ReqHandler = |_, _| ();
type Incoming = lsp_server::Incoming<(&'static str, Instant)>;
#[derive(Default)]
struct LoopState {
req_queue: ReqQueue<(&'static str, Instant), ReqHandler>,
mem_docs: FxHashSet<VfsPath>,
status: Status,
}
#[derive(Eq, PartialEq)]
enum Status {
Loading,
Ready,
}
impl Default for Status {
fn default() -> Self {
Status::Loading
}
}
fn loop_turn(
pool: &ThreadPool,
task_sender: &Sender<Task>,
connection: &Connection,
global_state: &mut GlobalState,
loop_state: &mut LoopState,
event: Event,
) -> Result<()> {
let loop_start = Instant::now();
@ -288,7 +240,7 @@ fn loop_turn(
let mut became_ready = false;
match event {
Event::Task(task) => {
on_task(task, &connection.sender, &mut loop_state.req_queue.incoming, global_state);
on_task(task, &connection.sender, global_state);
global_state.maybe_collect_garbage();
}
Event::Vfs(task) => match task {
@ -296,35 +248,29 @@ fn loop_turn(
let vfs = &mut global_state.vfs.write().0;
for (path, contents) in files {
let path = VfsPath::from(path);
if !loop_state.mem_docs.contains(&path) {
if !global_state.mem_docs.contains(&path) {
vfs.set_file_contents(path, contents)
}
}
}
vfs::loader::Message::Progress { n_total, n_done } => {
if n_done == n_total {
loop_state.status = Status::Ready;
global_state.status = Status::Ready;
became_ready = true;
}
report_progress(loop_state, &connection.sender, n_done, n_total, "roots scanned")
report_progress(global_state, &connection.sender, n_done, n_total, "roots scanned")
}
},
Event::CheckWatcher(task) => on_check_task(task, global_state, task_sender)?,
Event::Msg(msg) => match msg {
Message::Request(req) => on_request(
global_state,
&mut loop_state.req_queue.incoming,
pool,
task_sender,
&connection.sender,
loop_start,
req,
)?,
Message::Request(req) => {
on_request(global_state, pool, task_sender, &connection.sender, loop_start, req)?
}
Message::Notification(not) => {
on_notification(&connection.sender, global_state, loop_state, not)?;
on_notification(&connection.sender, global_state, not)?;
}
Message::Response(resp) => {
let handler = loop_state.req_queue.outgoing.complete(resp.id.clone());
let handler = global_state.req_queue.outgoing.complete(resp.id.clone());
handler(global_state, resp)
}
},
@ -338,8 +284,8 @@ fn loop_turn(
}
}
if loop_state.status == Status::Ready && (state_changed || became_ready) {
let subscriptions = loop_state
if global_state.status == Status::Ready && (state_changed || became_ready) {
let subscriptions = global_state
.mem_docs
.iter()
.map(|path| global_state.vfs.read().0.file_id(&path).unwrap())
@ -354,7 +300,7 @@ fn loop_turn(
pool.execute({
let subs = subscriptions;
let snap = global_state.snapshot();
move || snap.analysis().prime_caches(subs).unwrap_or_else(|_: Canceled| ())
move || snap.analysis.prime_caches(subs).unwrap_or_else(|_: Canceled| ())
});
}
@ -373,18 +319,15 @@ fn loop_turn(
Ok(())
}
fn on_task(
task: Task,
msg_sender: &Sender<Message>,
incoming_requests: &mut Incoming,
state: &mut GlobalState,
) {
fn on_task(task: Task, msg_sender: &Sender<Message>, global_state: &mut GlobalState) {
match task {
Task::Respond(response) => {
if let Some((method, start)) = incoming_requests.complete(response.id.clone()) {
if let Some((method, start)) =
global_state.req_queue.incoming.complete(response.id.clone())
{
let duration = start.elapsed();
log::info!("handled req#{} in {:?}", response.id, duration);
state.complete_request(RequestMetrics {
global_state.complete_request(RequestMetrics {
id: response.id.clone(),
method: method.to_string(),
duration,
@ -395,13 +338,12 @@ fn on_task(
Task::Notify(n) => {
msg_sender.send(n.into()).unwrap();
}
Task::Diagnostic(task) => on_diagnostic_task(task, msg_sender, state),
Task::Diagnostic(task) => on_diagnostic_task(task, msg_sender, global_state),
}
}
fn on_request(
global_state: &mut GlobalState,
incoming_requests: &mut Incoming,
pool: &ThreadPool,
task_sender: &Sender<Task>,
msg_sender: &Sender<Message>,
@ -414,7 +356,6 @@ fn on_request(
global_state,
task_sender,
msg_sender,
incoming_requests,
request_received,
};
pool_dispatcher
@ -469,7 +410,6 @@ fn on_request(
fn on_notification(
msg_sender: &Sender<Message>,
global_state: &mut GlobalState,
loop_state: &mut LoopState,
not: Notification,
) -> Result<()> {
let not = match notification_cast::<lsp_types::notification::Cancel>(not) {
@ -478,7 +418,7 @@ fn on_notification(
NumberOrString::Number(id) => id.into(),
NumberOrString::String(id) => id.into(),
};
if let Some(response) = loop_state.req_queue.incoming.cancel(id) {
if let Some(response) = global_state.req_queue.incoming.cancel(id) {
msg_sender.send(response.into()).unwrap()
}
return Ok(());
@ -488,7 +428,7 @@ fn on_notification(
let not = match notification_cast::<lsp_types::notification::DidOpenTextDocument>(not) {
Ok(params) => {
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
if !loop_state.mem_docs.insert(path.clone()) {
if !global_state.mem_docs.insert(path.clone()) {
log::error!("duplicate DidOpenTextDocument: {}", path)
}
global_state
@ -504,7 +444,7 @@ fn on_notification(
let not = match notification_cast::<lsp_types::notification::DidChangeTextDocument>(not) {
Ok(params) => {
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
assert!(loop_state.mem_docs.contains(&path));
assert!(global_state.mem_docs.contains(&path));
let vfs = &mut global_state.vfs.write().0;
let file_id = vfs.file_id(&path).unwrap();
let mut text = String::from_utf8(vfs.file_contents(file_id).to_vec()).unwrap();
@ -518,7 +458,7 @@ fn on_notification(
let not = match notification_cast::<lsp_types::notification::DidCloseTextDocument>(not) {
Ok(params) => {
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
if !loop_state.mem_docs.remove(&path) {
if !global_state.mem_docs.remove(&path) {
log::error!("orphan DidCloseTextDocument: {}", path)
}
if let Some(path) = path.as_path() {
@ -549,7 +489,7 @@ fn on_notification(
Ok(_) => {
// As stated in https://github.com/microsoft/language-server-protocol/issues/676,
// this notification's parameters should be ignored and the actual config queried separately.
let request = loop_state.req_queue.outgoing.register(
let request = global_state.req_queue.outgoing.register(
lsp_types::request::WorkspaceConfiguration::METHOD.to_string(),
lsp_types::ConfigurationParams {
items: vec![lsp_types::ConfigurationItem {
@ -732,7 +672,7 @@ fn on_diagnostic_task(task: DiagnosticTask, msg_sender: &Sender<Message>, state:
}
fn report_progress(
loop_state: &mut LoopState,
global_state: &mut GlobalState,
sender: &Sender<Message>,
done: usize,
total: usize,
@ -742,7 +682,7 @@ fn report_progress(
let message = Some(format!("{}/{} {}", done, total, message));
let percentage = Some(100.0 * done as f64 / total.max(1) as f64);
let work_done_progress = if done == 0 {
let work_done_progress_create = loop_state.req_queue.outgoing.register(
let work_done_progress_create = global_state.req_queue.outgoing.register(
lsp_types::request::WorkDoneProgressCreate::METHOD.to_string(),
lsp_types::WorkDoneProgressCreateParams { token: token.clone() },
DO_NOTHING,
@ -777,7 +717,6 @@ struct PoolDispatcher<'a> {
req: Option<Request>,
pool: &'a ThreadPool,
global_state: &'a mut GlobalState,
incoming_requests: &'a mut Incoming,
msg_sender: &'a Sender<Message>,
task_sender: &'a Sender<Task>,
request_received: Instant,
@ -806,7 +745,7 @@ impl<'a> PoolDispatcher<'a> {
result_to_task::<R>(id, result)
})
.map_err(|_| format!("sync task {:?} panicked", R::METHOD))?;
on_task(task, self.msg_sender, self.incoming_requests, self.global_state);
on_task(task, self.msg_sender, self.global_state);
Ok(self)
}
@ -853,7 +792,10 @@ impl<'a> PoolDispatcher<'a> {
return None;
}
};
self.incoming_requests.register(id.clone(), (R::METHOD, self.request_received));
self.global_state
.req_queue
.incoming
.register(id.clone(), (R::METHOD, self.request_received));
Some((id, params))
}
@ -882,14 +824,7 @@ where
let response = match result {
Ok(resp) => Response::new_ok(id, &resp),
Err(e) => match e.downcast::<LspError>() {
Ok(lsp_error) => {
if lsp_error.code == LspError::UNKNOWN_FILE {
// Work-around for https://github.com/rust-analyzer/rust-analyzer/issues/1521
Response::new_ok(id, ())
} else {
Response::new_err(id, lsp_error.code, lsp_error.message)
}
}
Ok(lsp_error) => Response::new_err(id, lsp_error.code, lsp_error.message),
Err(e) => {
if is_canceled(&e) {
Response::new_err(

View file

@ -440,7 +440,7 @@ pub(crate) fn location(
frange: FileRange,
) -> Result<lsp_types::Location> {
let url = url(snap, frange.file_id);
let line_index = snap.analysis().file_line_index(frange.file_id)?;
let line_index = snap.analysis.file_line_index(frange.file_id)?;
let range = range(&line_index, frange.range);
let loc = lsp_types::Location::new(url, range);
Ok(loc)
@ -453,7 +453,7 @@ pub(crate) fn location_link(
) -> Result<lsp_types::LocationLink> {
let origin_selection_range = match src {
Some(src) => {
let line_index = snap.analysis().file_line_index(src.file_id)?;
let line_index = snap.analysis.file_line_index(src.file_id)?;
let range = range(&line_index, src.range);
Some(range)
}
@ -473,7 +473,7 @@ fn location_info(
snap: &GlobalStateSnapshot,
target: NavigationTarget,
) -> Result<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
let line_index = snap.analysis().file_line_index(target.file_id())?;
let line_index = snap.analysis.file_line_index(target.file_id())?;
let target_uri = url(snap, target.file_id());
let target_range = range(&line_index, target.full_range());
@ -516,7 +516,7 @@ pub(crate) fn snippet_text_document_edit(
source_file_edit: SourceFileEdit,
) -> Result<lsp_ext::SnippetTextDocumentEdit> {
let text_document = versioned_text_document_identifier(snap, source_file_edit.file_id, None);
let line_index = snap.analysis().file_line_index(source_file_edit.file_id)?;
let line_index = snap.analysis.file_line_index(source_file_edit.file_id)?;
let line_endings = snap.file_line_endings(source_file_edit.file_id);
let edits = source_file_edit
.edit