Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fa70b0a

Browse files
committed
internal: Use Cancellable in favor of Result for clarity
1 parent 6a06f6f commit fa70b0a

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
@@ -180,7 +180,7 @@ impl fmt::Debug for InlayHintLabelPart {
180180
pub(crate) fn inlay_hints(
181181
db: &RootDatabase,
182182
file_id: FileId,
183-
range_limit: Option<FileRange>,
183+
range_limit: Option<TextRange>,
184184
config: &InlayHintsConfig,
185185
) -> Vec<InlayHint> {
186186
let _p = profile::span("inlay_hints");
@@ -195,7 +195,7 @@ pub(crate) fn inlay_hints(
195195

196196
let hints = |node| hints(&mut acc, &famous_defs, config, file_id, node);
197197
match range_limit {
198-
Some(FileRange { range, .. }) => match file.covering_element(range) {
198+
Some(range) => match file.covering_element(range) {
199199
NodeOrToken::Token(_) => return acc,
200200
NodeOrToken::Node(n) => n
201201
.descendants()
@@ -1213,7 +1213,6 @@ fn get_callable(
12131213
#[cfg(test)]
12141214
mod tests {
12151215
use expect_test::{expect, Expect};
1216-
use ide_db::base_db::FileRange;
12171216
use itertools::Itertools;
12181217
use syntax::{TextRange, TextSize};
12191218
use test_utils::extract_annotations;
@@ -1838,10 +1837,7 @@ fn main() {
18381837
.inlay_hints(
18391838
&InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG },
18401839
file_id,
1841-
Some(FileRange {
1842-
file_id,
1843-
range: TextRange::new(TextSize::from(500), TextSize::from(600)),
1844-
}),
1840+
Some(TextRange::new(TextSize::from(500), TextSize::from(600))),
18451841
)
18461842
.unwrap();
18471843
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(": "),
@@ -518,7 +518,7 @@ pub(crate) fn inlay_hint(
518518
fn inlay_hint_label(
519519
snap: &GlobalStateSnapshot,
520520
label: InlayHintLabel,
521-
) -> Result<lsp_types::InlayHintLabel> {
521+
) -> Cancellable<lsp_types::InlayHintLabel> {
522522
Ok(match label.as_simple_str() {
523523
Some(s) => lsp_types::InlayHintLabel::String(s.into()),
524524
None => lsp_types::InlayHintLabel::LabelParts(
@@ -536,7 +536,7 @@ fn inlay_hint_label(
536536
command: None,
537537
})
538538
})
539-
.collect::<Result<Vec<_>>>()?,
539+
.collect::<Cancellable<Vec<_>>>()?,
540540
),
541541
})
542542
}
@@ -794,7 +794,7 @@ pub(crate) fn optional_versioned_text_document_identifier(
794794
pub(crate) fn location(
795795
snap: &GlobalStateSnapshot,
796796
frange: FileRange,
797-
) -> Result<lsp_types::Location> {
797+
) -> Cancellable<lsp_types::Location> {
798798
let url = url(snap, frange.file_id);
799799
let line_index = snap.file_line_index(frange.file_id)?;
800800
let range = range(&line_index, frange.range);
@@ -806,7 +806,7 @@ pub(crate) fn location(
806806
pub(crate) fn location_from_nav(
807807
snap: &GlobalStateSnapshot,
808808
nav: NavigationTarget,
809-
) -> Result<lsp_types::Location> {
809+
) -> Cancellable<lsp_types::Location> {
810810
let url = url(snap, nav.file_id);
811811
let line_index = snap.file_line_index(nav.file_id)?;
812812
let range = range(&line_index, nav.full_range);
@@ -818,7 +818,7 @@ pub(crate) fn location_link(
818818
snap: &GlobalStateSnapshot,
819819
src: Option<FileRange>,
820820
target: NavigationTarget,
821-
) -> Result<lsp_types::LocationLink> {
821+
) -> Cancellable<lsp_types::LocationLink> {
822822
let origin_selection_range = match src {
823823
Some(src) => {
824824
let line_index = snap.file_line_index(src.file_id)?;
@@ -840,7 +840,7 @@ pub(crate) fn location_link(
840840
fn location_info(
841841
snap: &GlobalStateSnapshot,
842842
target: NavigationTarget,
843-
) -> Result<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
843+
) -> Cancellable<(lsp_types::Url, lsp_types::Range, lsp_types::Range)> {
844844
let line_index = snap.file_line_index(target.file_id)?;
845845

846846
let target_uri = url(snap, target.file_id);
@@ -854,20 +854,20 @@ pub(crate) fn goto_definition_response(
854854
snap: &GlobalStateSnapshot,
855855
src: Option<FileRange>,
856856
targets: Vec<NavigationTarget>,
857-
) -> Result<lsp_types::GotoDefinitionResponse> {
857+
) -> Cancellable<lsp_types::GotoDefinitionResponse> {
858858
if snap.config.location_link() {
859859
let links = targets
860860
.into_iter()
861861
.map(|nav| location_link(snap, src, nav))
862-
.collect::<Result<Vec<_>>>()?;
862+
.collect::<Cancellable<Vec<_>>>()?;
863863
Ok(links.into())
864864
} else {
865865
let locations = targets
866866
.into_iter()
867867
.map(|nav| {
868868
location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
869869
})
870-
.collect::<Result<Vec<_>>>()?;
870+
.collect::<Cancellable<Vec<_>>>()?;
871871
Ok(locations.into())
872872
}
873873
}
@@ -881,7 +881,7 @@ pub(crate) fn snippet_text_document_edit(
881881
is_snippet: bool,
882882
file_id: FileId,
883883
edit: TextEdit,
884-
) -> Result<lsp_ext::SnippetTextDocumentEdit> {
884+
) -> Cancellable<lsp_ext::SnippetTextDocumentEdit> {
885885
let text_document = optional_versioned_text_document_identifier(snap, file_id);
886886
let line_index = snap.file_line_index(file_id)?;
887887
let mut edits: Vec<_> =
@@ -958,7 +958,7 @@ pub(crate) fn snippet_text_document_ops(
958958
pub(crate) fn snippet_workspace_edit(
959959
snap: &GlobalStateSnapshot,
960960
source_change: SourceChange,
961-
) -> Result<lsp_ext::SnippetWorkspaceEdit> {
961+
) -> Cancellable<lsp_ext::SnippetWorkspaceEdit> {
962962
let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
963963

964964
for op in source_change.file_system_edits {
@@ -995,7 +995,7 @@ pub(crate) fn snippet_workspace_edit(
995995
pub(crate) fn workspace_edit(
996996
snap: &GlobalStateSnapshot,
997997
source_change: SourceChange,
998-
) -> Result<lsp_types::WorkspaceEdit> {
998+
) -> Cancellable<lsp_types::WorkspaceEdit> {
999999
assert!(!source_change.is_snippet);
10001000
snippet_workspace_edit(snap, source_change).map(|it| it.into())
10011001
}
@@ -1048,7 +1048,7 @@ impl From<lsp_ext::SnippetTextEdit>
10481048
pub(crate) fn call_hierarchy_item(
10491049
snap: &GlobalStateSnapshot,
10501050
target: NavigationTarget,
1051-
) -> Result<lsp_types::CallHierarchyItem> {
1051+
) -> Cancellable<lsp_types::CallHierarchyItem> {
10521052
let name = target.name.to_string();
10531053
let detail = target.description.clone();
10541054
let kind = target.kind.map(symbol_kind).unwrap_or(lsp_types::SymbolKind::FUNCTION);
@@ -1080,7 +1080,7 @@ pub(crate) fn code_action(
10801080
snap: &GlobalStateSnapshot,
10811081
assist: Assist,
10821082
resolve_data: Option<(usize, lsp_types::CodeActionParams)>,
1083-
) -> Result<lsp_ext::CodeAction> {
1083+
) -> Cancellable<lsp_ext::CodeAction> {
10841084
let mut res = lsp_ext::CodeAction {
10851085
title: assist.label.to_string(),
10861086
group: assist.group.filter(|_| snap.config.code_action_group()).map(|gr| gr.0),
@@ -1113,13 +1113,13 @@ pub(crate) fn code_action(
11131113
pub(crate) fn runnable(
11141114
snap: &GlobalStateSnapshot,
11151115
runnable: Runnable,
1116-
) -> Result<lsp_ext::Runnable> {
1116+
) -> Cancellable<lsp_ext::Runnable> {
11171117
let config = snap.config.runnables();
11181118
let spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id)?;
11191119
let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone());
11201120
let target = spec.as_ref().map(|s| s.target.clone());
11211121
let (cargo_args, executable_args) =
1122-
CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg)?;
1122+
CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg);
11231123
let label = runnable.label(target);
11241124
let location = location_link(snap, None, runnable.nav)?;
11251125

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

0 commit comments

Comments
 (0)