use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.
the class CSSProposalGeneratorForDeclarationValue method addSemiColon.
/**
*/
private void addSemiColon(List candidates) {
ICSSNode targetNode = fContext.getTargetNode();
if (targetNode instanceof ICSSStyleDeclItem) {
ICSSNode firstChild = targetNode.getFirstChild();
if (firstChild == null) {
return;
}
if (firstChild instanceof IndexedRegion) {
int startOffset = ((IndexedRegion) firstChild).getStartOffset();
if (fContext.getCursorPos() <= startOffset) {
return;
}
}
}
boolean bAddCloser = false;
ITextRegion targetRegion = fContext.getTargetRegion();
if (targetRegion != null && targetRegion.getType() != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
// find trailing ":" or ";"
// if ":" before ";" is found, add ";"
RegionIterator iterator = fContext.getRegionIterator();
IStructuredDocumentRegion container = iterator.getStructuredDocumentRegion();
while (iterator.hasNext()) {
ITextRegion region = iterator.next();
if (iterator.getStructuredDocumentRegion() != container) {
break;
}
if (region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) {
bAddCloser = true;
break;
}
}
if (!bAddCloser) {
// second chance:
// leading IStructuredDocumentRegion is not ";"
IStructuredDocumentRegion nextStructuredDocumentRegion = CSSUtil.findNextSignificantNode(container);
if (CSSUtil.getStructuredDocumentRegionType(nextStructuredDocumentRegion) != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
bAddCloser = true;
}
}
}
if (bAddCloser) {
CSSCACandidate item = new CSSCACandidate();
// $NON-NLS-1$
String text = fContext.getTextToReplace() + ";";
item.setReplacementString(text);
item.setCursorPosition(text.length());
// $NON-NLS-1$
item.setDisplayString(";");
item.setImageType(null);
candidates.add(item);
}
}
use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.
the class CSSContentAssistContext method getTargetRegionPrevious.
ITextRegion getTargetRegionPrevious() {
ITextRegion previousRegion = null;
ITextRegion targetRegion = getTargetRegion();
RegionIterator iterator = null;
if (targetRegion == null) {
if (0 < fCursorPos) {
iterator = new RegionIterator(fStructuredDocument, fCursorPos - 1);
}
} else {
iterator = getRegionIterator();
if (iterator.hasPrev()) {
iterator.prev();
} else {
iterator = null;
}
}
if (iterator != null) {
while (iterator.hasPrev()) {
ITextRegion region = iterator.prev();
String type = region.getType();
if (type != CSSRegionContexts.CSS_S && type != CSSRegionContexts.CSS_COMMENT && type != CSSRegionContexts.CSS_CDO && type != CSSRegionContexts.CSS_CDC) {
previousRegion = region;
break;
}
}
}
return previousRegion;
}
use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.
the class CSSContentAssistContext method targetFollows.
/**
* @return boolean
* @param regionType
* java.lang.String
*/
boolean targetFollows(String regionType) {
RegionIterator iRegion;
ITextRegion region = null;
if (fStructuredDocument.getLength() <= fTargetPos) {
iRegion = new RegionIterator(fStructuredDocument, fStructuredDocument.getLength() - 1);
} else {
iRegion = new RegionIterator(fStructuredDocument, fTargetPos);
try {
if (!Character.isWhitespace(fStructuredDocument.getChar(fTargetPos)) && iRegion.hasPrev()) {
region = iRegion.prev();
}
} catch (BadLocationException e) {
iRegion = new RegionIterator(fStructuredDocument, fStructuredDocument.getLength() - 1);
}
}
while (iRegion.hasPrev()) {
region = iRegion.prev();
String type = region.getType();
if (type == CSSRegionContexts.CSS_S || type == CSSRegionContexts.CSS_COMMENT) {
continue;
} else {
break;
}
}
if (region != null && region.getType() == regionType) {
return true;
} else {
return false;
}
}
use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.
the class StructuredAutoEditStrategyCSS method prevCorrespondence.
/**
*/
protected CompoundRegion prevCorrespondence(int position, String regionType) {
RegionIterator it = new RegionIterator(structuredDocument, position - 1);
ITextRegion region = null;
int nest = 1;
if (regionType == CSSRegionContexts.CSS_RBRACE) {
// skip to LBRACE
while (it.hasPrev()) {
region = it.prev();
if (region.getType() == CSSRegionContexts.CSS_LBRACE)
nest--;
else if (region.getType() == CSSRegionContexts.CSS_RBRACE)
nest++;
if (nest <= 0)
break;
}
if (nest == 0)
return new CompoundRegion(it.getStructuredDocumentRegion(), region);
}
if (regionType == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END) {
// skip to BRACKET_OPEN
while (it.hasPrev()) {
region = it.prev();
if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_START)
nest--;
else if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END)
nest++;
if (nest <= 0)
break;
}
if (nest == 0)
return new CompoundRegion(it.getStructuredDocumentRegion(), region);
}
if (regionType == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
// skip to PARENTHESIS_OPEN
while (it.hasPrev()) {
region = it.prev();
if (// CSSRegionContexts.CSS_PARENTHESIS_OPEN ||
region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
nest--;
else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE)
nest++;
if (nest <= 0)
break;
}
if (nest == 0)
return new CompoundRegion(it.getStructuredDocumentRegion(), region);
}
return null;
}
use of org.eclipse.wst.css.core.internal.util.RegionIterator in project webtools.sourceediting by eclipse.
the class StructuredAutoEditStrategyCSS method setRangeForClose.
/**
* Insert the method's description here.
*
* @return boolean
* @param command
* org.eclipse.jface.text.DocumentCommand
*/
protected boolean setRangeForClose(DocumentCommand command) {
int position = command.offset;
if (position == -1 || structuredDocument.getLength() == 0) {
return false;
}
try {
// find start of line
int p = (position == structuredDocument.getLength() ? position - 1 : position);
int line = structuredDocument.getLineOfOffset(p);
int start = structuredDocument.getLineOffset(line);
RegionIterator it = new RegionIterator(structuredDocument, start);
boolean allWhiteSpace = false;
// or not
while (it.hasNext()) {
ITextRegion region = it.next();
if (region.getType() != CSSRegionContexts.CSS_S)
break;
if (it.getStructuredDocumentRegion().getEndOffset(region) > p) {
allWhiteSpace = true;
break;
}
}
if (allWhiteSpace) {
command.length = command.length - (start - command.offset);
command.offset = start;
return true;
}
} catch (BadLocationException excp) {
Logger.logException(excp);
}
return false;
}
Aggregations