use of org.eclipse.jface.text.ITypedRegion in project webtools.sourceediting by eclipse.
the class NoRegionContentAssistProcessor method getPartitionType.
/**
* Gives you the document partition type (String) for the given
* StructuredTextViewer and documentPosition.
*
* @param viewer
* @param documentPosition
* @return String
*/
protected String getPartitionType(StructuredTextViewer viewer, int documentPosition) {
IDocument document = viewer.getDocument();
String partitionType = null;
ITypedRegion partition = null;
try {
partition = document.getPartition(documentPosition);
partitionType = partition.getType();
} catch (BadLocationException e) {
partitionType = null;
}
return partitionType;
}
use of org.eclipse.jface.text.ITypedRegion in project che by eclipse.
the class IndentUtil method indentLine.
/**
* Indents a single line using the java heuristic scanner. Javadoc and multi
* line comments are indented as specified by the
* <code>JavaDocAutoIndentStrategy</code>.
*
* @param document the document
* @param line the line to be indented
* @param indenter the java indenter
* @param scanner the heuristic scanner
* @param commentLines the indent token comment booleans
* @param lineIndex the zero-based line index
* @return <code>true</code> if the document was modified,
* <code>false</code> if not
* @throws BadLocationException if the document got changed concurrently
*/
private static boolean indentLine(IDocument document, int line, JavaIndenter indenter, JavaHeuristicScanner scanner, boolean[] commentLines, int lineIndex, int tabSize) throws BadLocationException {
IRegion currentLine = document.getLineInformation(line);
final int offset = currentLine.getOffset();
// where we start searching for non-WS; after the "//" in single line comments
int wsStart = offset;
String indent = null;
if (offset < document.getLength()) {
ITypedRegion partition = TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset, true);
ITypedRegion startingPartition = TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset, false);
String type = partition.getType();
if (type.equals(IJavaPartitions.JAVA_DOC) || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT)) {
indent = computeJavadocIndent(document, line, scanner, startingPartition);
} else if (!commentLines[lineIndex] && startingPartition.getOffset() == offset && startingPartition.getType().equals(IJavaPartitions.JAVA_SINGLE_LINE_COMMENT)) {
return false;
}
}
// standard java indentation
if (indent == null) {
StringBuffer computed = indenter.computeIndentation(offset);
if (computed != null)
indent = computed.toString();
else
indent = new String();
}
// change document:
// get current white space
int lineLength = currentLine.getLength();
int end = scanner.findNonWhitespaceForwardInAnyPartition(wsStart, offset + lineLength);
if (end == JavaHeuristicScanner.NOT_FOUND)
end = offset + lineLength;
int length = end - offset;
String currentIndent = document.get(offset, length);
// as opposed to commented out code, which should keep its slashes at column 0
if (length > 0) {
ITypedRegion partition = TextUtilities.getPartition(document, IJavaPartitions.JAVA_PARTITIONING, end, false);
if (partition.getOffset() == end && IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(partition.getType())) {
commentLines[lineIndex] = true;
}
}
// only change the document if it is a real change
if (!indent.equals(currentIndent)) {
document.replace(offset, length, indent);
return true;
}
return false;
}
use of org.eclipse.jface.text.ITypedRegion in project cubrid-manager by CUBRID.
the class NonRuleBasedDamagerRepairer method getDamageRegion.
/**
* Get damage region.
*
* @param partition ITypedRegion
* @param event DocumentEvent
* @param documentPartitioningChanged boolean
* @return damage region.
*/
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event, boolean documentPartitioningChanged) {
if (!documentPartitioningChanged) {
try {
IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
int start = Math.max(partition.getOffset(), info.getOffset());
int end = event.getOffset() + (event.getText() == null ? event.getLength() : event.getText().length());
if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
// optimize the case of the same line
end = info.getOffset() + info.getLength();
} else {
end = endOfLineOf(end);
}
end = Math.min(partition.getOffset() + partition.getLength(), end);
return new Region(start, end - start);
} catch (BadLocationException x) {
return partition;
}
}
return partition;
}
use of org.eclipse.jface.text.ITypedRegion in project hale by halestudio.
the class JavaStringAutoIndentStrategy method javaStringIndentAfterNewLine.
private void javaStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException {
ITypedRegion partition = TextUtilities.getPartition(document, fPartitioning, command.offset, true);
int offset = partition.getOffset();
int length = partition.getLength();
if (command.offset == offset + length && document.getChar(offset + length - 1) == '\"')
return;
String indentation = getLineIndentation(document, command.offset);
String delimiter = TextUtilities.getDefaultLineDelimiter(document);
IRegion line = document.getLineInformationOfOffset(offset);
String string = document.get(line.getOffset(), offset - line.getOffset()).trim();
if (// $NON-NLS-1$
string.length() != 0 && !string.equals("+"))
// $NON-NLS-1$
indentation += String.valueOf("\t\t");
boolean isLineDelimiter = isLineDelimiter(document, command.text);
if (WRAP_STRINGS && isLineDelimiter)
// $NON-NLS-1$//$NON-NLS-2$
command.text = "\" +" + command.text + indentation + "\"";
else if (command.text.length() > 1 && !isLineDelimiter && ESCAPE_STRINGS)
command.text = getModifiedText(command.text, indentation, delimiter);
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-xtend by eclipse.
the class RichStringAwareToggleCommentAction method isSelectionCommented.
@Override
protected boolean isSelectionCommented(ISelection selection) {
if (!(selection instanceof ITextSelection))
return false;
ITextSelection textSelection = (ITextSelection) selection;
if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
return false;
IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
try {
IRegion block = getTextBlockFromSelection(textSelection, document);
ITypedRegion[] regions = TextUtilities.computePartitioning(document, getDocumentPartitioning(), block.getOffset(), block.getLength(), false);
regions = merger.merge(regions);
// [startline, endline, startline, endline, ...]
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] = getFirstCompleteLineOfRegion(regions[i], document);
// 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 : document.getLineOfOffset(offset));
}
// Perform the check
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
String[] prefixes = getPrefixesMap().get(regions[i].getType());
if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
return false;
}
return true;
} catch (BadLocationException x) {
// should not happen
log.error(x.getMessage(), x);
}
return false;
}
Aggregations