From 97bc97b201dadea170644946859aa790b751a213 Mon Sep 17 00:00:00 2001 From: ANGkeith Date: Sun, 3 Dec 2023 12:50:39 +0800 Subject: [PATCH 1/2] fix: some refactoring and fix wrong suggested alias tips returned for partial match in recursive defs Signed-off-by: ANGkeith --- Makefile | 2 +- def-matcher.go | 9 +------ matcher/matcher.go | 14 ++--------- matcher/matcher_test.go | 55 ++++++++++++++++------------------------- 4 files changed, 25 insertions(+), 55 deletions(-) diff --git a/Makefile b/Makefile index 29e77ed..49d822d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ all: go build -o build/def-matcher def-matcher.go .PHONY: test test: - go test -v def-matcher.go def-matcher_test.go + go test ./... .PHONY: clean clean: rm -f build/def-matcher diff --git a/def-matcher.go b/def-matcher.go index f2368e7..758e64a 100644 --- a/def-matcher.go +++ b/def-matcher.go @@ -28,12 +28,5 @@ func main() { } command := os.Args[1] - match, isFullMatch := matcher.Match(defs, command) - if match != nil { - if isFullMatch { - fmt.Printf("%s\n", match.Name) - } else { - fmt.Printf("%s%s\n", match.Name, command[len(match.Abbr):]) - } - } + fmt.Println(matcher.Match(defs, command)) } diff --git a/matcher/matcher.go b/matcher/matcher.go index 260897a..04114a1 100644 --- a/matcher/matcher.go +++ b/matcher/matcher.go @@ -8,21 +8,17 @@ import ( "github.com/sei40kr/zsh-fast-alias-tips/model" ) -func Match(defs []model.AliasDef, command string) (*model.AliasDef, bool) { +func Match(defs []model.AliasDef, command string) string { sort.Slice(defs, func(i, j int) bool { return len(defs[j].Abbr) <= len(defs[i].Abbr) }) - var candidate model.AliasDef - isFullMatch := false - for true { var match model.AliasDef for _, def := range defs { if command == def.Abbr { match = def - isFullMatch = true break } else if strings.HasPrefix(command, def.Abbr) { match = def @@ -32,15 +28,9 @@ func Match(defs []model.AliasDef, command string) (*model.AliasDef, bool) { if match != (model.AliasDef{}) { command = fmt.Sprintf("%s%s", match.Name, command[len(match.Abbr):]) - candidate = match } else { break } } - - if candidate != (model.AliasDef{}) { - return &candidate, isFullMatch - } else { - return nil, false - } + return command } diff --git a/matcher/matcher_test.go b/matcher/matcher_test.go index ac9c837..6b5244e 100644 --- a/matcher/matcher_test.go +++ b/matcher/matcher_test.go @@ -8,54 +8,41 @@ import ( ) var mockAliasDefs = []model.AliasDef{ - { - Name: "dk", - Abbr: "docker", - }, - { - Name: "gb", - Abbr: "git branch", - }, - { - Name: "gco", - Abbr: "git checkout", - }, - { - Name: "gcb", - Abbr: "git checkout -b", - }, - { - Name: "ls", - Abbr: "ls -G", - }, - { - Name: "ll", - Abbr: "ls -lh", - }, + {Name: "dk", Abbr: "docker"}, + {Name: "gb", Abbr: "git branch"}, + {Name: "gco", Abbr: "git checkout"}, + {Name: "gcb", Abbr: "git checkout -b"}, + {Name: "ls", Abbr: "ls -G"}, + {Name: "ll", Abbr: "ls -lh"}, } func TestMatch_NoMatches(t *testing.T) { - candidate, _ := Match(mockAliasDefs, "cd ..") - assert.Nil(t, candidate, "should return nil when no matches found") + aliasTips := Match(mockAliasDefs, "cd ..") + assert.Equal(t, aliasTips, "cd ..") } func TestMatch_SingleToken(t *testing.T) { - candidate, _ := Match(mockAliasDefs, "docker") - assert.Equal(t, candidate.Name, "dk") + aliasTips := Match(mockAliasDefs, "docker") + assert.Equal(t, aliasTips, "dk") } func TestMatch_MultipleTokens(t *testing.T) { - candidate, _ := Match(mockAliasDefs, "git branch") - assert.Equal(t, candidate.Name, "gb") + aliasTips := Match(mockAliasDefs, "git branch") + assert.Equal(t, aliasTips, "gb") } func TestMatch_MultipleMatches(t *testing.T) { - candidate, _ := Match(mockAliasDefs, "git checkout -b") - assert.Equal(t, candidate.Name, "gcb", + aliasTips := Match(mockAliasDefs, "git checkout -b") + assert.Equal(t, aliasTips, "gcb", "should return the alias definition that has the longest abbreviation when multiple matches found") } func TestMatch_RecursiveDefs(t *testing.T) { - candidate, _ := Match(mockAliasDefs, "ls -G -lh") - assert.Equal(t, candidate.Name, "ll", "should apply aliases recursively") + aliasTips := Match(mockAliasDefs, "ls -G -lh") + assert.Equal(t, aliasTips, "ll", "should apply aliases recursively") +} + +func TestMatch_RecursiveDefsWithPartialMatch(t *testing.T) { + aliasTips := Match(mockAliasDefs, "ls -G -lh -a") + assert.Equal(t, aliasTips, "ll -a", "should apply aliases recursively") } From 827033af5ffb4d6f5ef59a0adf3462fa6948d3f3 Mon Sep 17 00:00:00 2001 From: ANGkeith Date: Sun, 3 Dec 2023 14:16:03 +0800 Subject: [PATCH 2/2] fix: logic for determining sub-commands Signed-off-by: ANGkeith --- matcher/matcher.go | 2 +- matcher/matcher_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/matcher/matcher.go b/matcher/matcher.go index 04114a1..1c90890 100644 --- a/matcher/matcher.go +++ b/matcher/matcher.go @@ -20,7 +20,7 @@ func Match(defs []model.AliasDef, command string) string { if command == def.Abbr { match = def break - } else if strings.HasPrefix(command, def.Abbr) { + } else if strings.HasPrefix(command, def.Abbr+" ") { match = def break } diff --git a/matcher/matcher_test.go b/matcher/matcher_test.go index 6b5244e..45adfcb 100644 --- a/matcher/matcher_test.go +++ b/matcher/matcher_test.go @@ -14,6 +14,7 @@ var mockAliasDefs = []model.AliasDef{ {Name: "gcb", Abbr: "git checkout -b"}, {Name: "ls", Abbr: "ls -G"}, {Name: "ll", Abbr: "ls -lh"}, + {Name: "yst", Abbr: "yarn start"}, } func TestMatch_NoMatches(t *testing.T) { @@ -21,6 +22,11 @@ func TestMatch_NoMatches(t *testing.T) { assert.Equal(t, aliasTips, "cd ..") } +func TestMatch_NoMatches2(t *testing.T) { + aliasTips := Match(mockAliasDefs, "yarn start :ja") + assert.Equal(t, aliasTips, "yarn start:ja") +} + func TestMatch_SingleToken(t *testing.T) { aliasTips := Match(mockAliasDefs, "docker") assert.Equal(t, aliasTips, "dk")