use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class AbstractCSSSourceFormatter method formatChildren.
/**
*/
protected final void formatChildren(ICSSNode node, IRegion region, StringBuffer source) {
ICSSNode child = node.getFirstChild();
int start = region.getOffset();
int end = region.getOffset() + region.getLength();
boolean first = true;
while (child != null) {
int curEnd = ((IndexedRegion) child).getEndOffset();
StringBuffer childSource = null;
boolean toFinish = false;
if (start < curEnd) {
int curStart = ((IndexedRegion) child).getStartOffset();
if (curStart < end) {
// append child
CSSSourceFormatter formatter = (CSSSourceFormatter) ((INodeNotifier) child).getAdapterFor(CSSSourceFormatter.class);
if (formatter == null) {
formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) child);
}
if (includes(region, curStart, curEnd))
childSource = ((AbstractCSSSourceFormatter) formatter).formatProc(child);
else
childSource = ((AbstractCSSSourceFormatter) formatter).formatProc(child, overlappedRegion(region, curStart, curEnd));
} else
toFinish = true;
}
// append between children
if (!first) {
// change
curEnd = ((IndexedRegion) child).getStartOffset();
// start
if (start < curEnd) {
int curStart = ((IndexedRegion) child.getPreviousSibling()).getEndOffset();
if (curStart < end) {
// $NON-NLS-1$
String toAppend = (childSource != null) ? new String(childSource) : "";
if (includes(region, curStart, curEnd))
formatBefore(node, child, toAppend, source, null);
else
formatBefore(node, child, overlappedRegion(region, curStart, curEnd), toAppend, source);
}
}
}
if (childSource != null) {
source.append(childSource);
}
first = false;
if (toFinish)
break;
child = child.getNextSibling();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class AttrFormatter method formatPre.
/**
*/
protected void formatPre(ICSSNode node, StringBuffer source) {
ICSSAttr attr = (ICSSAttr) node;
CSSCleanupStrategy stgy = getCleanupStrategy(node);
int end = ((IndexedRegion) node).getEndOffset();
if (end > 0) {
// format source
int start = ((IndexedRegion) node).getStartOffset();
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
if (attr.getName().equals(ICSSStyleRule.SELECTOR) || attr.getName().equals(ICSSPageRule.SELECTOR)) {
for (int i = 0; i < regions.length; i++) {
if (i != 0)
appendSpaceBetween(node, regions[i - 1], regions[i], source);
source.append(decoratedRegion(regions[i], 0, stgy));
}
} else if (attr.getName().equals(ICSSImportRule.HREF)) {
for (int i = 0; i < regions.length; i++) {
if (i != 0)
appendSpaceBefore(node, regions[i], source);
source.append(decoratedPropValueRegion(regions[i], stgy));
}
} else if (attr.getName().equals(ICSSCharsetRule.ENCODING)) {
for (int i = 0; i < regions.length; i++) {
if (i != 0)
appendSpaceBefore(node, regions[i], source);
source.append(decoratedIdentRegion(regions[i], stgy));
}
} else if (attr.getName().equals(ICSSStyleDeclItem.IMPORTANT)) {
for (int i = 0; i < regions.length; i++) {
if (i != 0)
appendSpaceBefore(node, regions[i], source);
source.append(decoratedPropValueRegion(regions[i], stgy));
}
} else if (attr.getName() == null || attr.getName().length() == 0) {
for (int i = 0; i < regions.length; i++) {
if (i != 0)
appendSpaceBefore(node, regions[i], source);
source.append(decoratedIdentRegion(regions[i], stgy));
}
} else {
// counter attributes
for (int i = 0; i < regions.length; i++) {
if (i != 0)
appendSpaceBefore(node, regions[i], source);
source.append(decoratedPropValueRegion(regions[i], stgy));
}
}
} else {
// generate source
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
String value = attr.getValue();
if (value == null)
// $NON-NLS-1$
value = "";
if (attr.getName().equals(ICSSStyleRule.SELECTOR) || attr.getName().equals(ICSSPageRule.SELECTOR)) {
} else if (attr.getName().equals(ICSSImportRule.HREF)) {
String uri = org.eclipse.wst.css.core.internal.util.CSSLinkConverter.stripFunc(value);
// $NON-NLS-2$//$NON-NLS-1$
String func = preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER ? "URL(" : "url(";
if (preferences.getBoolean(CSSCorePreferenceNames.FORMAT_QUOTE_IN_URI)) {
String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
quote = CSSUtil.detectQuote(uri, quote);
// $NON-NLS-1$
value = func + quote + uri + quote + ")";
} else {
// $NON-NLS-1$
value = func + uri + ")";
}
} else if (attr.getName().equals(ICSSCharsetRule.ENCODING)) {
String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
if (// $NON-NLS-2$//$NON-NLS-1$
!value.startsWith("\"") && !value.startsWith("\'"))
value = quote + value;
if (// $NON-NLS-2$//$NON-NLS-1$
!value.endsWith("\"") && !value.endsWith("\'"))
value = value + quote;
} else if (attr.getName().equals(ICSSStyleDeclItem.IMPORTANT)) {
if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
value = value.toUpperCase();
else
value = value.toLowerCase();
} else if (attr.getName() == null || attr.getName().length() == 0) {
if (CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_IDENTIFIER) == CSSCorePreferenceNames.UPPER)
value = value.toUpperCase();
else
value = value.toLowerCase();
} else {
if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
value = value.toUpperCase();
else
value = value.toLowerCase();
}
source.append(value);
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class CSSFormatUtil method collectCSSNodes.
public List collectCSSNodes(IStructuredModel model, int start, int length) {
List nodes = new ArrayList();
IndexedRegion startNode = model.getIndexedRegion(start);
IndexedRegion endNode = model.getIndexedRegion(start + length - 1);
if (startNode == null || endNode == null) {
return nodes;
}
if (model instanceof ICSSModel && startNode instanceof ICSSNode && endNode instanceof ICSSNode) {
// CSS model
ICSSNode ca = getCommonAncestor((ICSSNode) startNode, (ICSSNode) endNode);
if (ca != null) {
for (ICSSNode node = ca.getFirstChild(); node != null && start + length < ((IndexedRegion) node).getStartOffset(); node = node.getNextSibling()) {
if (start < ((IndexedRegion) node).getEndOffset()) {
nodes.add(node);
}
}
}
} else if (model instanceof IDOMModel && startNode instanceof IDOMNode && endNode instanceof IDOMNode) {
if (startNode instanceof Text) {
startNode = (IndexedRegion) ((Text) startNode).getParentNode();
}
if (endNode instanceof Text) {
endNode = (IndexedRegion) ((Text) endNode).getParentNode();
}
// HTML model, maybe
IDOMNode ca = (IDOMNode) getCommonAncestor((Node) startNode, (Node) endNode);
findCSS(nodes, ca);
}
return nodes;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class CharsetRuleFormatter method formatPre.
/**
*/
protected void formatPre(ICSSNode node, StringBuffer source) {
int end = ((IndexedRegion) node).getEndOffset();
if (end > 0) {
// format source
int start = ((IndexedRegion) node).getStartOffset();
formatPre(node, new FormatRegion(start, end - start), source);
} else {
// generate source
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
String str = CHARSET;
if (preferences.getInt(CSSCorePreferenceNames.CASE_IDENTIFIER) == CSSCorePreferenceNames.UPPER)
str = CHARSET.toUpperCase();
source.append(str);
String enc = ((ICSSCharsetRule) node).getEncoding();
quote = CSSUtil.detectQuote(enc, quote);
// $NON-NLS-1$ //$NON-NLS-2$
str = quote + ((enc != null) ? enc : "") + quote + ";";
appendSpaceBefore(node, str, source);
source.append(str);
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class CharsetRuleFormatter method getAttrInsertPos.
/**
*/
public int getAttrInsertPos(ICSSNode node, String attrName) {
if (node == null || attrName == null || attrName.length() == 0)
return -1;
CSSCleanupStrategy stgy = getCleanupStrategy(node);
if (!ICSSCharsetRule.ENCODING.equalsIgnoreCase(attrName))
return -1;
ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICSSCharsetRule.ENCODING);
if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
return ((IndexedRegion) attr).getStartOffset();
IndexedRegion iNode = (IndexedRegion) node;
if (iNode.getEndOffset() <= 0)
return -1;
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(node.getOwnerDocument().getModel().getStructuredDocument(), new FormatRegion(iNode.getStartOffset(), iNode.getEndOffset() - iNode.getStartOffset()), stgy);
for (int i = regions.length - 1; i >= 0; i--) {
if (regions[i].getType() != CSSRegionContexts.CSS_COMMENT)
return regions[i].getStartOffset();
}
return iNode.getEndOffset();
}
Aggregations