use of org.jetbrains.idea.maven.dom.model.MavenDomDependency 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 org.jetbrains.idea.maven.dom.model.MavenDomDependency 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 org.jetbrains.idea.maven.dom.model.MavenDomDependency in project intellij-community by JetBrains.
the class MavenDuplicateDependenciesInspection method collect.
private static void collect(MultiMap<DependencyConflictId, MavenDomDependency> duplicates, @NotNull MavenDomDependencies dependencies) {
for (MavenDomDependency dependency : dependencies.getDependencies()) {
DependencyConflictId mavenId = DependencyConflictId.create(dependency);
if (mavenId == null)
continue;
duplicates.putValue(mavenId, dependency);
}
}
use of org.jetbrains.idea.maven.dom.model.MavenDomDependency in project intellij-community by JetBrains.
the class MavenDuplicateDependenciesInspection method addProblem.
private static void addProblem(@NotNull MavenDomDependency dependency, @NotNull Collection<MavenDomDependency> dependencies, @NotNull DomElementAnnotationHolder holder) {
StringBuilder sb = new StringBuilder();
Set<MavenDomProjectModel> processed = new HashSet<>();
for (MavenDomDependency domDependency : dependencies) {
if (dependency.equals(domDependency))
continue;
MavenDomProjectModel model = domDependency.getParentOfType(MavenDomProjectModel.class, false);
if (model != null && !processed.contains(model)) {
if (processed.size() > 0)
sb.append(", ");
sb.append(createLinkText(model, domDependency));
processed.add(model);
}
}
holder.createProblem(dependency, HighlightSeverity.WARNING, MavenDomBundle.message("MavenDuplicateDependenciesInspection.has.duplicates", sb.toString()));
}
use of org.jetbrains.idea.maven.dom.model.MavenDomDependency in project intellij-community by JetBrains.
the class AddMavenDependencyQuickFix method invoke.
public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!myRef.isValid())
return;
MavenProject mavenProject = MavenDomUtil.findContainingProject(file);
if (mavenProject == null)
return;
final List<MavenId> ids = MavenArtifactSearchDialog.searchForClass(project, getReferenceText());
if (ids.isEmpty())
return;
final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(project, mavenProject.getFile());
if (model == null)
return;
new WriteCommandAction(project, "Add Maven Dependency", DomUtil.getFile(model)) {
@Override
protected void run(@NotNull Result result) throws Throwable {
boolean isTestSource = false;
VirtualFile virtualFile = file.getOriginalFile().getVirtualFile();
if (virtualFile != null) {
isTestSource = ProjectRootManager.getInstance(project).getFileIndex().isInTestSourceContent(virtualFile);
}
for (MavenId each : ids) {
MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, null, each);
if (isTestSource) {
dependency.getScope().setStringValue("test");
}
}
}
}.execute();
}
Aggregations