@Nullable @Override public final IndentOptions getIndentOptions(@NotNull final CodeStyleSettings settings, @NotNull final PsiFile file) { final Language language = file.getLanguage(); if (!(language instanceof PythonTemplateLanguage)) { return null; // We only care about python template files } // This template language has no settings, lets use parent language then final Language templateDataLanguage = PyTemplatesUtil.getTemplateDataLanguage(file, null); if (templateDataLanguage == null) { return null; // No template data language } return settings.getIndentOptions(templateDataLanguage.getAssociatedFileType()); }
public CommonCodeStyleSettings getDefaultCommonSettings() { CommonCodeStyleSettings defaultSettings = new CommonCodeStyleSettings(YamlLanguage.INSTANCE); IndentOptions indentOptions = defaultSettings.initIndentOptions(); indentOptions.INDENT_SIZE = 2; indentOptions.TAB_SIZE = 2; indentOptions.USE_TAB_CHARACTER = false; indentOptions.SMART_TABS = false; return defaultSettings; }
@Override public Result preprocessEnter( PsiFile file, Editor editor, Ref<Integer> caretOffset, Ref<Integer> caretAdvance, DataContext dataContext, EditorActionHandler originalHandler) { int offset = caretOffset.get(); if (editor instanceof EditorWindow) { file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); editor = InjectedLanguageUtil.getTopLevelEditor(editor); offset = editor.getCaretModel().getOffset(); } if (!isApplicable(file, dataContext)) { return Result.Continue; } // Previous enter handler's (e.g. EnterBetweenBracesHandler) can introduce a mismatch // between the editor's caret model and the offset we've been provided with. editor.getCaretModel().moveToOffset(offset); Document doc = editor.getDocument(); PsiDocumentManager.getInstance(file.getProject()).commitDocument(doc); CodeStyleSettings currentSettings = CodeStyleSettingsManager.getSettings(file.getProject()); IndentOptions indentOptions = currentSettings.getIndentOptions(file.getFileType()); Integer indent = determineIndent(file, editor, offset, indentOptions); if (indent == null) { return Result.Continue; } removeTrailingWhitespace(doc, file, offset); originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext); LogicalPosition position = editor.getCaretModel().getLogicalPosition(); if (position.column == indent) { return Result.Stop; } if (position.column > indent) { //default enter handler has added too many spaces -- remove them int excess = position.column - indent; doc.deleteString( editor.getCaretModel().getOffset() - excess, editor.getCaretModel().getOffset()); } else if (position.column < indent) { String spaces = StringUtil.repeatSymbol(' ', indent - position.column); doc.insertString(editor.getCaretModel().getOffset(), spaces); } editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(position.line, indent)); return Result.Stop; }
/** * 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; }
private static int additionalIndent(PsiElement parent, IndentOptions indentOptions) { return parent instanceof StatementListContainer ? indentOptions.INDENT_SIZE : indentOptions.CONTINUATION_INDENT_SIZE; }