use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class AbstractEditStrategy method getDocumentContent.
protected String getDocumentContent(IDocument document, DocumentCommand command) throws BadLocationException {
final ITypedRegion partition = document.getPartition(command.offset);
ITypedRegion[] partitions = document.getDocumentPartitioner().computePartitioning(0, document.getLength());
Iterable<ITypedRegion> partitionsOfCurrentType = Iterables.filter(Arrays.asList(partitions), new Predicate<ITypedRegion>() {
@Override
public boolean apply(ITypedRegion input) {
return input.getType().equals(partition.getType());
}
});
StringBuilder builder = new StringBuilder();
for (ITypedRegion position : partitionsOfCurrentType) {
builder.append(document.get(position.getOffset(), position.getLength()));
}
return builder.toString();
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class PresentationDamager method getDamageRegion.
@Override
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, boolean documentPartitioningChanged) {
if (!(e.getDocument() instanceof IXtextDocument)) {
return new Region(0, 0);
}
XtextDocument document = (XtextDocument) e.getDocument();
IRegion lastDamage = document.getLastDamage();
// check whether this is just a presentation invalidation not based on a real document change
if (lastDamage == null || !isEventMatchingLastDamage(e, lastDamage)) {
IRegion result = computeInterSection(partition, e, document);
return result;
}
if (!TextUtilities.overlaps(partition, lastDamage) && lastDamage.getOffset() < e.getDocument().getLength()) {
if (documentPartitioningChanged)
return partition;
return lastDamage;
}
int offset = Math.max(lastDamage.getOffset(), partition.getOffset());
int endOffset = Math.min(lastDamage.getOffset() + lastDamage.getLength(), partition.getOffset() + partition.getLength());
IRegion result = new Region(offset, endOffset - offset);
return result;
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class PresentationDamager method computeInterSection.
/**
* @return the common region of the given partition and the changed region in the DocumentEvent based on the underlying tokens.
*/
protected IRegion computeInterSection(ITypedRegion partition, DocumentEvent e, XtextDocument document) {
Iterable<ILexerTokenRegion> tokensInPartition = Iterables.filter(document.getTokens(), Regions.overlaps(partition.getOffset(), partition.getLength()));
Iterator<ILexerTokenRegion> tokens = Iterables.filter(tokensInPartition, Regions.overlaps(e.getOffset(), e.getLength())).iterator();
if (tokens.hasNext()) {
ILexerTokenRegion first = tokens.next();
ILexerTokenRegion last = first;
while (tokens.hasNext()) last = tokens.next();
return new Region(first.getOffset(), last.getOffset() + last.getLength() - first.getOffset());
}
// this shouldn't happen, but just in case return the whole partition
return partition;
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class PartitionEndSkippingEditStrategy method internalCustomizeDocumentCommand.
@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException {
if (command.length == 0 && command.text.length() > 0) {
ITypedRegion partition = document.getPartition(command.offset);
String part = document.get(partition.getOffset(), partition.getLength());
if (end != null) {
int relativeOffset = command.offset - partition.getOffset();
if (relativeOffset < partition.getLength() - end.length())
return;
if (!part.endsWith(end))
return;
String text = command.text;
if (part.length() - relativeOffset < text.length()) {
text = text.substring(0, part.length() - relativeOffset);
}
if (part.substring(relativeOffset, relativeOffset + text.length()).equals(text))
command.length = text.length();
} else {
if (part.substring(command.offset - partition.getOffset()).equals(command.text))
command.length = command.text.length();
}
}
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class PartitionInsertEditStrategy method internalCustomizeDocumentCommand.
@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException {
if (left.length() >= command.text.length() && command.text.length() > 0 && left.endsWith(command.text)) {
ITypedRegion partition = document.getPartition(command.offset);
if (command.offset != 0 && partition.getLength() == 0 && document.getLength() != 0) {
ITypedRegion precedingPartition = document.getPartition(command.offset - 1);
partition = precedingPartition;
}
if (partition.getOffset() + partition.getLength() >= command.offset + right.length()) {
if (!left.equals(right) && right.equals(document.get(command.offset, right.length())))
return;
}
if (isIdentifierPart(document, command.offset + command.length))
return;
if (left.length() > 1) {
int minDocumentLength = left.length() - command.text.length();
if (minDocumentLength > document.getLength()) {
return;
}
if (command.offset - minDocumentLength < 0)
return;
String existingLeftPart = document.get(command.offset - minDocumentLength, minDocumentLength);
if (!left.equals(existingLeftPart + command.text))
return;
}
if (left.equals(right)) {
String partitionContent = document.get(partition.getOffset(), partition.getLength());
if (count(left, partitionContent) % 2 != 0)
return;
IRegion currentLine = document.getLineInformationOfOffset(command.offset);
if (partition.getOffset() == command.offset && partition.getOffset() + partition.getLength() > currentLine.getOffset() + currentLine.getLength()) {
String trailingLine = document.get(command.offset, currentLine.getLength() - (command.offset - currentLine.getOffset()));
if (count(left, trailingLine) % 2 != 0)
return;
}
}
command.caretOffset = command.offset + command.text.length();
command.shiftsCaret = false;
command.text += right;
}
}
Aggregations