use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class DefaultPartitionerZeroLengthTest method assertComputeZeroLengthPartitioning_InterleavingPartitions.
private void assertComputeZeroLengthPartitioning_InterleavingPartitions(int startOffset, int endOffset, int[] offsets, String startType) {
ITypedRegion[] regions = fPartitioner.computePartitioning(startOffset, endOffset - startOffset, true);
String type = startType;
int previousOffset = startOffset;
assertEquals(offsets.length + 1, regions.length);
for (int i = 0; i <= offsets.length; i++) {
int currentOffset = (i == offsets.length) ? endOffset : offsets[i];
ITypedRegion region = regions[i];
assertTypedRegion(region, previousOffset, currentOffset, type);
// advance
if (type == DEFAULT)
type = COMMENT;
else
type = DEFAULT;
previousOffset = currentOffset;
}
}
use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class FastPartitionerTest method assertEqualPartition.
private void assertEqualPartition(int offset, int end, String type) {
int from = offset;
int to = end - 1;
for (int i = from; i <= to; i++) {
ITypedRegion region = fPartitioner.getPartition(i);
assertTypedRegion(region, offset, end, type);
}
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPAutoEditStrategy method smartIndentAfterClosingBracket.
private void smartIndentAfterClosingBracket(IDocument d, DocumentCommand c) {
if (c.offset == -1 || d.getLength() == 0)
return;
try {
int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
int line = d.getLineOfOffset(p);
int start = d.getLineOffset(line);
int whiteend = findEndOfWhiteSpace(d, start, c.offset);
STPHeuristicScanner scanner = new STPHeuristicScanner(d);
ITypedRegion partition = TextUtilities.getPartition(d, fPartitioning, p, false);
if (STPPartitionScanner.STP_CONDITIONAL.equals(partition.getType())) {
scanner = new STPHeuristicScanner(d, fPartitioning, STPPartitionScanner.STP_CONDITIONAL);
}
STPIndenter indenter = new STPIndenter(d, scanner, fProject);
// shift only when line does not contain any text up to the closing bracket
if (whiteend == c.offset) {
// evaluate the line with the opening bracket that matches out closing bracket
int reference = indenter.findReferencePosition(c.offset, false, MatchMode.MATCH_BRACE);
int indLine = d.getLineOfOffset(reference);
if (indLine != -1 && indLine != line) {
// take the indent of the found line
StringBuilder replaceText = new StringBuilder(getIndentOfLine(d, indLine));
// add the rest of the current line including the just added close bracket
replaceText.append(d.get(whiteend, c.offset - whiteend));
replaceText.append(c.text);
// modify document command
c.length += c.offset - start;
c.offset = start;
c.text = replaceText.toString();
}
}
} catch (BadLocationException e) {
IDEPlugin.log(e);
}
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class STPAutoEditStrategy method smartIndentAfterHash.
private void smartIndentAfterHash(IDocument doc, DocumentCommand c) {
try {
ITypedRegion partition = TextUtilities.getPartition(doc, fPartitioning, c.offset, false);
if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
IRegion startLine = doc.getLineInformationOfOffset(c.offset);
String indent = doc.get(startLine.getOffset(), c.offset - startLine.getOffset());
if (indent.trim().length() == 0) {
c.offset -= indent.length();
c.length += indent.length();
}
}
} catch (BadLocationException e) {
IDEPlugin.log(e);
}
}
use of org.eclipse.jface.text.ITypedRegion in project linuxtools by eclipse.
the class IndentHandler method indentLine.
/**
* Indents a single line using the heuristic scanner. Multiline comments are
* indented as specified by the <code>CCommentAutoIndentStrategy</code>.
*
* @param document
* the document
* @param line
* the line to be indented
* @param caret
* the caret position
* @param indenter
* the indenter
* @param scanner
* the heuristic scanner
* @param multiLine
* <code>true</code> if more than one line is being indented
* @return <code>true</code> if <code>document</code> was modified,
* <code>false</code> otherwise
* @throws BadLocationException
* if the document got changed concurrently
*/
private boolean indentLine(IDocument document, int line, int caret, STPIndenter indenter, STPHeuristicScanner scanner, boolean multiLine) throws BadLocationException {
IRegion currentLine = document.getLineInformation(line);
int offset = currentLine.getOffset();
// where we start searching for non-WS; after the
int wsStart = offset;
// "//" in single line comments
String indent = null;
if (offset < document.getLength()) {
ITypedRegion partition = TextUtilities.getPartition(document, STPPartitionScanner.STP_PARTITIONING, offset, true);
ITypedRegion startingPartition = TextUtilities.getPartition(document, STPPartitionScanner.STP_PARTITIONING, offset, false);
String type = partition.getType();
if (type.equals(STPPartitionScanner.STP_MULTILINE_COMMENT)) {
indent = computeCommentIndent(document, line, scanner, startingPartition);
} else if (startingPartition.getType().equals(STPPartitionScanner.STP_CONDITIONAL)) {
indent = computePreprocessorIndent(document, line, startingPartition);
} else if (startingPartition.getType().equals(STPPartitionScanner.STP_STRING) && offset > startingPartition.getOffset()) {
// don't indent inside (raw-)string
return false;
} else if (!fIsTabAction && startingPartition.getOffset() == offset && startingPartition.getType().equals(STPPartitionScanner.STP_COMMENT)) {
// line comment starting at position 0 -> indent inside
if (indentInsideLineComments()) {
int max = document.getLength() - offset;
int slashes = 2;
while (slashes < max - 1 && // $NON-NLS-1$
document.get(offset + slashes, 2).equals("//")) slashes += 2;
wsStart = offset + slashes;
StringBuilder computed = indenter.computeIndentation(offset);
if (computed == null)
computed = new StringBuilder(0);
int tabSize = getTabSize();
while (slashes > 0 && computed.length() > 0) {
char c = computed.charAt(0);
if (c == '\t') {
if (slashes > tabSize) {
slashes -= tabSize;
} else {
break;
}
} else if (c == ' ') {
slashes--;
} else {
break;
}
computed.deleteCharAt(0);
}
indent = document.get(offset, wsStart - offset) + computed;
}
}
}
// standard C code indentation
if (indent == null) {
StringBuilder computed = indenter.computeIndentation(offset);
if (computed != null) {
indent = computed.toString();
} else {
// $NON-NLS-1$
indent = "";
}
}
// change document:
// get current white space
int lineLength = currentLine.getLength();
int end = scanner.findNonWhitespaceForwardInAnyPartition(wsStart, offset + lineLength);
if (end == STPHeuristicScanner.NOT_FOUND) {
// an empty line
end = offset + lineLength;
if (multiLine && !indentEmptyLines()) {
// $NON-NLS-1$
indent = "";
}
}
int length = end - offset;
String currentIndent = document.get(offset, length);
// set the caret offset so it can be used when setting the selection
if (caret >= offset && caret <= end) {
fCaretOffset = offset + indent.length();
} else {
fCaretOffset = -1;
}
// only change the document if it is a real change
if (!indent.equals(currentIndent)) {
document.replace(offset, length, indent);
return true;
}
return false;
}
Aggregations