in jdtls.ext/com.microsoft.java.maven.plugin/src/main/java/com/microsoft/java/maven/AddDependencyHandler.java [106:149]
private static WorkspaceEdit pomEdit(AddDependencyParams params,
ICompilationUnit unit) throws JavaModelException {
final IPath path = unit.getJavaProject().getCorrespondingResource().getLocation().append("pom.xml");
final String info[] = params.artifactInfo.replaceAll(" ", "").split(":");
if (info.length < 3) {
return new WorkspaceEdit();
}
final PosInfo posInfo = getPosInfo(path.toString(), info[0] + ":" + info[1]);
final String linesep = System.lineSeparator();
String newtext = "";
final String pomUriString = unit.getJavaProject().getCorrespondingResource()
.getLocationURI().toString() + "/pom.xml";
final Map<String, List<TextEdit>> textEdits = new HashMap<>();
if (posInfo != null) {
if (posInfo.needAddDependency == false) {
textEdits.put(pomUriString, new ArrayList<TextEdit>());
} else if (posInfo.alreadyHasDependencies == false) {
final int space = 2;
newtext = linesep + StringUtils.repeat(" ", space) + "<dependencies>" + linesep +
StringUtils.repeat(" ", space + 2) + "<dependency>" + linesep +
StringUtils.repeat(" ", space + 4) + "<groupId>" + info[0] + "</groupId>" +
linesep + StringUtils.repeat(" ", space + 4) + "<artifactId>" + info[1] +
"</artifactId>" + linesep + StringUtils.repeat(" ", space + 4) + "<version>" +
info[2] + "</version>" + linesep + StringUtils.repeat(" ", space + 2) + "</dependency>" +
linesep + StringUtils.repeat(" ", space) + "</dependencies>" + linesep;
textEdits.put(pomUriString,
Arrays.asList(new TextEdit(new Range(posInfo.pos, posInfo.pos), newtext)));
} else {
final int space = posInfo.pos.getCharacter();
newtext = linesep + StringUtils.repeat(" ", space + 2) + "<dependency>" + linesep +
StringUtils.repeat(" ", space + 4) + "<groupId>" + info[0] + "</groupId>" + linesep +
StringUtils.repeat(" ", space + 4) + "<artifactId>" + info[1] + "</artifactId>" +
linesep + StringUtils.repeat(" ", space + 4) + "<version>" + info[2] + "</version>" +
linesep + StringUtils.repeat(" ", space + 2) + "</dependency>" + linesep +
StringUtils.repeat(" ", space);
textEdits.put(pomUriString,
Arrays.asList(new TextEdit(new Range(posInfo.pos, posInfo.pos), newtext)));
}
return new WorkspaceEdit(textEdits);
} else {
return new WorkspaceEdit();
}
}