-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support joinCommentLines setting with experimental formatter
Signed-off-by: Jessica He <jhe@redhat.com>
- Loading branch information
1 parent
0b0b63b
commit 742c1db
Showing
3 changed files
with
183 additions
and
39 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
...clipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMCommentFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/******************************************************************************* | ||
* 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 text formatter. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
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; | ||
} | ||
FormatElementCategory formatElementCategory = parentConstraints.getFormatElementCategory(); | ||
String text = formatterDocument.getText(); | ||
int availableLineWidth = parentConstraints.getAvailableLineWidth(); | ||
|
||
int spaceStart = -1; | ||
int spaceEnd = -1; | ||
|
||
int indentLevel = parentConstraints.getIndentLevel(); | ||
|
||
switch (formatElementCategory) { | ||
case PreserveSpace: | ||
// Preserve existing spaces | ||
break; | ||
case MixedContent: | ||
break; | ||
case IgnoreSpace: | ||
// remove spaces and indent hasLineBreak | ||
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) { | ||
replaceLeftSpacesWithIndentation(indentLevel, start, | ||
addLineSeparator, edits); | ||
} else if (start != 0 && isLineSeparator(text.charAt(start-1))) { | ||
replaceLeftSpacesWithOneSpace(leftWhitespaceOffset, start, edits); | ||
} | ||
case NormalizeSpace: | ||
break; | ||
} | ||
|
||
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 { | ||
// Text content... | ||
int contentStart = i; | ||
while (i < commentNode.getEnd() + 1 && !Character.isWhitespace(text.charAt(i + 1))) { | ||
i++; | ||
} | ||
|
||
int contentEnd = i; | ||
availableLineWidth -= (contentEnd - contentStart); | ||
if (availableLineWidth <= 0) { | ||
if (spaceStart != -1) { | ||
insertLineBreak(spaceStart, contentStart, edits); | ||
availableLineWidth = getMaxLineWidth(); | ||
} | ||
} else { | ||
replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits); | ||
spaceStart = -1; | ||
spaceEnd = -1; | ||
} | ||
} | ||
} | ||
replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits); | ||
|
||
} | ||
} | ||
|
||
private boolean isJoinCommentLines() { | ||
return formatterDocument.getSharedSettings().getFormattingSettings().isJoinCommentLines(); | ||
} | ||
|
||
private int getMaxLineWidth() { | ||
return formatterDocument.getMaxLineWidth(); | ||
} | ||
|
||
private void insertLineBreak(int start, int end, List<TextEdit> edits) { | ||
formatterDocument.insertLineBreak(start, end, edits); | ||
} | ||
|
||
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); | ||
} | ||
|
||
private void replaceLeftSpacesWithOneSpace(int from, int to, List<TextEdit> edits) { | ||
formatterDocument.replaceLeftSpacesWithOneSpace(from, to, edits); | ||
} | ||
|
||
private static boolean isLineSeparator(char c) { | ||
return c == '\r' || c == '\n'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters