Skip to content

Commit

Permalink
Fix #475: Improved caret behavior on first and last lines
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbylight committed Dec 11, 2022
1 parent 03e8dd4 commit 33ff065
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
Expand Up @@ -841,7 +841,7 @@ public static int getPositionAbove(RSyntaxTextArea c, int offs,
TokenOrientedView tov = (TokenOrientedView)e;
Token token = tov.getTokenListForPhysicalLineAbove(offs);
if (token==null) {
return -1;
return 0;
}
else if (token.getType()==Token.NULL) {
int line = c.getLineOfOffset(offs); // Sure to be >0 ??
Expand Down Expand Up @@ -875,7 +875,7 @@ public static int getPositionBelow(RSyntaxTextArea c, int offs,
TokenOrientedView tov = (TokenOrientedView)e;
Token token = tov.getTokenListForPhysicalLineBelow(offs);
if (token==null) {
return -1;
return c.getDocument().getLength();
}
else if (token.getType()==Token.NULL) {
int line = c.getLineOfOffset(offs); // Sure to be > c.getLineCount()-1 ??
Expand Down
Expand Up @@ -431,9 +431,6 @@ public Token getTokenListForPhysicalLineAbove(int offset) {
return document.getTokenListForLine(line);
}
}
// int line = map.getElementIndex(offset) - 1;
// if (line>=0)
// return document.getTokenListForLine(line);
return null;
}

Expand Down
Expand Up @@ -4,23 +4,61 @@
*/
package org.fife.ui.rsyntaxtextarea;


import org.fife.ui.SwingRunnerExtension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Position;
import java.awt.*;


/**
* Unit tests for the {@code SyntaxView} class.
*
* @author Robert Futrell
* @version 1.0
*/
@ExtendWith(SwingRunnerExtension.class)
class SyntaxViewTest extends AbstractRSyntaxTextAreaTest {


@Test
void testGetNextVisualPositionFrom_north_onFirstLine() throws BadLocationException {

String content = "This is the text";
RSyntaxTextArea textArea = createTextArea(SyntaxConstants.SYNTAX_STYLE_NONE, content);
textArea.setCaretPosition(content.indexOf("is"));

SyntaxView view = (SyntaxView)textArea.getUI().getRootView(textArea).getView(0);
Position.Bias[] biasRet = new Position.Bias[1];

// Pressing the up arrow on the top line moves the caret to offset 0
int actual = view.getNextVisualPositionFrom(textArea.getCaretPosition(),
Position.Bias.Backward, textArea.getBounds(), SwingConstants.NORTH, biasRet);
Assertions.assertEquals(0, actual);
}


@Test
void testGetNextVisualPositionFrom_south_onLastLine() throws BadLocationException {

String content = "This is the text";
RSyntaxTextArea textArea = createTextArea(SyntaxConstants.SYNTAX_STYLE_NONE, content);
textArea.setCaretPosition(content.indexOf("is"));

SyntaxView view = (SyntaxView)textArea.getUI().getRootView(textArea).getView(0);
Position.Bias[] biasRet = new Position.Bias[1];

// Pressing the up arrow on the top line moves the caret to offset 0
int actual = view.getNextVisualPositionFrom(textArea.getCaretPosition(),
Position.Bias.Forward, textArea.getBounds(), SwingConstants.SOUTH, biasRet);
Assertions.assertEquals(content.length(), actual);
}


@Test
void testGetTokenListForPhysicalLineAbove_foldingEnabled() throws BadLocationException {

Expand Down Expand Up @@ -150,7 +188,13 @@ void testNextTabStop_tabSize0() {
void testPaint_foldedFolds() {

RSyntaxTextArea textArea = createTextArea();
textArea.getFoldManager().getFold(0).setCollapsed(true);
textArea.paintImmediately(textArea.getVisibleRect());
// Fold one child fold so there is a mix of expanded and collapsed folds,
// and there is > 1 line at all
textArea.getFoldManager().getFold(0).getChild(0).setCollapsed(true);
textArea.setCaretPosition(1);
textArea.moveCaretPosition(textArea.getDocument().getLength() - 1);

SyntaxView view = (SyntaxView) textArea.getUI().getRootView(textArea).getView(0);
view.paint(createTestGraphics(), textArea.getVisibleRect());
}
}

0 comments on commit 33ff065

Please sign in to comment.