[327156] Formatting successive comments can condense them to the same line
diff --git a/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/AbstractCSSSourceFormatter.java b/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/AbstractCSSSourceFormatter.java
index ad2d140..8ceb138 100644
--- a/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/AbstractCSSSourceFormatter.java
+++ b/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/AbstractCSSSourceFormatter.java
@@ -80,12 +80,41 @@
RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
it.prev();
ITextRegion prev = it.prev();
- if (prev == null || (prev.getType() == CSSRegionContexts.CSS_S && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0)) {
+ int[] result = null;
+ if (prev == null || (prev.getType() == CSSRegionContexts.CSS_S && (result = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0))[0] >= 0)) {
+ // Collapse to one empty line if there's more than one.
+ int offset = result[0] + DefaultLineTracker.DELIMITERS[result[1]].length();
+ if (offset < it.getStructuredDocumentRegion().getText(prev).length() ) {
+ if (TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), offset)[0] >= 0) {
+ source.append(delim);
+ }
+ }
source.append(delim);
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
}
+ else if (prev.getType() == CSSRegionContexts.CSS_COMMENT) {
+ String fullText = toAppend.getDocumentRegion().getFullText(prev);
+ String trimmedText = toAppend.getDocumentRegion().getText(prev);
+ String whiteSpaces = "";//$NON-NLS-1$
+ if (fullText != null && trimmedText != null)
+ whiteSpaces = fullText.substring(trimmedText.length());
+ int[] delimiterFound = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, whiteSpaces, 0);
+ if (delimiterFound[0] != -1) {
+ source.append(delim);
+ }
+ else {
+ appendSpaceBefore(node, toAppend.getText(), source);
+
+ /*If two comments can't be adjusted in one line(combined length exceeds line width),
+ * a tab is also appended along with next line delimiter , we need to remove that.
+ */
+ if (source.toString().endsWith(getIndentString())) {
+ source.delete((source.length() - getIndentString().length()), source.length());
+ }
+ }
+ }
else {
appendSpaceBefore(node, toAppend.getText(), source);
}
diff --git a/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/StyleSheetFormatter.java b/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/StyleSheetFormatter.java
index 0d83caa..35681d5 100644
--- a/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/StyleSheetFormatter.java
+++ b/bundles/org.eclipse.wst.css.core/src/org/eclipse/wst/css/core/internal/formatter/StyleSheetFormatter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2005 IBM Corporation and others.
+ * Copyright (c) 2004, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -64,7 +64,11 @@
}
// generate source
if (prev != null) {
- appendDelimBefore(node, (regions.length > 0) ? regions[0] : null, source);
+ if (regions.length > 0 && !regions[0].getType().equals(CSSRegionContexts.CSS_COMMENT))
+ appendDelimBefore(node, regions[0], source);
+ else if (regions.length == 0) {
+ appendDelimBefore(node, null, source);
+ }
}
for (int i = 0; i < regions.length; i++) {
appendDelimBefore(node, regions[i], source);