Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better indentation when adding GCP libraries to pom.xml #3511

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void updateDependencies(Collection<Library> selectedLibraries,
dependency.appendChild(artifactIdElement);

if (testComment == null) {
dependencies.appendChild(dependency);
addChild(dependencies, dependency);
} else {
dependencies.insertBefore(dependency, testComment);
}
Expand Down Expand Up @@ -279,7 +279,7 @@ private void handleDependencyManaged(LibraryFile artifact, Element dependency) {
}
} else {
if (versionNode != null) {
dependency.removeChild(versionNode);
removeChild(dependency, versionNode);
}
}
}
Expand All @@ -305,8 +305,8 @@ private void createBOMIfNeeded(XPath xpath) throws CoreException {
dependencyManagement = document.createElementNS("http://maven.apache.org/POM/4.0.0",
"dependencyManagement");
}
dependencyManagement.appendChild(dependencies);
document.getDocumentElement().appendChild(dependencyManagement);
addChild(dependencyManagement, dependencies);
addChild(document.getDocumentElement(), dependencyManagement);
}

Element dependency =
Expand All @@ -321,7 +321,7 @@ private void createBOMIfNeeded(XPath xpath) throws CoreException {
if (bom != null) {
boms.add(bom);
}
dependencies.appendChild(dependency);
addChild(dependencies, dependency);
}
} catch (XPathExpressionException ex) {
IStatus status = StatusUtil.error(Pom.class, ex.getMessage(), ex);
Expand Down Expand Up @@ -444,7 +444,7 @@ static void removeUnusedDependencies(Element dependencies,
}

for (Node node : nodesToRemove) {
dependencies.removeChild(node);
removeChild(dependencies, node);
}
}

Expand Down Expand Up @@ -482,10 +482,29 @@ private static String getValue(Element dependency, String childName) {
private void writeDocument() throws CoreException, TransformerException {
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2"); //$NON-NLS-1$ //$NON-NLS-2$
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core fix. Other changes are for removing extra spaces and blank line when add or removing libaries.

ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(out));
InputStream in = new ByteArrayInputStream(out.toByteArray());

pomFile.setContents(in, true, true, null);
}

private void addChild(Node parent, Node newChild) {
parent.appendChild(newChild);
removeWhiteSpaceBefore(newChild);// for child indentation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space before //

}

private static void removeChild(Node parent, Node oldChild) {
removeWhiteSpaceBefore(oldChild);
parent.removeChild(oldChild);
}

private static void removeWhiteSpaceBefore(Node node) {
Node prevElement = node.getPreviousSibling();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previousElement

if (prevElement != null && prevElement.getNodeType() == Node.TEXT_NODE
&& prevElement.getNodeValue().trim().length() == 0) {
node.getParentNode().removeChild(prevElement);
}
}
}