private void smartIndentAfterNewLine(@NotNull IDocument document, @NotNull DocumentCommand command, boolean isLastTokenCaseUpdated) {
        int docLength = document.getLength();
        if (docLength == 0) {
            return;
        }

        SQLHeuristicScanner scanner = new SQLHeuristicScanner(document, syntaxManager);
        SQLIndenter indenter = new SQLIndenter(document, syntaxManager, scanner);

        //get previous token
        int previousToken = scanner.previousToken(command.offset - 1, SQLHeuristicScanner.UNBOUND);
        int previousTokenPos = scanner.getPosition();
        String lastTokenString = scanner.getLastToken();
        int nextToken = scanner.nextToken(command.offset, SQLHeuristicScanner.UNBOUND);

        SQLBlockCompletionInfo completion = isBlocksCompletionEnabled() ? DEFAULT_SQL_BLOCK_COMPLETIONS.findCompletionByHead(previousToken) : null;
        int prevPreviousToken = completion == null || completion.getHeadCancelTokenId() == null ?
            SQLHeuristicScanner.NOT_FOUND : scanner.previousToken(previousTokenPos, SQLHeuristicScanner.UNBOUND);
        boolean autoCompletionSupported = completion != null && (
            completion.getHeadCancelTokenId() == null || ((int)completion.getHeadCancelTokenId()) != prevPreviousToken
        );

        String indent;
        String beginIndentaion = "";

        if (autoCompletionSupported) {
            indent = indenter.computeIndentation(command.offset);
            beginIndentaion = indenter.getReferenceIndentation(command.offset);
//        } else if (nextToken == SQLIndentSymbols.TokenEND) {
//            indent = indenter.getReferenceIndentation(command.offset + 1);
        } else if (KEYWORD_INDENT_ENABLED) {
            if (previousToken == SQLIndentSymbols.TokenKeyword) {
                int nlIndent = syntaxManager.getDialect().getKeywordNextLineIndent(lastTokenString);
                beginIndentaion = indenter.getReferenceIndentation(command.offset);
                if (nlIndent > 0) {
                    //if (beginIndentaion.isEmpty()) {
                    indent = beginIndentaion + indenter.createIndent(nlIndent).toString();
//                } else {
//                    indent = beginIndentaion;
//                }
                } else if (nlIndent < 0) {
                    indent = indenter.unindent(beginIndentaion, nlIndent);
                } else {
                    indent = beginIndentaion;
                }
            } else {
                indent = indenter.getReferenceIndentation(command.offset);
                if (lastTokenString != null) {
                    lastTokenString = lastTokenString.trim();
                    if (lastTokenString.length() > 0) {
                        char lastTokenChar = lastTokenString.charAt(lastTokenString.length() - 1);
                        if (lastTokenChar == ',' || lastTokenChar == ':' || lastTokenChar == '-') {
                            // Keep current indent
                        } else {
                            // Last token seems to be some identifier (table or column or function name)
                            // Next line should contain some keyword then - let's unindent
                            indent = indenter.unindent(indent, 1);
                            // Do not unindent (#5753)
                        }
                    }
                }
            }
        } else {
            indent = indenter.getReferenceIndentation(command.offset);
        }

        if (indent == null) {
            indent = ""; //$NON-NLS-1$
        }

        try {
            int p = (command.offset == docLength ? command.offset - 1 : command.offset);
            int line = document.getLineOfOffset(p);

            StringBuilder buf = new StringBuilder(command.text + indent);

            IRegion reg = document.getLineInformation(line);
            int lineEnd = reg.getOffset() + reg.getLength();

            int contentStart = findEndOfWhiteSpace(document, command.offset, lineEnd);
            command.length = Math.max(contentStart - command.offset, 0);

            int start = reg.getOffset();
            ITypedRegion region = TextUtilities.getPartition(document, partitioning, start, true);
            if (SQLParserPartitions.CONTENT_TYPE_SQL_MULTILINE_COMMENT.equals(region.getType())) {
                start = document.getLineInformationOfOffset(region.getOffset()).getOffset();
            }

            command.caretOffset = command.offset + buf.length();

            if (autoCompletionSupported && getBlockBalance(document, command.offset, completion) > 0 && getTokenCount(start, command.offset, scanner, previousToken) > 0) {
                buf.setLength(0);
                for (String part: completion.getCompletionParts()) {
                    if (part == SQLBlockCompletions.NEW_LINE_COMPLETION_PART) {
                        buf.append(getLineDelimiter(document));
                        buf.append(beginIndentaion);
                    } else if (part.equals(SQLBlockCompletions.ONE_INDENT_COMPLETION_PART)) {
                        buf.append(oneIndent);
                    } else {
                        final String token = isLastTokenCaseUpdated ? syntaxManager.getKeywordCase().transform(lastTokenString) : lastTokenString;
                        buf.append(adjustCase(token, part));
                    }
                }
                if (completion.getTailEndTokenId() != null) {
                    command.caretOffset = command.offset;
                }
            } else {
                command.caretOffset = command.offset + buf.length();
            }
            command.shiftsCaret = false;
            command.text = buf.toString();

        } catch (BadLocationException e) {
            log.error(e);
        }
    }
Creates a new SQL auto indent strategy for the given document partitioning.
plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/indent/SQLAutoIndentStrategy.java