|
| 1 | +package pl.thedeem.intellij.dql.highlighting.hints; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.hints.*; |
| 4 | +import com.intellij.codeInsight.hints.presentation.PresentationFactory; |
| 5 | +import com.intellij.openapi.editor.Editor; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.psi.PsiFile; |
| 8 | +import com.intellij.ui.components.JBCheckBox; |
| 9 | +import com.intellij.util.xmlb.annotations.Attribute; |
| 10 | +import org.jetbrains.annotations.Nls; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import pl.thedeem.intellij.dql.DQLBundle; |
| 13 | +import pl.thedeem.intellij.dql.definition.model.MappedParameter; |
| 14 | +import pl.thedeem.intellij.dql.definition.model.Parameter; |
| 15 | +import pl.thedeem.intellij.dql.psi.DQLCommand; |
| 16 | +import pl.thedeem.intellij.dql.psi.DQLFunctionExpression; |
| 17 | +import pl.thedeem.intellij.dql.psi.DQLParameterExpression; |
| 18 | + |
| 19 | +import javax.swing.*; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +/** |
| 25 | + * Provides inlay for parameters in DQL. |
| 26 | + * Yields a lot of experimental usages warnings, but it is the recommended way to implement this feature. |
| 27 | + */ |
| 28 | +public class DQLParameterNameHintsProvider implements InlayHintsProvider<DQLParameterNameHintsProvider.DQLParameterNameHintsSettings> { |
| 29 | + private static final SettingsKey<DQLParameterNameHintsSettings> KEY = new SettingsKey<>("dql.parameter.name.hints"); |
| 30 | + |
| 31 | + @Override |
| 32 | + public @NotNull SettingsKey<DQLParameterNameHintsSettings> getKey() { |
| 33 | + return KEY; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public @NotNull String getName() { |
| 38 | + return DQLBundle.message("settings.inlayHints.parameters.name"); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public @NotNull @Nls String getDescription() { |
| 43 | + return DQLBundle.message("settings.inlayHints.parameters.description"); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public @NotNull DQLParameterNameHintsSettings createSettings() { |
| 48 | + return new DQLParameterNameHintsSettings(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public @NotNull ImmediateConfigurable createConfigurable(@NotNull DQLParameterNameHintsSettings settings) { |
| 53 | + return listener -> { |
| 54 | + JPanel panel = new JPanel(); |
| 55 | + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| 56 | + panel.add(createOptionComponent( |
| 57 | + DQLBundle.message("settings.inlayHints.parameters.functions.description"), |
| 58 | + settings.showFunctionHints, |
| 59 | + (selected) -> { |
| 60 | + settings.showFunctionHints = selected; |
| 61 | + listener.settingsChanged(); |
| 62 | + } |
| 63 | + )); |
| 64 | + panel.add(createOptionComponent( |
| 65 | + DQLBundle.message("settings.inlayHints.parameters.commands.description"), |
| 66 | + settings.showCommandHints, |
| 67 | + (selected) -> { |
| 68 | + settings.showCommandHints = selected; |
| 69 | + listener.settingsChanged(); |
| 70 | + } |
| 71 | + )); |
| 72 | + panel.add(createOptionComponent( |
| 73 | + DQLBundle.message("settings.inlayHints.parameters.hideHintsForFirstCommandParameter.description"), |
| 74 | + settings.hideHintsForFirstCommandParameter, |
| 75 | + (selected) -> { |
| 76 | + settings.hideHintsForFirstCommandParameter = selected; |
| 77 | + listener.settingsChanged(); |
| 78 | + } |
| 79 | + )); |
| 80 | + panel.add(createOptionComponent( |
| 81 | + DQLBundle.message("settings.inlayHints.parameters.showHintsForSingularParameter.description"), |
| 82 | + settings.showHintsForSingularParameter, |
| 83 | + (selected) -> { |
| 84 | + settings.showHintsForSingularParameter = selected; |
| 85 | + listener.settingsChanged(); |
| 86 | + } |
| 87 | + )); |
| 88 | + return panel; |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public @NotNull String getPreviewText() { |
| 94 | + return /* language=DQLPart */ """ |
| 95 | + data record(f1 = 5, f2 = 10, f3 = 10, field = "10") |
| 96 | + | filter matchesValue("", "value", "otherValue", caseSensitive: true) |
| 97 | + | parse field, ""\" |
| 98 | + INT:i |
| 99 | + ""\" |
| 100 | + | summarize by: { |
| 101 | + i |
| 102 | + }, { |
| 103 | + f1 = max(f1), f2 = min(f2), count() |
| 104 | + } |
| 105 | + | sort i desc |
| 106 | + """; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public InlayHintsCollector getCollectorFor(@NotNull PsiFile file, |
| 111 | + @NotNull Editor editor, |
| 112 | + @NotNull DQLParameterNameHintsSettings settings, |
| 113 | + @NotNull InlayHintsSink sink) { |
| 114 | + PresentationFactory factory = new PresentationFactory(editor); |
| 115 | + |
| 116 | + return new FactoryInlayHintsCollector(editor) { |
| 117 | + @Override |
| 118 | + public boolean collect(@NotNull PsiElement element, @NotNull Editor editor, @NotNull InlayHintsSink sink) { |
| 119 | + if (element instanceof DQLCommand command && settings.showCommandHints) { |
| 120 | + addHints(factory, sink, getHints(element, command.getParameters(), settings)); |
| 121 | + } else if (element instanceof DQLFunctionExpression function && settings.showFunctionHints) { |
| 122 | + addHints(factory, sink, getHints(element, function.getParameters(), settings)); |
| 123 | + } |
| 124 | + return true; |
| 125 | + } |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + @SuppressWarnings("UnstableApiUsage") |
| 130 | + protected void addHints(@NotNull PresentationFactory factory, |
| 131 | + @NotNull InlayHintsSink sink, |
| 132 | + @NotNull List<Hint> hints) { |
| 133 | + for (Hint hint : hints) { |
| 134 | + var p = factory.roundWithBackgroundAndSmallInset(factory.smallTextWithoutBackground(hint.text + ":")); |
| 135 | + sink.addInlineElement(hint.offset, false, p, false); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static @NotNull List<Hint> getHints(@NotNull PsiElement element, @NotNull List<MappedParameter> parameters, @NotNull DQLParameterNameHintsSettings settings) { |
| 140 | + List<Hint> result = new ArrayList<>(); |
| 141 | + if (parameters.size() == 1 && !settings.showHintsForSingularParameter) { |
| 142 | + return result; |
| 143 | + } |
| 144 | + for (int i = 0; i < parameters.size(); i++) { |
| 145 | + if (i == 0 && element instanceof DQLCommand && settings.hideHintsForFirstCommandParameter) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + MappedParameter parameter = parameters.get(i); |
| 149 | + Parameter definition = parameter.definition(); |
| 150 | + if (definition == null) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + List<List<PsiElement>> groups = parameter.getParameterGroups(); |
| 154 | + for (List<PsiElement> group : groups) { |
| 155 | + if (!(group.getFirst() instanceof DQLParameterExpression)) { |
| 156 | + int textOffset = group.getFirst().getTextOffset(); |
| 157 | + if (definition.variadic()) { |
| 158 | + result.add(new Hint((!parameter.included().isEmpty() ? "…" : "") + definition.name(), textOffset)); |
| 159 | + } else if (parameters.size() > 1) { |
| 160 | + result.add(new Hint(definition.name(), textOffset)); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return result; |
| 166 | + } |
| 167 | + |
| 168 | + protected @NotNull JComponent createOptionComponent(@NotNull String name, boolean isSelected, @NotNull Consumer<Boolean> onChange) { |
| 169 | + JCheckBox check = new JBCheckBox(name, isSelected); |
| 170 | + check.addActionListener(e -> onChange.accept(check.isSelected())); |
| 171 | + return check; |
| 172 | + } |
| 173 | + |
| 174 | + protected record Hint(@NotNull String text, int offset) { |
| 175 | + } |
| 176 | + |
| 177 | + public static final class DQLParameterNameHintsSettings { |
| 178 | + @Attribute("showFunctionHints") |
| 179 | + public boolean showFunctionHints = true; |
| 180 | + |
| 181 | + @Attribute("showCommandHints") |
| 182 | + public boolean showCommandHints = true; |
| 183 | + |
| 184 | + @Attribute("hideHintsForFirstCommandParameter") |
| 185 | + public boolean hideHintsForFirstCommandParameter = true; |
| 186 | + |
| 187 | + @Attribute("showHintsForSingularParameter") |
| 188 | + public boolean showHintsForSingularParameter = false; |
| 189 | + } |
| 190 | +} |
0 commit comments