use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.
the class MediaRuleFormatter method formatBefore.
/**
*/
protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) {
ICSSNode prev = (child != null) ? child.getPreviousSibling() : node.getLastChild();
int start = (prev != null) ? ((IndexedRegion) prev).getEndOffset() : 0;
int end = (child != null) ? ((IndexedRegion) child).getStartOffset() : 0;
if (start > 0 && start < end) {
// source formatting
CSSCleanupStrategy stgy = getCleanupStrategy(node);
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
// get meaning regions
CompoundRegion[] regions = null;
if (exceptFor == null)
regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
else {
String pickupType = null;
if ((prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE) || (prev == null && (child == null || child.getNodeType() != ICSSNode.MEDIALIST_NODE))) {
pickupType = CSSRegionContexts.CSS_LBRACE;
}
regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType);
}
// extract source
if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
// "@media" and mediatype
for (int i = 0; i < regions.length; i++) {
appendSpaceBefore(node, regions[i], source);
// must
source.append(decoratedRegion(regions[i], 0, stgy));
// be comments
}
appendSpaceBefore(node, toAppend, source);
} else if (child != null && (child.getPreviousSibling() == null || child.getPreviousSibling().getNodeType() == ICSSNode.MEDIALIST_NODE)) {
// mediatype and the first style rule
for (int i = 0; i < regions.length; i++) {
appendSpaceBefore(node, regions[i], source);
// must
source.append(decoratedRegion(regions[i], 0, stgy));
// be comments
}
appendDelimBefore(node, null, source);
} else {
// between styles
for (int i = 0; i < regions.length; i++) {
appendDelimBefore(node, regions[i], source);
// must
source.append(decoratedRegion(regions[i], 0, stgy));
// be comments
}
appendDelimBefore(node, null, source);
}
} else {
// source generation
if (child == null && prev != null && prev.getNodeType() != ICSSNode.MEDIALIST_NODE) {
// after
// the last style rule
appendDelimBefore(node.getParentNode(), null, source);
} else if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
// between
// "@media" and mediatype
appendSpaceBefore(node, toAppend, source);
} else if (prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) prev).getLength() > 0) {
// between
// mediatype and the first style rule
// $NON-NLS-1$
appendSpaceBefore(node, "{", source);
// $NON-NLS-1$
source.append("{");
if (child != null)
appendDelimBefore(node, null, source);
else
appendDelimBefore(node.getParentNode(), null, source);
} else {
// normal case
appendDelimBefore(node, null, source);
}
}
}
use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.
the class MediaRuleFormatter method formatPre.
/**
*/
protected void formatPre(ICSSNode node, IRegion region, StringBuffer source) {
CSSCleanupStrategy stgy = getCleanupStrategy(node);
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy);
CompoundRegion[] outside = getOutsideRegions(structuredDocument, region);
for (int i = 0; i < regions.length; i++) {
if (i != 0 || needS(outside[0]))
appendSpaceBefore(node, regions[i], source);
source.append(decoratedIdentRegion(regions[i], stgy));
}
if (needS(outside[1]) && !isIncludesPreEnd(node, region))
appendSpaceBefore(node, outside[1], source);
}
use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.
the class PrimitiveValueFormatter method formatPre.
/**
*/
protected void formatPre(ICSSNode node, StringBuffer source) {
int start = ((IndexedRegion) node).getStartOffset();
int end = ((IndexedRegion) node).getEndOffset();
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
CSSCleanupStrategy stgy = getCleanupStrategy(node);
if (end > 0) {
// format source
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
boolean appendQuote = regions.length > 1 && node.getParentNode() instanceof ICSSStyleDeclItem && isCleanup() && getCleanupStrategy(node).isQuoteValues() && (((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_FONT) || ((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_FONT_FAMILY) || ((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_VOICE_FAMILY));
String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
StringBuffer strBuf = new StringBuffer();
boolean skipSpace = false;
for (int i = 0; i < regions.length; i++) {
if (i != 0 && !skipSpace)
appendSpaceBefore(node, regions[i], strBuf);
skipSpace = false;
strBuf.append(decoratedPropValueRegion(regions[i], stgy));
if (regions[i].getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
skipSpace = true;
}
if (appendQuote) {
source.append(quote);
String str = strBuf.toString();
str = str.replace('\'', ' ');
str = str.replace('\"', ' ');
str = str.trim();
source.append(str);
source.append(quote);
} else {
source.append(strBuf);
}
} else {
// generate source
ICSSPrimitiveValue value = (ICSSPrimitiveValue) node;
short type = value.getPrimitiveType();
String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
String str = null;
switch(type) {
case CSSPrimitiveValue.CSS_NUMBER:
case CSSPrimitiveValue.CSS_PERCENTAGE:
case CSSPrimitiveValue.CSS_EMS:
case CSSPrimitiveValue.CSS_EXS:
case CSSPrimitiveValue.CSS_PX:
case CSSPrimitiveValue.CSS_CM:
case CSSPrimitiveValue.CSS_MM:
case CSSPrimitiveValue.CSS_IN:
case CSSPrimitiveValue.CSS_PT:
case CSSPrimitiveValue.CSS_PC:
case CSSPrimitiveValue.CSS_DEG:
case CSSPrimitiveValue.CSS_RAD:
case CSSPrimitiveValue.CSS_GRAD:
case CSSPrimitiveValue.CSS_MS:
case CSSPrimitiveValue.CSS_S:
case CSSPrimitiveValue.CSS_HZ:
case CSSPrimitiveValue.CSS_KHZ:
case CSSPrimitiveValue.CSS_DIMENSION:
case ICSSPrimitiveValue.CSS_INTEGER:
if (value.getFloatValue(type) == ((int) value.getFloatValue(type))) {
str = Integer.toString((int) value.getFloatValue(type));
} else {
str = Float.toString(value.getFloatValue(type));
}
break;
case CSSPrimitiveValue.CSS_IDENT:
case ICSSPrimitiveValue.CSS_HASH:
case ICSSPrimitiveValue.CSS_INHERIT_PRIMITIVE:
str = value.getStringValue();
if (str != null) {
if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
str = str.toUpperCase();
else
str.toLowerCase();
}
break;
case CSSPrimitiveValue.CSS_STRING:
case CSSPrimitiveValue.CSS_URI:
case CSSPrimitiveValue.CSS_ATTR:
case ICSSPrimitiveValue.CSS_URANGE:
case ICSSPrimitiveValue.CSS_FORMAT:
case ICSSPrimitiveValue.CSS_LOCAL:
case ICSSPrimitiveValue.CSS_COMMA:
case ICSSPrimitiveValue.CSS_SLASH:
str = value.getStringValue();
}
String preStr = null, postStr = null;
switch(type) {
case CSSPrimitiveValue.CSS_ATTR:
// $NON-NLS-1$
preStr = "attr(";
// $NON-NLS-1$
postStr = ")";
break;
case CSSPrimitiveValue.CSS_CM:
// $NON-NLS-1$
postStr = "cm";
break;
// case ICSSPrimitiveValue.CSS_COUNTER:
case CSSPrimitiveValue.CSS_DEG:
// $NON-NLS-1$
postStr = "deg";
break;
case CSSPrimitiveValue.CSS_EMS:
// $NON-NLS-1$
postStr = "em";
break;
case CSSPrimitiveValue.CSS_EXS:
// $NON-NLS-1$
postStr = "ex";
break;
case CSSPrimitiveValue.CSS_GRAD:
// $NON-NLS-1$
postStr = "grad";
break;
case CSSPrimitiveValue.CSS_HZ:
// $NON-NLS-1$
postStr = "Hz";
break;
case CSSPrimitiveValue.CSS_IN:
// $NON-NLS-1$
postStr = "in";
break;
case CSSPrimitiveValue.CSS_KHZ:
// $NON-NLS-1$
postStr = "kHz";
break;
case CSSPrimitiveValue.CSS_MM:
// $NON-NLS-1$
postStr = "mm";
break;
case CSSPrimitiveValue.CSS_MS:
// $NON-NLS-1$
postStr = "ms";
break;
case CSSPrimitiveValue.CSS_PC:
// $NON-NLS-1$
postStr = "pc";
break;
case CSSPrimitiveValue.CSS_PERCENTAGE:
// $NON-NLS-1$
postStr = "%";
break;
case CSSPrimitiveValue.CSS_PT:
// $NON-NLS-1$
postStr = "pt";
break;
case CSSPrimitiveValue.CSS_PX:
// $NON-NLS-1$
postStr = "px";
break;
case CSSPrimitiveValue.CSS_RAD:
// $NON-NLS-1$
postStr = "rad";
break;
// case ICSSPrimitiveValue.CSS_RGBCOLOR:
case CSSPrimitiveValue.CSS_S:
// $NON-NLS-1$
postStr = "s";
break;
case CSSPrimitiveValue.CSS_STRING:
quote = CSSUtil.detectQuote(str, quote);
preStr = quote;
postStr = quote;
break;
case CSSPrimitiveValue.CSS_URI:
quote = CSSUtil.detectQuote(str, quote);
// $NON-NLS-1$
preStr = "url(" + quote;
// $NON-NLS-1$
postStr = quote + ")";
break;
// the followings are original primitive values
case CSSPrimitiveValue.CSS_IDENT:
case CSSPrimitiveValue.CSS_UNKNOWN:
// use str
case ICSSPrimitiveValue.CSS_INTEGER:
case ICSSPrimitiveValue.CSS_HASH:
case ICSSPrimitiveValue.CSS_SLASH:
case ICSSPrimitiveValue.CSS_COMMA:
case CSSPrimitiveValue.CSS_DIMENSION:
case CSSPrimitiveValue.CSS_NUMBER:
case ICSSPrimitiveValue.CSS_INHERIT_PRIMITIVE:
break;
case ICSSPrimitiveValue.CSS_URANGE:
// $NON-NLS-1$
preStr = "U+";
break;
case ICSSPrimitiveValue.CSS_FORMAT:
quote = CSSUtil.detectQuote(str, quote);
// $NON-NLS-1$
preStr = "format(" + quote;
// $NON-NLS-1$
postStr = quote + ")";
break;
case ICSSPrimitiveValue.CSS_LOCAL:
quote = CSSUtil.detectQuote(str, quote);
// $NON-NLS-1$
preStr = "local(" + quote;
// $NON-NLS-1$
postStr = quote + ")";
break;
}
if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER) {
if (preStr != null)
preStr = preStr.toUpperCase();
if (postStr != null)
postStr = postStr.toUpperCase();
}
if (preStr != null)
source.append(preStr);
if (str != null)
source.append(str);
if (postStr != null)
source.append(postStr);
}
}
use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.
the class StyleSheetFormatter method formatPost.
/**
*/
protected void formatPost(ICSSNode node, StringBuffer source) {
int end = ((IndexedRegion) node).getEndOffset();
int start = (node.getLastChild() != null && ((IndexedRegion) node.getLastChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getLastChild()).getEndOffset() : getChildInsertPos(node);
if (end > 0 && start < end) {
CSSCleanupStrategy stgy = getCleanupStrategy(node);
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
for (int i = 0; i < regions.length; i++) {
appendDelimBefore(node, regions[i], source);
source.append(decoratedRegion(regions[i], 0, stgy));
}
if ((regions != null && regions.length > 0 && regions[regions.length - 1].getType() == CSSRegionContexts.CSS_CDC) || node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)
appendDelimBefore(node, null, source);
} else {
// source generation
if (node.getOwnerDocument().getModel() != null && node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)
appendDelimBefore(node, null, source);
// nothing
return;
}
}
use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.
the class StyleSheetFormatter method formatBefore.
/**
*/
protected void formatBefore(ICSSNode node, ICSSNode child, IRegion region, String toAppend, StringBuffer source) {
CSSCleanupStrategy stgy = getCleanupStrategy(node);
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy);
CompoundRegion[] outside = getOutsideRegions(structuredDocument, region);
for (int i = 0; i < regions.length; i++) {
if (i != 0 || needS(outside[0]))
appendDelimBefore(node, regions[i], source);
// must be
source.append(decoratedRegion(regions[i], 0, stgy));
// comments
}
if (needS(outside[1])) {
if (((IndexedRegion) child).getStartOffset() == region.getOffset() + region.getLength())
appendDelimBefore(node, null, source);
}
}
Aggregations