Skip to content

Commit 517de9f

Browse files
Merge pull request #21141 from Wilfred/scip_enclosing_range
feature: Set enclosing_range field on SCIP output
2 parents 1928486 + dacc74f commit 517de9f

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

crates/ide/src/static_index.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ pub struct ReferenceData {
4444
pub struct TokenStaticData {
4545
pub documentation: Option<Documentation>,
4646
pub hover: Option<HoverResult>,
47+
/// The position of the token itself.
48+
///
49+
/// For example, in `fn foo() {}` this is the position of `foo`.
4750
pub definition: Option<FileRange>,
51+
/// The position of the entire definition that this token belongs to.
52+
///
53+
/// For example, in `fn foo() {}` this is the position from `fn`
54+
/// to the closing brace.
55+
pub definition_body: Option<FileRange>,
4856
pub references: Vec<ReferenceData>,
4957
pub moniker: Option<MonikerResult>,
5058
pub display_name: Option<String>,
@@ -248,6 +256,10 @@ impl StaticIndex<'_> {
248256
definition: def.try_to_nav(&sema).map(UpmappingResult::call_site).map(|it| {
249257
FileRange { file_id: it.file_id, range: it.focus_or_full_range() }
250258
}),
259+
definition_body: def
260+
.try_to_nav(&sema)
261+
.map(UpmappingResult::call_site)
262+
.map(|it| FileRange { file_id: it.file_id, range: it.full_range }),
251263
references: vec![],
252264
moniker: current_crate.and_then(|cc| def_to_moniker(self.db, def, cc)),
253265
display_name: def

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ impl flags::Scip {
189189
symbol_roles |= scip_types::SymbolRole::Definition as i32;
190190
}
191191

192+
let enclosing_range = match token.definition_body {
193+
Some(def_body) if def_body.file_id == file_id => {
194+
text_range_to_scip_range(&line_index, def_body.range)
195+
}
196+
_ => Vec::new(),
197+
};
198+
192199
occurrences.push(scip_types::Occurrence {
193200
range: text_range_to_scip_range(&line_index, text_range),
194201
symbol,
@@ -197,7 +204,7 @@ impl flags::Scip {
197204
syntax_kind: Default::default(),
198205
diagnostics: Vec::new(),
199206
special_fields: Default::default(),
200-
enclosing_range: Vec::new(),
207+
enclosing_range,
201208
});
202209
}
203210

@@ -508,6 +515,7 @@ fn moniker_descriptors(identifier: &MonikerIdentifier) -> Vec<scip_types::Descri
508515
#[cfg(test)]
509516
mod test {
510517
use super::*;
518+
use hir::FileRangeWrapper;
511519
use ide::{FilePosition, TextSize};
512520
use test_fixture::ChangeFixture;
513521
use vfs::VfsPath;
@@ -887,4 +895,32 @@ pub mod example_mod {
887895

888896
assert_eq!(token.documentation.as_ref().map(|d| d.as_str()), Some("foo"));
889897
}
898+
899+
#[test]
900+
fn function_has_enclosing_range() {
901+
let s = "fn foo() {}";
902+
903+
let mut host = AnalysisHost::default();
904+
let change_fixture = ChangeFixture::parse(host.raw_database(), s);
905+
host.raw_database_mut().apply_change(change_fixture.change);
906+
907+
let analysis = host.analysis();
908+
let si = StaticIndex::compute(
909+
&analysis,
910+
VendoredLibrariesConfig::Included {
911+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
912+
},
913+
);
914+
915+
let file = si.files.first().unwrap();
916+
let (_, token_id) = file.tokens.get(1).unwrap(); // first token is file module, second is `foo`
917+
let token = si.tokens.get(*token_id).unwrap();
918+
919+
let expected_range = FileRangeWrapper {
920+
file_id: FileId::from_raw(0),
921+
range: TextRange::new(0.into(), 11.into()),
922+
};
923+
924+
assert_eq!(token.definition_body, Some(expected_range));
925+
}
890926
}

0 commit comments

Comments
 (0)