Search in sources :

Example 1 with IErlMember

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

the class AutoIndentStrategy method indentAfterNewLine.

protected void indentAfterNewLine(final IDocument d, final DocumentCommand c) throws BadLocationException {
    if (editor == null) {
        return;
    }
    final int offset = c.offset;
    String txt = null;
    editor.reconcileNow();
    final IErlElement element = editor.getElementAt(offset, false);
    final IErlMember member = (IErlMember) element;
    if (member != null) {
        final int start = member.getSourceRange().getOffset();
        if (offset >= start) {
            txt = d.get(start, offset - start);
        }
    }
    if (txt == null) {
        txt = "";
    }
    final int lineN = d.getLineOfOffset(offset);
    final int lineOffset = d.getLineOffset(lineN);
    final int lineLength = d.getLineLength(lineN);
    final String oldLine = d.get(offset, lineLength + lineOffset - offset);
    try {
        final int indentw = AutoIndentStrategy.getIndentWidthFromPreferences();
        final int tabw = EditorsUI.getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
        final Map<String, String> prefs = new TreeMap<>();
        IndentationPreferencePage.addKeysAndPrefs(prefs);
        SmartTypingPreferencePage.addAutoNLKeysAndPrefs(prefs);
        final boolean useTabs = AutoIndentStrategy.getUseTabsFromPreferences();
        final IndentResult res = ErlangEngine.getInstance().getIndentService().indentLine(oldLine, txt, c.text, indentw, tabw, useTabs, prefs);
        if (res.isAddNewLine()) {
            c.text += "\n";
        }
        c.text += res.getText();
        c.length += res.getRemoveNext();
    } catch (final Exception e) {
        ErlLogger.warn(e);
    }
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlMember(org.erlide.engine.model.erlang.IErlMember) IndentResult(org.erlide.engine.services.text.IndentResult) TreeMap(java.util.TreeMap) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with IErlMember

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

the class DefaultErlangFoldingStructureProvider method match.

/**
 * Matches deleted annotations to changed or added ones. A deleted
 * annotation/position tuple that has a matching addition / change is
 * updated and marked as changed. The matching tuple is not added (for
 * additions) or marked as deletion instead (for changes). The result is
 * that more annotations are changed and fewer get deleted/re-added.
 */
private void match(final List<ErlangProjectionAnnotation> deletions, final Map<ErlangProjectionAnnotation, Position> additions, final List<ErlangProjectionAnnotation> changes) {
    if (deletions.isEmpty() || additions.isEmpty() && changes.isEmpty()) {
        return;
    }
    final List<ErlangProjectionAnnotation> newDeletions = new ArrayList<>();
    final List<ErlangProjectionAnnotation> newChanges = new ArrayList<>();
    final Iterator<ErlangProjectionAnnotation> deletionIterator = deletions.iterator();
    while (deletionIterator.hasNext()) {
        final ErlangProjectionAnnotation deleted = deletionIterator.next();
        final Position deletedPosition = fCachedModel.getPosition(deleted);
        if (deletedPosition == null) {
            continue;
        }
        final Tuple deletedTuple = new Tuple(deleted, deletedPosition);
        Tuple match = findMatch(deletedTuple, changes, null);
        boolean addToDeletions = true;
        if (match == null) {
            match = findMatch(deletedTuple, additions.keySet(), additions);
            addToDeletions = false;
        }
        if (match != null) {
            final IErlElement element = match.annotation.getElement();
            deleted.setElement(element);
            deletedPosition.setLength(match.position.getLength());
            if (deletedPosition instanceof ErlangElementPosition && element instanceof IErlMember) {
                final ErlangElementPosition eep = (ErlangElementPosition) deletedPosition;
                eep.setMember((IErlMember) element);
            }
            deletionIterator.remove();
            newChanges.add(deleted);
            if (addToDeletions) {
                newDeletions.add(match.annotation);
            }
        }
    }
    deletions.addAll(newDeletions);
    changes.addAll(newChanges);
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlMember(org.erlide.engine.model.erlang.IErlMember) Position(org.eclipse.jface.text.Position) IProjectionPosition(org.eclipse.jface.text.source.projection.IProjectionPosition) ArrayList(java.util.ArrayList)

Example 3 with IErlMember

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

the class ErlTextMemberSelection method getMemberRange.

@Override
public IErlRange getMemberRange() {
    if (getErlElement() instanceof IErlMember) {
        IErlRange range = null;
        final IErlMember member = (IErlMember) getErlElement();
        int sL;
        int sC;
        int eL;
        int eC;
        sL = member.getLineStart() + 1;
        eL = member.getLineEnd() + 1;
        sC = WranglerUtils.calculateColumnFromOffset(member.getSourceRange().getOffset(), sL - 1, document);
        eC = WranglerUtils.calculateColumnFromOffset(member.getSourceRange().getOffset() + member.getSourceRange().getLength(), eL - 1, document);
        range = new ErlRange(sL, sC, eL, eC, member.getSourceRange().getOffset(), member.getSourceRange().getLength());
        return range;
    }
    return getSelectionRange();
}
Also used : IErlRange(org.erlide.wrangler.refactoring.util.IErlRange) IErlMember(org.erlide.engine.model.erlang.IErlMember) ErlRange(org.erlide.wrangler.refactoring.util.ErlRange) IErlRange(org.erlide.wrangler.refactoring.util.IErlRange)

Example 4 with IErlMember

use of org.erlide.engine.model.erlang.IErlMember 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)

Example 5 with IErlMember

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

the class ErlParser method addMacroDef.

private IErlMember addMacroDef(final IErlModule module, final OtpErlangObject pos, final OtpErlangObject val, final OtpErlangObject extra, final String nameS) {
    if (val instanceof OtpErlangAtom) {
        final String s = Util.stringValue(extra);
        final ErlMember r = new ErlMacroDef(module, ((OtpErlangAtom) val).atomValue(), s);
        setPos(r, pos);
        // r.setParseTree(val);
        return r;
    } else if (val instanceof OtpErlangList) {
        final OtpErlangList macroList = (OtpErlangList) val;
        if (macroList.elementAt(0) instanceof OtpErlangTuple) {
            final OtpErlangTuple macroNameTuple = (OtpErlangTuple) macroList.elementAt(0);
            OtpErlangObject o = macroNameTuple.elementAt(2);
            if (o instanceof OtpErlangTuple) {
                o = ((OtpErlangTuple) o).elementAt(2);
            }
            ErlMember r;
            if (o instanceof OtpErlangAtom) {
                final String macroName = ((OtpErlangAtom) o).atomValue();
                r = new ErlMacroDef(module, macroName, null);
            } else {
                // what do we do here? the define isn't correct
                // Erlang...
                ErlLogger.warn("Strange macro definition in %s: %s", module.getName(), o.toString());
                r = new ErlMacroDef(module, o.toString(), null);
            }
            setPos(r, pos);
            // r.setParseTree(val);
            return r;
        }
    }
    return addOtherAttribute(module, pos, val, extra, nameS);
}
Also used : ErlMacroDef(org.erlide.engine.internal.model.erlang.ErlMacroDef) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) ErlMember(org.erlide.engine.internal.model.erlang.ErlMember) IErlMember(org.erlide.engine.model.erlang.IErlMember) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple)

Aggregations

IErlMember (org.erlide.engine.model.erlang.IErlMember)8 IErlElement (org.erlide.engine.model.IErlElement)3 IErlComment (org.erlide.engine.model.erlang.IErlComment)3 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)1 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)1 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)1 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)1 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 Position (org.eclipse.jface.text.Position)1 TextSelection (org.eclipse.jface.text.TextSelection)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 IProjectionPosition (org.eclipse.jface.text.source.projection.IProjectionPosition)1 ISelection (org.eclipse.jface.viewers.ISelection)1 StyledText (org.eclipse.swt.custom.StyledText)1 Point (org.eclipse.swt.graphics.Point)1 ErlMacroDef (org.erlide.engine.internal.model.erlang.ErlMacroDef)1