-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display inlay hints for label references
- Loading branch information
Showing
8 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,59 @@ | ||
use base_db::{semantics::tex::LabelKind, util::render_label}; | ||
use base_db::{ | ||
semantics::tex::{Label, LabelKind}, | ||
util::{queries::Object, render_label}, | ||
FeatureParams, | ||
}; | ||
use rustc_hash::FxHashMap; | ||
|
||
use crate::InlayHintData; | ||
|
||
use super::InlayHintBuilder; | ||
use crate::{InlayHint, InlayHintBuilder, InlayHintData}; | ||
|
||
pub(super) fn find_hints(builder: &mut InlayHintBuilder) -> Option<()> { | ||
let definitions = base_db::semantics::tex::Label::find_all(&builder.params.feature.project) | ||
.into_iter() | ||
.filter(|(_, label)| label.kind == LabelKind::Definition) | ||
.map(|(_, label)| (label.name_text(), label)) | ||
.collect::<FxHashMap<_, _>>(); | ||
|
||
let params = &builder.params.feature; | ||
let data = params.document.data.as_tex()?; | ||
let range = builder.params.range; | ||
for label in data | ||
.semantics | ||
.labels | ||
.iter() | ||
.filter(|label| label.kind == LabelKind::Definition) | ||
.filter(|label| label.name.range.intersect(range).is_some()) | ||
{ | ||
let Some(rendered) = render_label(params.workspace, ¶ms.project, label) else { | ||
continue; | ||
}; | ||
|
||
builder.hints.push(crate::InlayHint { | ||
offset: label.full_range.end(), | ||
data: InlayHintData::LabelDefinition(rendered), | ||
}); | ||
if let Some(hint) = process_label(params, &definitions, label) { | ||
builder.hints.push(hint); | ||
} | ||
} | ||
|
||
Some(()) | ||
} | ||
|
||
fn process_label<'a>( | ||
params: &FeatureParams<'a>, | ||
definitions: &FxHashMap<&str, &'a Label>, | ||
label: &'a Label, | ||
) -> Option<InlayHint<'a>> { | ||
let config = ¶ms.workspace.config().inlay_hints; | ||
let offset = label.full_range.end(); | ||
let data = if label.kind == LabelKind::Definition { | ||
if !config.label_definitions { | ||
return None; | ||
} | ||
|
||
let label = render_label(params.workspace, ¶ms.project, label)?; | ||
InlayHintData::LabelDefinition(label) | ||
} else { | ||
if !config.label_references { | ||
return None; | ||
} | ||
|
||
let label = definitions.get(label.name.text.as_str())?; | ||
let label = render_label(params.workspace, ¶ms.project, label)?; | ||
InlayHintData::LabelReference(label) | ||
}; | ||
|
||
Some(InlayHint { offset, data }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters