Search in sources :

Example 1 with IndentResult

use of org.erlide.engine.services.text.IndentResult 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)

Aggregations

TreeMap (java.util.TreeMap)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IErlElement (org.erlide.engine.model.IErlElement)1 IErlMember (org.erlide.engine.model.erlang.IErlMember)1 IndentResult (org.erlide.engine.services.text.IndentResult)1