Skip to content

Commit

Permalink
Merge pull request #1216 from rsteube/helix-dynamic-languages
Browse files Browse the repository at this point in the history
helix: complete languages dynamically
  • Loading branch information
rsteube authored Jul 2, 2022
2 parents bdc145c + f253060 commit a10cb20
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 106 deletions.
104 changes: 0 additions & 104 deletions completers/helix_completer/cmd/action/language.go

This file was deleted.

4 changes: 2 additions & 2 deletions completers/helix_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/helix_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/helix"
"github.com/spf13/cobra"
)

Expand All @@ -28,7 +28,7 @@ func init() {

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"grammar": carapace.ActionValues("fetch", "build"),
"health": action.ActionLanguages(),
"health": helix.ActionLanguages(),
})

carapace.Gen(rootCmd).PositionalAnyCompletion(
Expand Down
33 changes: 33 additions & 0 deletions pkg/actions/tools/helix/language.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package helix

import (
"regexp"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
)

// ActionLanguages completes languages
// bash (✘ bash-language-server)
// c (✔ clangd)
func ActionLanguages() carapace.Action {
return carapace.ActionExecCommand("helix", "--health")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
r := regexp.MustCompile(`^\x1b\[39m(?P<language>[^ ]+) +\x1b\[[\d;]+m\x1b\[[\d;]+m(?P<lsp>(✘ |✔ )?[^ ]+)`)

vals := make([]string, 0)
for _, line := range lines {
if match := r.FindStringSubmatch(line); match != nil {
if lsp := match[2]; strings.HasPrefix(lsp, "✘") {
vals = append(vals, match[1], lsp, style.Red)
} else if strings.HasPrefix(lsp, "✔") {
vals = append(vals, match[1], lsp, style.Green)
} else {
vals = append(vals, match[1], lsp, style.Yellow)
}
}
}
return carapace.ActionStyledValuesDescribed(vals...)
})
}

0 comments on commit a10cb20

Please sign in to comment.