Skip to content

Commit 599db80

Browse files
committed
Accepting review suggestions
1 parent 579d8a7 commit 599db80

File tree

6 files changed

+42
-29
lines changed

6 files changed

+42
-29
lines changed

crates/rust-analyzer/src/handlers.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! `ide` crate.
44
55
use std::{
6+
fs,
67
io::Write as _,
78
process::{self, Stdio},
89
sync::Arc,
@@ -138,10 +139,10 @@ pub(crate) fn fetch_dependency_list(
138139
.into_iter()
139140
.filter_map(|it| {
140141
let root_file_path = state.file_id_to_file_path(it.root_file_id);
141-
crate_path(it.name.as_ref(), root_file_path).map(|path| CrateInfoResult {
142+
crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult {
142143
name: it.name,
143144
version: it.version,
144-
path: path.to_string(),
145+
path,
145146
})
146147
})
147148
.collect();
@@ -1933,33 +1934,32 @@ fn run_rustfmt(
19331934
}
19341935
}
19351936

1936-
/// Searches for the directory of a Rust crate with a given name in the directory tree
1937-
/// of the root file of that crate.
1937+
/// Searches for the directory of a Rust crate given this crate's root file path.
19381938
///
19391939
/// # Arguments
19401940
///
1941-
/// * `crate_name`: The name of the crate to search for. This should be a `Some` value if
1942-
/// a crate name has been specified, or `None` if no crate name has been specified.
19431941
/// * `root_file_path`: The path to the root file of the crate.
19441942
///
19451943
/// # Returns
19461944
///
19471945
/// An `Option` value representing the path to the directory of the crate with the given
19481946
/// name, if such a crate is found. If no crate with the given name is found, this function
19491947
/// returns `None`.
1950-
fn crate_path(crate_name: Option<&String>, root_file_path: VfsPath) -> Option<VfsPath> {
1951-
crate_name.and_then(|crate_name| {
1952-
let mut root_path = root_file_path;
1953-
while let Some(path) = root_path.parent() {
1954-
if let Some((name, _)) = path.name_and_extension() {
1955-
if name.starts_with(crate_name.as_str()) {
1956-
return Some(path);
1957-
}
1958-
} else {
1959-
break;
1960-
}
1961-
root_path = path;
1948+
fn crate_path(root_file_path: VfsPath) -> Option<VfsPath> {
1949+
let mut current_dir = root_file_path.parent();
1950+
while let Some(path) = current_dir {
1951+
let cargo_toml_path = path.join("../Cargo.toml")?;
1952+
if fs::metadata(cargo_toml_path.as_path()?).is_ok() {
1953+
let crate_path = cargo_toml_path.parent()?;
1954+
return Some(crate_path);
19621955
}
1963-
None
1964-
})
1956+
current_dir = path.parent();
1957+
}
1958+
None
1959+
}
1960+
1961+
fn to_url(path: VfsPath) -> Option<Url> {
1962+
let path = path.as_path()?;
1963+
let str_path = path.as_os_str().to_str()?;
1964+
Url::from_file_path(str_path).ok()
19651965
}

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

docs/dev/lsp-extensions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: d39721703177a537
2+
lsp_ext.rs hash: 12bcc60165316445
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

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)