Skip to content

Commit

Permalink
Merge pull request #9 from dantimofte/dtimofte/add_field_type
Browse files Browse the repository at this point in the history
add field type inlay
  • Loading branch information
dantimofte authored Sep 16, 2024
2 parents 3a88c75 + 4f454ff commit 2f7e597
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
- [ ] Click the <kbd>Watch</kbd> button on the top of the [IntelliJ Platform Plugin Template][template] to be notified about releases containing new features and fixes.

<!-- Plugin description -->
This Fancy IntelliJ Platform Plugin is going to be your implementation of the brilliant ideas that you have.
This Plugin is going to make it easier to work with the QuickFix XML Specification.

This specific section is a source for the [plugin.xml](/src/main/resources/META-INF/plugin.xml) file which will be extracted by the [Gradle](/build.gradle.kts) during the build process.

To keep everything working, do not remove `<!-- ... -->` sections.
- shows the field number next to the field or group name
- shows the field type next to the field or group name

<!-- Plugin description end -->

## Installation
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.dantimofte.quickfixspec
pluginName = quickfix-spec
pluginRepositoryUrl = https://github.com/dantimofte/quickfix-spec
# SemVer format -> https://semver.org
pluginVersion = 0.0.1
pluginVersion = 0.1.0

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 233
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public class QuickfixInlayHintsProvider implements InlayHintsProvider<NoSettings

private static class MyCollector implements InlayHintsCollector {

// Set the top and bottom insets using magic numbers for better alignment
// TODO - Use a better way to calculate insets
final int TOP_INSET = 6;
final int BOTTOM_INSET = 2;

@Override
public boolean collect(@NotNull PsiElement element, @NotNull Editor editor, @NotNull InlayHintsSink sink){

Expand Down Expand Up @@ -79,35 +84,64 @@ public boolean collect(@NotNull PsiElement element, @NotNull Editor editor, @Not
String fieldName = attributeValue.getValue();

// Find the tag number
String tagNumber = findTagNumber(getRootTag(tag), fieldName);

if (tagNumber != null) {
// Set the top and bottom insets using magic numbers for better alignment
// TODO - Use a better way to calculate insets
int topInset = 6;
int bottomInset = 2;

// Create the inlay presentation
PresentationFactory factory = new PresentationFactory(editor);
InlayPresentation textPresentation = factory.text(" (" + tagNumber + ")");

// Wrap with InsetPresentation
InlayPresentation centeredPresentation = new InsetPresentation(
textPresentation,
0, // Left inset
0, // Right inset
topInset, // Top inset
bottomInset // Bottom inset
);

int offset = attributeValue.getTextRange().getEndOffset();
sink.addInlineElement(offset, false, centeredPresentation, false);
}
final XmlTag fieldTag = findField(getRootTag(tag), fieldName);

String tagNumber = fieldTag != null ? fieldTag.getAttributeValue("number") : null;
String fieldType = fieldTag != null ? fieldTag.getAttributeValue("type") : null;

maybeAddTagNumber(editor, sink, attributeValue, tagNumber);
maybeAddFieldType(editor, sink, attributeValue, fieldType);

return true;
}

private @Nullable String findTagNumber(PsiElement root, String fieldName) {
private void maybeAddTagNumber(Editor editor, InlayHintsSink sink, XmlAttributeValue attributeValue, String tagNumber) {
if (tagNumber == null) {
return;
}

// Create the inlay presentation
PresentationFactory factory = new PresentationFactory(editor);
InlayPresentation textPresentation = factory.text(" (" + tagNumber + ")");

// Wrap with InsetPresentation
InlayPresentation centeredPresentation = new InsetPresentation(
textPresentation,
0, // Left inset
0, // Right inset
TOP_INSET, // Top inset
BOTTOM_INSET // Bottom inset
);

int offset = attributeValue.getTextRange().getEndOffset();
sink.addInlineElement(offset, false, centeredPresentation, false);
}

private void maybeAddFieldType(Editor editor, InlayHintsSink sink, XmlAttributeValue attributeValue, String fieldType) {
if (fieldType == null) {
return;
}

// Create the inlay presentation
PresentationFactory factory = new PresentationFactory(editor);
InlayPresentation textPresentation = factory.text(" (" + fieldType + ")");

// Wrap with InsetPresentation
InlayPresentation centeredPresentation = new InsetPresentation(
textPresentation,
0, // Left inset
0, // Right inset
TOP_INSET, // Top inset
BOTTOM_INSET // Bottom inset
);

int offset = attributeValue.getTextRange().getEndOffset();
sink.addInlineElement(offset, false, centeredPresentation, false);
}



private @Nullable XmlTag findField(PsiElement root, String fieldName) {
if (!(root instanceof XmlTag rootTag)) {
return null;
}
Expand All @@ -119,13 +153,14 @@ public boolean collect(@NotNull PsiElement element, @NotNull Editor editor, @Not
for (XmlTag fieldTag : fieldsTag.findSubTags("field")) {
String nameAttr = fieldTag.getAttributeValue("name");
if (fieldName.equals(nameAttr)) {
return fieldTag.getAttributeValue("number");
return fieldTag;
}
}
}
return null;
}


// Helper method to navigate to the root tag
private @Nullable XmlTag getRootTag(PsiElement element) {
PsiElement current = element;
Expand Down

0 comments on commit 2f7e597

Please sign in to comment.