use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class ImportRuleFormatter method formatPost.
/**
*/
protected void formatPost(ICSSNode node, StringBuffer source) {
CSSCleanupStrategy stgy = getCleanupStrategy(node);
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) {
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));
}
} else {
// $NON-NLS-1$
source.append(";");
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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();
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class MediaRuleFormatter method getLengthToReformatAfter.
/**
* @return int
* @param node
* org.eclipse.wst.css.core.model.interfaces.ICSSNode
* @param insertPos
* int
*/
public int getLengthToReformatAfter(ICSSNode node, int insertPos) {
if (node == null)
return 0;
IndexedRegion nnode = (IndexedRegion) node;
if (insertPos <= 0 || !nnode.contains(insertPos - 1))
return 0;
if (node.getFirstChild().getNextSibling() == node.getLastChild()) {
// inserted
// first style rule
IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(insertPos);
if (flatNode == null)
return 0;
ITextRegion region = flatNode.getRegionAtCharacterOffset(insertPos);
if (region == null)
return 0;
RegionIterator it = new RegionIterator(flatNode, region);
while (it.hasNext()) {
region = it.next();
if (region.getType() == CSSRegionContexts.CSS_LBRACE)
break;
if (nnode.getEndOffset() <= it.getStructuredDocumentRegion().getEndOffset(region))
break;
}
int pos = it.getStructuredDocumentRegion().getStartOffset(region) - insertPos;
return (pos >= 0) ? pos : 0;
}
return super.getLengthToReformatAfter(node, insertPos);
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class MediaRuleFormatter method formatPre.
/**
*/
protected void formatPre(ICSSNode node, StringBuffer source) {
int start = ((IndexedRegion) node).getStartOffset();
int end = (node.getFirstChild() != null && ((IndexedRegion) node.getFirstChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getFirstChild()).getStartOffset() : getChildInsertPos(node);
ICSSNode child = node.getFirstChild();
if (child != null && (child instanceof MediaList) && ((MediaList) child).getLength() == 0) {
if (child.getNextSibling() != null)
end = ((IndexedRegion) child.getNextSibling()).getStartOffset();
else
end = -1;
}
if (end > 0) {
// source formatting
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++) {
if (i != 0)
appendSpaceBefore(node, regions[i], source);
source.append(decoratedIdentRegion(regions[i], stgy));
}
} else {
// source generation
String str = MEDIA;
if (CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_IDENTIFIER) == CSSCorePreferenceNames.UPPER)
str = MEDIA.toUpperCase();
source.append(str);
}
if (child != null && (child instanceof MediaList) && ((MediaList) child).getLength() > 0) {
// $NON-NLS-1$
appendSpaceBefore(node, "", source);
}
}
Aggregations