Search in sources :

Example 6 with Interval

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

the class TTCN3ReferenceParser method referenceStartOffset.

private 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 == ')' || currentChar == ']') {
            if (!foundDot) {
                break;
            }
            foundWhiteSpaces = false;
            IRegion pair = pairMatcher.match(document, temporalOffset + 1);
            if (pair == null) {
                return -1;
            }
            temporalOffset = pair.getOffset();
        } else if ('_' == 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 if ('%' == currentChar) {
            temporalOffset--;
            break;
        } 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 7 with Interval

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

the class TTCN3FoldingSupport 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 : HeuristicalIntervalDetector(org.eclipse.titan.designer.editors.HeuristicalIntervalDetector) Interval(org.eclipse.titan.common.parsers.Interval)

Example 8 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 character is preceded by a closing
 * bracket. In this case the new line will be appended by the same
 * whitespace characters as in the line where the corresponding opening
 * bracket 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);
            // and if we found an interval just beginning
            if (endInterval.getDepth() <= 1 || 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)

Example 9 with Interval

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

the class FoldingSupport method recursiveTokens.

protected void recursiveTokens(final Interval interval) {
    int endOffset = interval.getEndOffset();
    if (documentText.length() <= endOffset || documentText.length() <= interval.getStartOffset()) {
        return;
    } else if (endOffset == -1) {
        endOffset = documentText.length() - 1;
    }
    if (endOffset <= interval.getStartOffset()) {
        ErrorReporter.INTERNAL_ERROR();
        return;
    }
    int endline = interval.getEndLine();
    if (endline > lastLineIndex) {
        return;
    } else if (endline == -1) {
        endline = lastLineIndex;
    }
    int distance = endline - interval.getStartLine();
    if (distance >= foldingDistance) {
        switch(documentText.charAt(interval.getStartOffset())) {
            case '{':
                if (preferencesService.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.FOLD_STATEMENT_BLOCKS, true, null)) {
                    positions.add(new Position(interval.getStartOffset(), endOffset - interval.getStartOffset()));
                }
                break;
            case '(':
                if (preferencesService.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.FOLD_PARENTHESIS, true, null)) {
                    positions.add(new Position(interval.getStartOffset(), endOffset - interval.getStartOffset()));
                }
                break;
            case '/':
            case '#':
                if (preferencesService.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.FOLD_COMMENTS, true, null)) {
                    positions.add(new Position(interval.getStartOffset(), endOffset - interval.getStartOffset()));
                }
                break;
            default:
                break;
        }
    }
    for (Interval subIntervall : interval.getSubIntervals()) {
        recursiveTokens(subIntervall);
    }
}
Also used : Position(org.eclipse.jface.text.Position) Interval(org.eclipse.titan.common.parsers.Interval)

Example 10 with Interval

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

the class GeneralPairMatcher method matchPairs.

/**
 * The function really doing the pair matching work.
 *
 * @return wheather it could find a matching bracket or not.
 */
public final boolean matchPairs() {
    char currBracket;
    try {
        currBracket = document.getChar(offset);
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return false;
    }
    Interval interval = GlobalIntervalHandler.getInterval(document);
    int temp;
    for (int i = 0; i < pairs.length; i++) {
        if (pairs[i].end == currBracket) {
            endPos = offset;
            anchor = ICharacterPairMatcher.RIGHT;
            if (interval != null) {
                temp = interval.getPairLocation(offset);
                if (temp != -1) {
                    startPos = temp;
                    return true;
                }
            }
            return findStartingBracket(pairs[i]);
        } else if (pairs[i].start == currBracket) {
            startPos = offset;
            anchor = ICharacterPairMatcher.LEFT;
            if (interval != null) {
                temp = interval.getPairLocation(offset);
                if (temp != -1) {
                    endPos = temp;
                    return true;
                }
            }
            return findEndingBracket(pairs[i]);
        }
    }
    return false;
}
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