Skip to content

Commit 8a29d07

Browse files
committed
Auto merge of rust-lang#15548 - Veykril:r-a-restructure, r=Veykril
Restructure some modules in rust-analyzer crate
2 parents 2557995 + 2dbc7e3 commit 8a29d07

22 files changed

+145
-134
lines changed

crates/rust-analyzer/src/caps.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ use lsp_types::{
1616
};
1717
use serde_json::json;
1818

19-
use crate::config::{Config, RustfmtConfig};
20-
use crate::line_index::PositionEncoding;
21-
use crate::lsp_ext::negotiated_encoding;
22-
use crate::semantic_tokens;
19+
use crate::{
20+
config::{Config, RustfmtConfig},
21+
line_index::PositionEncoding,
22+
lsp::semantic_tokens,
23+
lsp_ext::negotiated_encoding,
24+
};
2325

2426
pub fn server_capabilities(config: &Config) -> ServerCapabilities {
2527
ServerCapabilities {

crates/rust-analyzer/src/cli/lsif.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use vfs::{AbsPathBuf, Vfs};
2121
use crate::{
2222
cli::flags,
2323
line_index::{LineEndings, LineIndex, PositionEncoding},
24-
to_proto,
24+
lsp::to_proto,
2525
version::version,
2626
};
2727

crates/rust-analyzer/src/diagnostics.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use nohash_hasher::{IntMap, IntSet};
99
use rustc_hash::FxHashSet;
1010
use triomphe::Arc;
1111

12-
use crate::lsp_ext;
12+
use crate::{global_state::GlobalStateSnapshot, lsp, lsp_ext};
1313

1414
pub(crate) type CheckFixes = Arc<IntMap<usize, IntMap<FileId, Vec<Fix>>>>;
1515

@@ -122,3 +122,41 @@ fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagno
122122
&& left.range == right.range
123123
&& left.message == right.message
124124
}
125+
126+
pub(crate) fn fetch_native_diagnostics(
127+
snapshot: GlobalStateSnapshot,
128+
subscriptions: Vec<FileId>,
129+
) -> Vec<(FileId, Vec<lsp_types::Diagnostic>)> {
130+
let _p = profile::span("fetch_native_diagnostics");
131+
let _ctx = stdx::panic_context::enter("fetch_native_diagnostics".to_owned());
132+
subscriptions
133+
.into_iter()
134+
.filter_map(|file_id| {
135+
let line_index = snapshot.file_line_index(file_id).ok()?;
136+
let diagnostics = snapshot
137+
.analysis
138+
.diagnostics(
139+
&snapshot.config.diagnostics(),
140+
ide::AssistResolveStrategy::None,
141+
file_id,
142+
)
143+
.ok()?
144+
.into_iter()
145+
.map(move |d| lsp_types::Diagnostic {
146+
range: lsp::to_proto::range(&line_index, d.range),
147+
severity: Some(lsp::to_proto::diagnostic_severity(d.severity)),
148+
code: Some(lsp_types::NumberOrString::String(d.code.as_str().to_string())),
149+
code_description: Some(lsp_types::CodeDescription {
150+
href: lsp_types::Url::parse(&d.code.url()).unwrap(),
151+
}),
152+
source: Some("rust-analyzer".to_string()),
153+
message: d.message,
154+
related_information: None,
155+
tags: d.unused.then(|| vec![lsp_types::DiagnosticTag::UNNECESSARY]),
156+
data: None,
157+
})
158+
.collect::<Vec<_>>();
159+
Some((file_id, diagnostics))
160+
})
161+
.collect()
162+
}

crates/rust-analyzer/src/diagnostics/to_proto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use stdx::format_to;
88
use vfs::{AbsPath, AbsPathBuf};
99

1010
use crate::{
11-
global_state::GlobalStateSnapshot, line_index::PositionEncoding, lsp_ext,
12-
to_proto::url_from_abs_path,
11+
global_state::GlobalStateSnapshot, line_index::PositionEncoding,
12+
lsp::to_proto::url_from_abs_path, lsp_ext,
1313
};
1414

1515
use super::{DiagnosticsMapConfig, Fix};

crates/rust-analyzer/src/dispatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use stdx::thread::ThreadIntent;
88

99
use crate::{
1010
global_state::{GlobalState, GlobalStateSnapshot},
11+
lsp::LspError,
1112
main_loop::Task,
1213
version::version,
13-
LspError,
1414
};
1515

1616
/// A visitor for routing a raw JSON request to an appropriate handler function.

crates/rust-analyzer/src/global_state.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ use vfs::{AnchoredPathBuf, Vfs};
2222
use crate::{
2323
config::{Config, ConfigError},
2424
diagnostics::{CheckFixes, DiagnosticCollection},
25-
from_proto,
2625
line_index::{LineEndings, LineIndex},
26+
lsp::{from_proto, to_proto::url_from_abs_path},
2727
lsp_ext,
2828
main_loop::Task,
2929
mem_docs::MemDocs,
3030
op_queue::OpQueue,
3131
reload,
3232
task_pool::TaskPool,
33-
to_proto::url_from_abs_path,
3433
};
3534

3635
// Enforces drop order
@@ -40,7 +39,7 @@ pub(crate) struct Handle<H, C> {
4039
}
4140

4241
pub(crate) type ReqHandler = fn(&mut GlobalState, lsp_server::Response);
43-
pub(crate) type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>;
42+
type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>;
4443

4544
/// `GlobalState` is the primary mutable state of the language server
4645
///
@@ -49,6 +48,7 @@ pub(crate) type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>;
4948
/// incremental salsa database.
5049
///
5150
/// Note that this struct has more than one impl in various modules!
51+
#[doc(alias = "GlobalMess")]
5252
pub(crate) struct GlobalState {
5353
sender: Sender<lsp_server::Message>,
5454
req_queue: ReqQueue,

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ use triomphe::Arc;
1313
use vfs::{AbsPathBuf, ChangeKind, VfsPath};
1414

1515
use crate::{
16-
config::Config, from_proto, global_state::GlobalState, lsp_ext::RunFlycheckParams,
17-
lsp_utils::apply_document_changes, mem_docs::DocumentData, reload,
16+
config::Config,
17+
global_state::GlobalState,
18+
lsp::{from_proto, utils::apply_document_changes},
19+
lsp_ext::RunFlycheckParams,
20+
mem_docs::DocumentData,
21+
reload,
1822
};
1923

2024
pub(crate) fn handle_cancel(state: &mut GlobalState, params: CancelParams) -> anyhow::Result<()> {

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ use crate::{
3636
cargo_target_spec::CargoTargetSpec,
3737
config::{Config, RustfmtConfig, WorkspaceSymbolConfig},
3838
diff::diff,
39-
from_proto,
4039
global_state::{GlobalState, GlobalStateSnapshot},
4140
line_index::LineEndings,
41+
lsp::{
42+
from_proto, to_proto,
43+
utils::{all_edits_are_disjoint, invalid_params_error},
44+
LspError,
45+
},
4246
lsp_ext::{
4347
self, CrateInfoResult, ExternalDocsPair, ExternalDocsResponse, FetchDependencyListParams,
4448
FetchDependencyListResult, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams,
4549
},
46-
lsp_utils::{all_edits_are_disjoint, invalid_params_error},
47-
to_proto, LspError,
4850
};
4951

5052
pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {

crates/rust-analyzer/src/lib.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ mod cargo_target_spec;
2323
mod diagnostics;
2424
mod diff;
2525
mod dispatch;
26-
mod from_proto;
2726
mod global_state;
2827
mod line_index;
29-
mod lsp_utils;
3028
mod main_loop;
3129
mod markdown;
3230
mod mem_docs;
3331
mod op_queue;
3432
mod reload;
35-
mod semantic_tokens;
3633
mod task_pool;
37-
mod to_proto;
3834
mod version;
3935

4036
mod handlers {
@@ -43,13 +39,12 @@ mod handlers {
4339
}
4440

4541
pub mod config;
46-
pub mod lsp_ext;
42+
pub mod lsp;
43+
use self::lsp::ext as lsp_ext;
4744

4845
#[cfg(test)]
4946
mod integrated_benchmarks;
5047

51-
use std::fmt;
52-
5348
use serde::de::DeserializeOwned;
5449

5550
pub use crate::{caps::server_capabilities, main_loop::main_loop, version::version};
@@ -61,23 +56,3 @@ pub fn from_json<T: DeserializeOwned>(
6156
serde_json::from_value(json.clone())
6257
.map_err(|e| anyhow::format_err!("Failed to deserialize {what}: {e}; {json}"))
6358
}
64-
65-
#[derive(Debug)]
66-
struct LspError {
67-
code: i32,
68-
message: String,
69-
}
70-
71-
impl LspError {
72-
fn new(code: i32, message: String) -> LspError {
73-
LspError { code, message }
74-
}
75-
}
76-
77-
impl fmt::Display for LspError {
78-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79-
write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
80-
}
81-
}
82-
83-
impl std::error::Error for LspError {}

crates/rust-analyzer/src/lsp.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Custom LSP definitions and protocol conversions.
2+
3+
use core::fmt;
4+
5+
pub(crate) mod utils;
6+
pub(crate) mod semantic_tokens;
7+
pub mod ext;
8+
pub(crate) mod from_proto;
9+
pub(crate) mod to_proto;
10+
11+
#[derive(Debug)]
12+
pub(crate) struct LspError {
13+
pub(crate) code: i32,
14+
pub(crate) message: String,
15+
}
16+
17+
impl LspError {
18+
pub(crate) fn new(code: i32, message: String) -> LspError {
19+
LspError { code, message }
20+
}
21+
}
22+
23+
impl fmt::Display for LspError {
24+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25+
write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
26+
}
27+
}
28+
29+
impl std::error::Error for LspError {}

crates/rust-analyzer/src/from_proto.rs renamed to crates/rust-analyzer/src/lsp/from_proto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::{
1212
from_json,
1313
global_state::GlobalStateSnapshot,
1414
line_index::{LineIndex, PositionEncoding},
15+
lsp::utils::invalid_params_error,
1516
lsp_ext,
16-
lsp_utils::invalid_params_error,
1717
};
1818

1919
pub(crate) fn abs_path(url: &lsp_types::Url) -> anyhow::Result<AbsPathBuf> {

crates/rust-analyzer/src/to_proto.rs renamed to crates/rust-analyzer/src/lsp/to_proto.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ use crate::{
2222
config::{CallInfoConfig, Config},
2323
global_state::GlobalStateSnapshot,
2424
line_index::{LineEndings, LineIndex, PositionEncoding},
25+
lsp::{
26+
semantic_tokens::{self, standard_fallback_type},
27+
utils::invalid_params_error,
28+
LspError,
29+
},
2530
lsp_ext::{self, SnippetTextEdit},
26-
lsp_utils::invalid_params_error,
27-
semantic_tokens::{self, standard_fallback_type},
2831
};
2932

3033
pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position {
@@ -1425,8 +1428,8 @@ pub(crate) mod command {
14251428

14261429
use crate::{
14271430
global_state::GlobalStateSnapshot,
1431+
lsp::to_proto::{location, location_link},
14281432
lsp_ext,
1429-
to_proto::{location, location_link},
14301433
};
14311434

14321435
pub(crate) fn show_references(
@@ -1532,7 +1535,7 @@ pub(crate) fn markup_content(
15321535
lsp_types::MarkupContent { kind, value }
15331536
}
15341537

1535-
pub(crate) fn rename_error(err: RenameError) -> crate::LspError {
1538+
pub(crate) fn rename_error(err: RenameError) -> LspError {
15361539
// This is wrong, but we don't have a better alternative I suppose?
15371540
// https://github.com/microsoft/language-server-protocol/issues/1341
15381541
invalid_params_error(err.to_string())

crates/rust-analyzer/src/lsp_utils.rs renamed to crates/rust-analyzer/src/lsp/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use lsp_types::request::Request;
66
use triomphe::Arc;
77

88
use crate::{
9-
from_proto,
109
global_state::GlobalState,
1110
line_index::{LineEndings, LineIndex, PositionEncoding},
12-
lsp_ext, LspError,
11+
lsp::{from_proto, LspError},
12+
lsp_ext,
1313
};
1414

1515
pub(crate) fn invalid_params_error(message: String) -> LspError {

0 commit comments

Comments
 (0)