Search in sources :

Example 1 with EditorPosition

use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.

the class WizardUtil method getInsertionPosition.

/**
 * Determines the insertion position for a new interface/class declaration.
 * If the given ModuleDecl has a MainBlock the insertion point is right
 * before the main block. Otherwise, the insertion point is at the end of
 * the ModuleDecl.
 *
 * @param d
 *            The target document
 * @param astNode
 *            The target ModuleDecl
 * @return the insertion position of the new class or interface.
 * @throws BadLocationException
 *             if the location of the module declaration or the respective
 *             main block could not be found
 */
public static int getInsertionPosition(IDocument d, InternalASTNode<ModuleDecl> astNode) throws BadLocationException {
    Assert.isNotNull(d);
    Assert.isNotNull(astNode);
    final ModuleDecl m = astNode.getASTNode();
    Assert.isNotNull(m, "ModuleDecl argument is null!");
    final EditorPosition position;
    /* Classes and interfaces go before product lines / products in the grammar.
		 * We don't want to generate invalid input for the user.
		 */
    if (m.hasBlock()) {
        MainBlock block = m.getBlock();
        position = UtilityFunctions.getPosition(block);
        return d.getLineOffset(position.getLinestart()) + position.getColstart();
    } else {
        position = UtilityFunctions.getPosition(m);
        return d.getLineOffset(position.getLineend()) + position.getColend();
    }
}
Also used : EditorPosition(org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition) ModuleDecl(abs.frontend.ast.ModuleDecl) MainBlock(abs.frontend.ast.MainBlock)

Example 2 with EditorPosition

use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.

the class LinkHelper method getModuleDeclAtCurrentCursor.

private ModuleDecl getModuleDeclAtCurrentCursor(IFile file, AbsNature nature, ITextEditor editor) {
    ModuleDecl md = null;
    synchronized (nature.modelLock) {
        if (nature.getCompilationUnit(file).getNumModuleDecl() > 0) {
            // get start line of editor selection
            ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
            int startLine = selection.getStartLine();
            CompilationUnit cu = nature.getCompilationUnit(file);
            // find the module which the selection is located in...
            for (ModuleDecl m : cu.getModuleDecls()) {
                EditorPosition position = UtilityFunctions.getPosition(m);
                int moduleStartLine = position.getLinestart();
                int moduleEndLine = position.getLineend();
                if (startLine >= moduleStartLine && startLine <= moduleEndLine) {
                    md = m;
                    break;
                }
            }
            // if no module was found or no selection was made, take the first one in the compilation unit as fallback
            if (md == null) {
                md = cu.getModuleDecl(0);
            }
        }
    }
    return md;
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) EditorPosition(org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition) ModuleDecl(abs.frontend.ast.ModuleDecl) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 3 with EditorPosition

use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.

the class ModuleDecorator method hasModuleDeclErrors.

/**
 * Determines if a module declaration has any errors
 *
 * @param m
 *            the module declaration
 * @param nature
 *            the ABS nature
 * @return TRUE if the module declaration has errors, FALSE if not or m or
 *         nature is null
 */
public boolean hasModuleDeclErrors(ModuleDecl m, AbsNature nature) {
    synchronized (nature.modelLock) {
        if (m != null) {
            CompilationUnit cu = m.getCompilationUnit();
            EditorPosition pos = UtilityFunctions.getPosition(m);
            int startLine = pos.getLinestart();
            int endLine = pos.getLineend();
            List<ParserError> parserErrors = cu.getParserErrors();
            SemanticConditionList list = cu.getModel().getTypeErrors();
            if (checkParserErrorRange(startLine, endLine, parserErrors)) {
                return true;
            } else {
                return checkSemanticErrorRange(list, cu, startLine, endLine, nature);
            }
        }
        return false;
    }
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) EditorPosition(org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition) SemanticConditionList(abs.frontend.analyser.SemanticConditionList) ParserError(abs.frontend.parser.ParserError)

Example 4 with EditorPosition

use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.

the class ABSEditor method highlightInEditor.

/**
 * Highlights the given {@link ASTNode} in the editor.<br/>
 * Only one ASTNode at a time can be highlighted (This is a restriction of {@link ITextEditor}}).<br/><br/>
 *
 * @param edit The target editor
 * @param node The node that should be highlighted.
 */
void highlightInEditor(ASTNode<?> node, boolean moveCursor) {
    EditorPosition pos = UtilityFunctions.getPosition(node);
    if (pos != null) {
        IDocument doc = getDocumentProvider().getDocument(getEditorInput());
        try {
            /* Calculate the position on the editor by retrieving the char offset
				 * of the position line and the target column in this line.
				 */
            int startOff = doc.getLineOffset(pos.getLinestart()) + pos.getColstart();
            int endOff = doc.getLineOffset(pos.getLineend()) + pos.getColend();
            if (startOff < 0) {
                // TODO: This happens sometimes - new parser?
                Activator.logException(new IllegalArgumentException("FIXME"));
                return;
            }
            assert startOff > -1;
            assert endOff >= startOff;
            setHighlightRange(startOff, endOff - startOff, moveCursor);
        } catch (BadLocationException e) {
        /*
				 * Should not be thrown, as an ASTNode in a document must have a
				 * specific location.
				 */
        }
    }
}
Also used : EditorPosition(org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 5 with EditorPosition

use of org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition in project abstools by abstools.

the class AbsHyperlinkDetector method getHyperlinks.

public static IHyperlink[] getHyperlinks(ABSEditor editor, int offset) {
    try {
        InternalASTNode<CompilationUnit> cu = editor.getCompilationUnit();
        if (cu == null) {
            return null;
        }
        IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
        EditorPosition targetPos = null;
        ASTNode<?> node;
        ASTNode<?> decl;
        synchronized (cu.getNature()) {
            node = UtilityFunctions.getASTNodeOfOffset(doc, cu.getASTNode(), offset);
            if (node == null) {
                return null;
            }
            decl = getDecl(cu.getASTNode(), node);
            targetPos = getPosition(decl);
        }
        if (targetPos == null) {
            return null;
        }
        final int startOffset = getOffset(doc, node.getStartLine(), node.getStartColumn());
        final int endOffset = getOffset(doc, node.getEndLine(), node.getEndColumn());
        if (decl instanceof MethodSig) {
            MethodSig methodSig = (MethodSig) decl;
            if (methodSig.getContextDecl() instanceof InterfaceDecl) {
                // decl is an interface method
                Type typ = new InterfaceType((InterfaceDecl) methodSig.getContextDecl());
                if (node instanceof Call) {
                    // in case of a call the type can be determined more exactly
                    Call call = (Call) node;
                    typ = call.getCallee().getType();
                }
                return new IHyperlink[] { new JumpToDeclaration(editor, startOffset, endOffset, targetPos), new JumpToImplementation(editor, startOffset, endOffset, methodSig.getName(), typ) };
            } else if (methodSig.getContextDecl() instanceof ClassDecl) {
                // cursor is on a class method => provide links to methods in interfaces
                ClassDecl classDecl = (ClassDecl) methodSig.getContextDecl();
                List<IHyperlink> links = new ArrayList<IHyperlink>();
                Set<MethodSig> addedMethods = new HashSet<MethodSig>();
                for (InterfaceDecl sup : classDecl.getSuperTypes()) {
                    MethodSig lookupMethod = sup.lookupMethod(methodSig.getName());
                    if (lookupMethod != null && !addedMethods.contains(lookupMethod)) {
                        addedMethods.add(lookupMethod);
                        targetPos = getPosition(lookupMethod);
                        String name = lookupMethod.getContextDecl().getName() + "." + lookupMethod.getName();
                        links.add(new JumpToDeclaration(editor, startOffset, endOffset, targetPos, name));
                    }
                }
                if (links.size() > 0) {
                    return links.toArray(new IHyperlink[links.size()]);
                } else {
                    return null;
                }
            }
        }
        return new IHyperlink[] { new JumpToDeclaration(editor, startOffset, endOffset, targetPos) };
    } catch (BadLocationException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : EditorPosition(org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) List(java.util.List) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

EditorPosition (org.absmodels.abs.plugin.util.UtilityFunctions.EditorPosition)6 CompilationUnit (abs.frontend.ast.CompilationUnit)2 ModuleDecl (abs.frontend.ast.ModuleDecl)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocument (org.eclipse.jface.text.IDocument)2 SemanticConditionList (abs.frontend.analyser.SemanticConditionList)1 ASTNode (abs.frontend.ast.ASTNode)1 MainBlock (abs.frontend.ast.MainBlock)1 ParserError (abs.frontend.parser.ParserError)1 List (java.util.List)1 InternalASTNode (org.absmodels.abs.plugin.util.InternalASTNode)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 ISelection (org.eclipse.jface.viewers.ISelection)1 TreeSelection (org.eclipse.jface.viewers.TreeSelection)1