forked from JetBrains/android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AndroidDomExtender.java
75 lines (68 loc) · 2.64 KB
/
AndroidDomExtender.java
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
/*
* Copyright 2000-2010 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 org.jetbrains.android.dom;
import com.android.ide.common.rendering.api.AttributeFormat;
import com.android.tools.idea.flags.StudioFlags;
import com.intellij.util.xml.GenericAttributeValue;
import com.intellij.util.xml.reflect.DomExtender;
import com.intellij.util.xml.reflect.DomExtensionsRegistrar;
import java.util.Set;
import org.jetbrains.android.dom.resources.ResourceValue;
import org.jetbrains.android.facet.AndroidFacet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class AndroidDomExtender extends DomExtender<AndroidDomElement> {
@Override
public boolean supportsStubs() {
return false;
}
@NotNull
private static Class getValueClass(@Nullable AttributeFormat format) {
if (format == null) return String.class;
switch (format) {
case BOOLEAN:
return boolean.class;
case REFERENCE:
case DIMENSION:
case COLOR:
return ResourceValue.class;
default:
return String.class;
}
}
@Override
public void registerExtensions(@NotNull AndroidDomElement element, @NotNull final DomExtensionsRegistrar registrar) {
if (!areExtensionsKnown()) {
return;
}
AndroidDomUtil.checkThreading();
final AndroidFacet facet = AndroidFacet.getInstance(element);
if (facet == null) {
return;
}
AttributeProcessingUtil.processAttributes(element, facet, true, (xmlName, attrDef, parentStyleableName) -> {
Set<AttributeFormat> formats = attrDef.getFormats();
Class valueClass = formats.size() == 1 ? getValueClass(formats.iterator().next()) : String.class;
registrar.registerAttributeChildExtension(xmlName, GenericAttributeValue.class);
return registrar.registerGenericAttributeValueChildExtension(xmlName, valueClass);
});
SubtagsProcessingUtil.processSubtags(facet, element, registrar::registerCollectionChildrenExtension);
}
public static boolean areExtensionsKnown() {
// TODO(b/122487428): Implement computing in the background and caching.
return StudioFlags.RUN_DOM_EXTENDER.get();
}
}