From 1b8288ff966bcabadf8f15d71511ff96184e81ea Mon Sep 17 00:00:00 2001 From: Bruno Ortiz Date: Tue, 4 Apr 2023 13:47:01 -0300 Subject: [PATCH] Fixing naming from graph to list --- crates/ide/src/fetch_crates.rs | 52 +++++++++++++---------- crates/rust-analyzer/src/handlers.rs | 10 ++--- crates/rust-analyzer/src/lsp_ext.rs | 14 +++--- crates/rust-analyzer/src/main_loop.rs | 2 +- docs/dev/lsp-extensions.md | 2 +- editors/code/src/dependencies_provider.ts | 8 ++-- editors/code/src/lsp_ext.ts | 12 +++--- 7 files changed, 52 insertions(+), 48 deletions(-) diff --git a/crates/ide/src/fetch_crates.rs b/crates/ide/src/fetch_crates.rs index ec6d66d5c43..416082ae73b 100644 --- a/crates/ide/src/fetch_crates.rs +++ b/crates/ide/src/fetch_crates.rs @@ -22,16 +22,21 @@ pub(crate) fn fetch_crates(db: &RootDatabase) -> Vec { .iter() .map(|crate_id| &crate_graph[crate_id]) .filter(|&data| !matches!(data.origin, CrateOrigin::Local { .. })) - .map(|data| { - let crate_name = crate_name(data); - let version = data.version.clone().unwrap_or_else(|| "".to_owned()); - let crate_path = crate_path(db, data, &crate_name); - - CrateInfo { name: crate_name, version, path: crate_path } - }) + .filter_map(|data| crate_info(data, db)) .collect() } +fn crate_info(data: &ide_db::base_db::CrateData, db: &RootDatabase) -> Option { + let crate_name = crate_name(data); + let crate_path = crate_path(db, data, &crate_name); + if let Some(crate_path) = crate_path { + let version = data.version.clone().unwrap_or_else(|| "".to_owned()); + Some(CrateInfo { name: crate_name, version, path: crate_path }) + } else { + None + } +} + fn crate_name(data: &ide_db::base_db::CrateData) -> String { data.display_name .clone() @@ -39,27 +44,28 @@ fn crate_name(data: &ide_db::base_db::CrateData) -> String { .unwrap_or("unknown".to_string()) } -fn crate_path(db: &RootDatabase, data: &ide_db::base_db::CrateData, crate_name: &str) -> String { +fn crate_path( + db: &RootDatabase, + data: &ide_db::base_db::CrateData, + crate_name: &str, +) -> Option { let source_root_id = db.file_source_root(data.root_file_id); let source_root = db.source_root(source_root_id); let source_root_path = source_root.path_for_file(&data.root_file_id); - match source_root_path.cloned() { - Some(mut root_path) => { - let mut crate_path = "".to_string(); - while let Some(vfs_path) = root_path.parent() { - match vfs_path.name_and_extension() { - Some((name, _)) => { - if name.starts_with(crate_name) { - crate_path = vfs_path.to_string(); - break; - } + source_root_path.cloned().and_then(|mut root_path| { + let mut crate_path = None; + while let Some(vfs_path) = root_path.parent() { + match vfs_path.name_and_extension() { + Some((name, _)) => { + if name.starts_with(crate_name) { + crate_path = Some(vfs_path.to_string()); + break; } - None => break, } - root_path = vfs_path; + None => break, } - crate_path + root_path = vfs_path; } - None => "".to_owned(), - } + crate_path + }) } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 2324490e53b..5c966f0f68b 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -9,7 +9,7 @@ use crate::{ global_state::GlobalStateSnapshot, to_proto, Result, lsp_ext::{ - CrateInfoResult, FetchDependencyGraphResult, FetchDependencyGraphParams, + CrateInfoResult, FetchDependencyListResult, FetchDependencyListParams, }, }; @@ -49,12 +49,12 @@ pub(crate) fn publish_diagnostics( Ok(diagnostics) } -pub(crate) fn fetch_dependency_graph( +pub(crate) fn fetch_dependency_list( state: GlobalStateSnapshot, - _params: FetchDependencyGraphParams, -) -> Result { + _params: FetchDependencyListParams, +) -> Result { let crates = state.analysis.fetch_crates()?; - Ok(FetchDependencyGraphResult { + Ok(FetchDependencyListResult { crates: crates .into_iter() .map(|it| CrateInfoResult { name: it.name, version: it.version, path: it.path }) diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 18511da4683..6285578a6ce 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -34,21 +34,21 @@ pub struct CrateInfoResult { pub version: String, pub path: String, } -pub enum FetchDependencyGraph {} +pub enum FetchDependencyList {} -impl Request for FetchDependencyGraph { - type Params = FetchDependencyGraphParams; - type Result = FetchDependencyGraphResult; - const METHOD: &'static str = "rust-analyzer/fetchDependencyGraph"; +impl Request for FetchDependencyList { + type Params = FetchDependencyListParams; + type Result = FetchDependencyListResult; + const METHOD: &'static str = "rust-analyzer/fetchDependencyList"; } #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] -pub struct FetchDependencyGraphParams {} +pub struct FetchDependencyListParams {} #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] -pub struct FetchDependencyGraphResult { +pub struct FetchDependencyListResult { pub crates: Vec, } diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 7a81a18f4af..d3cfc5e40d6 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -660,7 +660,7 @@ fn on_request(&mut self, req: Request) { .on_sync::(handlers::handle_on_enter) .on_sync::(handlers::handle_selection_range) .on_sync::(handlers::handle_matching_brace) - .on::(handlers::fetch_dependency_graph) + .on::(handlers::fetch_dependency_list) .on::(handlers::handle_analyzer_status) .on::(handlers::handle_syntax_tree) .on::(handlers::handle_view_hir) diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 41bda554be9..d73ae3d519e 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -855,7 +855,7 @@ export interface Diagnostic { ## Dependency Tree -**Method:** `rust-analyzer/fetchDependencyGraph` +**Method:** `rust-analyzer/fetchDependencyList` **Request:** diff --git a/editors/code/src/dependencies_provider.ts b/editors/code/src/dependencies_provider.ts index 3713250b8fc..2e5ea04922f 100644 --- a/editors/code/src/dependencies_provider.ts +++ b/editors/code/src/dependencies_provider.ts @@ -3,13 +3,12 @@ import * as fspath from "path"; import * as fs from "fs"; import { CtxInit } from "./ctx"; import * as ra from "./lsp_ext"; -import { FetchDependencyGraphResult } from "./lsp_ext"; +import { FetchDependencyListResult } from "./lsp_ext"; import { Ctx } from "./ctx"; import { setFlagsFromString } from "v8"; import * as ra from "./lsp_ext"; - export class RustDependenciesProvider implements vscode.TreeDataProvider { @@ -82,8 +81,8 @@ export class RustDependenciesProvider private async getRootDependencies(): Promise { const crates = await this.ctx.client.sendRequest(ra.fetchDependencyGraph, {}); - const dependenciesResult: FetchDependencyGraphResult = await this.ctx.client.sendRequest( - ra.fetchDependencyGraph, + const dependenciesResult: FetchDependencyListResult = await this.ctx.client.sendRequest( + ra.fetchDependencyList, {} ); const crates = dependenciesResult.crates; @@ -97,7 +96,6 @@ export class RustDependenciesProvider } private toDep(moduleName: string, version: string, path: string): Dependency { - // const cratePath = fspath.join(basePath, `${moduleName}-${version}`); return new Dependency(moduleName, version, path, vscode.TreeItemCollapsibleState.Collapsed); } } diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index f066a1c6548..30de5035d63 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -70,9 +70,9 @@ export const viewItemTree = new lc.RequestType export type AnalyzerStatusParams = { textDocument?: lc.TextDocumentIdentifier }; -export interface FetchDependencyGraphParams {} +export interface FetchDependencyListParams {} -export interface FetchDependencyGraphResult { +export interface FetchDependencyListResult { crates: { name: string; version: string; @@ -80,11 +80,11 @@ export interface FetchDependencyGraphResult { }[]; } -export const fetchDependencyGraph = new lc.RequestType< - FetchDependencyGraphParams, - FetchDependencyGraphResult, +export const fetchDependencyList = new lc.RequestType< + FetchDependencyListParams, + FetchDependencyListResult, void ->("rust-analyzer/fetchDependencyGraph"); +>("rust-analyzer/fetchDependencyList"); export interface FetchDependencyGraphParams {}