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

Implement smartcase search #366

Merged
merged 2 commits into from
Jun 2, 2023
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
1 change: 1 addition & 0 deletions ov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copy it to `$XDG_CONFIG_HOME/ov/config.yaml` or start it with `ov --config ov.yaml`.
#
# CaseSensitive: false
# SmartCaseSensitive: false
# RegexpSearch: false
# Incsearch: true
# BeforeWriteOriginal: 1000
Expand Down
1 change: 1 addition & 0 deletions oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ type Config struct {
IsWriteOriginal bool
QuitSmall bool
CaseSensitive bool
SmartCaseSensitive bool
RegexpSearch bool
Incsearch bool
Debug bool
Expand Down
16 changes: 14 additions & 2 deletions oviewer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"sync/atomic"
"unicode"

"code.rocketnine.space/tslocum/cbind"
"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -228,9 +229,20 @@ func (root *Root) setSearcher(word string, caseSensitive bool) Searcher {
}
root.input.value = word
root.searchWord = word
root.searchReg = regexpCompile(root.searchWord, caseSensitive)

return NewSearcher(root.searchWord, root.searchReg, caseSensitive, root.Config.RegexpSearch)
smartCaseSensitive := caseSensitive
if root.Config.SmartCaseSensitive {
smartCaseSensitive = false
for _, ch := range word {
if unicode.IsUpper(ch) {
smartCaseSensitive = true
break
}
}
}
root.searchReg = regexpCompile(root.searchWord, smartCaseSensitive)

return NewSearcher(root.searchWord, root.searchReg, smartCaseSensitive, root.Config.RegexpSearch)
}

// searchMove searches forward/backward and moves to the nearest matching line.
Expand Down
18 changes: 18 additions & 0 deletions oviewer/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ func TestRoot_setSearch(t *testing.T) {
fields fields
args args
want Searcher
config Config
}{
{
name: "testNil",
Expand All @@ -499,11 +500,28 @@ func TestRoot_setSearch(t *testing.T) {
word: strings.ToLower("test"),
},
},
{
name: "testSmartCaseSensitiveTrue",
config: Config{
SmartCaseSensitive: true,
},
fields: fields{
input: &Input{},
},
args: args{
word: "Test",
caseSensitive: false,
},
want: sensitiveWord{
word: "Test",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := &Root{
input: tt.fields.input,
Config: tt.config,
}
if got := root.setSearcher(tt.args.word, tt.args.caseSensitive); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Root.setSearch() = %v, want %v", got, tt.want)
Expand Down