Search in sources :

Example 1 with StatementNode

use of org.eclipse.titanium.refactoring.scope.nodes.StatementNode in project titan.EclipsePlug-ins by eclipse.

the class ChangeCreator method calculateMultiDeclarationMoveContent.

/**
 * Returns the content of an {@InsertEdit} to move a variable from a declaration list
 */
private String calculateMultiDeclarationMoveContent(final String fileContent, final StatementNode declStNode) {
    final MultiDeclaration md = declStNode.getMultiDeclaration();
    final StatementNode firstDeclPart = md.getFirstStatement();
    final Definition firstDefInStmt = firstDeclPart.getDeclaredVar().getDefinition();
    final Definition defVarToMove = declStNode.getDeclaredVar().getDefinition();
    final Definition_Statement declStmt = (Definition_Statement) declStNode.getAstNode();
    final Location declStmtLoc = declStmt.getLocation();
    final String stmtContent = fileContent.substring(declStmtLoc.getOffset(), declStmtLoc.getEndOffset());
    if (!stmtContent.contains(",")) {
        ErrorReporter.logError("ChangeCreator.calculateMultiDeclarationMoveContent(): Given statement" + " is not a multi-declaration statement; loc: " + declStmtLoc.getOffset() + "-" + declStmtLoc.getEndOffset() + " in file " + declStmtLoc.getFile());
        return null;
    }
    int prefixOffset;
    int prefixEndOffset;
    if (firstDefInStmt.equals(defVarToMove)) {
        // first var to move
        prefixOffset = findLineBeginningOffset(fileContent, declStmtLoc.getOffset());
        prefixEndOffset = declStmtLoc.getOffset();
    } else {
        // not first var to move
        prefixOffset = findLineBeginningOffset(fileContent, declStmtLoc.getOffset());
        prefixEndOffset = firstDefInStmt.getIdentifier().getLocation().getOffset();
    }
    String prefixContent = fileContent.substring(prefixOffset, prefixEndOffset);
    // 
    final int varOffset = defVarToMove.getLocation().getOffset();
    final int varEndOffset = defVarToMove.getLocation().getEndOffset();
    String varContent = fileContent.substring(varOffset, varEndOffset);
    String suffixContent = "\n";
    if (varContent.charAt(varContent.length() - 1) != ';') {
        suffixContent = ";" + suffixContent;
    }
    // remove newlines from varContent
    prefixContent = prefixContent.replaceAll("[\n\r]", " ");
    varContent = varContent.replaceAll("[\n\r]", " ");
    // System.err.println("mdcopyloc -->>>" + prefixContent + "<>" + varContent + "<>" + suffixContent + "<<<");
    return prefixContent + varContent + suffixContent;
}
Also used : Definition_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Definition_Statement) MultiDeclaration(org.eclipse.titanium.refactoring.scope.nodes.MultiDeclaration) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) StatementNode(org.eclipse.titanium.refactoring.scope.nodes.StatementNode) Location(org.eclipse.titan.designer.AST.Location)

Example 2 with StatementNode

use of org.eclipse.titanium.refactoring.scope.nodes.StatementNode in project titan.EclipsePlug-ins by eclipse.

the class ChangeCreator method calculateMultiDeclarationCutLoc.

/**
 * Returns the {@link Location} of the {@DeleteEdit} to remove a variable from a declaration list
 */
private Location calculateMultiDeclarationCutLoc(final String fileContent, final StatementNode declStNode) {
    /*
		 * rules for removing multideclaration parts:
		 * 	if part is only one left: remove statement
		 * 	if part is first: remove trailing comma
		 * 	if part is last: remove leading comma
		 * 	if part is intermediate: remove trailing comma
		 * */
    final MultiDeclaration md = declStNode.getMultiDeclaration();
    final StatementNode firstDeclPart = md.getFirstStatement();
    final Definition defVarToMove = declStNode.getDeclaredVar().getDefinition();
    final Definition_Statement declStmt = (Definition_Statement) declStNode.getAstNode();
    final boolean firstDefInMdMoved = firstDeclPart.isMoved();
    final Location declStmtLoc = declStmt.getLocation();
    final String stmtContent = fileContent.substring(declStmtLoc.getOffset(), declStmtLoc.getEndOffset());
    if (!stmtContent.contains(",")) {
        ErrorReporter.logError("ChangeCreator.calculateMultiDeclarationCutLoc(): Given statement" + " is not a multi-declaration statement; loc: " + declStmtLoc.getOffset() + "-" + declStmtLoc.getEndOffset() + " in file " + declStmtLoc.getFile());
        return null;
    }
    // 
    if (md.getSize() <= 1) {
        final Location cutLoc = findStatementLocation(fileContent, declStmt.getLocation(), true);
        // System.err.println("mdcutloc <= 1 -->>>" + fileContent.substring(cutLoc.getOffset(), cutLoc.getEndOffset()) + "<<<");
        return cutLoc;
    }
    // 
    int cutOffset = defVarToMove.getLocation().getOffset();
    int cutEndOffset = defVarToMove.getLocation().getEndOffset();
    if (md.isFirstStatement(declStNode)) {
        // fist var
        if (!md.isAllStatementsMoved()) {
            cutOffset = defVarToMove.getIdentifier().getLocation().getOffset();
        }
        cutEndOffset = calculateEndOffsetIncludingTrailingComma(fileContent, cutEndOffset, declStmtLoc.getEndOffset());
    } else if (md.isLastStatement(declStNode)) {
        // last var
        cutOffset = calculateOffsetIncludingLeadingComma(fileContent, cutOffset, declStmtLoc.getOffset());
    } else {
        // intermediate var
        cutEndOffset = calculateEndOffsetIncludingTrailingComma(fileContent, cutEndOffset, declStmtLoc.getEndOffset());
    }
    // System.err.println("mdcutloc -->>>" + fileContent.substring(cutOffset, cutEndOffset) + "<<<");
    return new Location(declStmtLoc.getFile(), declStmtLoc.getLine(), cutOffset, cutEndOffset);
}
Also used : Definition_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Definition_Statement) MultiDeclaration(org.eclipse.titanium.refactoring.scope.nodes.MultiDeclaration) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) StatementNode(org.eclipse.titanium.refactoring.scope.nodes.StatementNode) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

Location (org.eclipse.titan.designer.AST.Location)2 Definition (org.eclipse.titan.designer.AST.TTCN3.definitions.Definition)2 Definition_Statement (org.eclipse.titan.designer.AST.TTCN3.statements.Definition_Statement)2 MultiDeclaration (org.eclipse.titanium.refactoring.scope.nodes.MultiDeclaration)2 StatementNode (org.eclipse.titanium.refactoring.scope.nodes.StatementNode)2