Skip to content

Commit

Permalink
Formatting support for trimFinalNewLines and insertFinalNewLine settings
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <[email protected]>
  • Loading branch information
dkwon17 authored and angelozerr committed Apr 28, 2020
1 parent a6cee0b commit f12cf5d
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private static class XMLFormatterDocument {
private DOMDocument rangeDomDocument;
private XMLBuilder xmlBuilder;
private int indentLevel;
private boolean linefeedOnNextWrite;

/**
* XML formatter document.
Expand All @@ -70,6 +71,7 @@ public XMLFormatterDocument(TextDocument textDocument, Range range, XMLFormattin
this.range = range;
this.options = options;
this.emptyElements = options.getEmptyElements();
this.linefeedOnNextWrite = false;
}

/**
Expand Down Expand Up @@ -261,14 +263,15 @@ private boolean startTagExistsInFullDocument(DOMNode node) {

private void format(DOMNode node) throws BadLocationException {

if (linefeedOnNextWrite && (!node.isText() || !((DOMText) node).isWhitespace())) {
this.xmlBuilder.linefeed();
linefeedOnNextWrite = false;
}

if (node.getNodeType() != DOMNode.DOCUMENT_NODE) {
boolean doLineFeed;
if (node.getOwnerDocument().isDTD()) {
doLineFeed = false;
} else {
doLineFeed = !(node.isComment() && ((DOMComment) node).isCommentSameLineEndTag())
&& (!node.isText() || (!((DOMText) node).isWhitespace() && ((DOMText) node).hasSiblings()));
}
boolean doLineFeed = !node.getOwnerDocument().isDTD() &&
!(node.isComment() && ((DOMComment) node).isCommentSameLineEndTag()) &&
(!node.isText() || (!((DOMText) node).isWhitespace() && ((DOMText) node).hasSiblings()));

if (this.indentLevel > 0 && doLineFeed) {
// add new line + indent
Expand Down Expand Up @@ -317,12 +320,12 @@ private void format(DOMNode node) throws BadLocationException {

/**
* Format the given DOM prolog
*
*
* @param node the DOM prolog to format.
*/
private void formatProlog(DOMNode node) {
addPrologToXMLBuilder(node, this.xmlBuilder);
this.xmlBuilder.linefeed();
linefeedOnNextWrite = true;
}

/**
Expand All @@ -331,10 +334,13 @@ private void formatProlog(DOMNode node) {
* @param textNode the DOM text node to format.
*/
private void formatText(DOMText textNode) {
// Generate content
String content = textNode.getData();
xmlBuilder.addContent(content, textNode.isWhitespace(), textNode.hasSiblings(), textNode.getDelimiter(),
this.indentLevel);
if (textNode.equals(this.fullDomDocument.getLastChild())) {
xmlBuilder.addContent(content);
} else {
xmlBuilder.addContent(content, textNode.isWhitespace(), textNode.hasSiblings(),
textNode.getDelimiter());
}
}

/**
Expand Down Expand Up @@ -363,7 +369,8 @@ private void formatDocumentType(DOMDocumentType documentType) {
if (documentType.isClosed()) {
xmlBuilder.endDoctype();
}
xmlBuilder.linefeed();
linefeedOnNextWrite = true;

} else {
formatDTD(documentType, 0, this.endOffset, this.xmlBuilder);
}
Expand All @@ -373,7 +380,7 @@ private void formatDocumentType(DOMDocumentType documentType) {
* Format the given DOM ProcessingIntsruction.
*
* @param element the DOM ProcessingIntsruction to format.
*
*
*/
private void formatProcessingInstruction(DOMNode node) {
addPIToXMLBuilder(node, this.xmlBuilder);
Expand All @@ -386,14 +393,14 @@ private void formatProcessingInstruction(DOMNode node) {
* Format the given DOM Comment
*
* @param element the DOM Comment to format.
*
*
*/
private void formatComment(DOMComment comment) {
this.xmlBuilder.startComment(comment);
this.xmlBuilder.addContentComment(comment.getData());
this.xmlBuilder.endComment();
if (this.indentLevel == 0) {
this.xmlBuilder.linefeed();
linefeedOnNextWrite = true;
}
}

Expand Down Expand Up @@ -616,6 +623,19 @@ private List<? extends TextEdit> getFormatTextEdit() throws BadLocationException
Position endPosition = this.textDocument.positionAt(this.endOffset);
Range r = new Range(startPosition, endPosition);
List<TextEdit> edits = new ArrayList<>();

// check if format range reaches the end of the document
if (this.endOffset == this.textDocument.getText().length()) {

if (options.isTrimFinalNewlines()) {
this.xmlBuilder.trimFinalNewlines();
}

if (this.options.isInsertFinalNewline() && !this.xmlBuilder.isLastLineEmptyOrWhitespace()) {
this.xmlBuilder.linefeed();
}
}

edits.add(new TextEdit(r, this.xmlBuilder.toString()));
return edits;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
public class XMLFormattingOptions extends FormattingOptions {

public static final String DEFAULT_QUOTATION = "\"";
public static final int DEFAULT_PRESERVER_NEW_LINES = 2;
public static final int DEFAULT_TAB_SIZE = 2;

// All possible keys
private static final String SPLIT_ATTRIBUTES = "splitAttributes";
Expand Down Expand Up @@ -85,7 +87,7 @@ enum Quotations {
* <example />
* }
* </pre>
*
*
* </li>
* <li>{@link #ignore} : keeps the original XML content for empty elements.
* </li>
Expand Down Expand Up @@ -115,7 +117,9 @@ public XMLFormattingOptions(boolean initializeDefaults) {
/**
* Necessary: Initialize default values in case client does not provide one
*/
public void initializeDefaultSettings() {
private void initializeDefaultSettings() {
super.setTabSize(DEFAULT_TAB_SIZE);
super.setInsertSpaces(true);
this.setSplitAttributes(false);
this.setJoinCDATALines(false);
this.setFormatComments(true);
Expand All @@ -125,15 +129,16 @@ public void initializeDefaultSettings() {
this.setSpaceBeforeEmptyCloseTag(true);
this.setQuotations(DOUBLE_QUOTES_VALUE);
this.setPreserveEmptyContent(false);
this.setPreservedNewlines(2);
this.setPreservedNewlines(DEFAULT_PRESERVER_NEW_LINES);
this.setEmptyElement(EmptyElements.ignore);
}

public XMLFormattingOptions(int tabSize, boolean insertSpaces, boolean initializeDefaultSettings) {
super(tabSize, insertSpaces);
if (initializeDefaultSettings) {
initializeDefaultSettings();
}
super.setTabSize(tabSize);
super.setInsertSpaces(insertSpaces);
}

public XMLFormattingOptions(int tabSize, boolean insertSpaces) {
Expand Down Expand Up @@ -315,7 +320,6 @@ public void setPreservedNewlines(final int preservedNewlines) {
}

public int getPreservedNewlines() {

final Number value = this.getNumber(XMLFormattingOptions.PRESERVED_NEWLINES);
if ((value != null)) {
return value.intValue();
Expand Down Expand Up @@ -355,4 +359,4 @@ public static XMLFormattingOptions create(FormattingOptions options, FormattingO
return new XMLFormattingOptions(options).merge(sharedFormattingOptions);
}

}
}
Loading

0 comments on commit f12cf5d

Please sign in to comment.