use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.
the class AntCreateTargetFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
final PsiFile containingFile = psiElement.getContainingFile();
Navigatable result = null;
if (containingFile instanceof XmlFile) {
final XmlFile xmlFile = (XmlFile) containingFile;
final XmlTag rootTag = xmlFile.getRootTag();
if (rootTag != null) {
final XmlTag propTag = rootTag.createChildTag(TAG_NAME, rootTag.getNamespace(), "", false);
propTag.setAttribute(NAME_ATTR, myCanonicalText);
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) {
result = new OpenFileDescriptor(project, containingFile.getVirtualFile(), ((XmlTag) generated).getValue().getTextRange().getEndOffset());
}
if (result == null && generated instanceof Navigatable) {
result = (Navigatable) generated;
}
}
}
if (result != null) {
result.navigate(true);
}
}
use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.
the class MavenDependenciesCompletionProvider method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
PsiElement xmlText = parameters.getPosition().getParent();
if (!(xmlText instanceof XmlText))
return;
PsiElement eDependencyTag = xmlText.getParent();
if (!(eDependencyTag instanceof XmlTag))
return;
XmlTag dependencyTag = (XmlTag) eDependencyTag;
if (!"dependency".equals(dependencyTag.getName()))
return;
if (!PsiImplUtil.isLeafElementOfType(xmlText.getPrevSibling(), XmlTokenType.XML_TAG_END) || !PsiImplUtil.isLeafElementOfType(xmlText.getNextSibling(), XmlTokenType.XML_END_TAG_START)) {
return;
}
Project project = dependencyTag.getProject();
DomElement domElement = DomManager.getDomManager(project).getDomElement(dependencyTag);
if (!(domElement instanceof MavenDomDependency)) {
return;
}
MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
for (String groupId : indicesManager.getGroupIds()) {
for (String artifactId : indicesManager.getArtifactIds(groupId)) {
LookupElement builder = LookupElementBuilder.create(groupId + ':' + artifactId).withIcon(AllIcons.Nodes.PpLib).withInsertHandler(MavenDependencyInsertHandler.INSTANCE);
result.addElement(builder);
}
}
}
use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.
the class MavenPomXmlCompletionTagListenerContributor method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet result) {
if (TemplateManager.getInstance(parameters.getOriginalFile().getProject()).getActiveTemplate(parameters.getEditor()) != null) {
// Don't brake the template.
return;
}
PsiFile psiFile = parameters.getOriginalFile();
if (!(psiFile instanceof XmlFile))
return;
if (!MavenDomUtil.isProjectFile(psiFile))
return;
DomFileDescription<?> description = DomManager.getDomManager(psiFile.getProject()).getDomFileDescription((XmlFile) psiFile);
if (!(description instanceof MavenDomProjectModelDescription))
return;
result.runRemainingContributors(parameters, r -> {
final LookupElement lookupElement = r.getLookupElement();
if (myHandledTags.contains(lookupElement.getLookupString())) {
LookupElement decorator = LookupElementDecorator.withInsertHandler(lookupElement, new InsertHandler<LookupElementDecorator<LookupElement>>() {
@Override
public void handleInsert(final InsertionContext context, LookupElementDecorator<LookupElement> item) {
lookupElement.handleInsert(context);
Object object = lookupElement.getObject();
if ("dependency".equals(lookupElement.getLookupString()) && object instanceof XmlTag && "maven-4.0.0.xsd".equals(((XmlTag) object).getContainingFile().getName())) {
context.commitDocument();
CaretModel caretModel = context.getEditor().getCaretModel();
PsiElement psiElement = context.getFile().findElementAt(caretModel.getOffset());
XmlTag xmlTag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
if (xmlTag != null) {
DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement(xmlTag);
if (domElement instanceof MavenDomDependency) {
String s = "\n<groupId></groupId>\n<artifactId></artifactId>\n";
context.getDocument().insertString(caretModel.getOffset(), s);
caretModel.moveToOffset(caretModel.getOffset() + s.length() - "</artifactId>\n".length());
context.commitDocument();
new ReformatCodeProcessor(context.getProject(), context.getFile(), xmlTag.getTextRange(), false).run();
MavenDependencyCompletionUtil.invokeCompletion(context, CompletionType.BASIC);
}
}
}
}
});
r = r.withLookupElement(decorator);
}
result.passResult(r);
});
}
use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.
the class IntroducePropertyDialog method getSuggestions.
private String[] getSuggestions(int level) {
Collection<String> result = new THashSet<>();
String value = mySelectedString.trim();
boolean addUnqualifiedForm = true;
XmlTag parent = PsiTreeUtil.getParentOfType(myContext, XmlTag.class, false);
DomElement domParent = DomUtil.getDomElement(parent);
if (domParent != null) {
DomElement domSuperParent = domParent.getParent();
DomFileElement<DomElement> domFile = DomUtil.getFileElement(domParent);
if (domSuperParent != null && domFile != null && domFile.getRootElement() == domSuperParent) {
value = domSuperParent.getXmlElementName();
addUnqualifiedForm = false;
} else {
MavenDomShortArtifactCoordinates coordinates = DomUtil.getParentOfType(domParent, MavenDomShortArtifactCoordinates.class, false);
if (coordinates != null && !(coordinates instanceof MavenDomProjectModel) && domParent != coordinates.getArtifactId()) {
String artifactId = coordinates.getArtifactId().getStringValue();
if (!StringUtil.isEmptyOrSpaces(artifactId)) {
value = artifactId;
addUnqualifiedForm = false;
}
}
}
}
while (true) {
String newValue = value.replaceAll(" ", " ");
if (newValue.equals(value))
break;
value = newValue;
}
value = value.replaceAll(" ", ".");
List<String> parts = StringUtil.split(value, ".");
String shortValue = parts.get(parts.size() - 1);
if (addUnqualifiedForm) {
result.add(value);
result.add(shortValue);
}
String suffix = "";
while (parent != null && level != 0) {
suffix = parent.getName() + suffix;
result.add(suffix);
result.add(value + "." + suffix);
result.add(shortValue + "." + suffix);
suffix = "." + suffix;
parent = parent.getParentTag();
level--;
}
result = new ArrayList<>(result);
Collections.sort((List) result, CodeStyleSettingsManager.getSettings(myProject).PREFER_LONGER_NAMES ? StringLenComparator.getDescendingInstance() : StringLenComparator.getInstance());
return ArrayUtil.toStringArray(result);
}
use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.
the class MavenPluginConfigurationDomExtender method registerPluginParameter.
private static void registerPluginParameter(boolean isInPluginManagement, DomExtensionsRegistrar r, final ParameterData data, final String parameterName) {
DomExtension e = r.registerFixedNumberChildExtension(new XmlName(parameterName), MavenDomConfigurationParameter.class);
if (isCollection(data.parameter)) {
e.addExtender(new DomExtender() {
public void registerExtensions(@NotNull DomElement domElement, @NotNull DomExtensionsRegistrar registrar) {
for (String each : collectPossibleNameForCollectionParameter(parameterName)) {
DomExtension inner = registrar.registerCollectionChildrenExtension(new XmlName(each), MavenDomConfigurationParameter.class);
inner.setDeclaringElement(data.parameter);
}
}
});
} else {
addValueConverter(e, data.parameter);
if (!isInPluginManagement) {
addRequiredAnnotation(e, data);
}
}
e.setDeclaringElement(data.parameter);
data.parameter.getXmlElement().putUserData(PLUGIN_PARAMETER_KEY, data);
}
Aggregations