import com.intellij.formatting.FormattingProgressTask;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.SelectionModel;
+import com.intellij.openapi.editor.ex.util.CaretVisualPositionKeeper;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
import java.util.concurrent.FutureTask;
public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor {
return !myRanges.isEmpty() ? myRanges : ContainerUtil.newArrayList(file.getTextRange());
}
-
- private static class CaretVisualPositionKeeper {
- private final Map<Editor, Integer> myCaretRelativeVerticalPositions = new HashMap<>();
-
- private CaretVisualPositionKeeper(@Nullable Document document) {
- if (document == null) return;
-
- Editor[] editors = EditorFactory.getInstance().getEditors(document);
- for (Editor editor : editors) {
- Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
- Point pos = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
- int relativePosition = pos.y - visibleArea.y;
- myCaretRelativeVerticalPositions.put(editor, relativePosition);
- }
- }
-
- private void restoreOriginalLocation() {
- for (Map.Entry<Editor, Integer> e : myCaretRelativeVerticalPositions.entrySet()) {
- Editor editor = e.getKey();
- int relativePosition = e.getValue();
- Point caretLocation = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
- int scrollOffset = caretLocation.y - relativePosition;
- editor.getScrollingModel().disableAnimation();
- editor.getScrollingModel().scrollVertically(scrollOffset);
- editor.getScrollingModel().enableAnimation();
- }
- }
- }
}
\ No newline at end of file
import com.intellij.openapi.editor.Inlay;
import com.intellij.openapi.editor.VisualPosition;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
+import com.intellij.openapi.editor.ex.util.CaretVisualPositionKeeper;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
ParameterHintsPresentationManager presentationManager = ParameterHintsPresentationManager.getInstance();
Set<String> removedHints = new HashSet<>();
TIntObjectHashMap<Caret> caretMap = new TIntObjectHashMap<>();
+ CaretVisualPositionKeeper keeper = new CaretVisualPositionKeeper(myEditor);
for (Caret caret : myEditor.getCaretModel().getAllCarets()) {
caretMap.put(caret.getOffset(), caret);
}
String text = e.getValue();
presentationManager.addHint(myEditor, offset, text, !firstTime && !removedHints.contains(text));
}
+ keeper.restoreOriginalLocation();
myEditor.putUserData(REPEATED_PASS, Boolean.TRUE);
}
--- /dev/null
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.openapi.editor.ex.util;
+
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.editor.EditorFactory;
+import org.jetbrains.annotations.Nullable;
+
+import java.awt.*;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Allows to keep caret's position in editor window at the same vertical position for operations, that can potentially move caret.
+ * An instance of this class is to be created before the operation, method {@link #restoreOriginalLocation()} should be called after it.
+ */
+public class CaretVisualPositionKeeper {
+ private final Map<Editor, Integer> myCaretRelativeVerticalPositions = new HashMap<>();
+
+ public CaretVisualPositionKeeper(@Nullable Editor editor) {
+ this(editor == null ? Editor.EMPTY_ARRAY : new Editor[]{editor});
+ }
+
+ public CaretVisualPositionKeeper(@Nullable Document document) {
+ this(document == null ? Editor.EMPTY_ARRAY : EditorFactory.getInstance().getEditors(document));
+ }
+
+ private CaretVisualPositionKeeper(Editor[] editors) {
+ for (Editor editor : editors) {
+ Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
+ Point pos = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
+ int relativePosition = pos.y - visibleArea.y;
+ myCaretRelativeVerticalPositions.put(editor, relativePosition);
+ }
+ }
+
+ public void restoreOriginalLocation() {
+ for (Map.Entry<Editor, Integer> e : myCaretRelativeVerticalPositions.entrySet()) {
+ Editor editor = e.getKey();
+ int relativePosition = e.getValue();
+ Point caretLocation = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
+ int scrollOffset = caretLocation.y - relativePosition;
+ editor.getScrollingModel().disableAnimation();
+ editor.getScrollingModel().scrollVertically(scrollOffset);
+ editor.getScrollingModel().enableAnimation();
+ }
+ }
+}