use of org.eclipse.jface.text.ITypedRegion in project xtext-xtend by eclipse.
the class PartitionTest method testPartitioningAfterModify_01.
@Test
public void testPartitioningAfterModify_01() throws BadLocationException {
String input = "class SomeType {\n" + " def someOperation() '''\n" + " Dear,\n" + " bla bla foo\n" + " \n" + " Yours sincerely,\n" + " \n" + " Joe Developer\n" + " \n" + " ''' \n" + "}";
document.set(input);
document.replace(input.indexOf("\t\tYours"), 0, "���");
ITypedRegion[] partitions = document.getDocumentPartitioner().computePartitioning(0, input.length() + 3);
assertEquals(4, partitions.length);
ITypedRegion first = partitions[0];
assertEquals(0, first.getOffset());
assertEquals(input.indexOf("'"), first.getLength());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, first.getType());
ITypedRegion second = partitions[1];
assertEquals(input.indexOf("'"), second.getOffset());
assertEquals(input.indexOf("\t\tYours") + 1, second.getLength() + second.getOffset());
assertEquals(TokenTypeToPartitionMapper.RICH_STRING_LITERAL_PARTITION, second.getType());
ITypedRegion third = partitions[2];
assertEquals(input.indexOf("\t\tYours") + 1, third.getOffset());
assertEquals(input.lastIndexOf("'") + 1 + 3, /*���*/
third.getLength() + third.getOffset());
assertEquals(TokenTypeToPartitionMapper.RICH_STRING_LITERAL_PARTITION, third.getType());
ITypedRegion forth = partitions[3];
assertEquals(input.lastIndexOf("'") + 1 + 3, /*���*/
forth.getOffset());
assertEquals(input.length() + 3, /*���*/
forth.getLength() + forth.getOffset());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, forth.getType());
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-xtend by eclipse.
the class PartitionTest method testInitialPartitioning.
@Test
public void testInitialPartitioning() {
String input = "class SomeType {\n" + " def someOperation() '''\n" + " Dear,\n" + " bla bla foo\n" + " \n" + " Yours sincerely,\n" + " \n" + " Joe Developer\n" + " \n" + " ''' \n" + "}";
document.set(input);
ITypedRegion[] partitions = document.getDocumentPartitioner().computePartitioning(0, input.length());
assertEquals(3, partitions.length);
ITypedRegion first = partitions[0];
assertEquals(0, first.getOffset());
assertEquals(input.indexOf("'"), first.getLength());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, first.getType());
ITypedRegion second = partitions[1];
assertEquals(input.indexOf("'"), second.getOffset());
assertEquals(input.lastIndexOf("'") + 1, second.getLength() + second.getOffset());
assertEquals(TokenTypeToPartitionMapper.RICH_STRING_LITERAL_PARTITION, second.getType());
ITypedRegion third = partitions[2];
assertEquals(input.lastIndexOf("'") + 1, third.getOffset());
assertEquals(input.length(), third.getLength() + third.getOffset());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, third.getType());
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-xtend by eclipse.
the class RichStringPartitionDelimiterSkippingStrategy method internalCustomizeDocumentCommand.
@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException {
if (command.length == 0 && command.text.length() == 1) {
ITypedRegion partition = document.getPartition(command.offset);
String partitionContent = document.get(partition.getOffset(), partition.getLength());
int relativeOffset = command.offset - partition.getOffset();
if (relativeOffset < partition.getLength() - partitionDelimiter.length()) {
if (relativeOffset == 0 || relativeOffset >= partitionDelimiter.length())
return;
if (!partitionContent.startsWith(partitionDelimiter))
return;
String text = command.text;
if (partitionContent.substring(relativeOffset, relativeOffset + text.length()).equals(text)) {
command.length = text.length();
}
} else {
if (!partitionContent.endsWith(partitionDelimiter))
return;
String text = command.text;
if (partitionContent.length() - relativeOffset < text.length()) {
text = text.substring(0, partitionContent.length() - relativeOffset);
}
if (partitionContent.substring(relativeOffset, relativeOffset + text.length()).equals(text))
command.length = text.length();
}
}
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-xtend by eclipse.
the class RichStringAwareSourceViewer method shift.
@SuppressWarnings("deprecation")
@Override
protected void shift(boolean useDefaultPrefixes, boolean right, boolean ignoreWhitespace) {
if (useDefaultPrefixes && ignoreWhitespace) {
// let's assume we toggled comments
if (fUndoManager != null)
fUndoManager.beginCompoundChange();
IDocument d = getDocument();
@SuppressWarnings("rawtypes") Map partitioners = null;
DocumentRewriteSession rewriteSession = null;
try {
ITextSelection selection = (ITextSelection) getSelection();
IRegion block = copiedGetTextBlockFromSelection(selection);
ITypedRegion[] regions = TextUtilities.computePartitioning(d, getDocumentPartitioning(), block.getOffset(), block.getLength(), false);
regions = merger.merge(regions);
int lineCount = 0;
// [start line, end line, start line, end line, ...]
int[] lines = new int[regions.length * 2];
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
// start line of region
lines[j] = copiedGetFirstCompleteLineOfRegion(regions[i]);
// end line of region
int length = regions[i].getLength();
int offset = regions[i].getOffset() + length;
if (length > 0)
offset--;
lines[j + 1] = (lines[j] == -1 ? -1 : d.getLineOfOffset(offset));
lineCount += lines[j + 1] - lines[j] + 1;
}
if (d instanceof IDocumentExtension4) {
IDocumentExtension4 extension = (IDocumentExtension4) d;
rewriteSession = extension.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
} else {
setRedraw(false);
startSequentialRewriteMode(true);
}
if (lineCount >= 20)
partitioners = TextUtilities.removeDocumentPartitioners(d);
// Perform the shift operation.
@SuppressWarnings("rawtypes") Map map = fDefaultPrefixChars;
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
String[] prefixes = (String[]) copiedSelectContentTypePlugin(regions[i].getType(), map);
if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0) {
if (right)
copiedShiftRight(lines[j], lines[j + 1], prefixes[0]);
else
copiedShiftLeft(lines[j], lines[j + 1], prefixes, ignoreWhitespace);
}
}
} catch (BadLocationException x) {
log.debug(x.getMessage(), x);
} finally {
if (partitioners != null)
TextUtilities.addDocumentPartitioners(d, partitioners);
if (d instanceof IDocumentExtension4) {
IDocumentExtension4 extension = (IDocumentExtension4) d;
extension.stopRewriteSession(rewriteSession);
} else {
stopSequentialRewriteMode();
setRedraw(true);
}
if (fUndoManager != null)
fUndoManager.endCompoundChange();
}
} else {
super.shift(useDefaultPrefixes, right, ignoreWhitespace);
}
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPAutoEditStrategy method smartIndentAfterNewLine.
private void smartIndentAfterNewLine(IDocument d, DocumentCommand c) {
int docLength = d.getLength();
if (c.offset == -1 || docLength == 0)
return;
int addIndent = 0;
STPHeuristicScanner scanner = new STPHeuristicScanner(d);
try {
ITypedRegion partition = TextUtilities.getPartition(d, fPartitioning, c.offset, false);
if (STPPartitionScanner.STP_CONDITIONAL.equals(partition.getType()) && c.offset > 0 && d.getChar(c.offset - 1) == '\\') {
scanner = new STPHeuristicScanner(d, fPartitioning, STPPartitionScanner.STP_CONDITIONAL);
addIndent = 1;
}
int line = d.getLineOfOffset(c.offset);
IRegion reg = d.getLineInformation(line);
int start = reg.getOffset();
int lineEnd = start + reg.getLength();
StringBuilder indent = null;
STPIndenter indenter = new STPIndenter(d, scanner, fProject);
indent = indenter.computeIndentation(c.offset);
if (indent == null) {
indent = new StringBuilder();
}
if (addIndent > 0 && indent.length() == 0) {
indent = indenter.createReusingIndent(indent, addIndent, 0);
}
StringBuilder buf = new StringBuilder(c.text + indent);
int contentStart = findEndOfWhiteSpace(d, c.offset, lineEnd);
c.length = Math.max(contentStart - c.offset, 0);
// insert closing brace on new line after an unclosed opening brace
if (getBracketCount(d, start, c.offset, true) > 0 && fCloseBrace && !isClosedBrace(d, c.offset)) {
c.caretOffset = c.offset + buf.length();
c.shiftsCaret = false;
// unless we think we are inserting an anonymous type definition
if (c.offset == 0 || !(computeAnonymousPosition(d, c.offset - 1, lineEnd) != -1)) {
if (lineEnd - contentStart > 0) {
c.length = lineEnd - c.offset;
buf.append(d.get(contentStart, lineEnd - contentStart).toCharArray());
}
}
buf.append(TextUtilities.getDefaultLineDelimiter(d));
StringBuilder reference = null;
int nonWS = findEndOfWhiteSpace(d, start, lineEnd);
if (nonWS < c.offset && d.getChar(nonWS) == '{') {
reference = new StringBuilder(d.get(start, nonWS - start));
} else {
reference = indenter.getReferenceIndentation(c.offset);
}
if (reference != null) {
buf.append(reference);
}
buf.append('}');
int bound = c.offset > 200 ? c.offset - 200 : STPHeuristicScanner.UNBOUND;
int bracePos = scanner.findOpeningPeer(c.offset - 1, bound, '{', '}');
if (bracePos != STPHeuristicScanner.NOT_FOUND) {
if (scanner.looksLikeCompositeTypeDefinitionBackward(bracePos, bound) || scanner.previousToken(bracePos - 1, bound) == STPSymbols.TokenEQUAL) {
buf.append(';');
}
}
} else // insert extra line upon new line between two braces
if (c.offset > start && contentStart < lineEnd && d.getChar(contentStart) == '}') {
int firstCharPos = scanner.findNonWhitespaceBackward(c.offset - 1, start);
if (firstCharPos != STPHeuristicScanner.NOT_FOUND && d.getChar(firstCharPos) == '{') {
c.caretOffset = c.offset + buf.length();
c.shiftsCaret = false;
StringBuilder reference = null;
int nonWS = findEndOfWhiteSpace(d, start, lineEnd);
if (nonWS < c.offset && d.getChar(nonWS) == '{') {
reference = new StringBuilder(d.get(start, nonWS - start));
} else {
reference = indenter.getReferenceIndentation(c.offset);
}
buf.append(TextUtilities.getDefaultLineDelimiter(d));
if (reference != null) {
buf.append(reference);
}
}
}
c.text = buf.toString();
} catch (BadLocationException e) {
IDEPlugin.log(e);
}
}
Aggregations