use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.
the class StyleDeclItemFormatter method getFirstChildRegionLength.
/**
*/
private int getFirstChildRegionLength(ICSSNode node) {
ICSSNode firstChild = node.getFirstChild();
if (firstChild == null)
return 1;
int start = ((IndexedRegion) firstChild).getStartOffset();
RegionIterator itr = new RegionIterator(node.getOwnerDocument().getModel().getStructuredDocument(), start);
while (itr.hasNext()) {
ITextRegion reg = itr.next();
if (reg != null && reg.getType() != CSSRegionContexts.CSS_S)
return reg.getTextLength();
}
return 1;
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.
the class CSSModelUpdater method valueChanged.
/**
*/
void valueChanged(CSSNodeImpl node, String oldValue) {
if (!(node instanceof CSSRegionContainer)) {
CSSUtil.debugOut(// $NON-NLS-1$
"Too Bad.." + // $NON-NLS-1$
((node == null) ? "null" : node.getClass().toString()));
return;
}
int start = node.getStartOffset();
if (node.getNodeType() == ICSSNode.ATTR_NODE) {
ICSSAttr attr = (ICSSAttr) node;
CSSSourceGenerator formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) attr.getOwnerCSSNode());
if (formatter != null)
start = formatter.getAttrInsertPos(attr.getOwnerCSSNode(), attr.getName());
}
int oldLength = (oldValue == null) ? 0 : oldValue.length();
// flash old IStructuredDocumentRegion/ITextRegion
if (node instanceof CSSStructuredDocumentRegionContainer) {
((CSSStructuredDocumentRegionContainer) node).setFirstStructuredDocumentRegion(null);
((CSSStructuredDocumentRegionContainer) node).setLastStructuredDocumentRegion(null);
} else if (node instanceof CSSRegionContainer) {
((CSSRegionContainer) node).setRangeRegion(null, null, null);
}
// generate new source
String newValue = node.generateSource();
ICSSNode parent;
if (node.getNodeType() == ICSSNode.ATTR_NODE) {
parent = ((ICSSAttr) node).getOwnerCSSNode();
} else {
parent = node.getParentNode();
}
fParser.setupUpdateContext(CSSModelUpdateContext.UPDATE_CHANGE_RCONTAINER, parent, node);
insertText(start, oldLength, newValue);
fParser.cleanupUpdateContext();
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.
the class ImportRuleFormatter method formatBefore.
/**
*/
protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) {
// for media-type
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) {
// format source
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 = CSSRegionContexts.CSS_MEDIA_SEPARATOR;
if (prev == null || child == null)
pickupType = null;
regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType);
}
// extract source
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 (prev != null && child != null) {
// generate source : ????
// $NON-NLS-1$
source.append(",");
appendSpaceBefore(node, toAppend, source);
} else if (child != null) {
// generate source : between 'url()' and
// media types
appendSpaceBefore(node, toAppend, source);
}
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.
the class ImportRuleFormatter method formatAttrChanged.
/**
*/
public StringBuffer formatAttrChanged(ICSSNode node, ICSSAttr attr, boolean insert, AttrChangeContext context) {
StringBuffer buf = new StringBuffer();
if (node == null || attr == null)
return buf;
if (!ICSSImportRule.HREF.equalsIgnoreCase(attr.getName()))
return buf;
// get region to replace
if (context != null && ((IndexedRegion) node).getEndOffset() > 0) {
if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0) {
IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) attr).getStartOffset());
ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) attr).getStartOffset());
RegionIterator it = new RegionIterator(flatNode, region);
it.prev();
if (it.hasPrev()) {
ITextRegion prev = it.prev();
if (prev.getType() == CSSRegionContexts.CSS_S)
context.start = it.getStructuredDocumentRegion().getStartOffset(prev);
else
context.start = it.getStructuredDocumentRegion().getStartOffset(region);
it.next();
} else
context.start = it.getStructuredDocumentRegion().getStartOffset(region);
it.next();
it.next();
if (it.hasNext()) {
ITextRegion next = it.next();
if (next.getType() == CSSRegionContexts.CSS_S)
context.end = it.getStructuredDocumentRegion().getEndOffset(next);
else
context.end = it.getStructuredDocumentRegion().getEndOffset(region);
} else
context.end = it.getStructuredDocumentRegion().getEndOffset(region);
} else {
ICSSNode child = node.getFirstChild();
IStructuredDocumentRegion flatNode = null;
ITextRegion region = null;
if (child == null) {
flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
} else {
flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) child).getStartOffset() - 1);
region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) child).getStartOffset() - 1);
}
if (region.getType() == CSSRegionContexts.CSS_S) {
context.start = flatNode.getStartOffset(region);
context.end = flatNode.getEndOffset(region);
} else {
context.start = flatNode.getEndOffset();
context.end = flatNode.getEndOffset();
}
}
}
// generate text
if (insert && attr.getValue() != null && attr.getValue().length() > 0) {
appendSpaceBefore(node, attr.getValue(), buf);
buf.append(attr.getValue());
// $NON-NLS-1$
appendSpaceBefore(node, "", buf);
}
return buf;
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNode in project webtools.sourceediting by eclipse.
the class ImportRuleFormatter method getAttrInsertPos.
/**
*/
public int getAttrInsertPos(ICSSNode node, String attrName) {
if (node == null || attrName == null || attrName.length() == 0)
return -1;
if (!ICSSImportRule.HREF.equalsIgnoreCase(attrName))
return -1;
ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICSSImportRule.HREF);
if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
return ((IndexedRegion) attr).getStartOffset();
IndexedRegion iNode = (IndexedRegion) node;
if (iNode.getEndOffset() <= 0)
return -1;
FormatRegion formatRegion = null;
ICSSNode child = node.getFirstChild();
if (child != null && ((IndexedRegion) child).getEndOffset() > 0)
formatRegion = new FormatRegion(iNode.getStartOffset(), ((IndexedRegion) child).getStartOffset() - iNode.getStartOffset());
else
formatRegion = new FormatRegion(iNode.getStartOffset(), iNode.getEndOffset() - iNode.getStartOffset());
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(node.getOwnerDocument().getModel().getStructuredDocument(), formatRegion, getCleanupStrategy(node));
boolean atrule = false;
for (int i = 0; i < regions.length; i++) {
if (regions[i].getType() == CSSRegionContexts.CSS_IMPORT) {
atrule = true;
continue;
} else if (!atrule)
continue;
if (regions[i].getType() != CSSRegionContexts.CSS_COMMENT)
return regions[i].getStartOffset();
}
return (child != null && ((IndexedRegion) child).getEndOffset() > 0) ? ((IndexedRegion) child).getStartOffset() : iNode.getEndOffset();
}
Aggregations