From fdd7b596b5f72300421563f8a328bb3c42292bf8 Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 4 Jun 2020 21:14:17 +0200 Subject: [PATCH] Range formatting inserts when formatting inside DOCTYPE element Fixes #682 Signed-off-by: azerr --- .../lemminx/services/XMLFormatter.java | 38 +++++++++---------- .../lemminx/services/XMLLanguageService.java | 3 +- .../lemminx/services/XMLFormatterTest.java | 25 +++++++++++- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java index 9880801f99..ee3f05a2da 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java @@ -32,7 +32,6 @@ import org.eclipse.lemminx.dom.DTDAttlistDecl; import org.eclipse.lemminx.dom.DTDDeclNode; import org.eclipse.lemminx.dom.DTDDeclParameter; -import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry; import org.eclipse.lemminx.settings.SharedSettings; import org.eclipse.lemminx.settings.XMLFormattingOptions.EmptyElements; import org.eclipse.lemminx.utils.XMLBuilder; @@ -47,7 +46,6 @@ class XMLFormatter { private static final Logger LOGGER = Logger.getLogger(XMLFormatter.class.getName()); - private final XMLExtensionsRegistry extensionsRegistry; private static class XMLFormatterDocument { private final TextDocument textDocument; @@ -112,13 +110,18 @@ private void setupRangeFormatting(Range range) throws BadLocationException { String fullText = this.textDocument.getText(); String rangeText = fullText.substring(this.startOffset, this.endOffset); - this.rangeDomDocument = DOMParser.getInstance().parse(rangeText, this.textDocument.getUri(), null, false); + boolean withinDTDContent = this.fullDomDocument.isWithinInternalDTD(startOffset); + String uri = this.textDocument.getUri(); + if (withinDTDContent) { + this.indentLevel = 1; + uri += ".dtd"; + } + this.rangeDomDocument = DOMParser.getInstance().parse(rangeText, uri, null, false); if (containsTextWithinStartTag()) { adjustOffsetToStartTag(); rangeText = fullText.substring(this.startOffset, this.endOffset); - this.rangeDomDocument = DOMParser.getInstance().parse(rangeText, this.textDocument.getUri(), null, - false); + this.rangeDomDocument = DOMParser.getInstance().parse(rangeText, uri, null, false); } this.xmlBuilder = new XMLBuilder(this.sharedSettings, "", @@ -161,8 +164,8 @@ private void setupFullFormatting(Range range) throws BadLocationException { this.rangeDomDocument = this.fullDomDocument; Position startPosition = textDocument.positionAt(startOffset); - this.xmlBuilder = new XMLBuilder(this.sharedSettings, - "", textDocument.lineDelimiter(startPosition.getLine())); + this.xmlBuilder = new XMLBuilder(this.sharedSettings, "", + textDocument.lineDelimiter(startPosition.getLine())); } private void enlargePositionToGutters(Position start, Position end) throws BadLocationException { @@ -271,9 +274,9 @@ private void format(DOMNode node) throws BadLocationException { } if (node.getNodeType() != DOMNode.DOCUMENT_NODE) { - boolean doLineFeed = !node.getOwnerDocument().isDTD() && - !(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 @@ -374,7 +377,7 @@ private void formatDocumentType(DOMDocumentType documentType) { linefeedOnNextWrite = true; } else { - formatDTD(documentType, 0, this.endOffset, this.xmlBuilder); + formatDTD(documentType, this.indentLevel, this.endOffset, this.xmlBuilder); } } @@ -633,7 +636,8 @@ private List getFormatTextEdit() throws BadLocationException this.xmlBuilder.trimFinalNewlines(); } - if (this.sharedSettings.getFormattingSettings().isInsertFinalNewline() && !this.xmlBuilder.isLastLineEmptyOrWhitespace()) { + if (this.sharedSettings.getFormattingSettings().isInsertFinalNewline() + && !this.xmlBuilder.isLastLineEmptyOrWhitespace()) { this.xmlBuilder.linefeed(); } } @@ -679,10 +683,6 @@ private static void addPrologAttributes(DOMNode node, XMLBuilder xmlBuilder) { } } - public XMLFormatter(XMLExtensionsRegistry extensionsRegistry) { - this.extensionsRegistry = extensionsRegistry; - } - /** * Returns a List containing a single TextEdit, containing the newly formatted * changes of the document. @@ -692,11 +692,9 @@ public XMLFormatter(XMLExtensionsRegistry extensionsRegistry) { * @param sharedSettings settings containing formatting preferences * @return List containing a TextEdit with formatting changes */ - public List format(TextDocument textDocument, Range range, - SharedSettings sharedSettings) { + public List format(TextDocument textDocument, Range range, SharedSettings sharedSettings) { try { - XMLFormatterDocument formatterDocument = new XMLFormatterDocument(textDocument, range, - sharedSettings); + XMLFormatterDocument formatterDocument = new XMLFormatterDocument(textDocument, range, sharedSettings); return formatterDocument.format(); } catch (BadLocationException e) { LOGGER.log(Level.SEVERE, "Formatting failed due to BadLocation", e); diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLLanguageService.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLLanguageService.java index 32c8205b76..3591035953 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLLanguageService.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLLanguageService.java @@ -28,7 +28,6 @@ import org.eclipse.lemminx.settings.SharedSettings; import org.eclipse.lemminx.settings.XMLCodeLensSettings; import org.eclipse.lemminx.settings.XMLFoldingSettings; -import org.eclipse.lemminx.settings.XMLHoverSettings; import org.eclipse.lemminx.settings.XMLSymbolSettings; import org.eclipse.lemminx.uriresolver.CacheResourceDownloadingException; import org.eclipse.lemminx.utils.XMLPositionUtility; @@ -84,7 +83,7 @@ public void checkCanceled() { private final XMLRename rename; public XMLLanguageService() { - this.formatter = new XMLFormatter(this); + this.formatter = new XMLFormatter(); this.highlighting = new XMLHighlighting(this); this.symbolsProvider = new XMLSymbolsProvider(this); this.completions = new XMLCompletions(this); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java index 7573c584dc..957fceb275 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java @@ -283,6 +283,30 @@ public void rangeSelectWithinText() throws BadLocationException { format(content, expected); } + @Test + public void rangeSelectEntityNoIndent() throws BadLocationException { + String content = "\r\n" + // + "\r\n" + // + "]>"; + String expected = "\r\n" + // + "\r\n" + // + "]>"; + format(content, expected); + } + + @Test + public void rangeSelectEntityWithIndent() throws BadLocationException { + String content = "\r\n" + // + "\r\n" + // + "]>"; + String expected = content; + format(content, expected); + } + + @Test public void testProlog() throws BadLocationException { String content = "\r\n"; @@ -2471,7 +2495,6 @@ public void dontEnforceDoubleQuoteStyle() throws BadLocationException { format(content, expected, settings); } - @Test public void testTemplate() throws BadLocationException { String content = "";