Skip to content

Commit

Permalink
Merge pull request #366 from resolritter/smartcase
Browse files Browse the repository at this point in the history
Implement smartcase search
  • Loading branch information
noborus committed Jun 2, 2023
2 parents 588a04c + f47b9c1 commit fa64ef8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
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

0 comments on commit fa64ef8

Please sign in to comment.