summaryrefslogtreecommitdiff
path: root/xml/impl/src/com/intellij/psi/filters/getters/XmlAttributeValueGetter.java
blob: 929162b3b01ec90ce2473fa083ec619c5c468fb2 (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
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.psi.filters.getters;

import com.intellij.codeInsight.completion.CompletionContext;
import com.intellij.psi.PsiElement;
import com.intellij.psi.filters.ContextGetter;
import com.intellij.psi.impl.source.xml.XmlTokenImpl;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTokenType;
import com.intellij.util.ArrayUtil;
import com.intellij.xml.XmlAttributeDescriptor;
import com.intellij.xml.impl.BasicXmlAttributeDescriptor;
import org.jetbrains.annotations.Nullable;

/**
 * Created by IntelliJ IDEA.
 * User: ik
 * Date: 24.11.2003
 * Time: 14:17:59
 * To change this template use Options | File Templates.
 */
public class XmlAttributeValueGetter implements ContextGetter {
  public XmlAttributeValueGetter() {}

  public Object[] get(PsiElement context, CompletionContext completionContext) {
    return getApplicableAttributeVariants(context);
  }

  @Nullable
  public static String[] getEnumeratedValues(XmlAttribute attribute) {
    final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
    if (descriptor == null) {
      return ArrayUtil.EMPTY_STRING_ARRAY;
    }

    return descriptor instanceof BasicXmlAttributeDescriptor ?
                      ((BasicXmlAttributeDescriptor)descriptor).getEnumeratedValues(attribute) : descriptor.getEnumeratedValues();
  }

  private Object[] getApplicableAttributeVariants(PsiElement _context) {
    if (_context instanceof XmlTokenImpl && ((XmlTokenImpl)_context).getTokenType() == XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN) {
      XmlAttribute attr = PsiTreeUtil.getParentOfType(_context, XmlAttribute.class);
      if (attr != null) {
        final XmlAttributeDescriptor descriptor = attr.getDescriptor();

        if (descriptor != null) {
          if (descriptor.isFixed()) {
            final String defaultValue = descriptor.getDefaultValue();
            return defaultValue == null ? ArrayUtil.EMPTY_OBJECT_ARRAY : new Object[]{defaultValue};
          }

          String[] values = getEnumeratedValues(attr);

          final String[] strings = addSpecificCompletions(attr);

          if (values == null || values.length == 0) {
            values = strings;
          }
          else if (strings != null) {
            values = ArrayUtil.mergeArrays(values, strings);
          }

          return values == null ? ArrayUtil.EMPTY_OBJECT_ARRAY : values;
        }
      }
    }

    return ArrayUtil.EMPTY_OBJECT_ARRAY;
  }

  @Nullable
  protected String[] addSpecificCompletions(final XmlAttribute context) {
    return null;
  }

}