use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class DocumentTokenSource method computeDamageRegion.
protected IRegion computeDamageRegion(final DocumentEvent e) {
// empty document -> no dirty region
if (e.getDocument().getLength() == 0) {
setTokens(createTokenInfos(e.fDocument.get()));
return new Region(0, 0);
}
// previously empty -> full document dirty
if (internalModifyableTokenInfos.isEmpty()) {
setTokens(createTokenInfos(e.fDocument.get()));
return new Region(0, e.getDocument().getLength());
}
try {
RepairEntryData repairEntryData = getRepairEntryData(e);
int tokenStartsAt = repairEntryData.offset;
int tokenInfoIdx = repairEntryData.index;
CommonToken token = repairEntryData.newToken;
if (token == Token.EOF_TOKEN)
internalModifyableTokenInfos.subList(tokenInfoIdx, internalModifyableTokenInfos.size()).clear();
int regionOffset = tokenStartsAt;
int regionLength = e.fDocument.getLength() - tokenStartsAt;
int lengthDiff = e.fText.length() - e.fLength;
// compute region length
while (true) {
if (token == Token.EOF_TOKEN || tokenInfoIdx >= internalModifyableTokenInfos.size())
break;
while (true) {
if (tokenInfoIdx >= internalModifyableTokenInfos.size())
break;
TokenInfo tokenInfo = internalModifyableTokenInfos.get(tokenInfoIdx);
if (token.getStartIndex() >= e.fOffset + e.fText.length()) {
if (tokenStartsAt + lengthDiff == token.getStartIndex() && tokenInfo.type == token.getType() && token.getStopIndex() - token.getStartIndex() + 1 == tokenInfo.length) {
return new Region(regionOffset, token.getStartIndex() - regionOffset);
}
}
if (tokenStartsAt + lengthDiff + tokenInfo.length > token.getStopIndex() + 1)
break;
internalModifyableTokenInfos.remove(tokenInfoIdx);
tokenStartsAt += tokenInfo.length;
if (tokenStartsAt + lengthDiff > token.getStartIndex())
break;
}
internalModifyableTokenInfos.add(tokenInfoIdx++, createTokenInfo(token));
token = (CommonToken) repairEntryData.tokenSource.nextToken();
}
internalModifyableTokenInfos.subList(tokenInfoIdx, internalModifyableTokenInfos.size()).clear();
// add subsequent tokens
if (tokenInfoIdx >= internalModifyableTokenInfos.size()) {
while (token != Token.EOF_TOKEN) {
internalModifyableTokenInfos.add(createTokenInfo(token));
token = (CommonToken) repairEntryData.tokenSource.nextToken();
}
}
return new Region(regionOffset, regionLength);
} catch (Exception exc) {
logger.error("Error computing damaged region", exc);
internalModifyableTokenInfos = createTokenInfos(e.fDocument.get());
return new Region(0, e.fDocument.getLength());
} finally {
setTokens(internalModifyableTokenInfos);
}
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class DocumentUtil method searchInSamePartition.
/**
* searches for the given string within the same partition type
*
* @return the region of the match or <code>null</code> if no match were found
* @since 2.4
*/
public IRegion searchInSamePartition(String toFind, String documentText, IDocument document, int startOffset) throws BadLocationException {
if (startOffset >= document.getLength()) {
return null;
}
String text = preProcessSearchString(documentText);
ITypedRegion partition = document.getPartition(startOffset);
int indexOf = text.indexOf(toFind, getOffset(toFind, startOffset));
while (indexOf >= 0 && indexOf < document.getLength()) {
ITypedRegion partition2 = document.getPartition(indexOf);
if (partition2.getType().equals(partition.getType())) {
return new Region(indexOf, toFind.length());
}
indexOf = text.indexOf(toFind, partition2.getOffset() + partition2.getLength());
}
String trimmed = toFind.trim();
if (trimmed.length() > 0 && trimmed.length() != toFind.length()) {
return searchInSamePartition(trimmed, documentText, document, startOffset);
}
return null;
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class WhitespaceHelper method calculateLeadingWhitespace.
protected String calculateLeadingWhitespace(IDocument document) {
try {
IRegion line = document.getLineInformationOfOffset(offset);
if (line != null) {
String linePrefix = document.get(line.getOffset(), offset - line.getOffset());
if (isEmpty(linePrefix.trim())) {
int lineNr = document.getLineOfOffset(offset);
if (lineNr > 0) {
IRegion lineInformation = document.getLineInformation(lineNr - 1);
if (isEmpty(document.get(lineInformation.getOffset(), lineInformation.getLength()).trim())) {
int lineDelimiterLength = document.getLineDelimiter(lineNr - 1).length();
int skipLeadingWhitespace = linePrefix.length() + lineDelimiterLength;
offset -= skipLeadingWhitespace;
length += skipLeadingWhitespace;
return lineSeparator;
} else {
return lineSeparator;
}
}
} else {
return lineSeparator + lineSeparator;
}
}
} catch (BadLocationException e) {
LOG.error("Error calculating leading whitespace", e);
}
return null;
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class WhitespaceHelper method calculateTrailingWhitespace.
protected String calculateTrailingWhitespace(IDocument document) {
try {
IRegion line = document.getLineInformationOfOffset(offset + length);
if (line != null) {
String lineSuffix = document.get(offset + length, line.getLength() - (offset + length - line.getOffset()));
if (isEmpty(lineSuffix.trim())) {
length += lineSuffix.length();
int lineNr = document.getLineOfOffset(offset + length);
if (lineNr < document.getNumberOfLines() - 1) {
IRegion lineInformation = document.getLineInformation(lineNr + 1);
if (!isEmpty(document.get(lineInformation.getOffset(), lineInformation.getLength()).trim())) {
return lineSeparator;
}
}
} else {
for (int i = 0; i < lineSuffix.length(); ++i) {
if (Character.isWhitespace(lineSuffix.charAt(i)))
++length;
else
break;
}
return lineSeparator + lineSeparator;
}
}
} catch (BadLocationException e) {
LOG.error("Error calculating trailing whitespace", e);
}
return null;
}
use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.
the class XbaseEditor method selectAndReveal.
@Override
protected void selectAndReveal(final int selectionStart, final int selectionLength, final int revealStart, final int revealLength) {
try {
reentrantCallFromSelf++;
if (expectJavaSelection > 0) {
try {
ITrace traceToSource = getTraceStorage();
if (traceToSource != null) {
IResource javaResource = typeRoot.getResource();
if (expectLineSelection && javaResource instanceof IStorage) {
if (isCompiledWithJSR45()) {
try {
String string = Files.readStreamIntoString(((IStorage) javaResource).getContents());
Document javaDocument = new Document(string);
int line = getLineInJavaDocument(javaDocument, selectionStart, selectionLength);
if (line != -1) {
int startOffsetOfContents = getStartOffsetOfContentsInJava(javaDocument, line);
if (startOffsetOfContents != -1) {
ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(startOffsetOfContents, 0));
if (bestSelection != null) {
final ITextRegionWithLineInformation textRegion = bestSelection.getTextRegion();
if (textRegion != null) {
int lineToSelect = textRegion.getLineNumber();
try {
IRegion lineInfo = getDocument().getLineInformation(lineToSelect);
super.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength(), lineInfo.getOffset(), lineInfo.getLength());
return;
} catch (BadLocationException e) {
log.error(e.getMessage(), e);
}
}
}
}
}
} catch (BadLocationException e) {
// do nothing
} catch (CoreException e) {
// do nothing
}
}
} else if (selectionStart >= 0 && selectionLength >= 0) {
ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(selectionStart, selectionLength));
if (bestSelection != null) {
ILocationInResource bestReveal = bestSelection;
if (selectionStart != revealStart || selectionLength != revealLength) {
bestReveal = traceToSource.getBestAssociatedLocation(new TextRegion(revealStart, revealLength));
if (bestReveal == null) {
bestReveal = bestSelection;
}
}
ITextRegion fixedSelection = bestSelection.getTextRegion();
if (fixedSelection != null) {
ITextRegion fixedReveal = bestReveal.getTextRegion();
if (fixedReveal == null) {
fixedReveal = fixedSelection;
}
super.selectAndReveal(fixedSelection.getOffset(), fixedSelection.getLength(), fixedReveal.getOffset(), fixedReveal.getLength());
return;
}
}
}
}
} finally {
expectLineSelection = false;
expectJavaSelection--;
}
}
super.selectAndReveal(selectionStart, selectionLength, revealStart, revealLength);
} finally {
reentrantCallFromSelf--;
}
}
Aggregations