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();
}
}
Please sign in to leave a comment.
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?
I didn't see it throwing an error in idea. The “org.jetbrains.intellij” version is “1.16.0”
The selection string will be replaced, and then the selection code will be formatted. The context is:
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.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.