mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-60781 After formatting cursor jumps from indented position to beginning of the line.
Caret location is restored if necessary after formatting
This commit is contained in:
+34
@@ -24,8 +24,10 @@ import com.intellij.injected.editor.DocumentWindow;
|
||||
import com.intellij.lang.*;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.CaretModel;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.VisualPosition;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -41,6 +43,7 @@ import com.intellij.psi.impl.source.PostprocessReformattingAspect;
|
||||
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
|
||||
import com.intellij.psi.impl.source.tree.*;
|
||||
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import com.intellij.util.CharTable;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
@@ -159,6 +162,37 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
|
||||
postProcessText(file, new TextRange(formatFromStart ? 0 : startElement.getTextRange().getStartOffset(),
|
||||
formatToEnd ? file.getTextLength() : endElement.getTextRange().getEndOffset()));
|
||||
}
|
||||
|
||||
Editor editor = PsiUtilBase.findEditor(file);
|
||||
if (editor == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
CaretModel caretModel = editor.getCaretModel();
|
||||
String indent = getLineIndent(file, caretModel.getOffset());
|
||||
int tabSize = getSettings().getTabSize(file.getFileType());
|
||||
int indentColumn = indentWithInVisualColumns(indent, tabSize);
|
||||
VisualPosition position = caretModel.getVisualPosition();
|
||||
if (indentColumn != position.column) {
|
||||
caretModel.moveToVisualPosition(new VisualPosition(position.line, indentColumn));
|
||||
}
|
||||
}
|
||||
|
||||
private static int indentWithInVisualColumns(String indent, int tabSize) {
|
||||
if (tabSize <= 1) {
|
||||
return indent.length();
|
||||
}
|
||||
int result = 0;
|
||||
for (int i = 0; i < indent.length(); i++) {
|
||||
char c = indent.charAt(i);
|
||||
if (c == '\t') {
|
||||
result += tabSize - result % tabSize;
|
||||
}
|
||||
else {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private PsiElement reformatRangeImpl(final PsiElement element,
|
||||
|
||||
Reference in New Issue
Block a user