Merge pull request #1558 from mitchellh/wordboundaries

add more word boundary characters
This commit is contained in:
Mitchell Hashimoto
2024-03-04 14:33:52 -08:00
committed by GitHub

View File

@ -1739,7 +1739,26 @@ pub fn selectWordBetween(
/// this happens is if the point pt is outside of the written screen space. /// this happens is if the point pt is outside of the written screen space.
pub fn selectWord(self: *Screen, pt: point.ScreenPoint) ?Selection { pub fn selectWord(self: *Screen, pt: point.ScreenPoint) ?Selection {
// Boundary characters for selection purposes // Boundary characters for selection purposes
const boundary = &[_]u32{ 0, ' ', '\t', '\'', '"' }; const boundary = &[_]u32{
0,
' ',
'\t',
'\'',
'"',
'│',
'`',
'|',
':',
',',
'(',
')',
'[',
']',
'{',
'}',
'<',
'>',
};
// Impossible to select anything outside of the area we've written. // Impossible to select anything outside of the area we've written.
const y_max = self.rowsWritten() - 1; const y_max = self.rowsWritten() - 1;
@ -4766,50 +4785,70 @@ test "Screen: selectWord whitespace across soft-wrap" {
} }
} }
test "Screen: selectWord with single quote boundary" { test "Screen: selectWord with character boundary" {
const testing = std.testing; const testing = std.testing;
const alloc = testing.allocator; const alloc = testing.allocator;
var s = try init(alloc, 10, 20, 0); const cases = [_][]const u8{
defer s.deinit(); " 'abc' \n123",
try s.testWriteString(" 'abc' \n123"); " \"abc\" \n123",
" │abc│ \n123",
" `abc` \n123",
" |abc| \n123",
" :abc: \n123",
" ,abc, \n123",
" (abc( \n123",
" )abc) \n123",
" [abc[ \n123",
" ]abc] \n123",
" {abc{ \n123",
" }abc} \n123",
" <abc< \n123",
" >abc> \n123",
};
// Inside quotes forward for (cases) |case| {
{ var s = try init(alloc, 10, 20, 0);
const sel = s.selectWord(.{ .x = 2, .y = 0 }).?; defer s.deinit();
try testing.expectEqual(@as(usize, 2), sel.start.x); try s.testWriteString(case);
try testing.expectEqual(@as(usize, 0), sel.start.y);
try testing.expectEqual(@as(usize, 4), sel.end.x);
try testing.expectEqual(@as(usize, 0), sel.end.y);
}
// Inside quotes backward // Inside character forward
{ {
const sel = s.selectWord(.{ .x = 4, .y = 0 }).?; const sel = s.selectWord(.{ .x = 2, .y = 0 }).?;
try testing.expectEqual(@as(usize, 2), sel.start.x); try testing.expectEqual(@as(usize, 2), sel.start.x);
try testing.expectEqual(@as(usize, 0), sel.start.y); try testing.expectEqual(@as(usize, 0), sel.start.y);
try testing.expectEqual(@as(usize, 4), sel.end.x); try testing.expectEqual(@as(usize, 4), sel.end.x);
try testing.expectEqual(@as(usize, 0), sel.end.y); try testing.expectEqual(@as(usize, 0), sel.end.y);
} }
// Inside quotes bidirectional // Inside character backward
{ {
const sel = s.selectWord(.{ .x = 3, .y = 0 }).?; const sel = s.selectWord(.{ .x = 4, .y = 0 }).?;
try testing.expectEqual(@as(usize, 2), sel.start.x); try testing.expectEqual(@as(usize, 2), sel.start.x);
try testing.expectEqual(@as(usize, 0), sel.start.y); try testing.expectEqual(@as(usize, 0), sel.start.y);
try testing.expectEqual(@as(usize, 4), sel.end.x); try testing.expectEqual(@as(usize, 4), sel.end.x);
try testing.expectEqual(@as(usize, 0), sel.end.y); try testing.expectEqual(@as(usize, 0), sel.end.y);
} }
// On quote // Inside character bidirectional
// NOTE: this behavior is not ideal, so we can change this one day, {
// but I think its also not that important compared to the above. const sel = s.selectWord(.{ .x = 3, .y = 0 }).?;
{ try testing.expectEqual(@as(usize, 2), sel.start.x);
const sel = s.selectWord(.{ .x = 1, .y = 0 }).?; try testing.expectEqual(@as(usize, 0), sel.start.y);
try testing.expectEqual(@as(usize, 0), sel.start.x); try testing.expectEqual(@as(usize, 4), sel.end.x);
try testing.expectEqual(@as(usize, 0), sel.start.y); try testing.expectEqual(@as(usize, 0), sel.end.y);
try testing.expectEqual(@as(usize, 1), sel.end.x); }
try testing.expectEqual(@as(usize, 0), sel.end.y);
// On quote
// NOTE: this behavior is not ideal, so we can change this one day,
// but I think its also not that important compared to the above.
{
const sel = s.selectWord(.{ .x = 1, .y = 0 }).?;
try testing.expectEqual(@as(usize, 0), sel.start.x);
try testing.expectEqual(@as(usize, 0), sel.start.y);
try testing.expectEqual(@as(usize, 1), sel.end.x);
try testing.expectEqual(@as(usize, 0), sel.end.y);
}
} }
} }