Accepting review suggestions

This commit is contained in:
Bruno Ortiz 2023-04-27 16:13:05 -03:00
parent 800b3b6323
commit bcb21311ea
5 changed files with 40 additions and 28 deletions

View file

@ -58,23 +58,20 @@ pub(crate) fn fetch_dependency_list(
.into_iter() .into_iter()
.filter_map(|it| { .filter_map(|it| {
let root_file_path = state.file_id_to_file_path(it.root_file_id); let root_file_path = state.file_id_to_file_path(it.root_file_id);
crate_path(it.name.as_ref(), root_file_path).map(|path| CrateInfoResult { crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult {
name: it.name, name: it.name,
version: it.version, version: it.version,
path: path.to_string(), path,
}) })
}) })
.collect(); .collect();
Ok(FetchDependencyListResult { crates: crate_infos }) Ok(FetchDependencyListResult { crates: crate_infos })
} }
/// Searches for the directory of a Rust crate with a given name in the directory tree /// Searches for the directory of a Rust crate given this crate's root file path.
/// of the root file of that crate.
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `crate_name`: The name of the crate to search for. This should be a `Some` value if
/// a crate name has been specified, or `None` if no crate name has been specified.
/// * `root_file_path`: The path to the root file of the crate. /// * `root_file_path`: The path to the root file of the crate.
/// ///
/// # Returns /// # Returns
@ -82,19 +79,21 @@ pub(crate) fn fetch_dependency_list(
/// An `Option` value representing the path to the directory of the crate with the given /// 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 /// name, if such a crate is found. If no crate with the given name is found, this function
/// returns `None`. /// returns `None`.
fn crate_path(crate_name: Option<&String>, root_file_path: VfsPath) -> Option<VfsPath> { fn crate_path(root_file_path: VfsPath) -> Option<VfsPath> {
crate_name.and_then(|crate_name| { let mut current_dir = root_file_path.parent();
let mut root_path = root_file_path; while let Some(path) = current_dir {
while let Some(path) = root_path.parent() { let cargo_toml_path = path.join("../Cargo.toml")?;
if let Some((name, _)) = path.name_and_extension() { if fs::metadata(cargo_toml_path.as_path()?).is_ok() {
if name.starts_with(crate_name.as_str()) { let crate_path = cargo_toml_path.parent()?;
return Some(path); return Some(crate_path);
} }
} else { current_dir = path.parent();
break;
}
root_path = path;
} }
None None
}) }
fn to_url(path: VfsPath) -> Option<Url> {
let path = path.as_path()?;
let str_path = path.as_os_str().to_str()?;
Url::from_file_path(str_path).ok()
} }

View file

@ -4,11 +4,11 @@ use std::{collections::HashMap, path::PathBuf};
use ide_db::line_index::WideEncoding; use ide_db::line_index::WideEncoding;
use lsp_types::request::Request; use lsp_types::request::Request;
use lsp_types::PositionEncodingKind;
use lsp_types::{ use lsp_types::{
notification::Notification, CodeActionKind, DocumentOnTypeFormattingParams, notification::Notification, CodeActionKind, DocumentOnTypeFormattingParams,
PartialResultParams, Position, Range, TextDocumentIdentifier, WorkDoneProgressParams, PartialResultParams, Position, Range, TextDocumentIdentifier, WorkDoneProgressParams,
}; };
use lsp_types::{PositionEncodingKind, Url};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::line_index::PositionEncoding; use crate::line_index::PositionEncoding;
@ -32,7 +32,7 @@ pub struct AnalyzerStatusParams {
pub struct CrateInfoResult { pub struct CrateInfoResult {
pub name: Option<String>, pub name: Option<String>,
pub version: Option<String>, pub version: Option<String>,
pub path: String, pub path: Url,
} }
pub enum FetchDependencyList {} pub enum FetchDependencyList {}

View file

@ -276,6 +276,9 @@ export function openCargoToml(ctx: CtxInit): Cmd {
export function revealDependency(ctx: CtxInit): Cmd { export function revealDependency(ctx: CtxInit): Cmd {
return async (editor: RustEditor) => { return async (editor: RustEditor) => {
if (!ctx.dependencies?.isInitialized()) {
return;
}
const documentPath = editor.document.uri.fsPath; const documentPath = editor.document.uri.fsPath;
const dep = ctx.dependencies?.getDependency(documentPath); const dep = ctx.dependencies?.getDependency(documentPath);
if (dep) { if (dep) {

View file

@ -32,6 +32,10 @@ export class RustDependenciesProvider
return filePath.toLowerCase() in this.dependenciesMap; return filePath.toLowerCase() in this.dependenciesMap;
} }
isInitialized(): boolean {
return Object.keys(this.dependenciesMap).length !== 0;
}
refresh(): void { refresh(): void {
this.dependenciesMap = {}; this.dependenciesMap = {};
this._onDidChangeTreeData.fire(); this._onDidChangeTreeData.fire();
@ -89,7 +93,12 @@ export class RustDependenciesProvider
} }
private toDep(moduleName: string, version: string, path: string): Dependency { private toDep(moduleName: string, version: string, path: string): Dependency {
return new Dependency(moduleName, version, path, vscode.TreeItemCollapsibleState.Collapsed); return new Dependency(
moduleName,
version,
vscode.Uri.parse(path).fsPath,
vscode.TreeItemCollapsibleState.Collapsed
);
} }
} }
@ -101,9 +110,9 @@ export class Dependency extends vscode.TreeItem {
public readonly collapsibleState: vscode.TreeItemCollapsibleState public readonly collapsibleState: vscode.TreeItemCollapsibleState
) { ) {
super(label, collapsibleState); super(label, collapsibleState);
this.id = this.dependencyPath.toLowerCase();
this.description = this.version;
this.resourceUri = vscode.Uri.file(dependencyPath); this.resourceUri = vscode.Uri.file(dependencyPath);
this.id = this.resourceUri.fsPath.toLowerCase();
this.description = this.version;
if (this.version) { if (this.version) {
this.tooltip = `${this.label}-${this.version}`; this.tooltip = `${this.label}-${this.version}`;
} else { } else {
@ -120,13 +129,13 @@ export class DependencyFile extends vscode.TreeItem {
public readonly collapsibleState: vscode.TreeItemCollapsibleState public readonly collapsibleState: vscode.TreeItemCollapsibleState
) { ) {
super(vscode.Uri.file(dependencyPath), collapsibleState); super(vscode.Uri.file(dependencyPath), collapsibleState);
this.id = this.dependencyPath.toLowerCase(); this.id = this.resourceUri!.fsPath.toLowerCase();
const isDir = fs.lstatSync(this.dependencyPath).isDirectory(); const isDir = fs.lstatSync(this.resourceUri!.fsPath).isDirectory();
if (!isDir) { if (!isDir) {
this.command = { this.command = {
command: "vscode.open", command: "vscode.open",
title: "Open File", title: "Open File",
arguments: [vscode.Uri.file(this.dependencyPath)], arguments: [this.resourceUri],
}; };
} }
} }

View file

@ -2,6 +2,7 @@
* This file mirrors `crates/rust-analyzer/src/lsp_ext.rs` declarations. * This file mirrors `crates/rust-analyzer/src/lsp_ext.rs` declarations.
*/ */
import { Uri } from "vscode";
import * as lc from "vscode-languageclient"; import * as lc from "vscode-languageclient";
// rust-analyzer overrides // rust-analyzer overrides