Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1951. TextView with selected text doesn't scroll beyond the cursor position. #1952

Merged
merged 2 commits into from
Aug 12, 2022
Merged
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
51 changes: 36 additions & 15 deletions Terminal.Gui/Views/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1613,12 +1613,7 @@ public ustring SelectedText {
return ustring.Empty;
}

SetWrapModel ();
var sel = GetRegion ();
UpdateWrapModel ();
Adjust ();

return sel;
return GetSelectedRegion ();
}
}

Expand Down Expand Up @@ -2027,10 +2022,18 @@ public override bool OnEnter (View view)
}

// Returns an encoded region start..end (top 32 bits are the row, low32 the column)
void GetEncodedRegionBounds (out long start, out long end)
{
long selection = ((long)(uint)selectionStartRow << 32) | (uint)selectionStartColumn;
long point = ((long)(uint)currentRow << 32) | (uint)currentColumn;
void GetEncodedRegionBounds (out long start, out long end,
int? startRow = null, int? startCol = null, int? cRow = null, int? cCol = null)
{
long selection;
long point;
if (startRow == null || startCol == null || cRow == null || cCol == null) {
selection = ((long)(uint)selectionStartRow << 32) | (uint)selectionStartColumn;
point = ((long)(uint)currentRow << 32) | (uint)currentColumn;
} else {
selection = ((long)(uint)startRow << 32) | (uint)startCol;
point = ((long)(uint)cRow << 32) | (uint)cCol;
}
if (selection > point) {
start = point;
end = selection;
Expand All @@ -2052,28 +2055,29 @@ bool PointInSelection (int col, int row)
// Returns a ustring with the text in the selected
// region.
//
ustring GetRegion ()
ustring GetRegion (int? sRow = null, int? sCol = null, int? cRow = null, int? cCol = null, TextModel model = null)
{
long start, end;
GetEncodedRegionBounds (out start, out end);
GetEncodedRegionBounds (out start, out end, sRow, sCol, cRow, cCol);
if (start == end) {
return ustring.Empty;
}
int startRow = (int)(start >> 32);
var maxrow = ((int)(end >> 32));
int startCol = (int)(start & 0xffffffff);
var endCol = (int)(end & 0xffffffff);
var line = model.GetLine (startRow);
var line = model == null ? this.model.GetLine (startRow) : model.GetLine (startRow);

if (startRow == maxrow)
return StringFromRunes (line.GetRange (startCol, endCol - startCol));

ustring res = StringFromRunes (line.GetRange (startCol, line.Count - startCol));

for (int row = startRow + 1; row < maxrow; row++) {
res = res + ustring.Make (Environment.NewLine) + StringFromRunes (model.GetLine (row));
res = res + ustring.Make (Environment.NewLine) + StringFromRunes (model == null
? this.model.GetLine (row) : model.GetLine (row));
}
line = model.GetLine (maxrow);
line = model == null ? this.model.GetLine (maxrow) : model.GetLine (maxrow);
res = res + ustring.Make (Environment.NewLine) + StringFromRunes (line.GetRange (0, endCol));
return res;
}
Expand Down Expand Up @@ -2339,6 +2343,23 @@ public virtual void OnUnwrappedCursorPosition ()
UnwrappedCursorPosition?.Invoke (new Point (col, row));
}

ustring GetSelectedRegion ()
{
var cRow = currentRow;
var cCol = currentColumn;
var startRow = selectionStartRow;
var startCol = selectionStartColumn;
var model = this.model;
if (wordWrap) {
cRow = wrapManager.GetModelLineFromWrappedLines (currentRow);
cCol = wrapManager.GetModelColFromWrappedLines (currentRow, currentColumn);
startRow = wrapManager.GetModelLineFromWrappedLines (selectionStartRow);
startCol = wrapManager.GetModelColFromWrappedLines (selectionStartRow, selectionStartColumn);
model = wrapManager.Model;
}
return GetRegion (startRow, startCol, cRow, cCol, model);
}

///<inheritdoc/>
public override void Redraw (Rect bounds)
{
Expand Down