Skip to content

Commit fa64ef8

Browse files
authored
Merge pull request #366 from resolritter/smartcase
Implement smartcase search
2 parents 588a04c + f47b9c1 commit fa64ef8

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

ov.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copy it to `$XDG_CONFIG_HOME/ov/config.yaml` or start it with `ov --config ov.yaml`.
33
#
44
# CaseSensitive: false
5+
# SmartCaseSensitive: false
56
# RegexpSearch: false
67
# Incsearch: true
78
# BeforeWriteOriginal: 1000

oviewer/oviewer.go

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ type Config struct {
189189
IsWriteOriginal bool
190190
QuitSmall bool
191191
CaseSensitive bool
192+
SmartCaseSensitive bool
192193
RegexpSearch bool
193194
Incsearch bool
194195
Debug bool

oviewer/search.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313
"strings"
1414
"sync/atomic"
15+
"unicode"
1516

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

233-
return NewSearcher(root.searchWord, root.searchReg, caseSensitive, root.Config.RegexpSearch)
233+
smartCaseSensitive := caseSensitive
234+
if root.Config.SmartCaseSensitive {
235+
smartCaseSensitive = false
236+
for _, ch := range word {
237+
if unicode.IsUpper(ch) {
238+
smartCaseSensitive = true
239+
break
240+
}
241+
}
242+
}
243+
root.searchReg = regexpCompile(root.searchWord, smartCaseSensitive)
244+
245+
return NewSearcher(root.searchWord, root.searchReg, smartCaseSensitive, root.Config.RegexpSearch)
234246
}
235247

236248
// searchMove searches forward/backward and moves to the nearest matching line.

oviewer/search_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ func TestRoot_setSearch(t *testing.T) {
474474
fields fields
475475
args args
476476
want Searcher
477+
config Config
477478
}{
478479
{
479480
name: "testNil",
@@ -499,11 +500,28 @@ func TestRoot_setSearch(t *testing.T) {
499500
word: strings.ToLower("test"),
500501
},
501502
},
503+
{
504+
name: "testSmartCaseSensitiveTrue",
505+
config: Config{
506+
SmartCaseSensitive: true,
507+
},
508+
fields: fields{
509+
input: &Input{},
510+
},
511+
args: args{
512+
word: "Test",
513+
caseSensitive: false,
514+
},
515+
want: sensitiveWord{
516+
word: "Test",
517+
},
518+
},
502519
}
503520
for _, tt := range tests {
504521
t.Run(tt.name, func(t *testing.T) {
505522
root := &Root{
506523
input: tt.fields.input,
524+
Config: tt.config,
507525
}
508526
if got := root.setSearcher(tt.args.word, tt.args.caseSensitive); !reflect.DeepEqual(got, tt.want) {
509527
t.Errorf("Root.setSearch() = %v, want %v", got, tt.want)

0 commit comments

Comments
 (0)