How to safely access the textual presentation of an InlayHint?
Answered
Hello!
I am currently developing a plugin and need to get acces to the text inside a DeclarativeInlayHint.
I have found a solution (see the code below) BUT the class InlayDumpUtil as well as InlayPresentationList.getEntries() are marked as “unstable” (marked with @ApiStatus.Experimental or Internal).
Are these safe to use, so can i ignore this warning? Or is there a better way to get access to the text inside a InlayHint?
InlayModel model = editor.getInlayModel();
TextRange range = file.getTextRange();
int currentPosition = 0;
int nextPosition;
String sourceText = file.getText();
var providerInfo = new InlayProviderPassInfo(provider, "provider.id", emptyMap());
var pass = new DeclarativeInlayHintsPass(file, editor, List.of(providerInfo), false, false);
pass.doCollectInformation(new EmptyProgressIndicator());
pass.applyInformationToEditor();
List<Inlay<?>> inlineElements = model.getInlineElementsInRange(range.getStartOffset(), range.getEndOffset());
if (inlineElements.isEmpty()) {
System.out.println("THERE ARE NO INLAY HINTS");
}
InlayDumpUtil.InlayData data; //!! marked unstable
for (Inlay<?> element : inlineElements) {
data = new InlayDumpUtil.InlayData(element, InlayDumpUtil.InlayType.Inline); //!! marked unstable
nextPosition = element.getOffset();
sb.append(sourceText, currentPosition, nextPosition);
sb.append("<#").append(data.render((r, a) -> { //!! marked unstable
TextInlayPresentationEntry text = (TextInlayPresentationEntry) ((DeclarativeInlayRenderer) r).getPresentationList().getEntries()[0]; //!! marked unstable
return text.getText();
})).append("#>");
currentPosition = nextPosition;
}
sb.append(sourceText, currentPosition, sourceText.length());
return sb.toString();
Please sign in to leave a comment.
Hi,
Experimental APIs can be used. The experimental status is a signal that an API can change in the future without preserving a backward compatibility, but it doesn't have to happen.
Internal APIs can't be used by plugins. I don't see which parts of your code use internal APIs.
I get the following IntelliJ when I use the
getEntries()
method:'getEntries()' is unstable because its signature references unstable 'com.intellij.codeInsight.hints.declarative.impl.InlayPresentationEntry' marked with @ApiStatus.Internal
Thanks for clarifying.
Could you also please explain the use case you are trying to implement? Why do you need texts of inlay hints? Maybe there is an alternative solution to your problem.
For a Unit Test
If you use it in unit tests, then I wouldn't worry about these warnings as test code isn't bundled into the plugin distribution. The only risk is that your unit tests may break if the internal API is changed, but the same can happen for experimental.
Alright. Thank you so much Karol!