Skip to content

Commit bcb2131

Browse files
committed
Accepting review suggestions
1 parent 800b3b6 commit bcb2131

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

crates/rust-analyzer/src/handlers.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -58,43 +58,42 @@ pub(crate) fn fetch_dependency_list(
5858
.into_iter()
5959
.filter_map(|it| {
6060
let root_file_path = state.file_id_to_file_path(it.root_file_id);
61-
crate_path(it.name.as_ref(), root_file_path).map(|path| CrateInfoResult {
61+
crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult {
6262
name: it.name,
6363
version: it.version,
64-
path: path.to_string(),
64+
path,
6565
})
6666
})
6767
.collect();
6868
Ok(FetchDependencyListResult { crates: crate_infos })
6969
}
7070

71-
/// Searches for the directory of a Rust crate with a given name in the directory tree
72-
/// of the root file of that crate.
71+
/// Searches for the directory of a Rust crate given this crate's root file path.
7372
///
7473
/// # Arguments
7574
///
76-
/// * `crate_name`: The name of the crate to search for. This should be a `Some` value if
77-
/// a crate name has been specified, or `None` if no crate name has been specified.
7875
/// * `root_file_path`: The path to the root file of the crate.
7976
///
8077
/// # Returns
8178
///
8279
/// An `Option` value representing the path to the directory of the crate with the given
8380
/// name, if such a crate is found. If no crate with the given name is found, this function
8481
/// returns `None`.
85-
fn crate_path(crate_name: Option<&String>, root_file_path: VfsPath) -> Option<VfsPath> {
86-
crate_name.and_then(|crate_name| {
87-
let mut root_path = root_file_path;
88-
while let Some(path) = root_path.parent() {
89-
if let Some((name, _)) = path.name_and_extension() {
90-
if name.starts_with(crate_name.as_str()) {
91-
return Some(path);
92-
}
93-
} else {
94-
break;
95-
}
96-
root_path = path;
82+
fn crate_path(root_file_path: VfsPath) -> Option<VfsPath> {
83+
let mut current_dir = root_file_path.parent();
84+
while let Some(path) = current_dir {
85+
let cargo_toml_path = path.join("../Cargo.toml")?;
86+
if fs::metadata(cargo_toml_path.as_path()?).is_ok() {
87+
let crate_path = cargo_toml_path.parent()?;
88+
return Some(crate_path);
9789
}
98-
None
99-
})
90+
current_dir = path.parent();
91+
}
92+
None
93+
}
94+
95+
fn to_url(path: VfsPath) -> Option<Url> {
96+
let path = path.as_path()?;
97+
let str_path = path.as_os_str().to_str()?;
98+
Url::from_file_path(str_path).ok()
10099
}

crates/rust-analyzer/src/lsp_ext.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use std::{collections::HashMap, path::PathBuf};
44

55
use ide_db::line_index::WideEncoding;
66
use lsp_types::request::Request;
7-
use lsp_types::PositionEncodingKind;
87
use lsp_types::{
98
notification::Notification, CodeActionKind, DocumentOnTypeFormattingParams,
109
PartialResultParams, Position, Range, TextDocumentIdentifier, WorkDoneProgressParams,
1110
};
11+
use lsp_types::{PositionEncodingKind, Url};
1212
use serde::{Deserialize, Serialize};
1313

1414
use crate::line_index::PositionEncoding;
@@ -32,7 +32,7 @@ pub struct AnalyzerStatusParams {
3232
pub struct CrateInfoResult {
3333
pub name: Option<String>,
3434
pub version: Option<String>,
35-
pub path: String,
35+
pub path: Url,
3636
}
3737
pub enum FetchDependencyList {}
3838

editors/code/src/commands.ts

+3
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ export function openCargoToml(ctx: CtxInit): Cmd {
276276

277277
export function revealDependency(ctx: CtxInit): Cmd {
278278
return async (editor: RustEditor) => {
279+
if (!ctx.dependencies?.isInitialized()) {
280+
return;
281+
}
279282
const documentPath = editor.document.uri.fsPath;
280283
const dep = ctx.dependencies?.getDependency(documentPath);
281284
if (dep) {

editors/code/src/dependencies_provider.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class RustDependenciesProvider
3232
return filePath.toLowerCase() in this.dependenciesMap;
3333
}
3434

35+
isInitialized(): boolean {
36+
return Object.keys(this.dependenciesMap).length !== 0;
37+
}
38+
3539
refresh(): void {
3640
this.dependenciesMap = {};
3741
this._onDidChangeTreeData.fire();
@@ -89,7 +93,12 @@ export class RustDependenciesProvider
8993
}
9094

9195
private toDep(moduleName: string, version: string, path: string): Dependency {
92-
return new Dependency(moduleName, version, path, vscode.TreeItemCollapsibleState.Collapsed);
96+
return new Dependency(
97+
moduleName,
98+
version,
99+
vscode.Uri.parse(path).fsPath,
100+
vscode.TreeItemCollapsibleState.Collapsed
101+
);
93102
}
94103
}
95104

@@ -101,9 +110,9 @@ export class Dependency extends vscode.TreeItem {
101110
public readonly collapsibleState: vscode.TreeItemCollapsibleState
102111
) {
103112
super(label, collapsibleState);
104-
this.id = this.dependencyPath.toLowerCase();
105-
this.description = this.version;
106113
this.resourceUri = vscode.Uri.file(dependencyPath);
114+
this.id = this.resourceUri.fsPath.toLowerCase();
115+
this.description = this.version;
107116
if (this.version) {
108117
this.tooltip = `${this.label}-${this.version}`;
109118
} else {
@@ -120,13 +129,13 @@ export class DependencyFile extends vscode.TreeItem {
120129
public readonly collapsibleState: vscode.TreeItemCollapsibleState
121130
) {
122131
super(vscode.Uri.file(dependencyPath), collapsibleState);
123-
this.id = this.dependencyPath.toLowerCase();
124-
const isDir = fs.lstatSync(this.dependencyPath).isDirectory();
132+
this.id = this.resourceUri!.fsPath.toLowerCase();
133+
const isDir = fs.lstatSync(this.resourceUri!.fsPath).isDirectory();
125134
if (!isDir) {
126135
this.command = {
127136
command: "vscode.open",
128137
title: "Open File",
129-
arguments: [vscode.Uri.file(this.dependencyPath)],
138+
arguments: [this.resourceUri],
130139
};
131140
}
132141
}

editors/code/src/lsp_ext.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* This file mirrors `crates/rust-analyzer/src/lsp_ext.rs` declarations.
33
*/
44

5+
import { Uri } from "vscode";
56
import * as lc from "vscode-languageclient";
67

78
// rust-analyzer overrides

0 commit comments

Comments
 (0)