introduce new getVisibleRange() method in Editor to allow Fleet override it in headless mode

GitOrigin-RevId: 968b308c7ee0c736edb921232e9745f044916502
This commit is contained in:
Alexey Kudravtsev
2023-05-15 14:31:27 +02:00
committed by intellij-monorepo-bot
parent 776172baba
commit e157e27072
8 changed files with 45 additions and 37 deletions

View File

@@ -1407,7 +1407,7 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);
int offset = editor.logicalPositionToOffset(editor.xyToLogicalPosition(viewPosition));
VisibleHighlightingPassFactory.setVisibleRangeForHeadlessMode(editor, new ProperTextRange(offset, offset));
return VisibleHighlightingPassFactory.calculateVisibleRange(editor);
return editor.calculateVisibleRange();
}
static void makeWholeEditorWindowVisible(@NotNull EditorImpl editor) {
@@ -1631,7 +1631,8 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
((EditorImpl)myEditor).getScrollPane().getViewport().setViewPosition(new Point(0, 0));
((EditorImpl)myEditor).getScrollPane().getViewport().setExtentSize(new Dimension(100, 100000));
ProperTextRange visibleRange = VisibleHighlightingPassFactory.calculateVisibleRange(getEditor());
@NotNull Editor editor = getEditor();
ProperTextRange visibleRange = editor.calculateVisibleRange();
assertTrue(visibleRange.getLength() > 0);
Document document = myEditor.getDocument();
assertTrue(visibleRange.getLength() < document.getTextLength());
@@ -2376,7 +2377,8 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
""";
configureByText(JavaFileType.INSTANCE, text1);
((EditorImpl)myEditor).getScrollPane().getViewport().setSize(1000, 1000);
assertEquals(getFile().getTextRange(), VisibleHighlightingPassFactory.calculateVisibleRange(getEditor()));
@NotNull Editor editor = getEditor();
assertEquals(getFile().getTextRange(), editor.calculateVisibleRange());
assertEquals("XXX", assertOneElement(doHighlighting(HighlightSeverity.WARNING)).getDescription());
@@ -2724,7 +2726,8 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
""";
configureByText(JavaFileType.INSTANCE, text);
((EditorImpl)myEditor).getScrollPane().getViewport().setSize(1000, 1000);
assertEquals(getFile().getTextRange(), VisibleHighlightingPassFactory.calculateVisibleRange(getEditor()));
@NotNull Editor editor = getEditor();
assertEquals(getFile().getTextRange(), editor.calculateVisibleRange());
toSleepMs.set(1_000_000);
@@ -2932,7 +2935,8 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
String text = "class X {\n}";
configureByText(JavaFileType.INSTANCE, text);
((EditorImpl)myEditor).getScrollPane().getViewport().setSize(1000, 1000);
assertEquals(getFile().getTextRange(), VisibleHighlightingPassFactory.calculateVisibleRange(getEditor()));
@NotNull Editor editor = getEditor();
assertEquals(getFile().getTextRange(), editor.calculateVisibleRange());
CodeInsightTestFixtureImpl.ensureIndexesUpToDate(getProject());
doHighlighting();
MyRecordingAnnotator.clearAll();
@@ -3157,7 +3161,7 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
Point2D visualPoint = editor.offsetToPoint2D(editor.getCaretModel().getOffset());
editor.getScrollPane().getViewport().setViewPosition(new Point((int)visualPoint.getX(), (int)visualPoint.getY()));
editor.getScrollPane().getViewport().setExtentSize(new Dimension(100, editor.getPreferredHeight() - (int)visualPoint.getY()));
ProperTextRange visibleRange = VisibleHighlightingPassFactory.calculateVisibleRange(editor);
ProperTextRange visibleRange = editor.calculateVisibleRange();
assertTrue(visibleRange.toString(), visibleRange.getStartOffset() > 0);
useAnnotatorsIn(PlainTextLanguage.INSTANCE, new MyRecordingAnnotator[]{new MiddleOfTextAnnotator()}, ()->{
MiddleOfTextAnnotator.doAnnotate = true;
@@ -3193,7 +3197,7 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
Point2D caretVisualPoint = editor.offsetToPoint2D(editor.getCaretModel().getOffset());
editor.getScrollPane().getViewport().setViewPosition(new Point((int)caretVisualPoint.getX(), (int)caretVisualPoint.getY()));
editor.getScrollPane().getViewport().setExtentSize(new Dimension(100, editor.getPreferredHeight() - (int)caretVisualPoint.getY()));
ProperTextRange visibleRange = VisibleHighlightingPassFactory.calculateVisibleRange(editor);
ProperTextRange visibleRange = editor.calculateVisibleRange();
assertTrue(visibleRange.toString(), visibleRange.getStartOffset() > 0);
myDaemonCodeAnalyzer.restart();
expectedVisibleRange = visibleRange;
@@ -3228,7 +3232,7 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
Point2D caretVisualPoint = editor.offsetToPoint2D(editor.getCaretModel().getOffset());
editor.getScrollPane().getViewport().setViewPosition(new Point((int)caretVisualPoint.getX(), (int)caretVisualPoint.getY()));
editor.getScrollPane().getViewport().setExtentSize(new Dimension(100, editor.getPreferredHeight() - (int)caretVisualPoint.getY()));
ProperTextRange visibleRange = VisibleHighlightingPassFactory.calculateVisibleRange(editor);
ProperTextRange visibleRange = editor.calculateVisibleRange();
assertTrue(visibleRange.toString(), visibleRange.getStartOffset() > 0);
myDaemonCodeAnalyzer.restart();
expectedVisibleRange = visibleRange;

View File

@@ -21,6 +21,7 @@ import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.command.undo.UndoManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.editor.impl.EditorImpl;
@@ -627,7 +628,8 @@ public class ImportHelperTest extends LightDaemonAnalyzerTestCase {
UIUtil.dispatchAllInvocationEvents();
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = false;
DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);
TextRange visibleRange = VisibleHighlightingPassFactory.calculateVisibleRange(getEditor());
@NotNull Editor editor = getEditor();
TextRange visibleRange = editor.calculateVisibleRange();
assertTrue(visibleRange.toString(), visibleRange.getStartOffset() > 5000 && visibleRange.getEndOffset() < 10_000); // sanity check that visible range has been indeed changed
List<HighlightInfo> errors = highlightErrors();

View File

@@ -127,7 +127,8 @@ public class LightAnnotatorHighlightingTest extends LightDaemonAnalyzerTestCase
""";
configureFromFileText("x.java", text);
((EditorImpl)getEditor()).getScrollPane().getViewport().setSize(1000, 1000);
assertEquals(getFile().getTextRange(), VisibleHighlightingPassFactory.calculateVisibleRange(getEditor()));
@NotNull Editor editor = getEditor();
assertEquals(getFile().getTextRange(), editor.calculateVisibleRange());
CodeInsightTestFixtureImpl.ensureIndexesUpToDate(getProject());
TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(getEditor());

View File

@@ -101,7 +101,9 @@ public final class HighlightingSessionImpl implements HighlightingSession {
@NotNull DaemonProgressIndicator progressIndicator) {
ApplicationManager.getApplication().assertIsDispatchThread();
TextRange fileRange = psiFile.getTextRange();
ProperTextRange visibleRange = editor == null ? ProperTextRange.create(ObjectUtils.notNull(fileRange, TextRange.EMPTY_RANGE)) : VisibleHighlightingPassFactory.calculateVisibleRange(editor);
ProperTextRange visibleRange;
visibleRange =
editor == null ? ProperTextRange.create(ObjectUtils.notNull(fileRange, TextRange.EMPTY_RANGE)) : editor.calculateVisibleRange();
CanISilentlyChange.Result canChangeFileSilently = CanISilentlyChange.thisFile(psiFile);
return (HighlightingSessionImpl)createHighlightingSession(psiFile, progressIndicator, editorColorsScheme, visibleRange, canChangeFileSilently);
}

View File

@@ -3,7 +3,6 @@ package com.intellij.codeInsight.daemon.impl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.util.ProperTextRange;
import com.intellij.ui.ComponentUtil;
@@ -15,20 +14,6 @@ import javax.swing.*;
import java.awt.*;
public abstract class VisibleHighlightingPassFactory {
@NotNull
public static ProperTextRange calculateVisibleRange(@NotNull Editor editor) {
ApplicationManager.getApplication().assertIsDispatchThread();
Rectangle rect = editor.getScrollingModel().getVisibleArea();
LogicalPosition startPosition = editor.xyToLogicalPosition(new Point(rect.x, rect.y));
int visibleStart = editor.logicalPositionToOffset(startPosition);
LogicalPosition endPosition = editor.xyToLogicalPosition(new Point(rect.x + rect.width, rect.y + rect.height));
int visibleEnd = editor.logicalPositionToOffset(new LogicalPosition(endPosition.line + 1, 0));
return new ProperTextRange(visibleStart, Math.max(visibleEnd, visibleStart));
}
@ApiStatus.Internal
public static void setVisibleRangeForHeadlessMode(@NotNull Editor editor, @NotNull ProperTextRange range) {
@@ -40,12 +25,10 @@ public abstract class VisibleHighlightingPassFactory {
Point viewPositionStart = editor.logicalPositionToXY(editor.offsetToLogicalPosition(range.getStartOffset()));
Point viewPositionEnd = editor.logicalPositionToXY(editor.offsetToLogicalPosition(range.getEndOffset()));
JScrollPane scrollPane = ComponentUtil.getScrollPane(editor.getContentComponent());
if (scrollPane != null) {
scrollPane.getViewport().setSize(editor.getContentComponent().getWidth(), Math.max(100, viewPositionEnd.y - viewPositionStart.y));
editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
scrollPane.getViewport().setViewPosition(viewPositionStart);
scrollPane.getViewport().setExtentSize(new Dimension(editor.getContentComponent().getWidth(), Math.max(100, viewPositionEnd.y - viewPositionStart.y)));
UIUtil.markAsFocused(editor.getContentComponent(), true); // to make ShowIntentionPass call its collectInformation()
}
scrollPane.getViewport().setSize(editor.getContentComponent().getWidth(), Math.max(100, viewPositionEnd.y - viewPositionStart.y));
editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
scrollPane.getViewport().setViewPosition(viewPositionStart);
scrollPane.getViewport().setExtentSize(new Dimension(editor.getContentComponent().getWidth(), Math.max(100, viewPositionEnd.y - viewPositionStart.y)));
UIUtil.markAsFocused(editor.getContentComponent(), true); // to make ShowIntentionPass call its collectInformation()
}
}

View File

@@ -3,6 +3,7 @@ package com.intellij.openapi.editor;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.editor.event.EditorMouseEventArea;
@@ -12,6 +13,7 @@ import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.markup.MarkupModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.ProperTextRange;
import com.intellij.openapi.util.UserDataHolder;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
@@ -411,4 +413,20 @@ public interface Editor extends UserDataHolder {
return (int)(getContentComponent().getFontMetrics(getColorsScheme().getFont(EditorFontType.PLAIN)).getAscent() *
getColorsScheme().getLineSpacing());
}
/**
* Computes an area which is visible on screen.
* By default, it retrieves the visible area from the scrolling pane attached to the editor component.
* Can only be called from the EDT.
*/
@NotNull
default ProperTextRange calculateVisibleRange() {
ApplicationManager.getApplication().assertIsDispatchThread();
Rectangle rect = getScrollingModel().getVisibleArea();
LogicalPosition startPosition = xyToLogicalPosition(new Point(rect.x, rect.y));
int visibleStart = logicalPositionToOffset(startPosition);
LogicalPosition endPosition = xyToLogicalPosition(new Point(rect.x + rect.width, rect.y + rect.height));
int visibleEnd = logicalPositionToOffset(new LogicalPosition(endPosition.line + 1, 0));
return new ProperTextRange(visibleStart, Math.max(visibleEnd, visibleStart));
}
}

View File

@@ -4,7 +4,6 @@ package com.intellij.codeInsight.highlighting;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.daemon.impl.IdentifierUtil;
import com.intellij.codeInsight.daemon.impl.VisibleHighlightingPassFactory;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.find.EditorSearchSession;
import com.intellij.injected.editor.EditorWindow;
@@ -94,7 +93,7 @@ public class HighlightUsagesHandler extends HighlightHandlerBase {
@Nullable
public static <T extends PsiElement> HighlightUsagesHandlerBase<T> createCustomHandler(@NotNull Editor editor, @NotNull PsiFile file) {
ApplicationManager.getApplication().assertIsDispatchThread();
ProperTextRange visibleRange = VisibleHighlightingPassFactory.calculateVisibleRange(editor);
ProperTextRange visibleRange = editor.calculateVisibleRange();
return createCustomHandler(editor, file, visibleRange);
}

View File

@@ -4,7 +4,6 @@ package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.application.options.XmlSettings;
import com.intellij.codeInsight.completion.ExtendedTagInsertHandler;
import com.intellij.codeInsight.daemon.impl.ShowAutoImportPass;
import com.intellij.codeInsight.daemon.impl.VisibleHighlightingPassFactory;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
@@ -229,7 +228,7 @@ public class CreateNSDeclarationIntentionFix implements HintAction, LocalQuickFi
final String title = getSelectNSActionTitle();
final ImportNSAction action = new ImportNSAction(namespaces, getFile(), element, editor, title);
if (element instanceof XmlTag && token != null) {
if (VisibleHighlightingPassFactory.calculateVisibleRange(editor).contains(token.getTextRange())) {
if (editor.calculateVisibleRange().contains(token.getTextRange())) {
HintManager.getInstance().showQuestionHint(editor, message,
token.getTextOffset(),
token.getTextOffset() + myNamespacePrefix.length(), action);