Skip to content

Commit 0aa0da9

Browse files
committed
Auto merge of #13572 - Veykril:cancellable, r=Veykril
internal: Use Cancellable in favor of Result for clarity
2 parents a27e4da + fa70b0a commit 0aa0da9

File tree

6 files changed

+44
-43
lines changed

6 files changed

+44
-43
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl fmt::Debug for InlayHintLabelPart {
190190
pub(crate) fn inlay_hints(
191191
db: &RootDatabase,
192192
file_id: FileId,
193-
range_limit: Option<FileRange>,
193+
range_limit: Option<TextRange>,
194194
config: &InlayHintsConfig,
195195
) -> Vec<InlayHint> {
196196
let _p = profile::span("inlay_hints");
@@ -205,7 +205,7 @@ pub(crate) fn inlay_hints(
205205

206206
let hints = |node| hints(&mut acc, &famous_defs, config, file_id, node);
207207
match range_limit {
208-
Some(FileRange { range, .. }) => match file.covering_element(range) {
208+
Some(range) => match file.covering_element(range) {
209209
NodeOrToken::Token(_) => return acc,
210210
NodeOrToken::Node(n) => n
211211
.descendants()
@@ -1289,7 +1289,6 @@ fn get_callable(
12891289
#[cfg(test)]
12901290
mod tests {
12911291
use expect_test::{expect, Expect};
1292-
use ide_db::base_db::FileRange;
12931292
use itertools::Itertools;
12941293
use syntax::{TextRange, TextSize};
12951294
use test_utils::extract_annotations;
@@ -1913,10 +1912,7 @@ fn main() {
19131912
.inlay_hints(
19141913
&InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG },
19151914
file_id,
1916-
Some(FileRange {
1917-
file_id,
1918-
range: TextRange::new(TextSize::from(500), TextSize::from(600)),
1919-
}),
1915+
Some(TextRange::new(TextSize::from(500), TextSize::from(600))),
19201916
)
19211917
.unwrap();
19221918
let actual =

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl Analysis {
367367
&self,
368368
config: &InlayHintsConfig,
369369
file_id: FileId,
370-
range: Option<FileRange>,
370+
range: Option<TextRange>,
371371
) -> Cancellable<Vec<InlayHint>> {
372372
self.with_db(|db| inlay_hints::inlay_hints(db, file_id, range, config))
373373
}

crates/rust-analyzer/src/cargo_target_spec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
use std::mem;
44

55
use cfg::{CfgAtom, CfgExpr};
6-
use ide::{FileId, RunnableKind, TestId};
6+
use ide::{Cancellable, FileId, RunnableKind, TestId};
77
use project_model::{self, CargoFeatures, ManifestPath, TargetKind};
88
use vfs::AbsPathBuf;
99

10-
use crate::{global_state::GlobalStateSnapshot, Result};
10+
use crate::global_state::GlobalStateSnapshot;
1111

1212
/// Abstract representation of Cargo target.
1313
///
@@ -29,7 +29,7 @@ impl CargoTargetSpec {
2929
spec: Option<CargoTargetSpec>,
3030
kind: &RunnableKind,
3131
cfg: &Option<CfgExpr>,
32-
) -> Result<(Vec<String>, Vec<String>)> {
32+
) -> (Vec<String>, Vec<String>) {
3333
let mut args = Vec::new();
3434
let mut extra_args = Vec::new();
3535

@@ -111,13 +111,13 @@ impl CargoTargetSpec {
111111
}
112112
}
113113
}
114-
Ok((args, extra_args))
114+
(args, extra_args)
115115
}
116116

117117
pub(crate) fn for_file(
118118
global_state_snapshot: &GlobalStateSnapshot,
119119
file_id: FileId,
120-
) -> Result<Option<CargoTargetSpec>> {
120+
) -> Cancellable<Option<CargoTargetSpec>> {
121121
let crate_id = match &*global_state_snapshot.analysis.crates_for(file_id)? {
122122
&[crate_id, ..] => crate_id,
123123
_ => return Ok(None),

crates/rust-analyzer/src/handlers.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use std::{
99

1010
use anyhow::Context;
1111
use ide::{
12-
AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange,
13-
HoverAction, HoverGotoTypeData, Query, RangeInfo, ReferenceCategory, Runnable, RunnableKind,
14-
SingleResolve, SourceChange, TextEdit,
12+
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FileId, FilePosition,
13+
FileRange, HoverAction, HoverGotoTypeData, Query, RangeInfo, ReferenceCategory, Runnable,
14+
RunnableKind, SingleResolve, SourceChange, TextEdit,
1515
};
1616
use ide_db::SymbolKind;
1717
use lsp_server::ErrorCode;
@@ -556,7 +556,7 @@ pub(crate) fn handle_will_rename_files(
556556
if source_change.source_file_edits.is_empty() {
557557
Ok(None)
558558
} else {
559-
to_proto::workspace_edit(&snap, source_change).map(Some)
559+
Ok(Some(to_proto::workspace_edit(&snap, source_change)?))
560560
}
561561
}
562562

@@ -1313,7 +1313,7 @@ pub(crate) fn handle_ssr(
13131313
position,
13141314
selections,
13151315
)??;
1316-
to_proto::workspace_edit(&snap, source_change)
1316+
to_proto::workspace_edit(&snap, source_change).map_err(Into::into)
13171317
}
13181318

13191319
pub(crate) fn publish_diagnostics(
@@ -1354,13 +1354,12 @@ pub(crate) fn handle_inlay_hints(
13541354
) -> Result<Option<Vec<InlayHint>>> {
13551355
let _p = profile::span("handle_inlay_hints");
13561356
let document_uri = &params.text_document.uri;
1357-
let file_id = from_proto::file_id(&snap, document_uri)?;
1358-
let line_index = snap.file_line_index(file_id)?;
1359-
let range = from_proto::file_range(
1357+
let FileRange { file_id, range } = from_proto::file_range(
13601358
&snap,
13611359
TextDocumentIdentifier::new(document_uri.to_owned()),
13621360
params.range,
13631361
)?;
1362+
let line_index = snap.file_line_index(file_id)?;
13641363
let inlay_hints_config = snap.config.inlay_hints();
13651364
Ok(Some(
13661365
snap.analysis
@@ -1369,7 +1368,7 @@ pub(crate) fn handle_inlay_hints(
13691368
.map(|it| {
13701369
to_proto::inlay_hint(&snap, &line_index, inlay_hints_config.render_colons, it)
13711370
})
1372-
.collect::<Result<Vec<_>>>()?,
1371+
.collect::<Cancellable<Vec<_>>>()?,
13731372
))
13741373
}
13751374

@@ -1426,7 +1425,7 @@ pub(crate) fn handle_call_hierarchy_prepare(
14261425
.into_iter()
14271426
.filter(|it| it.kind == Some(SymbolKind::Function))
14281427
.map(|it| to_proto::call_hierarchy_item(&snap, it))
1429-
.collect::<Result<Vec<_>>>()?;
1428+
.collect::<Cancellable<Vec<_>>>()?;
14301429

14311430
Ok(Some(res))
14321431
}

crates/rust-analyzer/src/mem_docs.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use vfs::VfsPath;
77

88
/// Holds the set of in-memory documents.
99
///
10-
/// For these document, there true contents is maintained by the client. It
10+
/// For these document, their true contents is maintained by the client. It
1111
/// might be different from what's on disk.
1212
#[derive(Default, Clone)]
1313
pub(crate) struct MemDocs {
@@ -19,31 +19,37 @@ impl MemDocs {
1919
pub(crate) fn contains(&self, path: &VfsPath) -> bool {
2020
self.mem_docs.contains_key(path)
2121
}
22+
2223
pub(crate) fn insert(&mut self, path: VfsPath, data: DocumentData) -> Result<(), ()> {
2324
self.added_or_removed = true;
2425
match self.mem_docs.insert(path, data) {
2526
Some(_) => Err(()),
2627
None => Ok(()),
2728
}
2829
}
30+
2931
pub(crate) fn remove(&mut self, path: &VfsPath) -> Result<(), ()> {
3032
self.added_or_removed = true;
3133
match self.mem_docs.remove(path) {
3234
Some(_) => Ok(()),
3335
None => Err(()),
3436
}
3537
}
38+
3639
pub(crate) fn get(&self, path: &VfsPath) -> Option<&DocumentData> {
3740
self.mem_docs.get(path)
3841
}
42+
3943
pub(crate) fn get_mut(&mut self, path: &VfsPath) -> Option<&mut DocumentData> {
4044
// NB: don't set `self.added_or_removed` here, as that purposefully only
4145
// tracks changes to the key set.
4246
self.mem_docs.get_mut(path)
4347
}
48+
4449
pub(crate) fn iter(&self) -> impl Iterator<Item = &VfsPath> {
4550
self.mem_docs.keys()
4651
}
52+
4753
pub(crate) fn take_changes(&mut self) -> bool {
4854
mem::replace(&mut self.added_or_removed, false)
4955
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
line_index::{LineEndings, LineIndex, PositionEncoding},
2525
lsp_ext,
2626
lsp_utils::invalid_params_error,
27-
semantic_tokens, Result,
27+
semantic_tokens,
2828
};
2929

3030
pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position {
@@ -429,7 +429,7 @@ pub(crate) fn inlay_hint(
429429
line_index: &LineIndex,
430430
render_colons: bool,
431431
mut inlay_hint: InlayHint,
432-
) -> Result<lsp_types::InlayHint> {
432+
) -> Cancellable<lsp_types::InlayHint> {
433433
match inlay_hint.kind {
434434
InlayKind::ParameterHint if render_colons => inlay_hint.label.append_str(":"),
435435
InlayKind::TypeHint if render_colons => inlay_hint.label.prepend_str(": "),
@@ -522,7 +522,7 @@ pub(crate) fn inlay_hint(
522522
fn inlay_hint_label(
523523
snap: &GlobalStateSnapshot,
524524
label: InlayHintLabel,
525-
) -> Result<lsp_types::InlayHintLabel> {
525+
) -> Cancellable<lsp_types::InlayHintLabel> {
526526
Ok(match label.as_simple_str() {
527527
Some(s) => lsp_types::InlayHintLabel::String(s.into()),
528528
None => lsp_types::InlayHintLabel::LabelParts(
@@ -540,7 +540,7 @@ fn inlay_hint_label(
540540
command: None,
541541
})
542542
})
543-
.collect::<Result<Vec<_>>>()?,
543+
.collect::<Cancellable<Vec<_>>>()?,
544544
),
545545
})
546546
}
@@ -798,7 +798,7 @@ pub(crate) fn optional_versioned_text_document_identifier(
798798
pub(crate) fn location(
799799
snap: &GlobalStateSnapshot,
800800
frange: FileRange,
801-
) -> Result<lsp_types::Location> {
801+
) -> Cancellable<lsp_types::Location> {
802802
let url = url(snap, frange.file_id);
803803
let line_index = snap.file_line_index(frange.file_id)?;
804804
let range = range(&line_index, frange.range);
@@ -810,7 +810,7 @@ pub(crate) fn location(
810810
pub(crate) fn location_from_nav(
811811
snap: &GlobalStateSnapshot,
812812
nav: NavigationTarget,
813-
) -> Result<lsp_types::Location> {
813+
) -> Cancellable<lsp_types::Location> {
814814
let url = url(snap, nav.file_id);
815815
let line_index = snap.file_line_index(nav.file_id)?;
816816
let range = range(&line_index, nav.full_range);
@@ -822,7 +822,7 @@ pub(crate) fn location_link(
822822
snap: &GlobalStateSnapshot,
823823
src: Option<FileRange>,
824824
target: NavigationTarget,
825-
) -> Result<lsp_types::LocationLink> {
825+
) -> Cancellable<lsp_types::LocationLink> {
826826
let origin_selection_range = match src {
827827
Some(src) => {
828828
let line_index = snap.file_line_index(src.file_id)?;
@@ -844,7 +844,7 @@ pub(crate) fn location_link(
844844
fn location_info(
845845
snap: &GlobalStateSnapshot,
846846
target: NavigationTarget,
847-
) -> Result<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
847+
) -> Cancellable<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
848848
let line_index = snap.file_line_index(target.file_id)?;
849849

850850
let target_uri = url(snap, target.file_id);
@@ -858,20 +858,20 @@ pub(crate) fn goto_definition_response(
858858
snap: &GlobalStateSnapshot,
859859
src: Option<FileRange>,
860860
targets: Vec<NavigationTarget>,
861-
) -> Result<lsp_types::GotoDefinitionResponse> {
861+
) -> Cancellable<lsp_types::GotoDefinitionResponse> {
862862
if snap.config.location_link() {
863863
let links = targets
864864
.into_iter()
865865
.map(|nav| location_link(snap, src, nav))
866-
.collect::<Result<Vec<_>>>()?;
866+
.collect::<Cancellable<Vec<_>>>()?;
867867
Ok(links.into())
868868
} else {
869869
let locations = targets
870870
.into_iter()
871871
.map(|nav| {
872872
location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
873873
})
874-
.collect::<Result<Vec<_>>>()?;
874+
.collect::<Cancellable<Vec<_>>>()?;
875875
Ok(locations.into())
876876
}
877877
}
@@ -885,7 +885,7 @@ pub(crate) fn snippet_text_document_edit(
885885
is_snippet: bool,
886886
file_id: FileId,
887887
edit: TextEdit,
888-
) -> Result<lsp_ext::SnippetTextDocumentEdit> {
888+
) -> Cancellable<lsp_ext::SnippetTextDocumentEdit> {
889889
let text_document = optional_versioned_text_document_identifier(snap, file_id);
890890
let line_index = snap.file_line_index(file_id)?;
891891
let mut edits: Vec<_> =
@@ -962,7 +962,7 @@ pub(crate) fn snippet_text_document_ops(
962962
pub(crate) fn snippet_workspace_edit(
963963
snap: &GlobalStateSnapshot,
964964
source_change: SourceChange,
965-
) -> Result<lsp_ext::SnippetWorkspaceEdit> {
965+
) -> Cancellable<lsp_ext::SnippetWorkspaceEdit> {
966966
let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
967967

968968
for op in source_change.file_system_edits {
@@ -999,7 +999,7 @@ pub(crate) fn snippet_workspace_edit(
999999
pub(crate) fn workspace_edit(
10001000
snap: &GlobalStateSnapshot,
10011001
source_change: SourceChange,
1002-
) -> Result<lsp_types::WorkspaceEdit> {
1002+
) -> Cancellable<lsp_types::WorkspaceEdit> {
10031003
assert!(!source_change.is_snippet);
10041004
snippet_workspace_edit(snap, source_change).map(|it| it.into())
10051005
}
@@ -1052,7 +1052,7 @@ impl From<lsp_ext::SnippetTextEdit>
10521052
pub(crate) fn call_hierarchy_item(
10531053
snap: &GlobalStateSnapshot,
10541054
target: NavigationTarget,
1055-
) -> Result<lsp_types::CallHierarchyItem> {
1055+
) -> Cancellable<lsp_types::CallHierarchyItem> {
10561056
let name = target.name.to_string();
10571057
let detail = target.description.clone();
10581058
let kind = target.kind.map(symbol_kind).unwrap_or(lsp_types::SymbolKind::FUNCTION);
@@ -1084,7 +1084,7 @@ pub(crate) fn code_action(
10841084
snap: &GlobalStateSnapshot,
10851085
assist: Assist,
10861086
resolve_data: Option<(usize, lsp_types::CodeActionParams)>,
1087-
) -> Result<lsp_ext::CodeAction> {
1087+
) -> Cancellable<lsp_ext::CodeAction> {
10881088
let mut res = lsp_ext::CodeAction {
10891089
title: assist.label.to_string(),
10901090
group: assist.group.filter(|_| snap.config.code_action_group()).map(|gr| gr.0),
@@ -1117,13 +1117,13 @@ pub(crate) fn code_action(
11171117
pub(crate) fn runnable(
11181118
snap: &GlobalStateSnapshot,
11191119
runnable: Runnable,
1120-
) -> Result<lsp_ext::Runnable> {
1120+
) -> Cancellable<lsp_ext::Runnable> {
11211121
let config = snap.config.runnables();
11221122
let spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id)?;
11231123
let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone());
11241124
let target = spec.as_ref().map(|s| s.target.clone());
11251125
let (cargo_args, executable_args) =
1126-
CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg)?;
1126+
CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg);
11271127
let label = runnable.label(target);
11281128
let location = location_link(snap, None, runnable.nav)?;
11291129

@@ -1146,7 +1146,7 @@ pub(crate) fn code_lens(
11461146
acc: &mut Vec<lsp_types::CodeLens>,
11471147
snap: &GlobalStateSnapshot,
11481148
annotation: Annotation,
1149-
) -> Result<()> {
1149+
) -> Cancellable<()> {
11501150
let client_commands_config = snap.config.client_commands();
11511151
match annotation.kind {
11521152
AnnotationKind::Runnable(run) => {

0 commit comments

Comments
 (0)