Skip to content

Commit dda641c

Browse files
committed
Set documentation field in SCIP from doc comment
Previously, the documentation field was the same as the text shown to users when they hover over that symbol. The documentation should really just be the doc comment, and as of rust-lang#16179 the signature is already stored in the signatureDocumentation field.
1 parent dba5997 commit dda641c

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

crates/ide/src/static_index.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
//! This module provides `StaticIndex` which is used for powering
22
//! read-only code browsers and emitting LSIF
33
4-
use hir::{db::HirDatabase, Crate, HirFileIdExt, Module};
4+
use hir::{db::HirDatabase, Crate, HirFileIdExt, Module, Semantics};
55
use ide_db::{
66
base_db::{FileId, FileRange, SourceDatabaseExt},
77
defs::Definition,
8+
documentation::Documentation,
9+
famous_defs::FamousDefs,
810
helpers::get_definition,
911
FxHashMap, FxHashSet, RootDatabase,
1012
};
11-
use syntax::{AstNode, SyntaxKind::*, TextRange, T};
13+
use syntax::{AstNode, SyntaxKind::*, SyntaxNode, TextRange, T};
1214

1315
use crate::inlay_hints::InlayFieldsToResolve;
1416
use crate::navigation_target::UpmappingResult;
@@ -22,7 +24,7 @@ use crate::{
2224

2325
/// A static representation of fully analyzed source code.
2426
///
25-
/// The intended use-case is powering read-only code browsers and emitting LSIF
27+
/// The intended use-case is powering read-only code browsers and emitting LSIF/SCIP.
2628
#[derive(Debug)]
2729
pub struct StaticIndex<'a> {
2830
pub files: Vec<StaticIndexedFile>,
@@ -40,6 +42,7 @@ pub struct ReferenceData {
4042

4143
#[derive(Debug)]
4244
pub struct TokenStaticData {
45+
pub documentation: Option<Documentation>,
4346
pub hover: Option<HoverResult>,
4447
pub definition: Option<FileRange>,
4548
pub references: Vec<ReferenceData>,
@@ -103,6 +106,19 @@ fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
103106
modules
104107
}
105108

109+
fn documentation_for_definition(
110+
sema: &Semantics<'_, RootDatabase>,
111+
def: Definition,
112+
scope_node: &SyntaxNode,
113+
) -> Option<Documentation> {
114+
let famous_defs = match &def {
115+
Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(scope_node)?.krate())),
116+
_ => None,
117+
};
118+
119+
def.docs(sema.db, famous_defs.as_ref())
120+
}
121+
106122
impl StaticIndex<'_> {
107123
fn add_file(&mut self, file_id: FileId) {
108124
let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
@@ -169,6 +185,7 @@ impl StaticIndex<'_> {
169185
*it
170186
} else {
171187
let it = self.tokens.insert(TokenStaticData {
188+
documentation: documentation_for_definition(&sema, def, &node),
172189
hover: hover_for_definition(&sema, file_id, def, &node, &hover_config),
173190
definition: def.try_to_nav(self.db).map(UpmappingResult::call_site).map(|it| {
174191
FileRange { file_id: it.file_id, range: it.focus_or_full_range() }

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

+24-7
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,11 @@ impl flags::Scip {
135135
}
136136

137137
if symbols_emitted.insert(id) {
138-
let documentation = token
139-
.hover
140-
.as_ref()
141-
.map(|hover| hover.markup.as_str())
142-
.filter(|it| !it.is_empty())
143-
.map(|it| vec![it.to_owned()]);
138+
let documentation = match &token.documentation {
139+
Some(doc) => vec![doc.as_str().to_owned()],
140+
None => vec![],
141+
};
142+
144143
let position_encoding =
145144
scip_types::PositionEncoding::UTF8CodeUnitOffsetFromLineStart.into();
146145
let signature_documentation =
@@ -153,7 +152,7 @@ impl flags::Scip {
153152
});
154153
let symbol_info = scip_types::SymbolInformation {
155154
symbol: symbol.clone(),
156-
documentation: documentation.unwrap_or_default(),
155+
documentation,
157156
relationships: Vec::new(),
158157
special_fields: Default::default(),
159158
kind: symbol_kind(token.kind).into(),
@@ -599,4 +598,22 @@ pub mod example_mod {
599598
"rust-analyzer cargo main . MyTypeAlias#",
600599
);
601600
}
601+
602+
#[test]
603+
fn documentation_matches_doc_comment() {
604+
let s = "/// foo\nfn bar() {}";
605+
606+
let mut host = AnalysisHost::default();
607+
let change_fixture = ChangeFixture::parse(s);
608+
host.raw_database_mut().apply_change(change_fixture.change);
609+
610+
let analysis = host.analysis();
611+
let si = StaticIndex::compute(&analysis);
612+
613+
let file = si.files.first().unwrap();
614+
let (_, token_id) = file.tokens.first().unwrap();
615+
let token = si.tokens.get(*token_id).unwrap();
616+
617+
assert_eq!(token.documentation.as_ref().map(|d| d.as_str()), Some("foo"));
618+
}
602619
}

0 commit comments

Comments
 (0)