Why does the removeSelection method undo the results of ReformatCodeProcessor?

Answered

I want to cancel the selection after formatting the code block, but when I use the removeSelection method, I find that the code is not formatted. If I don't use this method, the code can be formatted normally.

this is my code

    public void formatCode(int start, int end){
        PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
        PsiFile psiFile = manager.getPsiFile(document);
        if (psiFile != null) {
            SelectionModel selectionModel = editor.getSelectionModel();
            selectionModel.setSelection(start, end);
            new ReformatCodeProcessor(psiFile, selectionModel).run();

            manager.commitDocument(document);
            editor.getSelectionModel().removeSelection();

        }
    }

 

0
4 comments

Hi,

Please provide more details. What API do you use? Where is it called from? What is the context? Is there any error thrown? Is there any warning on error in the log?

0

I didn't see it throwing an error in idea.  The “org.jetbrains.intellij” version is “1.16.0”

platformType=IC
platformVersion=2020.3

The selection string will be replaced, and then the selection code will be formatted. The context is:


    public void writeMessages(int start, int end, String result) {
        WriteCommandAction.runWriteCommandAction(project, () -> {
            if (result != null) {
                document.replaceString(start, end, result);
                this.formatCode(start, start+result.length());
            }
        });
    }

    public void formatCode(int start, int end){
        PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
        PsiFile psiFile = manager.getPsiFile(document);
        if (psiFile != null) {
            SelectionModel selectionModel = editor.getSelectionModel();
            selectionModel.setSelection(start, end);
            new ReformatCodeProcessor(psiFile, selectionModel).run();

            manager.commitDocument(document);
            editor.getSelectionModel().removeSelection();
        }
    }

 

0

Hi,

I guess that the formatting code is run in the background thread and the removeSelection() is executed before the actual formatting takes place. I suggest verifying it.

Also, I suggest performing the formatting with com.intellij.psi.codeStyle.CodeStyleManager.reformatRange(PsiElement, int, int). This API seems a higher level.

0

Thanks for your reply, but reformatRange is not working, but I found that reformatText can format the code correctly.

This is a python dictionary formatted using reformatRange.

    public void writeMessages(int start, int end, String result) {
        WriteCommandAction.runWriteCommandAction(project, () -> {
            if (result != null) {
                document.replaceString(start, end, result);
                this.formatCode(start, start+result.length());
            }
        });

    }

    public void formatCode(int start, int end){
        PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
        PsiFile psiFile = manager.getPsiFile(document);
        if (psiFile != null) {
//            SelectionModel selectionModel = editor.getSelectionModel();
//            selectionModel.setSelection(start, end);
            CodeStyleManager.getInstance(project).reformatRange(psiFile, start, end);
//            CodeStyleManager.getInstance(project).reformatText(psiFile, start, end);
//            new ReformatCodeProcessor(psiFile, selectionModel).run();
//            manager.commitDocument(document);
//            editor.getSelectionModel().removeSelection();
//            selectionModel.removeSelection();
//            editor.getCaretModel().moveToOffset(end);
//            editor.getScrollingModel().scrollToCaret(ScrollType.CENTER_UP);
        }
    }
0

Please sign in to leave a comment.