Skip to content

Commit bee27eb

Browse files
committed
Auto merge of rust-lang#13632 - Veykril:scip, r=Veykril
Make it more obvious which SCIP features we do not yet emit in code
2 parents e162d58 + 656d886 commit bee27eb

File tree

1 file changed

+68
-47
lines changed
  • crates/rust-analyzer/src/cli

1 file changed

+68
-47
lines changed

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

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,27 @@ impl flags::Scip {
4747

4848
let si = StaticIndex::compute(&analysis);
4949

50-
let mut index = scip_types::Index {
51-
metadata: Some(scip_types::Metadata {
52-
version: scip_types::ProtocolVersion::UnspecifiedProtocolVersion.into(),
53-
tool_info: Some(scip_types::ToolInfo {
54-
name: "rust-analyzer".to_owned(),
55-
version: "0.1".to_owned(),
56-
arguments: vec![],
57-
..Default::default()
58-
})
59-
.into(),
60-
project_root: format!(
61-
"file://{}",
62-
path.normalize()
63-
.as_os_str()
64-
.to_str()
65-
.ok_or(anyhow::anyhow!("Unable to normalize project_root path"))?
66-
.to_string()
67-
),
68-
text_document_encoding: scip_types::TextEncoding::UTF8.into(),
69-
..Default::default()
50+
let metadata = scip_types::Metadata {
51+
version: scip_types::ProtocolVersion::UnspecifiedProtocolVersion.into(),
52+
tool_info: Some(scip_types::ToolInfo {
53+
name: "rust-analyzer".to_owned(),
54+
version: "0.1".to_owned(),
55+
arguments: vec![],
56+
special_fields: Default::default(),
7057
})
7158
.into(),
72-
..Default::default()
59+
project_root: format!(
60+
"file://{}",
61+
path.normalize()
62+
.as_os_str()
63+
.to_str()
64+
.ok_or(anyhow::anyhow!("Unable to normalize project_root path"))?
65+
.to_string()
66+
),
67+
text_document_encoding: scip_types::TextEncoding::UTF8.into(),
68+
special_fields: Default::default(),
7369
};
70+
let mut documents = Vec::new();
7471

7572
let mut symbols_emitted: HashSet<TokenId> = HashSet::default();
7673
let mut tokens_to_symbol: HashMap<TokenId, String> = HashMap::new();
@@ -95,53 +92,77 @@ impl flags::Scip {
9592
endings: LineEndings::Unix,
9693
};
9794

98-
let mut doc = scip_types::Document {
99-
relative_path,
100-
language: "rust".to_string(),
101-
..Default::default()
102-
};
95+
let mut occurrences = Vec::new();
96+
let mut symbols = Vec::new();
10397

104-
tokens.into_iter().for_each(|(range, id)| {
98+
tokens.into_iter().for_each(|(text_range, id)| {
10599
let token = si.tokens.get(id).unwrap();
106100

107-
let mut occurrence = scip_types::Occurrence::default();
108-
occurrence.range = text_range_to_scip_range(&line_index, range);
109-
occurrence.symbol = tokens_to_symbol
101+
let range = text_range_to_scip_range(&line_index, text_range);
102+
let symbol = tokens_to_symbol
110103
.entry(id)
111104
.or_insert_with(|| {
112105
let symbol = token_to_symbol(&token).unwrap_or_else(&mut new_local_symbol);
113106
scip::symbol::format_symbol(symbol)
114107
})
115108
.clone();
116109

110+
let mut symbol_roles = Default::default();
111+
117112
if let Some(def) = token.definition {
118-
if def.range == range {
119-
occurrence.symbol_roles |= scip_types::SymbolRole::Definition as i32;
113+
if def.range == text_range {
114+
symbol_roles |= scip_types::SymbolRole::Definition as i32;
120115
}
121116

122117
if symbols_emitted.insert(id) {
123-
let mut symbol_info = scip_types::SymbolInformation::default();
124-
symbol_info.symbol = occurrence.symbol.clone();
125-
if let Some(hover) = &token.hover {
126-
if !hover.markup.as_str().is_empty() {
127-
symbol_info.documentation = vec![hover.markup.as_str().to_string()];
128-
}
129-
}
130-
131-
doc.symbols.push(symbol_info)
118+
let documentation = token
119+
.hover
120+
.as_ref()
121+
.map(|hover| hover.markup.as_str())
122+
.filter(|it| !it.is_empty())
123+
.map(|it| vec![it.to_owned()]);
124+
let symbol_info = scip_types::SymbolInformation {
125+
symbol: symbol.clone(),
126+
documentation: documentation.unwrap_or_default(),
127+
relationships: Vec::new(),
128+
special_fields: Default::default(),
129+
};
130+
131+
symbols.push(symbol_info)
132132
}
133133
}
134134

135-
doc.occurrences.push(occurrence);
135+
occurrences.push(scip_types::Occurrence {
136+
range,
137+
symbol,
138+
symbol_roles,
139+
override_documentation: Vec::new(),
140+
syntax_kind: Default::default(),
141+
diagnostics: Vec::new(),
142+
special_fields: Default::default(),
143+
});
136144
});
137145

138-
if doc.occurrences.is_empty() {
146+
if occurrences.is_empty() {
139147
continue;
140148
}
141149

142-
index.documents.push(doc);
150+
documents.push(scip_types::Document {
151+
relative_path,
152+
language: "rust".to_string(),
153+
occurrences,
154+
symbols,
155+
special_fields: Default::default(),
156+
});
143157
}
144158

159+
let index = scip_types::Index {
160+
metadata: Some(metadata).into(),
161+
documents,
162+
external_symbols: Vec::new(),
163+
special_fields: Default::default(),
164+
};
165+
145166
scip::write_message_to_file("index.scip", index)
146167
.map_err(|err| anyhow::anyhow!("Failed to write scip to file: {}", err))?;
147168

@@ -181,7 +202,7 @@ fn new_descriptor_str(
181202
name: name.to_string(),
182203
disambiguator: "".to_string(),
183204
suffix: suffix.into(),
184-
..Default::default()
205+
special_fields: Default::default(),
185206
}
186207
}
187208

@@ -232,11 +253,11 @@ fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
232253
manager: "cargo".to_string(),
233254
name: package_name,
234255
version: version.unwrap_or_else(|| ".".to_string()),
235-
..Default::default()
256+
special_fields: Default::default(),
236257
})
237258
.into(),
238259
descriptors,
239-
..Default::default()
260+
special_fields: Default::default(),
240261
})
241262
}
242263

0 commit comments

Comments
 (0)