Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add field type inlay #9

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5,17 +5,17 @@
import com.intellij.lang.Language;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorFontType;

Check warning on line 8 in src/main/java/ac/quant/quickfixspec/inlay/QuickfixInlayHintsProvider.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import com.intellij.openapi.editor.colors.EditorFontType;`
import com.intellij.psi.*;
import com.intellij.psi.xml.*;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.codeInsight.hints.presentation.InlayPresentation;

Check warning on line 14 in src/main/java/ac/quant/quickfixspec/inlay/QuickfixInlayHintsProvider.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import com.intellij.codeInsight.hints.presentation.InlayPresentation;`
import com.intellij.codeInsight.hints.presentation.PresentationFactory;

Check warning on line 15 in src/main/java/ac/quant/quickfixspec/inlay/QuickfixInlayHintsProvider.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import com.intellij.codeInsight.hints.presentation.PresentationFactory;`

import javax.swing.*;
import java.awt.*;

Check warning on line 18 in src/main/java/ac/quant/quickfixspec/inlay/QuickfixInlayHintsProvider.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import java.awt.*;`

@SuppressWarnings("UnstableApiUsage")
@Slf4j
Expand Down Expand Up @@ -50,6 +50,11 @@

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 @@
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 @@
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