String (@NotNull Editor editor, int afterLineEnd) { final Project project = editor.getProject(); StringBuilder buf = new StringBuilder(); final Document doc = editor.getDocument(); final int caretOffset = editor.getCaretModel().getOffset(); boolean atLineStart = caretOffset >= doc.getTextLength() || doc.getLineStartOffset(doc.getLineNumber(caretOffset)) == caretOffset; if (atLineStart && project != null) { int offset = editor.getCaretModel().getOffset(); PsiDocumentManager.getInstance(project).commitDocument(doc); // Sync document and PSI before formatting. String properIndent = offset >= doc.getTextLength() ? "" : CodeStyleFacade.getInstance(project).getLineIndent(doc, offset); if (properIndent != null) { int tabSize = editor.getSettings().getTabSize(project); for (int i = 0; i < properIndent.length(); i++) { if (properIndent.charAt(i) == ' ') { afterLineEnd--; } else if (properIndent.charAt(i) == '\t') { if (afterLineEnd < tabSize) { break; } afterLineEnd -= tabSize; } buf.append(properIndent.charAt(i)); if (afterLineEnd == 0) break; } } else { EditorSettings editorSettings = editor.getSettings(); boolean useTab = editorSettings.isUseTabCharacter(editor.getProject()); if (useTab) { int tabSize = editorSettings.getTabSize(project); while (afterLineEnd >= tabSize) { buf.append('\t'); afterLineEnd -= tabSize; } } } } for (int i = 0; i < afterLineEnd; i++) { buf.append(' '); } return buf.toString(); }