This commit is contained in:
Alexey Kudravtsev
2016-02-10 16:58:40 +03:00
parent 56ffe0ccf4
commit 9ac8d4ea5e
2 changed files with 30 additions and 30 deletions
@@ -2041,9 +2041,6 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
if (!dim.equals(myPreferredSize) && !myDocument.isInBulkUpdate()) {
dim = mySizeAdjustmentStrategy.adjust(dim, myPreferredSize, this);
if (dim == null) {
return;
}
myPreferredSize = dim;
myGutterComponent.updateSize();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* 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.
@@ -25,7 +25,7 @@ import javax.swing.*;
import java.awt.*;
/**
* This class is aimed to help {@link EditorImpl the editor} when user extensively modifies the longest line
* This class is aimed to help {@link EditorImpl the editor} when user extensively modifies the longest line
* at the document (e.g. is typing at its end).
* <p/>
* The problem is that the longest line's width is a {@link JComponent#getPreferredSize() preferred size's width} as well.
@@ -37,39 +37,43 @@ import java.awt.*;
* is permanently increasing (the user is typing at the end of the longest line) etc. See method javadocs for more details.
* <p/>
* Not thread-safe.
*
*
* @author Denis Zhdanov
* @since 6/17/11 2:45 PM
*/
public class EditorSizeAdjustmentStrategy {
/** Amount of time (in milliseconds) to keep information about preferred size change. */
class EditorSizeAdjustmentStrategy {
/**
* Amount of time (in milliseconds) to keep information about preferred size change.
*/
private static final long TIMING_TTL_MILLIS = 10000L;
/** Constant that indicates minimum number of preferred size changes per target amount of time that is considered to be frequent. */
/**
* Constant that indicates minimum number of preferred size changes per target amount of time that is considered to be frequent.
*/
private static final int FREQUENT_SIZE_CHANGES_NUMBER = 10;
/** Default number of columns to reserve during frequent typing at the end of the longest document line. */
/**
* Default number of columns to reserve during frequent typing at the end of the longest document line.
*/
private static final int DEFAULT_RESERVE_COLUMNS_NUMBER = 4;
private final Alarm myAlarm = new Alarm();
private final Alarm myAlarm = new Alarm();
private final TLongArrayList myTimings = new TLongArrayList();
private int myReserveColumns = DEFAULT_RESERVE_COLUMNS_NUMBER;
private boolean mySkip;
private boolean myInsideValidation;
/**
* Asks to adjust new preferred size appliance if necessary.
*
* @param newPreferredSize newly calculated preferred size that differs from the old preferred size
* @param oldPreferredSize old preferred size (if any)
* @param editor target editor
* @return preferred size to use (given 'new preferred size' may be adjusted); <code>null</code> if given
* new preferred size should not be applied
*
* @param newPreferredSize newly calculated preferred size that differs from the old preferred size
* @param oldPreferredSize old preferred size (if any)
* @param editor target editor
* @return preferred size to use (given 'new preferred size' may be adjusted)
*/
@Nullable
public Dimension adjust(@NotNull Dimension newPreferredSize, @Nullable Dimension oldPreferredSize, @NotNull EditorImpl editor) {
if (oldPreferredSize == null || mySkip) {
@NotNull
Dimension adjust(@NotNull Dimension newPreferredSize, @Nullable Dimension oldPreferredSize, @NotNull EditorImpl editor) {
if (oldPreferredSize == null || myInsideValidation) {
return newPreferredSize;
}
// Process only width change.
@@ -113,16 +117,15 @@ public class EditorSizeAdjustmentStrategy {
}
if (endIndex > 0) {
myTimings.remove(0, endIndex);
}
}
}
private void scheduleSizeUpdate(@NotNull EditorImpl editor) {
myAlarm.cancelAllRequests();
myAlarm.addRequest(new UpdateSizeTask(editor), 1000);
}
private class UpdateSizeTask implements Runnable {
private final EditorImpl myEditor;
UpdateSizeTask(@NotNull EditorImpl editor) {
@@ -131,14 +134,14 @@ public class EditorSizeAdjustmentStrategy {
@Override
public void run() {
mySkip = true;
myInsideValidation = true;
myReserveColumns = DEFAULT_RESERVE_COLUMNS_NUMBER;
myTimings.clear();
try {
myEditor.validateSize();
}
finally {
mySkip = false;
myInsideValidation = false;
}
}
}