Skip to content

Commit

Permalink
Merge pull request apache#2139 from sdedic/bugfix/NETBEANS-2082
Browse files Browse the repository at this point in the history
NETBEANS-2082: do not scroll to caret after fold collapse, if caret is s off-screen.
  • Loading branch information
ebarboni authored May 18, 2020
2 parents cd600b0 + 150a06b commit 563a638
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,25 @@ public void finishCreation() {

@Override
public void foldHierarchyChanged(FoldHierarchyEvent evt) {
if (!collapsedFoldEncountered) {
// Check if any collapsed fold was added or a collapsed/expanded state changed
for (int i = evt.getAddedFoldCount() - 1; i >= 0; i--) {
if (evt.getAddedFold(i).isCollapsed()) {
collapsedFoldEncountered = true;
break;
}
boolean collapsedAdded = false;
boolean changedToCollapsed = false;
// Check if any collapsed fold was added or a collapsed/expanded state changed
for (int i = evt.getAddedFoldCount() - 1; i >= 0; i--) {
if (evt.getAddedFold(i).isCollapsed()) {
collapsedAdded = true;
break;
}
if (!collapsedFoldEncountered) {
for (int i = evt.getFoldStateChangeCount() - 1; i >= 0; i--) {
FoldStateChange foldStateChange = evt.getFoldStateChange(i);
if (foldStateChange.isCollapsedChanged() && foldStateChange.getFold().isCollapsed()) {
collapsedFoldEncountered = true;
break;
}
}
if (!collapsedAdded) {
for (int i = evt.getFoldStateChangeCount() - 1; i >= 0; i--) {
FoldStateChange foldStateChange = evt.getFoldStateChange(i);
if (foldStateChange.isCollapsedChanged() && foldStateChange.getFold().isCollapsed()) {
changedToCollapsed = true;
break;
}
}
}
collapsedFoldEncountered |= collapsedAdded || changedToCollapsed;
JTextComponent comp = textComponent();
if (collapsedFoldEncountered && comp != null) {
// [TODO] there could be more detailed inspection done among folds
Expand All @@ -289,7 +290,11 @@ public void foldHierarchyChanged(FoldHierarchyEvent evt) {
ViewUtils.log(CHANGE_LOG, "CHANGE in FoldViewFactory: <" + // NOI18N
startOffset + "," + endOffset + ">\n"); // NOI18N
}
comp.putClientProperty("editorcaret.updateRetainsVisibleOnce", Boolean.TRUE);
if (collapsedAdded) {
// this hint covers a specific case when a fold is created as *initially* collapsed
// to maintain caret position on screen, if the caret was visible.
comp.putClientProperty("editorcaret.updateRetainsVisibleOnce", Boolean.TRUE);
}
fireEvent(EditorViewFactoryChange.createList(startOffset, endOffset,
EditorViewFactoryChange.Type.PARAGRAPH_CHANGE));
}
Expand Down
12 changes: 12 additions & 0 deletions ide/editor.lib2/src/org/netbeans/api/editor/caret/EditorCaret.java
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,9 @@ private void update(boolean calledFromPaint) {
if (cbounds != null) {
// save relative position of the main caret
maybeSaveCaretOffset(cbounds);
if (log) {
LOG.fine("EditorCaret.update: forced:true, savedBounds=" + cbounds + ", relativeOffset=" + lastCaretVisualOffset + "\n"); // NOI18N
}
}
}
if (!calledFromPaint && !c.isValid() /* && maintainVisible == null */) {
Expand All @@ -2025,6 +2028,11 @@ private void update(boolean calledFromPaint) {
scroll = scrollToLastCaret;
scrollToLastCaret = false;
}
if (lastCaretVisualOffset == -1) {
// wasn't able to save the visual offset, the caret was already off screen. Do not scroll just because of fold updates.
forceUpdate = false;
c.putClientProperty("editorcaret.updateRetainsVisibleOnce", null); // NOI18N
}
if (scroll || forceUpdate) {
Rectangle caretBounds;
Rectangle oldCaretBounds;
Expand All @@ -2035,6 +2043,10 @@ private void update(boolean calledFromPaint) {
caretBounds = lastCaretItem.getCaretBounds();
oldCaretBounds = caretBounds;
}
if (log) {
LOG.fine("EditorCaret.update: caretBounds=" + caretBounds + "\n"); // NOI18N
LOG.fine("EditorCaret.update: oldCaretBounds=" + oldCaretBounds + "\n"); // NOI18N
}
if (caretBounds != null) {
Rectangle scrollBounds = new Rectangle(caretBounds); // Must possibly be cloned upon change
if (viewport != null && isWrapping()) {
Expand Down

0 comments on commit 563a638

Please sign in to comment.