Search in sources :

Example 71 with ITypedRegion

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;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 72 with ITypedRegion

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;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 73 with ITypedRegion

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;
}
Also used : Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 74 with ITypedRegion

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);
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 75 with ITypedRegion

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;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITypedRegion (org.eclipse.jface.text.ITypedRegion)129 BadLocationException (org.eclipse.jface.text.BadLocationException)59 IRegion (org.eclipse.jface.text.IRegion)42 IDocument (org.eclipse.jface.text.IDocument)21 Region (org.eclipse.jface.text.Region)19 ArrayList (java.util.ArrayList)17 TypedRegion (org.eclipse.jface.text.TypedRegion)16 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)14 Position (org.eclipse.jface.text.Position)14 TypedPosition (org.eclipse.jface.text.TypedPosition)13 List (java.util.List)11 StyleRange (org.eclipse.swt.custom.StyleRange)7 Test (org.junit.Test)7 Iterator (java.util.Iterator)6 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)6 ITextSelection (org.eclipse.jface.text.ITextSelection)5 BadPartitioningException (org.eclipse.jface.text.BadPartitioningException)4 Document (org.eclipse.jface.text.Document)4 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)4 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)4