use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class AntDomExtender method registerExtensions.
public void registerExtensions(@NotNull final AntDomElement antDomElement, @NotNull DomExtensionsRegistrar registrar) {
final XmlElement xmlElement = antDomElement.getXmlElement();
if (xmlElement instanceof XmlTag) {
final XmlTag xmlTag = (XmlTag) xmlElement;
final String tagName = xmlTag.getName();
final AntDomProject antProject = antDomElement.getAntProject();
if (antProject == null) {
return;
}
final ReflectedProject reflected = ReflectedProject.getProject(antProject.getClassLoader());
if (reflected.getProject() == null) {
return;
}
final DomGenericInfo genericInfo = antDomElement.getGenericInfo();
AntIntrospector classBasedIntrospector = null;
final Hashtable<String, Class> coreTaskDefs = reflected.getTaskDefinitions();
final Hashtable<String, Class> coreTypeDefs = reflected.getDataTypeDefinitions();
final boolean isCustom = antDomElement instanceof AntDomCustomElement;
if ("project".equals(tagName)) {
classBasedIntrospector = getIntrospector(reflected.getProject().getClass());
} else if ("target".equals(tagName)) {
classBasedIntrospector = getIntrospector(reflected.getTargetClass());
} else {
if (isCustom) {
final AntDomCustomElement custom = (AntDomCustomElement) antDomElement;
final Class definitionClass = custom.getDefinitionClass();
if (definitionClass != null) {
classBasedIntrospector = getIntrospector(definitionClass);
}
} else {
Class elemType = antDomElement.getChildDescription().getUserData(ELEMENT_IMPL_CLASS_KEY);
if (elemType == null) {
if (coreTaskDefs != null) {
elemType = coreTaskDefs.get(tagName);
}
}
if (elemType == null) {
if (coreTypeDefs != null) {
elemType = coreTypeDefs.get(tagName);
}
}
if (elemType != null) {
classBasedIntrospector = getIntrospector(elemType);
}
}
}
AbstractIntrospector parentIntrospector = null;
if (classBasedIntrospector != null) {
parentIntrospector = new ClassIntrospectorAdapter(classBasedIntrospector, coreTaskDefs, coreTypeDefs);
} else {
if (isCustom) {
final AntDomNamedElement declaringElement = ((AntDomCustomElement) antDomElement).getDeclaringElement();
if (declaringElement instanceof AntDomMacroDef) {
parentIntrospector = new MacrodefIntrospectorAdapter((AntDomMacroDef) declaringElement);
} else if (declaringElement instanceof AntDomMacrodefElement) {
parentIntrospector = new MacrodefElementOccurrenceIntrospectorAdapter((AntDomMacrodefElement) declaringElement);
} else if (declaringElement instanceof AntDomScriptDef) {
parentIntrospector = new ScriptdefIntrospectorAdapter((AntDomScriptDef) declaringElement);
}
}
}
if (parentIntrospector != null) {
defineAttributes(xmlTag, registrar, genericInfo, parentIntrospector);
if ("project".equals(tagName) || parentIntrospector.isContainer()) {
// can contain any task or/and type definition
if (coreTaskDefs != null) {
for (Map.Entry<String, Class> entry : coreTaskDefs.entrySet()) {
final DomExtension extension = registerChild(registrar, genericInfo, entry.getKey());
if (extension != null) {
final Class type = entry.getValue();
if (type != null) {
extension.putUserData(ELEMENT_IMPL_CLASS_KEY, type);
}
extension.putUserData(AntDomElement.ROLE, AntDomElement.Role.TASK);
}
}
}
if (coreTypeDefs != null) {
for (Map.Entry<String, Class> entry : coreTypeDefs.entrySet()) {
final DomExtension extension = registerChild(registrar, genericInfo, entry.getKey());
if (extension != null) {
final Class type = entry.getValue();
if (type != null) {
extension.putUserData(ELEMENT_IMPL_CLASS_KEY, type);
}
extension.putUserData(AntDomElement.ROLE, AntDomElement.Role.DATA_TYPE);
}
}
}
registrar.registerCustomChildrenExtension(AntDomCustomElement.class, new AntCustomTagNameDescriptor());
} else {
final Iterator<String> nested = parentIntrospector.getNestedElementsIterator();
while (nested.hasNext()) {
final String nestedElementName = nested.next();
final DomExtension extension = registerChild(registrar, genericInfo, nestedElementName);
if (extension != null) {
Class type = parentIntrospector.getNestedElementType(nestedElementName);
if (type != null && CommonClassNames.JAVA_LANG_OBJECT.equals(type.getName())) {
// hack to support badly written tasks
type = null;
}
if (type == null) {
if (coreTypeDefs != null) {
type = coreTypeDefs.get(nestedElementName);
}
}
if (type != null) {
extension.putUserData(ELEMENT_IMPL_CLASS_KEY, type);
}
AntDomElement.Role role = AntDomElement.Role.DATA_TYPE;
if (coreTaskDefs != null && coreTaskDefs.containsKey(nestedElementName)) {
role = AntDomElement.Role.TASK;
} else if (type != null && isAssignableFrom(Task.class.getName(), type)) {
role = AntDomElement.Role.TASK;
}
if (role != null) {
extension.putUserData(AntDomElement.ROLE, role);
}
}
}
registrar.registerCustomChildrenExtension(AntDomCustomElement.class, new AntCustomTagNameDescriptor());
}
}
}
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class AntDomDocumentationProvider method getQuickNavigateInfo.
@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
// todo!
if (element instanceof PomTargetPsiElement) {
final PomTarget pomTarget = ((PomTargetPsiElement) element).getTarget();
if (pomTarget instanceof DomTarget) {
final DomElement domElement = ((DomTarget) pomTarget).getDomElement();
if (domElement instanceof AntDomTarget) {
final AntDomTarget antTarget = (AntDomTarget) domElement;
final String description = antTarget.getDescription().getRawText();
if (description != null && description.length() > 0) {
final String targetName = antTarget.getName().getRawText();
final StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
builder.append("Target");
if (targetName != null) {
builder.append(" \"").append(targetName).append("\"");
}
final XmlElement xmlElement = antTarget.getXmlElement();
if (xmlElement != null) {
final PsiFile containingFile = xmlElement.getContainingFile();
if (containingFile != null) {
final String fileName = containingFile.getName();
builder.append(" [").append(fileName).append("]");
}
}
return builder.append(" ").append(description).toString();
} finally {
StringBuilderSpinAllocator.dispose(builder);
}
}
}
} else if (pomTarget instanceof DomChildrenDescription) {
final DomChildrenDescription description = (DomChildrenDescription) pomTarget;
Type type = null;
try {
type = description.getType();
} catch (UnsupportedOperationException e) {
LOG.info(e);
}
if (type instanceof Class && AntDomElement.class.isAssignableFrom(((Class) type))) {
final String elemName = description.getName();
if (elemName != null) {
final AntDomElement.Role role = description.getUserData(AntDomElement.ROLE);
final StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
if (role == AntDomElement.Role.TASK) {
builder.append("Task ");
} else if (role == AntDomElement.Role.DATA_TYPE) {
builder.append("Data structure ");
}
builder.append(elemName);
return builder.toString();
} finally {
StringBuilderSpinAllocator.dispose(builder);
}
}
}
}
}
return null;
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class AntDomProject method getNavigationElement.
@Nullable
public PsiElement getNavigationElement(String propertyName) {
final DomTarget target = DomTarget.getTarget(this);
final PsiElement nameElementPsi = target != null ? PomService.convertToPsi(target) : null;
if (nameElementPsi != null) {
return nameElementPsi;
}
final XmlElement xmlElement = getXmlElement();
return xmlElement != null ? xmlElement.getNavigationElement() : null;
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class AntResolveInspection method getPropertyFiles.
@NotNull
private static Collection<PropertiesFile> getPropertyFiles(@Nullable AntDomProject antDomProject, @NotNull XmlElement stopElement) {
if (antDomProject == null) {
return Collections.emptyList();
}
final Set<PropertiesFile> files = new java.util.HashSet<>();
final int stopOffset = stopElement.getTextOffset();
for (Iterator<AntDomElement> iterator = antDomProject.getAntChildrenIterator(); iterator.hasNext(); ) {
AntDomElement child = iterator.next();
final XmlElement xmlElement = child.getXmlElement();
if (xmlElement != null && xmlElement.getTextOffset() >= stopOffset) {
// no need to offer to add properties to files that are imported after the property reference
break;
}
if (child instanceof AntDomProperty) {
final AntDomProperty property = (AntDomProperty) child;
final PropertiesFile file = property.getPropertiesFile();
if (file != null) {
files.add(file);
}
}
}
return files;
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class AntDomTargetDependsListConverter method createReferences.
@NotNull
public PsiReference[] createReferences(GenericDomValue<TargetResolver.Result> value, PsiElement element, ConvertContext context) {
final XmlElement xmlElement = value.getXmlElement();
if (!(xmlElement instanceof XmlAttribute)) {
return PsiReference.EMPTY_ARRAY;
}
final XmlAttributeValue valueElement = ((XmlAttribute) xmlElement).getValueElement();
if (valueElement == null) {
return PsiReference.EMPTY_ARRAY;
}
final String refsString = value.getStringValue();
if (refsString == null) {
return PsiReference.EMPTY_ARRAY;
}
final List<PsiReference> refs = new ArrayList<>();
final AntDomTargetReference.ReferenceGroup group = new AntDomTargetReference.ReferenceGroup();
final TextRange wholeStringRange = ElementManipulators.getValueTextRange(valueElement);
final StringTokenizer tokenizer = new StringTokenizer(refsString, ",", false);
while (tokenizer.hasMoreTokens()) {
final String token = tokenizer.nextToken();
int tokenStartOffset = tokenizer.getCurrentPosition() - token.length();
final String ref = token.trim();
if (ref.length() != token.length()) {
for (int idx = 0; idx < token.length(); idx++) {
if (Character.isWhitespace(token.charAt(idx))) {
tokenStartOffset++;
} else {
break;
}
}
}
refs.add(new AntDomTargetReference(element, TextRange.from(wholeStringRange.getStartOffset() + tokenStartOffset, ref.length()), group));
}
return refs.toArray(new PsiReference[refs.size()]);
}
Aggregations