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;
}
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;
}
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/>");
}
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);
}
}
}
}
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);
}
}
Aggregations