Skip to content

Commit 1e2ffbb

Browse files
caarlos0Copilot
andauthored
feat(textarea): get the word under the cursor (#814)
* feat(textarea): get the word under the cursor * fix: lint * Update textarea/textarea_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * test: fix Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a4c42b5 commit 1e2ffbb

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

filepicker/filepicker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ func (m Model) canSelect(file string) bool {
518518
}
519519

520520
// HighlightedPath returns the path of the currently highlighted file or directory.
521-
func (M Model) HighlightedPath() string {
522-
if len(M.files) == 0 || M.selected < 0 || M.selected >= len(M.files) {
521+
func (m Model) HighlightedPath() string {
522+
if len(m.files) == 0 || m.selected < 0 || m.selected >= len(m.files) {
523523
return ""
524524
}
525-
return filepath.Join(M.CurrentDirectory, M.files[M.selected].Name())
525+
return filepath.Join(m.CurrentDirectory, m.files[m.selected].Name())
526526
}

textarea/textarea.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,41 @@ func (m *Model) Reset() {
722722
m.SetCursorColumn(0)
723723
}
724724

725+
// Word returns the word at the cursor position.
726+
// A word is delimited by spaces or line-breaks.
727+
func (m *Model) Word() string {
728+
line := m.value[m.row]
729+
col := m.col - 1
730+
731+
if col < 0 {
732+
return ""
733+
}
734+
735+
// If cursor is beyond the line, return empty string
736+
if col >= len(line) {
737+
return ""
738+
}
739+
740+
// If cursor is on a space, return empty string
741+
if unicode.IsSpace(line[col]) {
742+
return ""
743+
}
744+
745+
// Find the start of the word by moving left
746+
start := col
747+
for start > 0 && !unicode.IsSpace(line[start-1]) {
748+
start--
749+
}
750+
751+
// Find the end of the word by moving right
752+
end := col
753+
for end < len(line) && !unicode.IsSpace(line[end]) {
754+
end++
755+
}
756+
757+
return string(line[start:end])
758+
}
759+
725760
// san initializes or retrieves the rune sanitizer.
726761
func (m *Model) san() runeutil.Sanitizer {
727762
if m.rsan == nil {

textarea/textarea_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,62 @@ func TestView(t *testing.T) {
17381738
}
17391739
}
17401740

1741+
func TestWord(t *testing.T) {
1742+
textarea := newTextArea()
1743+
1744+
textarea.SetHeight(3)
1745+
textarea.SetWidth(20)
1746+
textarea.CharLimit = 500
1747+
1748+
textarea, _ = textarea.Update(nil)
1749+
1750+
t.Run("regular input", func(t *testing.T) {
1751+
input := "Word1 Word2 Word3 Word4"
1752+
for _, k := range input {
1753+
textarea, _ = textarea.Update(keyPress(k))
1754+
textarea.View()
1755+
}
1756+
1757+
expect := "Word4"
1758+
if word := textarea.Word(); word != expect {
1759+
t.Fatalf("Expected last word to be '%s', got '%s'", expect, word)
1760+
}
1761+
})
1762+
1763+
t.Run("navigate", func(t *testing.T) {
1764+
for _, k := range []tea.KeyPressMsg{
1765+
{Code: tea.KeyLeft, Mod: tea.ModAlt, Text: "alt+left"},
1766+
{Code: tea.KeyLeft, Mod: tea.ModAlt, Text: "alt+left"},
1767+
{Code: tea.KeyRight, Text: "right"},
1768+
} {
1769+
textarea, _ = textarea.Update(k)
1770+
textarea.View()
1771+
}
1772+
1773+
expect := "Word3"
1774+
if word := textarea.Word(); word != expect {
1775+
t.Fatalf("Expected last word to be '%s', got '%s'", expect, word)
1776+
}
1777+
})
1778+
1779+
t.Run("delete", func(t *testing.T) {
1780+
for _, k := range []tea.KeyPressMsg{
1781+
{Code: tea.KeyEnd, Text: "end"},
1782+
{Code: tea.KeyBackspace, Mod: tea.ModAlt, Text: "alt+backspace"},
1783+
{Code: tea.KeyBackspace, Mod: tea.ModAlt, Text: "alt+backspace"},
1784+
{Code: tea.KeyBackspace, Text: "backspace"},
1785+
} {
1786+
textarea, _ = textarea.Update(k)
1787+
textarea.View()
1788+
}
1789+
1790+
expect := "Word2"
1791+
if word := textarea.Word(); word != expect {
1792+
t.Fatalf("Expected last word to be '%s', got '%s'", expect, word)
1793+
}
1794+
})
1795+
}
1796+
17411797
func newTextArea() Model {
17421798
textarea := New()
17431799

0 commit comments

Comments
 (0)