Skip to content

Commit e2c2f29

Browse files
committed
Hover implicits
1 parent a772d77 commit e2c2f29

File tree

8 files changed

+102
-40
lines changed

8 files changed

+102
-40
lines changed

vhdl_lang/src/analysis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ mod visibility;
2626
mod tests;
2727

2828
pub use self::root::DesignRoot;
29-
pub use named_entity::{HasNamedEntity, NamedEntity};
29+
pub use named_entity::{HasNamedEntity, NamedEntity, NamedEntityKind};

vhdl_lang/src/analysis/declarative.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'a> AnalyzeContext<'a> {
244244
Declaration::Alias(alias) => {
245245
if let Some(ent) = self.analyze_alias_declaration(region, alias, diagnostics)? {
246246
region.add(ent.clone(), diagnostics);
247-
region.add_implicit_declaration_aliases(&ent, diagnostics);
247+
region.add_implicit_declaration_aliases(ent, diagnostics);
248248
}
249249
}
250250
Declaration::Object(ref mut object_decl) => {

vhdl_lang/src/analysis/named_entity.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub struct NamedEntity {
341341
/// A unique id of the entity.
342342
/// Entities with the same id will be the same.
343343
id: EntityId,
344-
implicit: bool,
344+
pub implicit_of: Option<Arc<NamedEntity>>,
345345
/// The location where the declaration was made.
346346
/// Builtin and implicit declaration will not have a source position.
347347
designator: Designator,
@@ -366,21 +366,22 @@ impl NamedEntity {
366366
) -> NamedEntity {
367367
NamedEntity {
368368
id,
369-
implicit: false,
369+
implicit_of: None,
370370
decl_pos,
371371
designator,
372372
kind,
373373
}
374374
}
375375

376376
pub fn implicit(
377+
of_ent: Arc<NamedEntity>,
377378
designator: impl Into<Designator>,
378379
kind: NamedEntityKind,
379380
decl_pos: Option<&SrcPos>,
380381
) -> NamedEntity {
381382
NamedEntity {
382383
id: new_id(),
383-
implicit: true,
384+
implicit_of: Some(of_ent),
384385
decl_pos: decl_pos.cloned(),
385386
designator: designator.into(),
386387
kind,
@@ -392,7 +393,7 @@ impl NamedEntity {
392393
}
393394

394395
pub fn is_implicit(&self) -> bool {
395-
self.implicit
396+
self.implicit_of.is_some()
396397
}
397398

398399
pub fn is_subprogram(&self) -> bool {
@@ -404,7 +405,7 @@ impl NamedEntity {
404405
}
405406

406407
pub fn is_explicit(&self) -> bool {
407-
!self.implicit
408+
self.implicit_of.is_none()
408409
}
409410

410411
pub fn decl_pos(&self) -> Option<&SrcPos> {
@@ -592,7 +593,7 @@ impl TypeEnt {
592593
) -> TypeEnt {
593594
let ent = Arc::new(NamedEntity {
594595
id: id.unwrap_or_else(new_id),
595-
implicit: false,
596+
implicit_of: None,
596597
decl_pos: Some(ident.tree.pos.clone()),
597598
designator: ident.tree.item.clone().into(),
598599
kind: NamedEntityKind::Type(kind),

vhdl_lang/src/analysis/region.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,12 @@ impl<'a> Region<'a> {
397397

398398
pub fn add_implicit_declaration_aliases(
399399
&mut self,
400-
ent: &NamedEntity,
400+
ent: Arc<NamedEntity>,
401401
diagnostics: &mut dyn DiagnosticHandler,
402402
) {
403403
for entity in ent.actual_kind().implicit_declarations() {
404404
let entity = NamedEntity::implicit(
405+
ent.clone(),
405406
entity.designator().clone(),
406407
NamedEntityKind::NonObjectAlias(entity.clone()),
407408
ent.decl_pos(),

vhdl_lang/src/analysis/standard.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl<'a> StandardRegion<'a> {
104104
file_type.decl_pos(),
105105
)));
106106
implicit.push(Arc::new(NamedEntity::implicit(
107+
file_type.clone().into(),
107108
self.symbol("FILE_OPEN"),
108109
NamedEntityKind::Subprogram(Signature::new(params, None)),
109110
file_type.decl_pos(),
@@ -150,6 +151,7 @@ impl<'a> StandardRegion<'a> {
150151
file_type.decl_pos(),
151152
)));
152153
implicit.push(Arc::new(NamedEntity::implicit(
154+
file_type.clone().into(),
153155
self.symbol("FILE_OPEN"),
154156
NamedEntityKind::Subprogram(Signature::new(params, None)),
155157
file_type.decl_pos(),
@@ -166,6 +168,7 @@ impl<'a> StandardRegion<'a> {
166168
)));
167169

168170
implicit.push(Arc::new(NamedEntity::implicit(
171+
file_type.clone().into(),
169172
self.symbol("FILE_CLOSE"),
170173
NamedEntityKind::Subprogram(Signature::new(params, None)),
171174
file_type.decl_pos(),
@@ -193,6 +196,7 @@ impl<'a> StandardRegion<'a> {
193196
)));
194197

195198
implicit.push(Arc::new(NamedEntity::implicit(
199+
file_type.clone().into(),
196200
self.symbol("READ"),
197201
NamedEntityKind::Subprogram(Signature::new(params, None)),
198202
file_type.decl_pos(),
@@ -220,6 +224,7 @@ impl<'a> StandardRegion<'a> {
220224
)));
221225

222226
implicit.push(Arc::new(NamedEntity::implicit(
227+
file_type.clone().into(),
223228
self.symbol("WRITE"),
224229
NamedEntityKind::Subprogram(Signature::new(params, None)),
225230
file_type.decl_pos(),
@@ -236,6 +241,7 @@ impl<'a> StandardRegion<'a> {
236241
)));
237242

238243
implicit.push(Arc::new(NamedEntity::implicit(
244+
file_type.clone().into(),
239245
self.symbol("FLUSH"),
240246
NamedEntityKind::Subprogram(Signature::new(params, None)),
241247
file_type.decl_pos(),
@@ -252,6 +258,7 @@ impl<'a> StandardRegion<'a> {
252258
)));
253259

254260
implicit.push(Arc::new(NamedEntity::implicit(
261+
file_type.clone().into(),
255262
self.symbol("ENDFILE"),
256263
NamedEntityKind::Subprogram(Signature::new(params, Some(boolean))),
257264
file_type.decl_pos(),
@@ -277,6 +284,7 @@ impl<'a> StandardRegion<'a> {
277284
)));
278285

279286
Arc::new(NamedEntity::implicit(
287+
type_ent.clone().into(),
280288
self.symbol("TO_STRING"),
281289
NamedEntityKind::Subprogram(Signature::new(params, Some(self.string()))),
282290
type_ent.decl_pos(),
@@ -311,6 +319,7 @@ impl<'a> StandardRegion<'a> {
311319
)));
312320

313321
Arc::new(NamedEntity::implicit(
322+
type_ent.clone().into(),
314323
self.symbol(name),
315324
NamedEntityKind::Subprogram(Signature::new(params, Some(type_ent.clone()))),
316325
type_ent.decl_pos(),
@@ -341,6 +350,7 @@ impl<'a> StandardRegion<'a> {
341350
)));
342351

343352
Arc::new(NamedEntity::implicit(
353+
type_ent.clone().into(),
344354
self.symbol("DEALLOCATE"),
345355
NamedEntityKind::Subprogram(Signature::new(params, None)),
346356
type_ent.decl_pos(),

vhdl_lang/src/analysis/tests/implicit.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,37 @@ end package;
175175
Some(code.s1("enum_t").pos())
176176
);
177177
}
178+
179+
#[test]
180+
fn hover_for_implicit() {
181+
let mut builder = LibraryBuilder::new();
182+
let code = builder.code(
183+
"lib",
184+
"
185+
package pkg is
186+
type enum_t is (alpha, beta);
187+
alias thealias is to_string[enum_t return string];
188+
end package;
189+
",
190+
);
191+
192+
let (root, diagnostics) = builder.get_analyzed_root();
193+
check_no_diagnostics(&diagnostics);
194+
195+
let to_string = code.s1("to_string");
196+
assert_eq!(
197+
root.format_declaration(
198+
root.search_reference(to_string.source(), to_string.start())
199+
.unwrap()
200+
),
201+
Some(
202+
"\
203+
-- function 'TO_STRING' with signature [enum_t return STRING]
204+
205+
-- Implicitly defined by:
206+
type enum_t is (alpha, beta);
207+
"
208+
.to_owned()
209+
)
210+
);
211+
}

vhdl_lang/src/ast/search.rs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,12 +1249,28 @@ impl FormatDeclaration {
12491249

12501250
impl Searcher for FormatDeclaration {
12511251
fn search_decl(&mut self, decl: FoundDeclaration) -> SearchState {
1252-
if decl.named_entity().map(|ent| ent.id()) == Some(self.ent.id()) {
1253-
self.result = Some(decl.to_hover());
1254-
Finished(Found)
1252+
let ent = if let Some(ent) = decl.named_entity() {
1253+
ent
12551254
} else {
1256-
NotFinished
1255+
return NotFinished;
1256+
};
1257+
1258+
if let Some(ref implicit_of) = self.ent.implicit_of {
1259+
// Implicit
1260+
if implicit_of.id() == ent.id() {
1261+
self.result = Some(format!(
1262+
"-- {}\n\n-- Implicitly defined by:\n{}\n",
1263+
self.ent.describe(),
1264+
decl,
1265+
));
1266+
return Finished(Found);
1267+
}
1268+
} else if self.ent.id() == ent.id() {
1269+
// Explicit
1270+
self.result = Some(decl.to_string());
1271+
return Finished(Found);
12571272
}
1273+
NotFinished
12581274
}
12591275
}
12601276

@@ -1355,74 +1371,74 @@ impl<'a> HasSrcPos for FoundDeclaration<'a> {
13551371
}
13561372
}
13571373

1358-
impl FoundDeclaration<'_> {
1359-
fn to_hover(&self) -> String {
1374+
impl std::fmt::Display for FoundDeclaration<'_> {
1375+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13601376
match self {
13611377
FoundDeclaration::InterfaceObject(ref value) => match value.list_type {
1362-
InterfaceListType::Port => format!("```vhdl\nport {};\n```", value),
1363-
InterfaceListType::Generic => format!("```vhdl\ngeneric {};\n```", value),
1364-
InterfaceListType::Parameter => format!("```vhdl\n{};\n```", value),
1378+
InterfaceListType::Port => write!(f, "port {};", value),
1379+
InterfaceListType::Generic => write!(f, "generic {};", value),
1380+
InterfaceListType::Parameter => write!(f, "{};", value),
13651381
},
13661382
FoundDeclaration::ForIndex(ref ident, ref drange) => {
1367-
format!("```vhdl\nfor {} in {} loop\n```", ident, drange)
1383+
write!(f, "for {} in {} loop", ident, drange)
13681384
}
13691385
FoundDeclaration::ForGenerateIndex(ref ident, ref value) => match ident {
1370-
Some(ident) => format!("```vhdl\n{}: {}\n```", ident, value),
1371-
None => format!("```vhdl\n{}\n```", value),
1386+
Some(ident) => write!(f, "{}: {}", ident, value),
1387+
None => write!(f, "{}", value),
13721388
},
13731389
FoundDeclaration::Library(ref value) => {
1374-
format!("```vhdl\nlibrary {};\n```", value)
1390+
write!(f, "library {};", value)
13751391
}
13761392
FoundDeclaration::Function(ref value) => {
1377-
format!("```vhdl\n{};\n```", value)
1393+
write!(f, "{};", value)
13781394
}
13791395
FoundDeclaration::Procedure(ref value) => {
1380-
format!("```vhdl\n{};\n```", value)
1396+
write!(f, "{};", value)
13811397
}
13821398
FoundDeclaration::Object(ref value) => {
1383-
format!("```vhdl\n{}\n```", value)
1399+
write!(f, "{}", value)
13841400
}
13851401
FoundDeclaration::ElementDeclaration(elem) => {
1386-
format!("```vhdl\n{}\n```", elem)
1402+
write!(f, "{}", elem)
13871403
}
13881404
FoundDeclaration::EnumerationLiteral(ident, elem) => {
1389-
format!("```vhdl\n{} : {}\n```", elem, ident)
1405+
write!(f, "{} : {}", elem, ident)
13901406
}
13911407
FoundDeclaration::File(ref value) => {
1392-
format!("```vhdl\n{}\n```", value)
1408+
write!(f, "{}", value)
13931409
}
13941410
FoundDeclaration::Type(ref value) => {
1395-
format!("```vhdl\n{}\n```", value)
1411+
write!(f, "{}", value)
13961412
}
13971413
FoundDeclaration::Component(ref value) => {
1398-
format!("```vhdl\n{}\n```", value)
1414+
write!(f, "{}", value)
13991415
}
14001416
FoundDeclaration::Alias(ref value) => {
1401-
format!("```vhdl\n{}\n```", value)
1417+
write!(f, "{}", value)
14021418
}
14031419
FoundDeclaration::Package(ref value) => {
1404-
format!("```vhdl\n{}\n```", value)
1420+
write!(f, "{}", value)
14051421
}
14061422
FoundDeclaration::PackageInstance(ref value) => {
1407-
format!("```vhdl\n{}\n```", value)
1423+
write!(f, "{}", value)
14081424
}
14091425
FoundDeclaration::Configuration(ref value) => {
1410-
format!("```vhdl\n{}\n```", value)
1426+
write!(f, "{}", value)
14111427
}
14121428
FoundDeclaration::Entity(ref value) => {
1413-
format!("```vhdl\n{}\n```", value)
1429+
write!(f, "{}", value)
14141430
}
14151431
FoundDeclaration::Context(ref value) => {
1416-
format!("```vhdl\n{}\n```", value)
1432+
write!(f, "{}", value)
14171433
}
14181434
FoundDeclaration::GenerateBody(value) => {
1419-
format!("```vhdl\n{}\n```", value)
1435+
write!(f, "{}", value)
14201436
}
14211437
FoundDeclaration::ConcurrentStatement(value) => {
1422-
format!("```vhdl\n{}\n```", value)
1438+
write!(f, "{}", value)
14231439
}
14241440
FoundDeclaration::SequentialStatement(value) => {
1425-
format!("```vhdl\n{}\n```", value)
1441+
write!(f, "{}", value)
14261442
}
14271443
}
14281444
}

vhdl_ls/src/vhdl_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<T: RpcChannel + Clone> InitializedVHDLServer<T> {
418418
Some(Hover {
419419
contents: HoverContents::Markup(MarkupContent {
420420
kind: MarkupKind::Markdown,
421-
value,
421+
value: format!("```vhdl\n{}\n```", value),
422422
}),
423423
range: None,
424424
})

0 commit comments

Comments
 (0)