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
58 changes: 58 additions & 0 deletions goto_symbol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"strings"
"testing"
"unicode/utf8"

"github.com/charmbracelet/x/ansi"
"github.com/mattn/go-runewidth"
"github.com/stretchr/testify/require"

"github.com/antonmedv/fx/internal/fuzzy"
"github.com/antonmedv/fx/internal/theme"
)

func TestGotoSymbolUnicodeRendering(t *testing.T) {
saved := theme.CurrentTheme
t.Cleanup(func() { theme.CurrentTheme = saved })
theme.CurrentTheme.StatusBar = func(s string) string { return s }
theme.CurrentTheme.Search = func(s string) string { return "\x1b[31m" + s + "\x1b[0m" }

for _, tc := range []struct {
text, pattern string
}{
{"name", "m"},
{"名前", "前"},
{"éclair", "c"},
{"🍎apple", "a"},
} {
t.Run(tc.text, func(t *testing.T) {
m := newQueryModel(t, `{"name": 1}`)
m.gotoSymbolInput = newInput()
m.gotoSymbolInput.Focus()
m.fuzzyMatch = fuzzy.Find([]rune(tc.pattern), []string{tc.text})
require.NotNil(t, m.fuzzyMatch)

lines := strings.Split(view(m), "\n")
status := lines[len(lines)-2]
require.True(t, utf8.ValidString(status))
require.Equal(t, tc.text, strings.TrimRight(ansi.Strip(status), " "))
require.Contains(t, status, "\x1b[31m"+tc.pattern+"\x1b[0m")
require.Equal(t, m.termWidth, runewidth.StringWidth(ansi.Strip(status)))
})
}
}

func TestGotoSymbolUnicodeKey(t *testing.T) {
m := newQueryModel(t, `{"名前": 1}`)
m.gotoSymbolInput = newInput()
pressKey(m, press("@"))
typeKeys(m, "前")
require.NotNil(t, m.fuzzyMatch)

lines := strings.Split(view(m), "\n")
status := ansi.Strip(lines[len(lines)-2])
require.Contains(t, status, "名前")
require.Equal(t, m.termWidth, runewidth.StringWidth(status))
}
9 changes: 5 additions & 4 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

tea "charm.land/bubbletea/v2"
"github.com/mattn/go-runewidth"

"github.com/antonmedv/fx/internal/ident"
. "github.com/antonmedv/fx/internal/jsonx"
Expand Down Expand Up @@ -163,14 +164,14 @@ func (m *model) render() string {
if m.gotoSymbolInput.Focused() && m.fuzzyMatch != nil {
var matchedStr []byte
str := m.fuzzyMatch.Str
for i := 0; i < len(str); i++ {
for i, char := range []rune(str) {
if utils.Contains(i, m.fuzzyMatch.Pos) {
matchedStr = append(matchedStr, theme.CurrentTheme.Search(string(str[i]))...)
matchedStr = append(matchedStr, theme.CurrentTheme.Search(string(char))...)
} else {
matchedStr = append(matchedStr, theme.CurrentTheme.StatusBar(string(str[i]))...)
matchedStr = append(matchedStr, theme.CurrentTheme.StatusBar(string(char))...)
}
}
repeatCount := m.termWidth - len(str)
repeatCount := m.termWidth - runewidth.StringWidth(str)
if repeatCount > 0 {
matchedStr = append(matchedStr, theme.CurrentTheme.StatusBar(strings.Repeat(" ", repeatCount))...)
}
Expand Down
Loading