Search in sources :

Example 1 with IErlComment

use of org.erlide.engine.model.erlang.IErlComment in project erlide_eclipse by erlang.

the class ErlParser method createComment.

/**
 * create an IErlComment from a token record
 *
 * @param IErlModule
 *            module containing comment
 * @param OtpErlangTuple
 *            token record from noparse
 * @return IErlComment
 */
private IErlComment createComment(final IErlModule module, final OtpErlangTuple c) {
    final OtpErlangLong lineL = (OtpErlangLong) c.elementAt(ErlParser.LINE);
    final OtpErlangObject s = c.elementAt(ErlParser.TEXT);
    int line;
    int lastLine;
    try {
        line = lineL.intValue();
    } catch (final OtpErlangRangeException x) {
        line = 0;
    }
    lastLine = line;
    try {
        if (c.elementAt(ErlParser.LAST_LINE) instanceof OtpErlangLong) {
            final OtpErlangLong lastLineL = (OtpErlangLong) c.elementAt(ErlParser.LAST_LINE);
            lastLine = lastLineL.intValue();
        }
    } catch (final OtpErlangRangeException e1) {
        lastLine = line;
    }
    final ErlComment comment = new ErlComment(module, Util.stringValue(s), line <= ErlParser.MODULE_HEADER_COMMENT_THRESHOLD);
    try {
        final int ofs = ((OtpErlangLong) c.elementAt(ErlParser.OFFSET)).intValue();
        final int len = ((OtpErlangLong) c.elementAt(ErlParser.LENGTH)).intValue();
        setPos(comment, line, lastLine, ofs + 1, len);
    } catch (final OtpErlangRangeException e) {
        return null;
    }
    return comment;
}
Also used : IErlComment(org.erlide.engine.model.erlang.IErlComment) ErlComment(org.erlide.engine.internal.model.erlang.ErlComment) OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangRangeException(com.ericsson.otp.erlang.OtpErlangRangeException)

Example 2 with IErlComment

use of org.erlide.engine.model.erlang.IErlComment in project erlide_eclipse by erlang.

the class ErlParser method parse.

public boolean parse(final IErlModule module, final String scannerName, final boolean initialParse, final String path, final String initialText, final boolean updateSearchServer) {
    if (module == null) {
        return false;
    }
    OtpErlangList forms = null;
    OtpErlangList comments = null;
    OtpErlangTuple res = null;
    if (initialParse) {
        final String stateDir = ErlangEngine.getInstance().getStateDir();
        final String pathNotNull = path == null ? "" : path;
        res = ErlideNoparse.initialParse(backend, scannerName, pathNotNull, initialText, stateDir, updateSearchServer);
    } else {
        res = ErlideNoparse.reparse(backend, scannerName, updateSearchServer);
    }
    if (Util.isOk(res)) {
        final OtpErlangTuple t = (OtpErlangTuple) res.elementAt(1);
        forms = (OtpErlangList) t.elementAt(1);
        comments = (OtpErlangList) t.elementAt(2);
    } else {
        ErlLogger.error("error when parsing %s: %s", path, res);
    }
    if (forms == null) {
        module.setChildren(null);
    } else {
        final List<IErlElement> children = createForms(module, forms);
        module.setChildren(children);
    }
    if (comments == null) {
        module.setComments(null);
    } else {
        final List<IErlComment> moduleComments = createComments(module, comments);
        module.setComments(moduleComments);
    }
    attachFunctionComments(module);
    String cached = "reparsed";
    if (res != null && res.arity() > 2) {
        final OtpErlangObject res2 = res.elementAt(2);
        if (res2 instanceof OtpErlangAtom) {
            final OtpErlangAtom atom = (OtpErlangAtom) res2;
            cached = atom.atomValue();
        }
    }
    if (ErlParser.TRACE) {
        ErlLogger.debug("Parsed %d forms and %d comments (%s)", forms != null ? forms.arity() : 0, comments != null ? comments.arity() : 0, cached);
    }
    return forms != null && comments != null;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlComment(org.erlide.engine.model.erlang.IErlComment) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom)

Example 3 with IErlComment

use of org.erlide.engine.model.erlang.IErlComment in project erlide_eclipse by erlang.

the class DocumentationFormatter method getDocumentationString.

public static String getDocumentationString(final Collection<IErlComment> comments, final IErlTypespec typespec) {
    final StringBuilder stringBuilder = new StringBuilder();
    if (!comments.isEmpty()) {
        stringBuilder.append("<pre class='edoc'>");
        for (final IErlComment member : comments) {
            try {
                String source = "\n" + DocumentationFormatter.convertToHTML(member.getSource());
                source = source.replaceAll("\n%%%", "\n").replaceAll("\n%%", "\n").replaceAll("\n%", "\n").substring(1);
                source = source.replaceAll("\n( *([-=] *)+\n)+", "\n<hr/>\n").replaceAll("^ *([-=] *)+\n", "\n").replaceAll("\n *([-=] *)+$", "\n");
                stringBuilder.append(source);
                if (!source.endsWith("\n")) {
                    stringBuilder.append('\n');
                }
                stringBuilder.append('\n');
            } catch (final ErlModelException e) {
                ErlLogger.warn(e);
            }
        }
        stringBuilder.append("</pre>");
    }
    if (typespec != null) {
        try {
            stringBuilder.append("<hr/><pre class='typespec'>").append(DocumentationFormatter.convertToHTML(typespec.getSource())).append("</pre>");
        } catch (final ErlModelException e) {
            ErlLogger.warn(e);
        }
    }
    return stringBuilder.toString().replace("\n", "<br/>");
}
Also used : IErlComment(org.erlide.engine.model.erlang.IErlComment) ErlModelException(org.erlide.engine.model.ErlModelException)

Example 4 with IErlComment

use of org.erlide.engine.model.erlang.IErlComment in project erlide_eclipse by erlang.

the class DefaultErlangFoldingStructureProvider method computeAdditions.

private void computeAdditions(final IErlElement element, final Map<ErlangProjectionAnnotation, Position> map) {
    boolean createProjection = false;
    boolean collapse = false;
    if (element.getKind() == ErlElementKind.CLAUSE || element.getKind() == ErlElementKind.FUNCTION) {
        collapse = fAllowCollapsing && fCollapseClauses;
        createProjection = true;
    } else if (element.getKind() == ErlElementKind.COMMENT) {
        final IErlComment c = (IErlComment) element;
        if (c.isHeader()) {
            collapse = fAllowCollapsing && fCollapseHeaderComments;
        } else {
            collapse = fAllowCollapsing && fCollapseComments;
        }
        createProjection = true;
    } else if (element.getKind() == ErlElementKind.ATTRIBUTE) {
        createProjection = true;
    } else if (element.getKind() == ErlElementKind.EXPORT) {
        createProjection = true;
    } else if (element.getKind() == ErlElementKind.RECORD_DEF) {
        createProjection = true;
    } else if (element.getKind() == ErlElementKind.MACRO_DEF) {
        createProjection = true;
    } else if (element.getKind() == ErlElementKind.TYPESPEC) {
        collapse = fAllowCollapsing && fCollapseTypespecs;
        createProjection = true;
    }
    if (createProjection) {
        final IRegion region = computeProjectionRanges(element);
        if (region != null) {
            final Position position = createProjectionPosition(region, element);
            if (position != null) {
                map.put(new ErlangProjectionAnnotation(element, collapse && fFirstTimeInitialCollapse, element instanceof IErlComment), position);
            }
        }
    }
}
Also used : IErlComment(org.erlide.engine.model.erlang.IErlComment) Position(org.eclipse.jface.text.Position) IProjectionPosition(org.eclipse.jface.text.source.projection.IProjectionPosition) IRegion(org.eclipse.jface.text.IRegion)

Example 5 with IErlComment

use of org.erlide.engine.model.erlang.IErlComment in project erlide_eclipse by erlang.

the class ErlParser method attachFunctionComments.

/**
 * attach local function documentation with heuristics: if a comment is within 3 lines
 * before function, or a sequence of comment, -spec, comment, then they should be
 * added to function documentation
 *
 * If any typespec is available for the function (wherever it is located), then it
 * should be attached too.
 *
 * @param module
 */
private void attachFunctionComments(final IErlModule module) {
    // TODO rewrite in Erlang? would be so much less code...
    try {
        final Collection<IErlComment> comments = module.getComments();
        final List<IErlElement> children = module.getChildren();
        final List<IErlMember> all = Lists.newArrayListWithCapacity(children.size() + comments.size());
        all.addAll(comments);
        for (final IErlElement element : children) {
            if (element instanceof IErlMember) {
                all.add((IErlMember) element);
            }
        }
        all.sort(new SourceOffsetComparator());
        for (int i = 1; i < all.size(); i++) {
            checkForComment(all, i);
        }
    } catch (final ErlModelException e) {
        ErlLogger.warn(e);
    }
}
Also used : IErlComment(org.erlide.engine.model.erlang.IErlComment) IErlElement(org.erlide.engine.model.IErlElement) IErlMember(org.erlide.engine.model.erlang.IErlMember) ErlModelException(org.erlide.engine.model.ErlModelException)

Aggregations

IErlComment (org.erlide.engine.model.erlang.IErlComment)7 IErlMember (org.erlide.engine.model.erlang.IErlMember)3 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)2 ErlModelException (org.erlide.engine.model.ErlModelException)2 IErlElement (org.erlide.engine.model.IErlElement)2 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)1 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)1 OtpErlangLong (com.ericsson.otp.erlang.OtpErlangLong)1 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)1 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)1 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1 IProjectionPosition (org.eclipse.jface.text.source.projection.IProjectionPosition)1 ErlComment (org.erlide.engine.internal.model.erlang.ErlComment)1 IErlFunction (org.erlide.engine.model.erlang.IErlFunction)1 IErlTypespec (org.erlide.engine.model.erlang.IErlTypespec)1