use of com.google.idea.blaze.base.lang.buildfile.psi.StatementListContainer 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;
}
Aggregations