Search in sources :

Example 1 with BuildListType

use of com.google.idea.blaze.base.lang.buildfile.psi.BuildListType in project intellij by bazelbuild.

the class BuildEnterHandler method determineIndent.

/**
 * Returns null if an appropriate indent cannot be found. In that case we do nothing, and pass it
 * along to the next EnterHandler.
 */
@Nullable
private static Integer determineIndent(PsiFile file, Editor editor, int offset, IndentOptions indentOptions) {
    if (offset == 0) {
        return null;
    }
    Document doc = editor.getDocument();
    PsiElement element = getRelevantElement(file, doc, offset);
    PsiElement parent = element != null ? element.getParent() : null;
    if (parent == null) {
        return null;
    }
    if (endsBlock(element)) {
        // current line indent subtract block indent
        return Math.max(0, getIndent(doc, element) - indentOptions.INDENT_SIZE);
    }
    if (parent instanceof BuildListType) {
        BuildListType list = (BuildListType) parent;
        if (endsList(list, element) && element.getTextOffset() < offset) {
            return null;
        }
        int listOffset = list.getStartOffset();
        LogicalPosition caretPosition = editor.getCaretModel().getLogicalPosition();
        LogicalPosition listStart = editor.offsetToLogicalPosition(listOffset);
        if (listStart.line != caretPosition.line) {
            // take the minimum of the current line's indent and the current caret position
            return indentOfLineUpToCaret(doc, caretPosition.line, offset);
        }
        BuildElement firstChild = ((BuildListType) parent).getFirstElement();
        if (firstChild != null && firstChild.getNode().getStartOffset() < offset) {
            return getIndent(doc, firstChild);
        }
        return lineIndent(doc, listStart.line) + additionalIndent(parent, indentOptions);
    }
    if (parent instanceof StatementListContainer && afterColon(doc, offset)) {
        return getIndent(doc, parent) + additionalIndent(parent, indentOptions);
    }
    return null;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) StatementListContainer(com.google.idea.blaze.base.lang.buildfile.psi.StatementListContainer) BuildListType(com.google.idea.blaze.base.lang.buildfile.psi.BuildListType) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) BuildElement(com.google.idea.blaze.base.lang.buildfile.psi.BuildElement) Nullable(javax.annotation.Nullable)

Aggregations

BuildElement (com.google.idea.blaze.base.lang.buildfile.psi.BuildElement)1 BuildListType (com.google.idea.blaze.base.lang.buildfile.psi.BuildListType)1 StatementListContainer (com.google.idea.blaze.base.lang.buildfile.psi.StatementListContainer)1 Document (com.intellij.openapi.editor.Document)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (javax.annotation.Nullable)1