Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -429,22 +429,28 @@ private void eraseLine(int line) {
@Override
public void eraseLineToEnd() {
synchronized (fTerminal) {
eraseWhole(toAbsoluteLine(fCursorLine), fCursorColumn, fColumns);
int line = toAbsoluteLine(fCursorLine);
eraseWhole(line, fCursorColumn, fColumns);
// Nothing is left out to the margin, so the line ends where the cursor is.
fTerminal.clearWrappedLine(line);
}
}

/**
* Erasing either half of a wide character erases all of it, in the same
* background as the rest of the range.
*
* @return where the erase ended, past a wide character it took whole
*/
private void eraseWhole(int line, int from, int to) {
private int eraseWhole(int line, int from, int to) {
if (isSecondCell(line, from)) {
from--;
}
if (isSecondCell(line, to)) {
to++;
}
eraseCells(line, from, to);
return to;
}

/**
Expand Down Expand Up @@ -518,6 +524,8 @@ public void deleteCharacters(int n) {
fTerminal.setChar(line, col - n, c, style);
}
eraseCells(line, Math.max(fCursorColumn, fColumns - n), fColumns);
// what reached the margin moved off it, so the line ends before it (xterm)
fTerminal.clearWrappedLine(line);
}
}

Expand Down Expand Up @@ -884,7 +892,14 @@ public void eraseCharacters(int n) {
return; // nothing is erased, so no wide character may be broken either
}
synchronized (fTerminal) {
eraseWhole(toAbsoluteLine(fCursorLine), fCursorColumn, Math.min(fCursorColumn + n, fColumns));
int line = toAbsoluteLine(fCursorLine);
int end = n >= fColumns - fCursorColumn ? fColumns : fCursorColumn + n;
// a wide character the erase ends in is erased whole, maybe up to the margin
if (eraseWhole(line, fCursorColumn, end) == fColumns) {
// Nothing is left out to the margin, so the line ends before it, as
// erasing to the end of the line says (xterm; Ghostty on any ECH).
fTerminal.clearWrappedLine(line);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,9 @@ synchronized public boolean isWrappedLine(int line) {
synchronized public void setWrappedLine(int line) {
fData.setWrappedLine(line);
}

@Override
synchronized public void clearWrappedLine(int line) {
fData.clearWrappedLine(line);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,12 @@ public boolean isWrappedLine(int line) {
@Override
public void setWrappedLine(int line) {
fData.setWrappedLine(line);
sendLineChangedToSnapshots(line);
}

@Override
public void clearWrappedLine(int line) {
fData.clearWrappedLine(line);
sendLineChangedToSnapshots(line);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,10 @@ public void setWrappedLine(int line) {
fData.setWrappedLine(getPositionOfLine(line));
}

@Override
public void clearWrappedLine(int line) {
validateLineParameter(line);
fData.clearWrappedLine(getPositionOfLine(line));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,9 @@ public boolean isWrappedLine(int line) {
public void setWrappedLine(int line) {
fWrappedLines.set(line);
}

@Override
public void clearWrappedLine(int line) {
fWrappedLines.clear(line);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,11 @@ public void setWrappedLine(int line) {
fData.setWrappedLine(line - fWindowStartLine);
}
}

@Override
public void clearWrappedLine(int line) {
if (isInWindow(line)) {
fData.clearWrappedLine(line - fWindowStartLine);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class TextCanvas extends GridCanvas {
private final ILinelRenderer fCellRenderer;
private boolean fScrollLock;
private Point fDraggingStart;
/** -1 above the top, 1 below the bottom, 0 while the pointer is inside */
private boolean fDragScrolling;
private static final int DRAG_SCROLL_INTERVAL = 60;
private Point fDraggingEnd;
private boolean fHasSelection;
private ResizeListener fResizeListener;
Expand Down Expand Up @@ -270,21 +273,13 @@ public void mouseUp(MouseEvent e) {
});
addMouseMoveListener(e -> {
if (fDraggingStart != null) {
Point curr = screenPointToCell(e.x, e.y);
updateHasSelection(e);
switch (fSelMode) {
case WORD:
updateWordSelection(curr);
break;
case LINE:
updateLineSelection(curr);
break;
case DRAG:
default:
setSelection(curr);
break;
// Dragging past an edge keeps the lines beyond it coming into view.
if (dragPast(e.y) != 0 && !fDragScrolling) {
fDragScrolling = true;
getDisplay().timerExec(DRAG_SCROLL_INTERVAL, this::dragScroll);
}
fCellCanvasModel.expandHoverSelectionAt(-1, -1);
updateHasSelection(e);
extendSelection(screenPointToCell(e.x, e.y));
} else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.MOD1) {
// highlight (underline) word that would be used by MOD1 + mouse click
Point pt = screenPointToCell(e.x, e.y);
Expand All @@ -298,6 +293,47 @@ public void mouseUp(MouseEvent e) {
setHorizontalBarVisible(false);
}

/**
* A drag moves the end of the selection to the cell, by characters, words or
* lines as it began: the one way a drag does, whether the pointer moved or the
* view scrolled under it.
*/
private void extendSelection(Point cell) {
switch (fSelMode) {
case WORD:
updateWordSelection(cell);
break;
case LINE:
updateLineSelection(cell);
break;
case DRAG:
default:
setSelection(cell);
break;
}
fCellCanvasModel.expandHoverSelectionAt(-1, -1);
}

/** @return -1 above the canvas, 1 below it, 0 on it */
private int dragPast(int y) {
return y < 0 ? -1 : y >= getClientArea().height ? 1 : 0;
}

private void dragScroll() {
Point p = isDisposed() ? null : toControl(getDisplay().getCursorLocation());
// where the pointer is now, not where the last move left it: a new click may have
// come in between
int past = p == null ? 0 : dragPast(p.y);
if (p == null || fDraggingStart == null || past == 0) {
fDragScrolling = false;
return;
}
scrollYDelta(past * getCellHeight());
extendSelection(screenPointToCell(p.x, p.y));
redraw();
getDisplay().timerExec(DRAG_SCROLL_INTERVAL, this::dragScroll);
}

private static class Range {
final Point start;
final Point end;
Expand Down Expand Up @@ -495,6 +531,11 @@ private void calculateGrid() {
} finally {
setRedraw(true);
}
// NO_REDRAW_RESIZE paints only what a resize uncovers. When the grid gets
// narrower, what was drawn in the columns now past its edge stays there.
if (getVirtualBounds().width < virtualBounds.width) {
redraw();
}
}

void scrollToEnd() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public void drawLine(ITextCanvasModel model, GC gc, int line, int x, int y, int
setupGC(gc, style);
Point start = model.getSelectionStart();
Point end = model.getSelectionEnd();
// Everything the selection covers on this line goes down first. Drawing
// it as the text is drawn leaves out whatever the text does not reach:
// the cells past the last character a program put on the line, and the
// cells where the font draws a glyph narrower than the one it sits in.
int from = Math.max(start.y == line ? start.x : 0, colFirst);
int to = end.y == line ? Math.min(end.x + 1, colLast) : colLast;
if (to > from) {
gc.fillRectangle(x + (from - colFirst) * getCellWidth(), y, (to - from) * getCellWidth(),
getCellHeight());
}
char[] chars = model.getTerminalText().getChars(line);
if (chars != null) {
int offset = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,14 @@ public interface ITerminalTextData extends ITerminalTextDataReadOnly {
*/
void setWrappedLine(int line);

/**
* Says the line no longer runs on to the next, undoing
* {@link #setWrappedLine(int)}: what ran on to the margin was erased.
*
* @param line line to unmark, must be &gt;=0 and &lt; {@link #getHeight()}
* @since 1.2
*/
default void clearWrappedLine(int line) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1762,4 +1762,54 @@ public void testInsertAndDeleteOnHalfOfWideKeepBackground() {
assertEquals(GREEN, term.getStyle(line, 0).getBackgroundTerminalColor(), "line " + line);
}
}

@Test
public void testWrappedLineMarkComesOff() {
ITerminalTextData term = makeITerminalTextData();
IVT100EmulatorBackend vt100 = makeBakend(term);
term.setMaxHeight(3);
vt100.setDimensions(3, 5);
vt100.setCursor(0, 0);
// a line the terminal folded is marked as running on to the next
vt100.appendString("abcdefg");
assertTrue(term.isWrappedLine(0));
assertFalse(term.isWrappedLine(1));
// writing over part of it leaves it running on: aXcde is still followed by fg
vt100.setCursor(0, 1);
vt100.appendString("X");
assertTrue(term.isWrappedLine(0));
// erasing to the end of the line ends it there, as in xterm and Ghostty
vt100.setCursor(0, 3);
vt100.eraseLineToEnd();
assertFalse(term.isWrappedLine(0));
// and so does erasing characters as far as the margin, but not short of it
vt100.setCursor(0, 0);
vt100.appendString("abcdefg");
vt100.setCursor(0, 1);
vt100.eraseCharacters(2);
assertTrue(term.isWrappedLine(0));
vt100.eraseCharacters(9);
assertFalse(term.isWrappedLine(0));
// however large the count
vt100.setCursor(0, 0);
vt100.appendString("abcdefg");
vt100.setCursor(0, 1);
vt100.eraseCharacters(Integer.MAX_VALUE);
assertFalse(term.isWrappedLine(0));
assertEquals('a', term.getChar(0, 0));
assertEquals(0, term.getChar(0, 1));
// deleting characters pulls blanks in at the margin, so it ends the line too (xterm)
vt100.setCursor(0, 0);
vt100.appendString("abcdefg");
vt100.setCursor(0, 1);
vt100.deleteCharacters(1);
assertFalse(term.isWrappedLine(0));
// erasing the first cell of a wide character at the margin erases it whole, to the margin
vt100.setCursor(0, 0);
vt100.appendString("abc\ud55cfg");
assertTrue(term.isWrappedLine(0));
vt100.setCursor(0, 3);
vt100.eraseCharacters(1);
assertFalse(term.isWrappedLine(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -654,5 +654,8 @@ public void testWrappedLines() {
assertTrue(term.isWrappedLine(3));
term.cleanLine(0);
assertFalse(term.isWrappedLine(0));
term.setWrappedLine(2);
term.clearWrappedLine(2);
assertFalse(term.isWrappedLine(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1288,4 +1288,21 @@ public void testCopyRefreshesSnapshotOfAnotherSize() {
assertEquals(3, snapshot.getWidth());
assertEquals(toMultiLineText(source), toMultiLineText(snapshot));
}

@Test
public void testWrapMarkChangesReachTheSnapshot() {
ITerminalTextData term = new TerminalTextData();
term.setDimensions(2, 4);
ITerminalTextDataSnapshot snapshot = term.makeSnapshot();
snapshot.updateSnapshot(false);
// setting or clearing the mark alone is a change readers have to see
term.setWrappedLine(0);
assertTrue(snapshot.isOutOfDate());
snapshot.updateSnapshot(false);
assertTrue(snapshot.isWrappedLine(0));
term.clearWrappedLine(0);
assertTrue(snapshot.isOutOfDate());
snapshot.updateSnapshot(false);
assertFalse(snapshot.isWrappedLine(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,9 @@ public void testWrappedLines() {
assertTrue(term.isWrappedLine(3));
term.cleanLine(3);
assertFalse(term.isWrappedLine(3));
term.setWrappedLine(3);
term.clearWrappedLine(3);
assertFalse(term.isWrappedLine(3));
term.clearWrappedLine(0); // outside window, harmless
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,20 @@ public void testSelectionCoversWideCharactersWhole() {
assertEquals(new Point(0, 0), model.getSelectionStart());
assertEquals(new Point(3, 0), model.getSelectionEnd());
}

@Test
public void testCopyKeepsSpacesBeforeAFold() {
TerminalTextData data = new TerminalTextData();
data.setDimensions(2, 4);
// "foo bar" folded at four columns: the space ends the first row
data.setChars(0, 0, "foo ".toCharArray(), null);
data.setWrappedLine(0);
data.setChars(1, 0, "bar".toCharArray(), null);
ITerminalTextDataSnapshot snapshot = data.makeSnapshot();
snapshot.updateSnapshot(true);
AbstractTextCanvasModel model = new AbstractTextCanvasModel(snapshot) {
};
model.setSelection(0, 1, 0, 3);
assertEquals("foo bar", model.getSelectedText());
}
}
Loading