From b4949f7939d55aeed4ca1eceae8440a309402ce6 Mon Sep 17 00:00:00 2001 From: resolritter <17429390+resolritter@users.noreply.github.com> Date: Thu, 1 Jun 2023 11:46:22 -0300 Subject: [PATCH 1/2] implement smartcase --- ov.yaml | 1 + oviewer/oviewer.go | 1 + oviewer/search.go | 16 ++++++++++++++-- oviewer/search_test.go | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ov.yaml b/ov.yaml index 5246b7b4..0ecfd155 100644 --- a/ov.yaml +++ b/ov.yaml @@ -2,6 +2,7 @@ # Copy it to `$XDG_CONFIG_HOME/ov/config.yaml` or start it with `ov --config ov.yaml`. # # CaseSensitive: false +# SmartCase: false # RegexpSearch: false # Incsearch: true # BeforeWriteOriginal: 1000 diff --git a/oviewer/oviewer.go b/oviewer/oviewer.go index 6cacf149..db7afcb0 100644 --- a/oviewer/oviewer.go +++ b/oviewer/oviewer.go @@ -192,6 +192,7 @@ type Config struct { RegexpSearch bool Incsearch bool Debug bool + SmartCase bool } // OVStyle represents a style in addition to the original style. diff --git a/oviewer/search.go b/oviewer/search.go index ba836737..120b7472 100644 --- a/oviewer/search.go +++ b/oviewer/search.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "sync/atomic" + "unicode" "code.rocketnine.space/tslocum/cbind" "github.com/gdamore/tcell/v2" @@ -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.SmartCase { + 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. diff --git a/oviewer/search_test.go b/oviewer/search_test.go index 051ddd98..8b869616 100644 --- a/oviewer/search_test.go +++ b/oviewer/search_test.go @@ -474,6 +474,7 @@ func TestRoot_setSearch(t *testing.T) { fields fields args args want Searcher + config Config }{ { name: "testNil", @@ -499,11 +500,28 @@ func TestRoot_setSearch(t *testing.T) { word: strings.ToLower("test"), }, }, + { + name: "testSmartCaseTrue", + config: Config{ + SmartCase: 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) From f47b9c11cf6cb3b21aaed7ad16f93e543bb35b2c Mon Sep 17 00:00:00 2001 From: resolritter <17429390+resolritter@users.noreply.github.com> Date: Thu, 1 Jun 2023 16:26:00 -0300 Subject: [PATCH 2/2] s/SmartCase/SmartCaseSensitive --- ov.yaml | 2 +- oviewer/oviewer.go | 2 +- oviewer/search.go | 2 +- oviewer/search_test.go | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ov.yaml b/ov.yaml index 0ecfd155..707f031b 100644 --- a/ov.yaml +++ b/ov.yaml @@ -2,7 +2,7 @@ # Copy it to `$XDG_CONFIG_HOME/ov/config.yaml` or start it with `ov --config ov.yaml`. # # CaseSensitive: false -# SmartCase: false +# SmartCaseSensitive: false # RegexpSearch: false # Incsearch: true # BeforeWriteOriginal: 1000 diff --git a/oviewer/oviewer.go b/oviewer/oviewer.go index db7afcb0..04ce27d4 100644 --- a/oviewer/oviewer.go +++ b/oviewer/oviewer.go @@ -189,10 +189,10 @@ type Config struct { IsWriteOriginal bool QuitSmall bool CaseSensitive bool + SmartCaseSensitive bool RegexpSearch bool Incsearch bool Debug bool - SmartCase bool } // OVStyle represents a style in addition to the original style. diff --git a/oviewer/search.go b/oviewer/search.go index 120b7472..acb98aa8 100644 --- a/oviewer/search.go +++ b/oviewer/search.go @@ -231,7 +231,7 @@ func (root *Root) setSearcher(word string, caseSensitive bool) Searcher { root.searchWord = word smartCaseSensitive := caseSensitive - if root.Config.SmartCase { + if root.Config.SmartCaseSensitive { smartCaseSensitive = false for _, ch := range word { if unicode.IsUpper(ch) { diff --git a/oviewer/search_test.go b/oviewer/search_test.go index 8b869616..44081020 100644 --- a/oviewer/search_test.go +++ b/oviewer/search_test.go @@ -501,9 +501,9 @@ func TestRoot_setSearch(t *testing.T) { }, }, { - name: "testSmartCaseTrue", + name: "testSmartCaseSensitiveTrue", config: Config{ - SmartCase: true, + SmartCaseSensitive: true, }, fields: fields{ input: &Input{},