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

Fix command matching #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 1 addition & 8 deletions def-matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
16 changes: 3 additions & 13 deletions matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,29 @@ 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) {
} else if strings.HasPrefix(command, def.Abbr+" ") {
match = def
break
}
}

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
}
61 changes: 27 additions & 34 deletions matcher/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,47 @@ 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"},
{Name: "yst", Abbr: "yarn start"},
}

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_NoMatches2(t *testing.T) {
aliasTips := Match(mockAliasDefs, "yarn start :ja")
assert.Equal(t, aliasTips, "yarn start:ja")
}

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")
}