summaryrefslogtreecommitdiff
path: root/plugins/tasks/tasks-core/jira/src/com/intellij/tasks/jira/jql/codeinsight/JqlCompletionContributor.java
blob: fd4975f83d95e92b0ddcfe09fe10b59d40ee53e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.intellij.tasks.jira.jql.codeinsight;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.completion.util.ParenthesesInsertHandler;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.filters.ElementFilter;
import com.intellij.psi.filters.position.FilterPattern;
import com.intellij.psi.impl.DebugUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.tasks.jira.jql.JqlTokenTypes;
import com.intellij.tasks.jira.jql.psi.*;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.intellij.patterns.PlatformPatterns.psiElement;

/**
 * @author Mikhail Golubev
 */
public class JqlCompletionContributor extends CompletionContributor {
  private static final Logger LOG = Logger.getInstance(JqlCompletionContributor.class);

  private static final FilterPattern BEGINNING_OF_LINE = new FilterPattern(new ElementFilter() {
    @Override
    public boolean isAcceptable(Object element, @Nullable PsiElement context) {
      if (!(element instanceof PsiElement)) return false;
      PsiElement p = (PsiElement)element;
      PsiFile file = p.getContainingFile().getOriginalFile();
      char[] chars = file.textToCharArray();
      for (int offset = p.getTextOffset() - 1; offset >= 0; offset--) {
        char c = chars[offset];
        if (c == '\n') return true;
        if (!StringUtil.isWhiteSpace(c)) return false;
      }
      return true;
    }

    @Override
    public boolean isClassAcceptable(Class hintClass) {
      return true;
    }
  });

  private static FilterPattern rightAfterElement(final PsiElementPattern.Capture<? extends PsiElement> pattern) {
    return new FilterPattern(new ElementFilter() {
      @Override
      public boolean isAcceptable(Object element, @Nullable PsiElement context) {
        if (!(element instanceof PsiElement)) return false;
        PsiElement prevLeaf = PsiTreeUtil.prevVisibleLeaf((PsiElement)element);
        if (prevLeaf == null) return false;
        PsiElement parent = PsiTreeUtil.findFirstParent(prevLeaf, new Condition<PsiElement>() {
          @Override
          public boolean value(PsiElement element) {
            return pattern.accepts(element);
          }
        });
        if (parent == null) return false;
        if (PsiTreeUtil.hasErrorElements(parent)) return false;
        return prevLeaf.getTextRange().getEndOffset() == parent.getTextRange().getEndOffset();
      }

      @Override
      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    });
  }

  private static FilterPattern rightAfterElement(Class<? extends PsiElement> aClass) {
    return rightAfterElement(psiElement(aClass));
  }

  // Patterns:

  private static final PsiElementPattern.Capture<PsiElement> AFTER_CLAUSE_WITH_HISTORY_PREDICATE =
    psiElement().and(rightAfterElement(JqlClauseWithHistoryPredicates.class));

  private static final PsiElementPattern.Capture<PsiElement> AFTER_ANY_CLAUSE =
    psiElement().andOr(
      rightAfterElement(JqlTerminalClause.class),
      // in other words after closing parenthesis
      rightAfterElement(JqlSubClause.class));

  private static final PsiElementPattern.Capture<PsiElement> AFTER_ORDER_KEYWORD =
    psiElement().afterLeaf(psiElement(JqlTokenTypes.ORDER_KEYWORD));

  private static final PsiElementPattern.Capture<PsiElement> AFTER_FIELD_IN_CLAUSE =
    psiElement().and(rightAfterElement(
      psiElement(JqlIdentifier.class).
        andNot(psiElement().inside(JqlFunctionCall.class)).
        andNot(psiElement().inside(JqlOrderBy.class))));


  /**
   * e.g. "not | ...", "status = closed and |" or "status = closed or |"
   */
  private static final PsiElementPattern.Capture<PsiElement> BEGINNING_OF_CLAUSE = psiElement().andOr(
    BEGINNING_OF_LINE,
    psiElement().afterLeaf(psiElement().andOr(
      psiElement().withElementType(JqlTokenTypes.AND_OPERATORS),
      psiElement().withElementType(JqlTokenTypes.OR_OPERATORS),
      psiElement().withElementType(JqlTokenTypes.NOT_OPERATORS).
        andNot(psiElement().inside(JqlTerminalClause.class)),
      psiElement().withElementType(JqlTokenTypes.LPAR).
        andNot(psiElement().inside(JqlTerminalClause.class))
    )));

  /**
   * e.g. "status changed on |"
   */
  private static final PsiElementPattern.Capture<PsiElement> AFTER_KEYWORD_IN_HISTORY_PREDICATE = psiElement().
    inside(JqlHistoryPredicate.class). // do not consider "by" inside "order by"
    afterLeaf(psiElement().withElementType(JqlTokenTypes.HISTORY_PREDICATES));

  /**
   * e.g. "duedate > |" or "type was in |"
   */
  private static final PsiElementPattern.Capture<PsiElement> AFTER_OPERATOR_EXCEPT_IS = psiElement().
    inside(JqlTerminalClause.class).
    afterLeaf(
      psiElement().andOr(
        psiElement().withElementType(JqlTokenTypes.SIMPLE_OPERATORS),
        psiElement(JqlTokenTypes.WAS_KEYWORD),
        psiElement(JqlTokenTypes.IN_KEYWORD),
        // "not" is considered only as part of other complex operators
        // "is" and "is not" are not suitable also
        psiElement(JqlTokenTypes.NOT_KEYWORD).
          afterLeaf(psiElement(JqlTokenTypes.WAS_KEYWORD))));
  /**
   * e.g. "foo is |" or "foo is not |"
   */
  private static final PsiElementPattern.Capture<PsiElement> AFTER_IS_OPERATOR = psiElement().
    inside(JqlTerminalClause.class).andOr(
    psiElement().afterLeaf(psiElement(JqlTokenTypes.IS_KEYWORD)),
    psiElement().afterLeaf(psiElement(JqlTokenTypes.NOT_KEYWORD).
      afterLeaf(psiElement(JqlTokenTypes.IS_KEYWORD)))
  );

  /**
   * e.g. "commentary ~ 'spam' order by |" or "assignee = currentUser() order by duedate desc, |"
   */
  private static final PsiElementPattern.Capture<PsiElement> BEGINNING_OF_SORT_KEY = psiElement().
    inside(JqlOrderBy.class).
    andOr(
      psiElement().afterLeaf(psiElement(JqlTokenTypes.COMMA)),
      psiElement().afterLeaf(psiElement(JqlTokenTypes.BY_KEYWORD))
    );

  /**
   * e.g. "status = 'in progress' order by reported |"
   */
  private static final PsiElementPattern.Capture<PsiElement> AFTER_FIELD_IN_SORT_KEY = psiElement().
    afterLeaf(psiElement().withElementType(JqlTokenTypes.VALID_FIELD_NAMES).inside(JqlSortKey.class));

  private static final PsiElementPattern.Capture<PsiElement> INSIDE_LIST = psiElement().
    inside(JqlList.class).
    afterLeaf(
      psiElement().andOr(
        psiElement(JqlTokenTypes.LPAR),
        psiElement(JqlTokenTypes.COMMA)
        // e.g. assignee in ('mark', 'bob', currentUser() | )
      ).andNot(psiElement().inside(JqlFunctionCall.class))
    );

  public JqlCompletionContributor() {
    addKeywordsCompletion();
    addFieldNamesCompletion();
    addFunctionNamesCompletion();
    addEmptyOrNullCompletion();
  }

  @Override
  public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    LOG.debug(DebugUtil.psiToString(parameters.getOriginalFile(), true));
    super.fillCompletionVariants(parameters, result);
  }

  private void addKeywordsCompletion() {
    extend(CompletionType.BASIC,
           AFTER_ANY_CLAUSE,
           new JqlKeywordCompletionProvider("and", "or", "order by"));
    extend(CompletionType.BASIC,
           AFTER_CLAUSE_WITH_HISTORY_PREDICATE,
           new JqlKeywordCompletionProvider("on", "before", "after", "during", "from", "to", "by"));
    extend(CompletionType.BASIC,
           AFTER_FIELD_IN_CLAUSE,
           new JqlKeywordCompletionProvider("was", "in", "not", "is", "changed"));
    extend(CompletionType.BASIC,
           psiElement().andOr(
             BEGINNING_OF_CLAUSE,
             psiElement().inside(JqlTerminalClause.class).andOr(
               psiElement().afterLeaf(psiElement(JqlTokenTypes.WAS_KEYWORD)),
               psiElement().afterLeaf(psiElement(JqlTokenTypes.IS_KEYWORD)))),
           new JqlKeywordCompletionProvider("not"));
    extend(CompletionType.BASIC,
           psiElement().afterLeaf(
             psiElement().andOr(
               psiElement(JqlTokenTypes.NOT_KEYWORD).
                 andNot(psiElement().afterLeaf(
                   psiElement(JqlTokenTypes.IS_KEYWORD))).
                 andNot(psiElement().withParent(JqlNotClause.class)),
               psiElement(JqlTokenTypes.WAS_KEYWORD))),
           new JqlKeywordCompletionProvider("in"));
    extend(CompletionType.BASIC,
           AFTER_ORDER_KEYWORD,
           new JqlKeywordCompletionProvider("by"));
    extend(CompletionType.BASIC,
           AFTER_FIELD_IN_SORT_KEY,
           new JqlKeywordCompletionProvider("asc", "desc"));
  }

  private void addFieldNamesCompletion() {
    extend(CompletionType.BASIC,
           psiElement().andOr(
             BEGINNING_OF_CLAUSE,
             BEGINNING_OF_SORT_KEY),
           new JqlFieldCompletionProvider(JqlFieldType.UNKNOWN));
  }

  private void addFunctionNamesCompletion() {
    extend(CompletionType.BASIC,
           psiElement().andOr(
             AFTER_OPERATOR_EXCEPT_IS,
             INSIDE_LIST,
             // NOTE: function calls can't be used as other functions arguments according to grammar
             AFTER_KEYWORD_IN_HISTORY_PREDICATE),
           new JqlFunctionCompletionProvider());
  }

  private void addEmptyOrNullCompletion() {
    extend(CompletionType.BASIC,
           AFTER_IS_OPERATOR,
           new JqlKeywordCompletionProvider("empty", "null"));
  }

  private static class JqlKeywordCompletionProvider extends CompletionProvider<CompletionParameters> {
    private final String[] myKeywords;

    private JqlKeywordCompletionProvider(String... keywords) {
      myKeywords = keywords;
    }

    @Override
    protected void addCompletions(@NotNull CompletionParameters parameters,
                                  ProcessingContext context,
                                  @NotNull CompletionResultSet result) {
      for (String keyword : myKeywords) {
        result.addElement(LookupElementBuilder.create(keyword).withBoldness(true));
      }
    }
  }

  private static class JqlFunctionCompletionProvider extends CompletionProvider<CompletionParameters> {

    @Override
    protected void addCompletions(@NotNull CompletionParameters parameters,
                                  ProcessingContext context,
                                  @NotNull CompletionResultSet result) {
      JqlFieldType operandType;
      boolean listFunctionExpected;
      PsiElement curElem = parameters.getPosition();
      JqlHistoryPredicate predicate = PsiTreeUtil.getParentOfType(curElem, JqlHistoryPredicate.class);
      if (predicate != null) {
        listFunctionExpected = false;
        JqlHistoryPredicate.Type predicateType = predicate.getType();
        switch (predicateType) {
          case BEFORE:
          case AFTER:
          case DURING:
          case ON:
            operandType = JqlFieldType.DATE;
            break;
          case BY:
            operandType = JqlFieldType.USER;
            break;
          // from, to
          default:
            operandType = findTypeOfField(curElem);
        }
      }
      else {
        operandType = findTypeOfField(curElem);
        listFunctionExpected = insideClauseWithListOperator(curElem);
      }
      for (String functionName : JqlStandardFunction.allOfType(operandType, listFunctionExpected)) {
        result.addElement(LookupElementBuilder.create(functionName)
          .withInsertHandler(ParenthesesInsertHandler.NO_PARAMETERS));
      }
    }

    private static JqlFieldType findTypeOfField(PsiElement element) {
      JqlTerminalClause clause = PsiTreeUtil.getParentOfType(element, JqlTerminalClause.class);
      if (clause != null) {
        return JqlStandardField.typeOf(clause.getFieldName());
      }
      return JqlFieldType.UNKNOWN;
    }

    private static boolean insideClauseWithListOperator(PsiElement element) {
      JqlTerminalClause clause = PsiTreeUtil.getParentOfType(element, JqlTerminalClause.class);
      if (clause == null || clause.getType() == null) {
        return false;
      }
      return clause.getType().isListOperator();
    }
  }

  private static class JqlFieldCompletionProvider extends CompletionProvider<CompletionParameters> {
    private final JqlFieldType myFieldType;

    private JqlFieldCompletionProvider(JqlFieldType fieldType) {
      myFieldType = fieldType;
    }

    @Override
    protected void addCompletions(@NotNull CompletionParameters parameters,
                                  ProcessingContext context,
                                  @NotNull CompletionResultSet result) {
      for (String field : JqlStandardField.allOfType(myFieldType)) {
        result.addElement(LookupElementBuilder.create(field));
      }
    }
  }
}