From ecfe7c04888a9c2567773370b127d2e6e1cdaa22 Mon Sep 17 00:00:00 2001 From: Bruno Ortiz Date: Tue, 2 May 2023 11:24:08 -0300 Subject: [PATCH] last fixes after rebase --- crates/rust-analyzer/src/handlers.rs | 64 ++------------------ crates/rust-analyzer/src/handlers/request.rs | 57 ++++++++++++++++- docs/dev/lsp-extensions.md | 2 +- 3 files changed, 61 insertions(+), 62 deletions(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 76ad847b133..c19be196544 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -3,16 +3,11 @@ //! `ide` crate. use ide::AssistResolveStrategy; -use lsp_types::{Diagnostic, DiagnosticTag, NumberOrString}; +use lsp_types::{Diagnostic, DiagnosticTag, NumberOrString, Url}; + use vfs::FileId; -use crate::{ - global_state::GlobalStateSnapshot, to_proto, Result, - lsp_ext::{ - CrateInfoResult, FetchDependencyListResult, FetchDependencyListParams, - }, -}; - +use crate::{global_state::GlobalStateSnapshot, to_proto, Result}; pub(crate) mod request; pub(crate) mod notification; @@ -33,11 +28,11 @@ pub(crate) fn publish_diagnostics( severity: Some(to_proto::diagnostic_severity(d.severity)), code: Some(NumberOrString::String(d.code.as_str().to_string())), code_description: Some(lsp_types::CodeDescription { - href: lsp_types::Url::parse(&format!( + href: Url::parse(&format!( "https://rust-analyzer.github.io/manual.html#{}", d.code.as_str() )) - .unwrap(), + .unwrap(), }), source: Some("rust-analyzer".to_string()), message: d.message, @@ -48,52 +43,3 @@ pub(crate) fn publish_diagnostics( .collect(); Ok(diagnostics) } - -pub(crate) fn fetch_dependency_list( - state: GlobalStateSnapshot, - _params: lsp_ext::FetchDependencyListParams, -) -> Result { - let crates = state.analysis.fetch_crates()?; - let crate_infos = crates - .into_iter() - .filter_map(|it| { - let root_file_path = state.file_id_to_file_path(it.root_file_id); - crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult { - name: it.name, - version: it.version, - path, - }) - }) - .collect(); - Ok(FetchDependencyListResult { crates: crate_infos }) -} - -/// Searches for the directory of a Rust crate given this crate's root file path. -/// -/// # Arguments -/// -/// * `root_file_path`: The path to the root file of the crate. -/// -/// # Returns -/// -/// An `Option` value representing the path to the directory of the crate with the given -/// name, if such a crate is found. If no crate with the given name is found, this function -/// returns `None`. -fn crate_path(root_file_path: VfsPath) -> Option { - let mut current_dir = root_file_path.parent(); - while let Some(path) = current_dir { - let cargo_toml_path = path.join("../Cargo.toml")?; - if fs::metadata(cargo_toml_path.as_path()?).is_ok() { - let crate_path = cargo_toml_path.parent()?; - return Some(crate_path); - } - current_dir = path.parent(); - } - None -} - -fn to_url(path: VfsPath) -> Option { - let path = path.as_path()?; - let str_path = path.as_os_str().to_str()?; - Url::from_file_path(str_path).ok() -} \ No newline at end of file diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index 03e08d9cdfc..f25dc74a142 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -2,6 +2,7 @@ //! Protocol. This module specifically handles requests. use std::{ + fs, io::Write as _, process::{self, Stdio}, sync::Arc, @@ -29,7 +30,7 @@ use project_model::{ManifestPath, ProjectWorkspace, TargetKind}; use serde_json::json; use stdx::{format_to, never}; use syntax::{algo, ast, AstNode, TextRange, TextSize}; -use vfs::{AbsPath, AbsPathBuf}; +use vfs::{AbsPath, AbsPathBuf, VfsPath}; use crate::{ cargo_target_spec::CargoTargetSpec, @@ -38,7 +39,10 @@ use crate::{ from_proto, global_state::{GlobalState, GlobalStateSnapshot}, line_index::LineEndings, - lsp_ext::{self, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams}, + lsp_ext::{ + self, CrateInfoResult, FetchDependencyListParams, FetchDependencyListResult, + PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams, + }, lsp_utils::{all_edits_are_disjoint, invalid_params_error}, to_proto, LspError, Result, }; @@ -1881,3 +1885,52 @@ fn run_rustfmt( Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text)))) } } + +pub(crate) fn fetch_dependency_list( + state: GlobalStateSnapshot, + _params: FetchDependencyListParams, +) -> Result { + let crates = state.analysis.fetch_crates()?; + let crate_infos = crates + .into_iter() + .filter_map(|it| { + let root_file_path = state.file_id_to_file_path(it.root_file_id); + crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult { + name: it.name, + version: it.version, + path, + }) + }) + .collect(); + Ok(FetchDependencyListResult { crates: crate_infos }) +} + +/// Searches for the directory of a Rust crate given this crate's root file path. +/// +/// # Arguments +/// +/// * `root_file_path`: The path to the root file of the crate. +/// +/// # Returns +/// +/// An `Option` value representing the path to the directory of the crate with the given +/// name, if such a crate is found. If no crate with the given name is found, this function +/// returns `None`. +fn crate_path(root_file_path: VfsPath) -> Option { + let mut current_dir = root_file_path.parent(); + while let Some(path) = current_dir { + let cargo_toml_path = path.join("../Cargo.toml")?; + if fs::metadata(cargo_toml_path.as_path()?).is_ok() { + let crate_path = cargo_toml_path.parent()?; + return Some(crate_path); + } + current_dir = path.parent(); + } + None +} + +fn to_url(path: VfsPath) -> Option { + let path = path.as_path()?; + let str_path = path.as_os_str().to_str()?; + Url::from_file_path(str_path).ok() +} diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 0fb92638e0d..a4ad3e5a553 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -1,5 +1,5 @@