Java Code Examples for com.intellij.psi.codeStyle.CodeStyleManager#adjustLineIndent()
The following examples show how to use
com.intellij.psi.codeStyle.CodeStyleManager#adjustLineIndent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DustTypedHandler.java From Intellij-Dust with MIT License | 6 votes |
/** * When appropriate, automatically reduce the indentation for else tags "{:else}" */ private void adjustFormatting(Project project, int offset, Editor editor, PsiFile file, FileViewProvider provider) { PsiElement elementAtCaret = provider.findElementAt(offset - 1, DustLanguage.class); PsiElement elseParent = PsiTreeUtil.findFirstParent(elementAtCaret, true, new Condition<PsiElement>() { @Override public boolean value(PsiElement element) { return element != null && (element instanceof DustElseTag); } }); // run the formatter if the user just completed typing a SIMPLE_INVERSE or a CLOSE_BLOCK_STACHE if (elseParent != null) { // grab the current caret position (AutoIndentLinesHandler is about to mess with it) PsiDocumentManager.getInstance(project).commitAllDocuments(); CaretModel caretModel = editor.getCaretModel(); CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); codeStyleManager.adjustLineIndent(file, editor.getDocument().getLineStartOffset(caretModel.getLogicalPosition().line)); } }
Example 2
Source File: AutoIndentLinesHandler.java From consulo with Apache License 2.0 | 5 votes |
private static void adjustLineIndent(PsiFile file, Document document, int startOffset, int endOffset, int line, Project project) { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); if (startOffset == endOffset) { int lineStart = document.getLineStartOffset(line); if (codeStyleManager.isLineToBeIndented(file, lineStart)) { codeStyleManager.adjustLineIndent(file, lineStart); } } else { codeStyleManager.adjustLineIndent(file, new TextRange(startOffset, endOffset)); } }
Example 3
Source File: PasteHandler.java From consulo with Apache License 2.0 | 5 votes |
private static void indentEachLine(Project project, Editor editor, int startOffset, int endOffset) { PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); final CharSequence text = editor.getDocument().getCharsSequence(); if (startOffset > 0 && endOffset > startOffset + 1 && text.charAt(endOffset - 1) == '\n' && text.charAt(startOffset - 1) == '\n') { // There is a possible situation that pasted text ends by a line feed. We don't want to proceed it when a text is // pasted at the first line column. // Example: // text to paste: //'if (true) { //' // source: // if (true) { // int i = 1; // int j = 1; // } // // // We get the following on paste then: // if (true) { // if (true) { // int i = 1; // int j = 1; // } // // We don't want line 'int i = 1;' to be indented here. endOffset--; } try { codeStyleManager.adjustLineIndent(file, new TextRange(startOffset, endOffset)); } catch (IncorrectOperationException e) { LOG.error(e); } }
Example 4
Source File: MoverWrapper.java From consulo with Apache License 2.0 | 5 votes |
private static void indentLinesIn(final Editor editor, final PsiFile file, final Document document, final Project project, RangeMarker range) { final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); int line1 = editor.offsetToLogicalPosition(range.getStartOffset()).line; int line2 = editor.offsetToLogicalPosition(range.getEndOffset()).line; while (!lineContainsNonSpaces(document, line1) && line1 < line2) line1++; while (!lineContainsNonSpaces(document, line2) && line1 < line2) line2--; final FileViewProvider provider = file.getViewProvider(); PsiFile rootToAdjustIndentIn = provider.getPsi(provider.getBaseLanguage()); codeStyleManager.adjustLineIndent(rootToAdjustIndentIn, new TextRange(document.getLineStartOffset(line1), document.getLineStartOffset(line2))); }
Example 5
Source File: EmacsStyleIndentAction.java From consulo with Apache License 2.0 | 5 votes |
@RequiredUIAccess @Override public void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull final PsiFile file) { if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return; PsiDocumentManager.getInstance(project).commitAllDocuments(); if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) { return; } EmacsProcessingHandler emacsProcessingHandler = LanguageEmacsExtension.INSTANCE.forLanguage(file.getLanguage()); if (emacsProcessingHandler != null) { EmacsProcessingHandler.Result result = emacsProcessingHandler.changeIndent(project, editor, file); if (result == EmacsProcessingHandler.Result.STOP) { return; } } final Document document = editor.getDocument(); final int startOffset = editor.getCaretModel().getOffset(); final int line = editor.offsetToLogicalPosition(startOffset).line; final int col = editor.getCaretModel().getLogicalPosition().column; final int lineStart = document.getLineStartOffset(line); final int initLineEnd = document.getLineEndOffset(line); try{ final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); final int newPos = codeStyleManager.adjustLineIndent(file, lineStart); final int newCol = newPos - lineStart; final int lineInc = document.getLineEndOffset(line) - initLineEnd; if (newCol >= col + lineInc && newCol >= 0) { final LogicalPosition pos = new LogicalPosition(line, newCol); editor.getCaretModel().moveToLogicalPosition(pos); editor.getSelectionModel().removeSelection(); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } } catch(IncorrectOperationException e){ LOG.error(e); } }
Example 6
Source File: EnterHandler.java From consulo with Apache License 2.0 | 5 votes |
@Nullable private PsiComment createComment(final CharSequence buffer, final CodeInsightSettings settings) throws IncorrectOperationException { myDocument.insertString(myOffset, buffer); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); CodeStyleManager.getInstance(getProject()).adjustLineIndent(myFile, myOffset + buffer.length() - 2); PsiComment comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class); comment = createJavaDocStub(settings, comment, getProject()); if (comment == null) { return null; } CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject()); final Ref<PsiComment> commentRef = Ref.create(comment); codeStyleManager.runWithDocCommentFormattingDisabled(myFile, () -> formatComment(commentRef, codeStyleManager)); comment = commentRef.get(); PsiElement next = comment.getNextSibling(); if (next == null && comment.getParent().getClass() == comment.getClass()) { next = comment.getParent().getNextSibling(); // expanding chameleon comment produces comment under comment } if (next != null) { next = myFile.findElementAt(next.getTextRange().getStartOffset()); // maybe switch to another tree } if (next != null && (!FormatterUtil.containsWhiteSpacesOnly(next.getNode()) || !next.getText().contains(LINE_SEPARATOR))) { int lineBreakOffset = comment.getTextRange().getEndOffset(); myDocument.insertString(lineBreakOffset, LINE_SEPARATOR); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); codeStyleManager.adjustLineIndent(myFile, lineBreakOffset + 1); comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class); } return comment; }
Example 7
Source File: TemplateState.java From consulo with Apache License 2.0 | 4 votes |
private void reformat(RangeMarker rangeMarkerToReformat) { final PsiFile file = getPsiFile(); if (file != null) { CodeStyleManager style = CodeStyleManager.getInstance(myProject); DumbService.getInstance(myProject).withAlternativeResolveEnabled(() -> { for (TemplateOptionalProcessor optionalProcessor : Extensions.getExtensions(TemplateOptionalProcessor.EP_NAME)) { optionalProcessor.processText(myProject, myTemplate, myDocument, myTemplateRange, myEditor); } }); PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument); // for Python, we need to indent the template even if reformatting is enabled, because otherwise indents would be broken // and reformat wouldn't be able to fix them if (myTemplate.isToIndent()) { if (!myTemplateIndented) { LOG.assertTrue(myTemplateRange.isValid(), presentTemplate(myTemplate)); smartIndent(myTemplateRange.getStartOffset(), myTemplateRange.getEndOffset()); myTemplateIndented = true; } } if (myTemplate.isToReformat()) { try { int endSegmentNumber = myTemplate.getEndSegmentNumber(); PsiDocumentManager.getInstance(myProject).commitDocument(myDocument); RangeMarker dummyAdjustLineMarkerRange = null; int endVarOffset = -1; if (endSegmentNumber >= 0) { endVarOffset = mySegments.getSegmentStart(endSegmentNumber); TextRange range = CodeStyleManagerImpl.insertNewLineIndentMarker(file, myDocument, endVarOffset); if (range != null) dummyAdjustLineMarkerRange = myDocument.createRangeMarker(range); } int reformatStartOffset = myTemplateRange.getStartOffset(); int reformatEndOffset = myTemplateRange.getEndOffset(); if (rangeMarkerToReformat != null) { reformatStartOffset = rangeMarkerToReformat.getStartOffset(); reformatEndOffset = rangeMarkerToReformat.getEndOffset(); } if (dummyAdjustLineMarkerRange == null && endVarOffset >= 0) { // There is a possible case that indent marker element was not inserted (e.g. because there is no blank line // at the target offset). However, we want to reformat white space adjacent to the current template (if any). PsiElement whiteSpaceElement = CodeStyleManagerImpl.findWhiteSpaceNode(file, endVarOffset); if (whiteSpaceElement != null) { TextRange whiteSpaceRange = whiteSpaceElement.getTextRange(); if (whiteSpaceElement.getContainingFile() != null) { // Support injected white space nodes. whiteSpaceRange = InjectedLanguageManager.getInstance(file.getProject()).injectedToHost(whiteSpaceElement, whiteSpaceRange); } reformatStartOffset = Math.min(reformatStartOffset, whiteSpaceRange.getStartOffset()); reformatEndOffset = Math.max(reformatEndOffset, whiteSpaceRange.getEndOffset()); } } style.reformatText(file, reformatStartOffset, reformatEndOffset); PsiDocumentManager.getInstance(myProject).commitDocument(myDocument); PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument); if (dummyAdjustLineMarkerRange != null && dummyAdjustLineMarkerRange.isValid()) { //[ven] TODO: [max] correct javadoc reformatting to eliminate isValid() check!!! mySegments.replaceSegmentAt(endSegmentNumber, dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset()); myDocument.deleteString(dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset()); PsiDocumentManager.getInstance(myProject).commitDocument(myDocument); } if (endSegmentNumber >= 0) { final int offset = mySegments.getSegmentStart(endSegmentNumber); final int lineStart = myDocument.getLineStartOffset(myDocument.getLineNumber(offset)); // if $END$ is at line start, put it at correct indentation if (myDocument.getCharsSequence().subSequence(lineStart, offset).toString().trim().isEmpty()) { final int adjustedOffset = style.adjustLineIndent(file, offset); mySegments.replaceSegmentAt(endSegmentNumber, adjustedOffset, adjustedOffset); } } } catch (IncorrectOperationException e) { LOG.error(e); } } } }