Skip to content

Commit 1ed9c51

Browse files
committed
feat: Auto-select word on starting touch selection(if available)
1 parent 0d49ec3 commit 1ed9c51

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

src/components/terminal/terminalTouchSelection.js

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,14 +540,24 @@ export default class TerminalTouchSelection {
540540
this.wasFocusedBeforeSelection = this.isTerminalFocused();
541541

542542
this.isSelecting = true;
543-
this.selectionStart = coords;
544-
this.selectionEnd = coords;
543+
544+
// Try to auto-select word at touch position
545+
const wordBounds = this.getWordBoundsAt(coords);
546+
if (wordBounds) {
547+
// Select the entire word
548+
this.selectionStart = wordBounds.start;
549+
this.selectionEnd = wordBounds.end;
550+
} else {
551+
// Fallback to single character selection
552+
this.selectionStart = coords;
553+
this.selectionEnd = coords;
554+
}
545555

546556
// Clear any existing selection
547557
this.terminal.clearSelection();
548558

549-
// Start with single character selection
550-
this.terminal.select(coords.col, coords.row, 1);
559+
// Apply the selection
560+
this.updateSelection();
551561

552562
// Store current selection for immediate access
553563
this.currentSelection = this.terminal.getSelection();
@@ -947,6 +957,60 @@ export default class TerminalTouchSelection {
947957
}
948958
}
949959

960+
/**
961+
* Get word boundaries at the given coordinates
962+
*/
963+
getWordBoundsAt(coords) {
964+
try {
965+
const buffer = this.terminal.buffer.active;
966+
const line = buffer.getLine(coords.row);
967+
if (!line) return null;
968+
969+
const lineText = line.translateToString(false);
970+
if (!lineText || coords.col >= lineText.length) return null;
971+
972+
const char = lineText[coords.col];
973+
if (!this.isWordCharacter(char)) return null;
974+
975+
// Find word start
976+
let startCol = coords.col;
977+
while (startCol > 0 && this.isWordCharacter(lineText[startCol - 1])) {
978+
startCol--;
979+
}
980+
981+
// Find word end
982+
let endCol = coords.col;
983+
while (
984+
endCol < lineText.length - 1 &&
985+
this.isWordCharacter(lineText[endCol + 1])
986+
) {
987+
endCol++;
988+
}
989+
990+
// Only auto-select if we found a meaningful word (more than just one character)
991+
if (endCol > startCol) {
992+
return {
993+
start: { row: coords.row, col: startCol },
994+
end: { row: coords.row, col: endCol },
995+
};
996+
}
997+
998+
return null;
999+
} catch (error) {
1000+
console.warn("Error finding word bounds:", error);
1001+
return null;
1002+
}
1003+
}
1004+
1005+
/**
1006+
* Check if a character is part of a word
1007+
*/
1008+
isWordCharacter(char) {
1009+
if (!char) return false;
1010+
// Word characters: letters, numbers, underscore, hyphen, dot
1011+
return /[a-zA-Z0-9_\-.]/.test(char);
1012+
}
1013+
9501014
/**
9511015
* Start pinch zoom gesture
9521016
*/

0 commit comments

Comments
 (0)