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

Support joinCommentLines setting with experimental formatter #1254

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,113 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.services.format;

import java.util.List;

import org.eclipse.lemminx.dom.DOMComment;
import org.eclipse.lsp4j.TextEdit;

/**
* DOM comment formatter.
*/
public class DOMCommentFormatter {

private final XMLFormatterDocumentNew formatterDocument;

public DOMCommentFormatter(XMLFormatterDocumentNew formatterDocument) {
this.formatterDocument = formatterDocument;
}

public void formatComment(DOMComment commentNode, XMLFormattingConstraints parentConstraints,
List<TextEdit> edits) {

// Check the comment is closed properly
if (commentNode.getEnd() == commentNode.getEndContent()) {
return;
}

String text = formatterDocument.getText();
int availableLineWidth = parentConstraints.getAvailableLineWidth();
int start = commentNode.getStart();
int leftWhitespaceOffset = start > 0 ? start - 1 : 0;

while (leftWhitespaceOffset > 0 && Character.isWhitespace(text.charAt(leftWhitespaceOffset))) {
leftWhitespaceOffset--;
}

boolean addLineSeparator = formatterDocument.hasLineBreak(leftWhitespaceOffset, start);
if (addLineSeparator) {
// Indent in case of comment being at the start of line
replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), start,
addLineSeparator, edits);
}
int spaceStart = -1;
int spaceEnd = -1;

if (isJoinCommentLines()) {
for (int i = commentNode.getStartContent(); i < commentNode.getEndContent(); i++) {
char c = text.charAt(i);
if (Character.isWhitespace(c)) {
// Whitespaces
if (spaceStart == -1) {
spaceStart = i;
} else {
spaceEnd = i;
}
} else {
int contentStart = i;
while (i < commentNode.getEnd() + 1 && !Character.isWhitespace(text.charAt(i + 1))) {
i++;
}
int contentEnd = i;
availableLineWidth -= (contentEnd + 1 - contentStart);
if (availableLineWidth <= 0) {
if (spaceStart != -1) {
// Add new line when the comment extends over the maximum line width
replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), contentStart,
true, edits);
int indentSpaces = (getTabSize() * parentConstraints.getIndentLevel());
availableLineWidth = getMaxLineWidth() - indentSpaces - (contentEnd + 1 - contentStart);
}
} else {
replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits);
availableLineWidth--;
spaceStart = -1;
spaceEnd = -1;
}
}
}
replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits);
}
}

private boolean isJoinCommentLines() {
return formatterDocument.getSharedSettings().getFormattingSettings().isJoinCommentLines();
}

private int getTabSize() {
return formatterDocument.getSharedSettings().getFormattingSettings().getTabSize();
}

private int getMaxLineWidth() {
return formatterDocument.getMaxLineWidth();
}

private void replaceSpacesWithOneSpace(int spaceStart, int spaceEnd, List<TextEdit> edits) {
formatterDocument.replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits);
}

private int replaceLeftSpacesWithIndentation(int indentLevel, int offset, boolean addLineSeparator,
List<TextEdit> edits) {
return formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, offset, addLineSeparator, edits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.dom.DOMProcessingInstruction;
import org.eclipse.lemminx.dom.DOMText;
import org.eclipse.lemminx.dom.DOMComment;
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.Position;
Expand Down Expand Up @@ -66,6 +67,8 @@ public class XMLFormatterDocumentNew {

private final DOMTextFormatter textFormatter;

private final DOMCommentFormatter commentFormatter;

private final Collection<IFormatterParticipant> formatterParticipants;

private int startOffset = -1;
Expand Down Expand Up @@ -96,6 +99,8 @@ public XMLFormatterDocumentNew(DOMDocument xmlDocument, Range range, SharedSetti
this.elementFormatter = new DOMElementFormatter(this, attributeFormatter);
this.processingInstructionFormatter = new DOMProcessingInstructionFormatter(this, attributeFormatter);
this.textFormatter = new DOMTextFormatter(this);
this.commentFormatter = new DOMCommentFormatter(this);


}

Expand Down Expand Up @@ -264,36 +269,42 @@ private void format(DOMNode child, XMLFormattingConstraints parentConstraints, i

switch (child.getNodeType()) {

case Node.DOCUMENT_TYPE_NODE:
DOMDocumentType docType = (DOMDocumentType) child;
docTypeFormatter.formatDocType(docType, parentConstraints, start, end, edits);
break;

case Node.DOCUMENT_NODE:
DOMDocument document = (DOMDocument) child;
formatChildren(document, parentConstraints, start, end, edits);
break;

case DOMNode.PROCESSING_INSTRUCTION_NODE:
DOMProcessingInstruction processingInstruction = (DOMProcessingInstruction) child;
processingInstructionFormatter.formatProcessingInstruction(processingInstruction, parentConstraints, edits);
break;

case Node.ELEMENT_NODE:
DOMElement element = (DOMElement) child;
elementFormatter.formatElement(element, parentConstraints, start, end, edits);
break;

case Node.TEXT_NODE:
DOMText textNode = (DOMText) child;
textFormatter.formatText(textNode, parentConstraints, edits);
break;

default:
// unknown, so just leave alone for now but make sure to update
// available line width
int width = updateLineWidthWithLastLine(child, parentConstraints.getAvailableLineWidth());
parentConstraints.setAvailableLineWidth(width);
case Node.DOCUMENT_TYPE_NODE:
DOMDocumentType docType = (DOMDocumentType) child;
docTypeFormatter.formatDocType(docType, parentConstraints, start, end, edits);
break;

case Node.DOCUMENT_NODE:
DOMDocument document = (DOMDocument) child;
formatChildren(document, parentConstraints, start, end, edits);
break;

case DOMNode.PROCESSING_INSTRUCTION_NODE:
DOMProcessingInstruction processingInstruction = (DOMProcessingInstruction) child;
processingInstructionFormatter.formatProcessingInstruction(processingInstruction, parentConstraints,
edits);
break;

case Node.ELEMENT_NODE:
DOMElement element = (DOMElement) child;
elementFormatter.formatElement(element, parentConstraints, start, end, edits);
break;

case Node.TEXT_NODE:
DOMText textNode = (DOMText) child;
textFormatter.formatText(textNode, parentConstraints, edits);
break;

case Node.COMMENT_NODE:
DOMComment commentNode = (DOMComment) child;
commentFormatter.formatComment(commentNode, parentConstraints, edits);
break;

default:
// unknown, so just leave alone for now but make sure to update
// available line width
int width = updateLineWidthWithLastLine(child, parentConstraints.getAvailableLineWidth());
parentConstraints.setAvailableLineWidth(width);
}
}

Expand Down Expand Up @@ -329,7 +340,7 @@ void replaceLeftSpacesWith(int leftLimit, int to, String replacement, List<TextE
createTextEditIfNeeded(from, to, replacement, edits);
}

void replaceQuoteWithPreferred(int from, int to, String replacement, List<TextEdit> edits){
void replaceQuoteWithPreferred(int from, int to, String replacement, List<TextEdit> edits) {
createTextEditIfNeeded(from, to, replacement, edits);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,6 @@ public void testCommentNotClosed() throws BadLocationException {
assertFormat(content, expected);
}

@Disabled
JessicaJHee marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void testCommentWithRange() throws BadLocationException {
String content = "<foo>\r\n" + //
Expand All @@ -584,72 +583,6 @@ public void testCommentWithRange() throws BadLocationException {
assertFormat(content, expected);
}

@Disabled
@Test
public void testJoinCommentLines() throws BadLocationException {
String content = "<!--" + lineSeparator() + //
" line 1" + lineSeparator() + //
" " + lineSeparator() + //
" " + lineSeparator() + //
" line 2" + lineSeparator() + //
" -->";
String expected = "<!-- line 1 line 2 -->";
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setJoinCommentLines(true);
assertFormat(content, expected, settings);
}

@Disabled
@Test
public void testUnclosedEndTagTrailingComment() throws BadLocationException {
String content = "<root>" + lineSeparator() + //
" <a> content </a" + lineSeparator() + //
" <!-- comment -->" + lineSeparator() + //
" </root>";
String expected = "<root>" + lineSeparator() + //
" <a> content </a" + lineSeparator() + //
" <!-- comment -->" + lineSeparator() + //
"</root>";
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setJoinCommentLines(true);
assertFormat(content, expected, settings);
}

@Disabled
@Test
public void testJoinCommentLinesNested() throws BadLocationException {
String content = "<a>" + lineSeparator() + //
" <!--" + lineSeparator() + //
" line 1" + lineSeparator() + //
" " + lineSeparator() + //
" " + lineSeparator() + //
" line 2" + lineSeparator() + //
" -->" + lineSeparator() + //
"</a>";
String expected = "<a>" + lineSeparator() + //
" <!-- line 1 line 2 -->" + lineSeparator() + //
"</a>";

SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setJoinCommentLines(true);
assertFormat(content, expected, settings);
}

@Disabled
@Test
public void testCommentFormatSameLine() throws BadLocationException {
String content = "<a>" + lineSeparator() + //
" Content" + lineSeparator() + //
"</a> <!-- My Comment -->";
String expected = "<a>" + lineSeparator() + //
" Content" + lineSeparator() + //
"</a> <!-- My Comment -->";

SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setJoinCommentLines(true);
assertFormat(content, expected, settings);
}

// ---------- Tests for CDATA formatting

@Test
Expand Down Expand Up @@ -936,6 +869,11 @@ private static void assertFormat(String unformatted, String actual, TextEdit...
assertFormat(unformatted, actual, new SharedSettings(), expectedEdits);
}

private static void assertFormat(String unformatted, String expected, SharedSettings sharedSettings)
throws BadLocationException {
assertFormat(unformatted, expected, sharedSettings, "test://test.html", (TextEdit[]) null);
}

private static void assertFormat(String unformatted, String expected, SharedSettings sharedSettings,
TextEdit... expectedEdits) throws BadLocationException {
assertFormat(unformatted, expected, sharedSettings, "test://test.html", expectedEdits);
Expand Down
Loading