Search in sources :

Example 1 with Interval

use of org.eclipse.titan.common.parsers.Interval in project titan.EclipsePlug-ins by eclipse.

the class AbstractIndentAction method doIndent.

/**
 * Do the actual indentation work.
 */
private void doIndent() {
    if (targetEditor == null) {
        return;
    }
    final IDocument document = getDocument();
    if (null == document) {
        return;
    }
    Interval rootInterval = GlobalIntervalHandler.getInterval(document);
    if (rootInterval == null) {
        rootInterval = (new HeuristicalIntervalDetector()).buildIntervals(document);
        GlobalIntervalHandler.putInterval(document, rootInterval);
    }
    if (rootInterval == null) {
        return;
    }
    int startLine = -1;
    int endLine = -1;
    if (!selection.isEmpty()) {
        if (selection instanceof TextSelection) {
            final TextSelection tSelection = (TextSelection) selection;
            if (tSelection.getLength() != 0) {
                startLine = tSelection.getStartLine();
                endLine = tSelection.getEndLine();
            }
        }
    }
    if (startLine == -1 || endLine == -1) {
        startLine = 0;
        endLine = document.getNumberOfLines() - 1;
    }
    indentArray.clear();
    indentArray.add("");
    final int nofLines = endLine - startLine;
    int offset;
    int realOffset;
    int lineLength;
    Interval interval;
    try {
        final int regionStartOffset = document.getLineOffset(startLine);
        final int regionLength = document.getLineOffset(endLine) + document.getLineLength(endLine) - regionStartOffset;
        multiEdit = new MultiTextEdit(regionStartOffset, regionLength);
        final RewriteSessionEditProcessor processor = new RewriteSessionEditProcessor(document, multiEdit, TextEdit.UPDATE_REGIONS | TextEdit.CREATE_UNDO);
        for (int i = nofLines; i >= 0; i--) {
            lineLength = document.getLineLength(startLine + i);
            offset = document.getLineOffset(startLine + i);
            realOffset = getRealLineStart(document, offset, lineLength);
            interval = rootInterval.getSmallestEnclosingInterval(realOffset);
            final int indentLevel = lineIndentationLevel(document, realOffset, offset + lineLength, interval);
            setIndentLevel(document, document.getLineOffset(startLine + i), indentLevel);
        }
        performEdits(processor);
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
    return;
}
Also used : RewriteSessionEditProcessor(org.eclipse.jface.text.RewriteSessionEditProcessor) TextSelection(org.eclipse.jface.text.TextSelection) HeuristicalIntervalDetector(org.eclipse.titan.designer.editors.HeuristicalIntervalDetector) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) Interval(org.eclipse.titan.common.parsers.Interval)

Example 2 with Interval

use of org.eclipse.titan.common.parsers.Interval in project titan.EclipsePlug-ins by eclipse.

the class FoldingSupport method calculatePositions.

/**
 * Calculates the list of folding intervals.
 *
 * @param document
 *                The document where the search for folding regions
 *                should take place.
 * @return The list of identified folding regions.
 */
public List<Position> calculatePositions(final IDocument document) {
    positions.clear();
    this.lastLineIndex = document.getNumberOfLines();
    this.documentText = document.get();
    preferencesService = Platform.getPreferencesService();
    foldingDistance = preferencesService.getInt(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.FOLD_DISTANCE, 0, null);
    if (preferencesService.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.FOLDING_ENABLED, true, null)) {
        Interval interval = GlobalIntervalHandler.getInterval(document);
        if (interval == null) {
            return positions;
        }
        for (Interval subintervall : interval.getSubIntervals()) {
            recursiveTokens(subintervall);
        }
    }
    return positions;
}
Also used : Interval(org.eclipse.titan.common.parsers.Interval)

Example 3 with Interval

use of org.eclipse.titan.common.parsers.Interval in project titan.EclipsePlug-ins by eclipse.

the class ASN1FoldingSupport method calculatePositions.

@Override
public List<Position> calculatePositions(final IDocument document) {
    positions.clear();
    this.lastLineIndex = document.getNumberOfLines();
    this.documentText = document.get();
    preferencesService = Platform.getPreferencesService();
    foldingDistance = preferencesService.getInt(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.FOLD_DISTANCE, 0, null);
    if (preferencesService.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.FOLDING_ENABLED, true, null)) {
        Interval interval = GlobalIntervalHandler.getInterval(document);
        if (interval == null) {
            interval = (new HeuristicalIntervalDetector()).buildIntervals(document);
            GlobalIntervalHandler.putInterval(document, interval);
        }
        for (Interval subintervall : interval.getSubIntervals()) {
            recursiveTokens(subintervall);
        }
    }
    return positions;
}
Also used : Interval(org.eclipse.titan.common.parsers.Interval)

Example 4 with Interval

use of org.eclipse.titan.common.parsers.Interval in project titan.EclipsePlug-ins by eclipse.

the class ASN1ReferenceParser method referenceStartOffset.

private final int referenceStartOffset(final int offset, final IDocument document, final GeneralPairMatcher pairMatcher) throws BadLocationException {
    Interval rootInterval = GlobalIntervalHandler.getInterval(document);
    if (rootInterval == null) {
        rootInterval = (new HeuristicalIntervalDetector()).buildIntervals(document);
        GlobalIntervalHandler.putInterval(document, rootInterval);
    }
    int temporalOffset = offset;
    char currentChar = document.getChar(temporalOffset);
    Interval interval = null;
    boolean foundWhiteSpaces = false;
    boolean foundDot = false;
    while (temporalOffset > 0) {
        if (rootInterval != null) {
            interval = rootInterval.getSmallestEnclosingInterval(temporalOffset);
        }
        currentChar = document.getChar(temporalOffset);
        if (interval != null && (interval_type.SINGLELINE_COMMENT.equals(interval.getType()) || interval_type.MULTILINE_COMMENT.equals(interval.getType()))) {
            temporalOffset = interval.getStartOffset();
        } else if (currentChar == '}') {
            if (foundWhiteSpaces && !foundDot) {
                break;
            }
            foundWhiteSpaces = false;
            foundDot = false;
            IRegion pair = pairMatcher.match(document, temporalOffset + 1);
            if (pair == null) {
                return -1;
            }
            temporalOffset = pair.getOffset();
        } else if ('-' == currentChar || '_' == currentChar || Character.isLetterOrDigit(currentChar)) {
            if (foundWhiteSpaces && !foundDot) {
                break;
            }
            foundWhiteSpaces = false;
            foundDot = false;
        } else if ('.' == currentChar) {
            foundDot = true;
        } else if (' ' == currentChar || '\t' == currentChar || '\n' == currentChar || '\r' == currentChar) {
            foundWhiteSpaces = true;
        } else {
            break;
        }
        temporalOffset--;
    }
    return temporalOffset;
}
Also used : HeuristicalIntervalDetector(org.eclipse.titan.designer.editors.HeuristicalIntervalDetector) IRegion(org.eclipse.jface.text.IRegion) Interval(org.eclipse.titan.common.parsers.Interval)

Example 5 with Interval

use of org.eclipse.titan.common.parsers.Interval in project titan.EclipsePlug-ins by eclipse.

the class SmartIndentAfterNewLineAutoEditStrategy method smartIndentAfterNewLine.

/**
 * Smart indentation after new line characters. This method handles two
 * cases. First when the new line characted is preceded by a closing
 * bracket. In this case the new line will be appended by the same
 * whitespace charaters as in the line where the corresponding opening
 * baracket is. In the second case when an other arbitrary character is
 * followed by the new line the indentation of the current line is
 * copied to the new line.
 *
 * @param document
 *                - the document being parsed
 * @param command
 *                - the command being performed
 */
protected void smartIndentAfterNewLine(final IDocument document, final DocumentCommand command) {
    int docLength = document.getLength();
    if (command.offset == -1 || docLength == 0) {
        return;
    }
    try {
        int p = command.offset == docLength ? command.offset - 1 : command.offset;
        int line = document.getLineOfOffset(p);
        final StringBuilder builder = new StringBuilder(command.text);
        String lineDelimeter = document.getLineDelimiter(line);
        if (lineDelimeter == null) {
            lineDelimeter = "";
        }
        // if the carret needs to be shifted we have to
        // calculate with the new line originally in the buffer
        int carretShiftSize = lineDelimeter.length();
        String lineIndent = getIndentOfLine(document, line, command);
        builder.append(lineIndent);
        carretShiftSize += lineIndent.length();
        int start = document.getLineOffset(line);
        int end = start + document.getLineLength(line) - command.text.length();
        // will be tabulated.
        if (containsUnclosedInterval(start, command.offset)) {
            Interval endInterval = rootInterval.getSmallestEnclosingInterval(command.offset);
            // if we found an interval just beginning
            if (endInterval.getDepth() <= 0 || endInterval.getStartOffset() == command.offset) {
                command.text = builder.toString();
                return;
            }
            builder.append(indentString);
            carretShiftSize += indentString.length();
        }
        boolean willInsertClosingBracket = preferenceStore.getBoolean(PreferenceConstants.CLOSE_BRACES) && canStatementBlockBeOpen(document, command.offset);
        // Insert the bracket moving new line and indentation
        if (preferenceStore.getBoolean(PreferenceConstants.AUTOMATICALLY_MOVE_BRACES) && (willInsertClosingBracket || containsUnopenedInterval(command.offset, end))) {
            builder.append(lineDelimeter + lineIndent);
            command.caretOffset = command.offset + carretShiftSize;
            command.shiftsCaret = false;
        }
        // insert the closing bracket if needed
        if (willInsertClosingBracket) {
            builder.append('}');
        }
        command.text = builder.toString();
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
}
Also used : BadLocationException(org.eclipse.jface.text.BadLocationException) Interval(org.eclipse.titan.common.parsers.Interval)

Aggregations

Interval (org.eclipse.titan.common.parsers.Interval)16 BadLocationException (org.eclipse.jface.text.BadLocationException)4 HeuristicalIntervalDetector (org.eclipse.titan.designer.editors.HeuristicalIntervalDetector)4 IRegion (org.eclipse.jface.text.IRegion)3 Position (org.eclipse.jface.text.Position)2 IDocument (org.eclipse.jface.text.IDocument)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1 RewriteSessionEditProcessor (org.eclipse.jface.text.RewriteSessionEditProcessor)1 TextSelection (org.eclipse.jface.text.TextSelection)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 CfgInterval (org.eclipse.titan.common.parsers.cfg.CfgInterval)1