Skip to content

Commit ecfe7c0

Browse files
committed
last fixes after rebase
1 parent 0aed507 commit ecfe7c0

File tree

3 files changed

+61
-62
lines changed

3 files changed

+61
-62
lines changed

crates/rust-analyzer/src/handlers.rs

+5-59
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
//! `ide` crate.
44
55
use ide::AssistResolveStrategy;
6-
use lsp_types::{Diagnostic, DiagnosticTag, NumberOrString};
7-
use vfs::FileId;
6+
use lsp_types::{Diagnostic, DiagnosticTag, NumberOrString, Url};
87

9-
use crate::{
10-
global_state::GlobalStateSnapshot, to_proto, Result,
11-
lsp_ext::{
12-
CrateInfoResult, FetchDependencyListResult, FetchDependencyListParams,
13-
},
14-
};
8+
use vfs::FileId;
159

10+
use crate::{global_state::GlobalStateSnapshot, to_proto, Result};
1611

1712
pub(crate) mod request;
1813
pub(crate) mod notification;
@@ -33,11 +28,11 @@ pub(crate) fn publish_diagnostics(
3328
severity: Some(to_proto::diagnostic_severity(d.severity)),
3429
code: Some(NumberOrString::String(d.code.as_str().to_string())),
3530
code_description: Some(lsp_types::CodeDescription {
36-
href: lsp_types::Url::parse(&format!(
31+
href: Url::parse(&format!(
3732
"https://rust-analyzer.github.io/manual.html#{}",
3833
d.code.as_str()
3934
))
40-
.unwrap(),
35+
.unwrap(),
4136
}),
4237
source: Some("rust-analyzer".to_string()),
4338
message: d.message,
@@ -48,52 +43,3 @@ pub(crate) fn publish_diagnostics(
4843
.collect();
4944
Ok(diagnostics)
5045
}
51-
52-
pub(crate) fn fetch_dependency_list(
53-
state: GlobalStateSnapshot,
54-
_params: lsp_ext::FetchDependencyListParams,
55-
) -> Result<lsp_ext::FetchDependencyListResult> {
56-
let crates = state.analysis.fetch_crates()?;
57-
let crate_infos = crates
58-
.into_iter()
59-
.filter_map(|it| {
60-
let root_file_path = state.file_id_to_file_path(it.root_file_id);
61-
crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult {
62-
name: it.name,
63-
version: it.version,
64-
path,
65-
})
66-
})
67-
.collect();
68-
Ok(FetchDependencyListResult { crates: crate_infos })
69-
}
70-
71-
/// Searches for the directory of a Rust crate given this crate's root file path.
72-
///
73-
/// # Arguments
74-
///
75-
/// * `root_file_path`: The path to the root file of the crate.
76-
///
77-
/// # Returns
78-
///
79-
/// An `Option` value representing the path to the directory of the crate with the given
80-
/// name, if such a crate is found. If no crate with the given name is found, this function
81-
/// returns `None`.
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);
89-
}
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()
99-
}

crates/rust-analyzer/src/handlers/request.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! Protocol. This module specifically handles requests.
33
44
use std::{
5+
fs,
56
io::Write as _,
67
process::{self, Stdio},
78
sync::Arc,
@@ -29,7 +30,7 @@ use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
2930
use serde_json::json;
3031
use stdx::{format_to, never};
3132
use syntax::{algo, ast, AstNode, TextRange, TextSize};
32-
use vfs::{AbsPath, AbsPathBuf};
33+
use vfs::{AbsPath, AbsPathBuf, VfsPath};
3334

3435
use crate::{
3536
cargo_target_spec::CargoTargetSpec,
@@ -38,7 +39,10 @@ use crate::{
3839
from_proto,
3940
global_state::{GlobalState, GlobalStateSnapshot},
4041
line_index::LineEndings,
41-
lsp_ext::{self, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams},
42+
lsp_ext::{
43+
self, CrateInfoResult, FetchDependencyListParams, FetchDependencyListResult,
44+
PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams,
45+
},
4246
lsp_utils::{all_edits_are_disjoint, invalid_params_error},
4347
to_proto, LspError, Result,
4448
};
@@ -1881,3 +1885,52 @@ fn run_rustfmt(
18811885
Ok(Some(to_proto::text_edit_vec(&line_index, diff(&file, &new_text))))
18821886
}
18831887
}
1888+
1889+
pub(crate) fn fetch_dependency_list(
1890+
state: GlobalStateSnapshot,
1891+
_params: FetchDependencyListParams,
1892+
) -> Result<FetchDependencyListResult> {
1893+
let crates = state.analysis.fetch_crates()?;
1894+
let crate_infos = crates
1895+
.into_iter()
1896+
.filter_map(|it| {
1897+
let root_file_path = state.file_id_to_file_path(it.root_file_id);
1898+
crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult {
1899+
name: it.name,
1900+
version: it.version,
1901+
path,
1902+
})
1903+
})
1904+
.collect();
1905+
Ok(FetchDependencyListResult { crates: crate_infos })
1906+
}
1907+
1908+
/// Searches for the directory of a Rust crate given this crate's root file path.
1909+
///
1910+
/// # Arguments
1911+
///
1912+
/// * `root_file_path`: The path to the root file of the crate.
1913+
///
1914+
/// # Returns
1915+
///
1916+
/// An `Option` value representing the path to the directory of the crate with the given
1917+
/// name, if such a crate is found. If no crate with the given name is found, this function
1918+
/// returns `None`.
1919+
fn crate_path(root_file_path: VfsPath) -> Option<VfsPath> {
1920+
let mut current_dir = root_file_path.parent();
1921+
while let Some(path) = current_dir {
1922+
let cargo_toml_path = path.join("../Cargo.toml")?;
1923+
if fs::metadata(cargo_toml_path.as_path()?).is_ok() {
1924+
let crate_path = cargo_toml_path.parent()?;
1925+
return Some(crate_path);
1926+
}
1927+
current_dir = path.parent();
1928+
}
1929+
None
1930+
}
1931+
1932+
fn to_url(path: VfsPath) -> Option<Url> {
1933+
let path = path.as_path()?;
1934+
let str_path = path.as_os_str().to_str()?;
1935+
Url::from_file_path(str_path).ok()
1936+
}

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: 31ca513a249753ab
2+
lsp_ext.rs hash: fdf1afd34548abbc
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:

0 commit comments

Comments
 (0)