use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.
the class PropertyKeyReferenceProvider method getReferencesByElement.
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
if (myTagMode && element instanceof XmlTag) {
return getTagReferences(((XmlTag) element));
}
if (element instanceof XmlAttributeValue) {
final XmlAttribute xmlAttribute = (XmlAttribute) element.getParent();
if (element.getTextLength() < 2) {
return PsiReference.EMPTY_ARRAY;
}
final XmlTag tag = xmlAttribute.getParent();
String value = null;
String bundle = tag.getAttributeValue("bundle");
if ("key".equals(xmlAttribute.getName())) {
value = xmlAttribute.getValue();
} else if (myFallbackKeyName.equals(xmlAttribute.getName())) {
value = xmlAttribute.getValue();
final String groupBundle = tag.getAttributeValue(myFallbackGroupName);
if (groupBundle != null) {
bundle = groupBundle;
}
}
if (value != null) {
return new PsiReference[] { new MyPropertyReference(value, xmlAttribute.getValueElement(), bundle) };
}
}
return PsiReference.EMPTY_ARRAY;
}
use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.
the class AntCreatePropertyFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
final PsiFile containingFile = psiElement.getContainingFile();
final FileModificationService modificationService = FileModificationService.getInstance();
Navigatable result = null;
if (myPropFile != null) {
final VirtualFile vFile = myPropFile.getVirtualFile();
boolean canModify = true;
if (myPropFile instanceof PsiFile) {
canModify = modificationService.prepareFileForWrite((PsiFile) myPropFile);
} else if (vFile != null) {
canModify = modificationService.prepareVirtualFilesForWrite(project, Collections.singleton(vFile));
}
if (canModify) {
final IProperty generatedProperty = myPropFile.addProperty(myCanonicalText, "");
result = vFile != null ? new OpenFileDescriptor(project, vFile, generatedProperty.getPsiElement().getTextRange().getEndOffset()) : generatedProperty;
}
} else {
if (containingFile instanceof XmlFile) {
final XmlFile xmlFile = (XmlFile) containingFile;
final XmlTag rootTag = xmlFile.getRootTag();
if (rootTag != null && modificationService.prepareFileForWrite(xmlFile)) {
final XmlTag propTag = rootTag.createChildTag(PROPERTY, rootTag.getNamespace(), null, false);
propTag.setAttribute(NAME_ATTR, myCanonicalText);
propTag.setAttribute(VALUE_ATTR, "");
final DomElement contextElement = DomUtil.getDomElement(descriptor.getPsiElement());
PsiElement generated;
if (contextElement == null) {
generated = rootTag.addSubTag(propTag, true);
} else {
final AntDomTarget containingTarget = contextElement.getParentOfType(AntDomTarget.class, false);
final DomElement anchor = containingTarget != null ? containingTarget : contextElement;
final XmlTag tag = anchor.getXmlTag();
if (!rootTag.equals(tag)) {
generated = tag.getParent().addBefore(propTag, tag);
} else {
generated = rootTag.addSubTag(propTag, true);
}
}
if (generated instanceof XmlTag) {
final XmlAttribute valueAttrib = ((XmlTag) generated).getAttribute(VALUE_ATTR);
if (valueAttrib != null) {
final XmlAttributeValue valueElement = valueAttrib.getValueElement();
if (valueElement instanceof Navigatable) {
result = (Navigatable) valueElement;
}
}
}
if (result == null && generated instanceof Navigatable) {
result = (Navigatable) generated;
}
}
}
}
if (result != null) {
result.navigate(true);
}
}
use of com.intellij.psi.xml.XmlAttribute 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()]);
}
use of com.intellij.psi.xml.XmlAttribute in project intellij-plugins by JetBrains.
the class MxmlTagNameReference method removeNamespaceDeclarationIfNotUsed.
private static void removeNamespaceDeclarationIfNotUsed(final SchemaPrefix schemaPrefix) {
if (schemaPrefix == null)
return;
final Ref<Boolean> hasUsagesRef = new Ref<>(false);
ReferencesSearch.search(schemaPrefix, GlobalSearchScope.fileScope(schemaPrefix.getContainingFile())).forEach(reference -> {
final TextRange range = schemaPrefix.getTextRange();
if (range != null && (reference.getElement().getTextRange().getStartOffset() + reference.getRangeInElement().getStartOffset() == range.getStartOffset()) && reference.getRangeInElement().getLength() == range.getLength()) {
return true;
}
hasUsagesRef.set(true);
return false;
});
if (!hasUsagesRef.get()) {
final XmlAttribute attribute = schemaPrefix.getDeclaration();
MxmlLanguageTagsUtil.RemoveNamespaceDeclarationIntention.removeXmlAttribute(attribute);
}
}
use of com.intellij.psi.xml.XmlAttribute in project intellij-plugins by JetBrains.
the class FlexImplicitUsageProvider method isImplicitUsage.
@Override
public boolean isImplicitUsage(PsiElement element) {
if (element instanceof XmlAttribute && ((XmlAttribute) element).isNamespaceDeclaration() && JavaScriptSupportLoader.isLanguageNamespace(((XmlAttribute) element).getValue())) {
return true;
}
if (element instanceof JSClass) {
JSClass clazz = (JSClass) element;
final Module module = ModuleUtilCore.findModuleForPsiElement(clazz);
if (module == null || ModuleType.get(module) != FlexModuleType.getInstance())
return false;
if (FlashRunConfigurationProducer.isAcceptedMainClass(clazz, module))
return true;
if (ActionScriptClassResolver.isParentClass(clazz, FlashRunConfigurationForm.MODULE_BASE_CLASS_NAME))
return true;
FlexUnitSupport flexUnitSupport = FlexUnitSupport.getSupport(module);
if (flexUnitSupport != null && flexUnitSupport.isTestClass(clazz, true))
return true;
} else if (element instanceof JSFunction) {
if (isTestMethod((JSFunction) element))
return true;
if (isAnnotatedByUnknownAttribute((JSAttributeListOwner) element))
return true;
} else if (element instanceof JSVariable) {
if (isAnnotatedByUnknownAttribute((JSAttributeListOwner) element))
return true;
if (JSResolveUtil.findParent(element) instanceof JSClass) {
final JSAttributeList varAttrList = ((JSVariable) element).getAttributeList();
if (varAttrList != null && varAttrList.findAttributeByName(FlexAnnotationNames.EMBED) != null) {
return true;
}
}
}
if (element instanceof JSParameter) {
JSFunction function = PsiTreeUtil.getParentOfType(element, JSFunction.class);
if (function != null) {
final JSParameter[] params = function.getParameterVariables();
if (params.length == 1 && element == params[0]) {
@NonNls String type = ((JSParameter) element).getTypeString();
if (type != null)
type = JSImportHandlingUtil.resolveTypeName(type, element);
if (type != null) {
if (FlexCommonTypeNames.FLASH_EVENT_FQN.equals(type) || FlexCommonTypeNames.STARLING_EVENT_FQN.equals(type)) {
return true;
}
boolean b = JSResolveUtil.processHierarchy(type, element.getContainingFile(), jsClass -> !FlexCommonTypeNames.FLASH_EVENT_FQN.equals(jsClass.getQualifiedName()) && !FlexCommonTypeNames.STARLING_EVENT_FQN.equals(jsClass.getQualifiedName()), false);
if (!b)
return true;
}
}
}
}
return false;
}
Aggregations