Skip to content

Commit

Permalink
template_context hover usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Apr 13, 2024
1 parent 6f8e894 commit 2c253fc
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package helmdocs

type HelmDocumentation struct {
Name string
Expand All @@ -7,9 +7,10 @@ type HelmDocumentation struct {
}

var (
basicItems = []HelmDocumentation{
BuiltInObjects = []HelmDocumentation{
{"Values", ".Values", `The values made available through values.yaml, --set and -f.`},
{"Chart", ".Chart", "Chart metadata"},
{"Subcharts", ".Subcharts", "This provides access to the scope (.Values, .Charts, .Releases etc.) of subcharts to the parent. For example .Subcharts.mySubChart.myValue to access the myValue in the mySubChart chart."},
{"Files", ".Files.Get $str", "access non-template files within the chart"},
{"Capabilities", ".Capabilities.KubeVersion ", "access capabilities of Kubernetes"},
{"Release", ".Release", `Built-in release values. Attributes include:
Expand All @@ -21,8 +22,9 @@ var (
- .Release.IsInstall: True if this is an install
- .Release.Revision: The revision number
`},
{"Template", ".Template", "Contains information about the current template that is being executed"},
}
builtinFuncs = []HelmDocumentation{
BuiltinFuncs = []HelmDocumentation{
{"template", "template $str $ctx", "render the template at location $str"},
{"define", "define $str", "define a template with the name $str"},
{"and", "and $a $b ...", "if $a then $b else $a"},
Expand All @@ -44,7 +46,7 @@ var (
{"le", "le $a $b", "returns true if $a <= $b"},
{"ge", "ge $a $b", "returns true if $a >= $b"},
}
sprigFuncs = []HelmDocumentation{
SprigFuncs = []HelmDocumentation{
// 2.12.0
{"snakecase", "snakecase $str", "Convert $str to snake_case"},
{"camelcase", "camelcase $str", "convert string to camelCase"},
Expand Down Expand Up @@ -173,7 +175,7 @@ var (
{"derivePassword", "derivePassword $counter $long $pass $user $domain", "generate a password from [Master Password](http://masterpasswordapp.com/algorithm.html) spec"},
{"generatePrivateKey", "generatePrivateKey 'ecdsa'", "generate private PEM key (takes dsa, rsa, or ecdsa)"},
}
helmFuncs = []HelmDocumentation{
HelmFuncs = []HelmDocumentation{
{"include", "include $str $ctx", "(chainable) include the named template with the given context."},
{"toYaml", "toYaml $var", "convert $var to YAML"},
{"toJson", "toJson $var", "convert $var to JSON"},
Expand All @@ -183,13 +185,22 @@ var (
{"required", "required $str $val", "fail template with message $str if $val is not provided or is empty"},
}

capabilitiesVals = []HelmDocumentation{
{"KubeVersion", ".Capabilities.KubeVersion", "Kubernetes version"},
CapabilitiesVals = []HelmDocumentation{
{"TillerVersion", ".Capabilities.TillerVersion", "Tiller version"},
{"ApiVersions.Has", `.Capabilities.ApiVersions.Has "batch/v1"`, "Returns true if the given Kubernetes API/version is present on the cluster"},
{"APIVersions", "Capabilities.APIVersions", "A set of versions."},
{"APIVersions.Has", "Capabilities.APIVersions.Has $version", "Indicates whether a version (e.g., batch/v1) or resource (e.g., apps/v1/Deployment) is available on the cluster."},
{"KubeVersion", "Capabilities.KubeVersion", "The Kubernetes version."},
{"KubeVersion.Version", "Capabilities.KubeVersion.Version", "The Kubernetes version in semver format."},
{"KubeVersion.Major", "Capabilities.KubeVersion.Major", "The Kubernetes major version."},
{"KubeVersion.Minor", "Capabilities.KubeVersion.Minor", "The Kubernetes minor version."},
{"KubeVersion.GitCommit", "Capabilities.HelmVersion", "The object containing the Helm Version details, it is the same output of helm version."},
{"KubeVersion.GitTreeState", "Capabilities.HelmVersion.Version", "The current Helm version in semver format."},
{"HelmVersion.GitCommit", "Capabilities.HelmVersion.GitCommit", "The Helm git sha1."},
{"HelmVersion.GitTreeState", "Capabilities.HelmVersion.GitTreeState", "The state of the Helm git tree."},
{"HelmVersion.GoVersion", "Capabilities.HelmVersion.GoVersion", "The version of the Go compiler used."},
}

chartVals = []HelmDocumentation{
ChartVals = []HelmDocumentation{
{"Name", ".Chart.Name", "Name of the chart"},
{"Version", ".Chart.Version", "Version of the chart"},
{"Description", ".Chart.Description", "Chart description"},
Expand All @@ -201,9 +212,21 @@ var (
{"AppVersion", ".Chart.AppVersion", "The version of the main app contained in this chart"},
{"Deprecated", ".Chart.Deprecated", "If true, this chart is no longer maintained"},
{"TillerVersion", ".Chart.TillerVersion", "The version (range) if Tiller that this chart can run on."},
{"APIVersion", ".Chart.APIVersion", "The API Version of this chart"},
{"Condition", ".Chart.Condition", "The condition to check to enable chart"},
{"Tags", ".Chart.Tags", "The tags to check to enable chart"},
{"Annotations", ".Chart.Annotations", "Additional annotations (key-value pairs)"},
{"KubeVersion", ".Chart.KubeVersion", "Kubernetes version required"},
{"Dependencies", ".Chart.Dependencies", "List of chart dependencies"},
{"Type", ".Chart.Type", "Chart type (application or library)"},
}

releaseVals = []HelmDocumentation{
TemplateVals = []HelmDocumentation{
{"Name", ".Template.Name", "A namespaced file path to the current template (e.g. mychart/templates/mytemplate.yaml)"},
{"BasePath", ".Template.BasePath", "The namespaced path to the templates directory of the current chart (e.g. mychart/templates)."},
}

ReleaseVals = []HelmDocumentation{
{"Name", ".Release.Name", "Name of the release"},
{"Time", ".Release.Time", "Time of the release"},
{"Namespace", ".Release.Namespace", "Default namespace of the release"},
Expand All @@ -213,12 +236,20 @@ var (
{"Revision", ".Release.Revision", "Release revision number (starts at 1)"},
}

filesVals = []HelmDocumentation{
FilesVals = []HelmDocumentation{
{"Get", ".Files.Get $path", "Get file contents. Path is relative to chart."},
{"GetBytes", ".Files.GetBytes $path", "Get file contents as a byte array. Path is relative to chart."},
{"Glob", ".Files.Glob $glob", "Returns a list of files whose names match the given shell glob pattern."},
{"Lines", ".Files.Lines $path", "Reads a file line-by-line. This is useful for iterating over each line in a file."},
{"AsSecrets", ".Files.AsSecrets $path", "Returns the file bodies as Base 64 encoded strings."},
{"AsConfig", ".Files.AsConfig $path", "Returns file bodies as a YAML map."},
}

BuiltInOjectVals = map[string][]HelmDocumentation{
"Chart": ChartVals,
"Release": ReleaseVals,
"Files": FilesVals,
"Capabilities": CapabilitiesVals,
"Template": TemplateVals,
}
)
37 changes: 16 additions & 21 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/mrjosh/helm-ls/internal/charts"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
gotemplate "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/mrjosh/helm-ls/pkg/chartutil"
sitter "github.com/smacker/go-tree-sitter"
"go.lsp.dev/protocol"
lsp "go.lsp.dev/protocol"
yaml "gopkg.in/yaml.v2"

"github.com/mrjosh/helm-ls/internal/documentation/godocs"
helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm"
)

var (
Expand All @@ -26,9 +28,9 @@ var (
)

func init() {
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(helmFuncs)...)
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(builtinFuncs)...)
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(sprigFuncs)...)
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(helmdocs.HelmFuncs)...)
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(helmdocs.BuiltinFuncs)...)
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(helmdocs.SprigFuncs)...)
textCompletionsItems = append(textCompletionsItems, getTextCompletionItems(godocs.TextSnippets)...)
}

Expand Down Expand Up @@ -69,7 +71,7 @@ func (h *langHandler) Completion(ctx context.Context, params *lsp.CompletionPara
logger.Println(fmt.Sprintf("Word found for completions is < %s >", word))

items = make([]lsp.CompletionItem, 0)
for _, v := range basicItems {
for _, v := range helmdocs.BuiltInObjects {
items = append(items, lsp.CompletionItem{
Label: v.Name,
InsertText: v.Name,
Expand All @@ -89,17 +91,17 @@ func (h *langHandler) Completion(ctx context.Context, params *lsp.CompletionPara

switch variableSplitted[0] {
case "Chart":
items = getVariableCompletionItems(chartVals)
items = getVariableCompletionItems(helmdocs.ChartVals)
case "Values":
items = h.getValuesCompletions(chart, variableSplitted[1:])
case "Release":
items = getVariableCompletionItems(releaseVals)
items = getVariableCompletionItems(helmdocs.ReleaseVals)
case "Files":
items = getVariableCompletionItems(filesVals)
items = getVariableCompletionItems(helmdocs.FilesVals)
case "Capabilities":
items = getVariableCompletionItems(capabilitiesVals)
items = getVariableCompletionItems(helmdocs.CapabilitiesVals)
default:
items = getVariableCompletionItems(basicItems)
items = getVariableCompletionItems(helmdocs.BuiltInObjects)
items = append(items, functionsCompletionItems...)
}

Expand Down Expand Up @@ -214,7 +216,7 @@ func (h *langHandler) setItem(items []lsp.CompletionItem, value interface{}, var
documentation = h.toYAML(value)
case reflect.Bool:
itemKind = lsp.CompletionItemKindVariable
documentation = h.getBoolType(value)
documentation = util.GetBoolType(value)
case reflect.Float32, reflect.Float64:
documentation = fmt.Sprintf("%.2f", valueOf.Float())
itemKind = lsp.CompletionItemKindVariable
Expand All @@ -238,21 +240,14 @@ func (h *langHandler) toYAML(value interface{}) string {
return string(valBytes)
}

func (h *langHandler) getBoolType(value interface{}) string {
if val, ok := value.(bool); ok && val {
return "True"
}
return "False"
}

func getVariableCompletionItems(helmDocs []HelmDocumentation) (result []lsp.CompletionItem) {
func getVariableCompletionItems(helmDocs []helmdocs.HelmDocumentation) (result []lsp.CompletionItem) {
for _, item := range helmDocs {
result = append(result, variableCompletionItem(item))
}
return result
}

func variableCompletionItem(helmDocumentation HelmDocumentation) lsp.CompletionItem {
func variableCompletionItem(helmDocumentation helmdocs.HelmDocumentation) lsp.CompletionItem {
return lsp.CompletionItem{
Label: helmDocumentation.Name,
InsertText: helmDocumentation.Name,
Expand All @@ -262,14 +257,14 @@ func variableCompletionItem(helmDocumentation HelmDocumentation) lsp.CompletionI
}
}

func getFunctionCompletionItems(helmDocs []HelmDocumentation) (result []lsp.CompletionItem) {
func getFunctionCompletionItems(helmDocs []helmdocs.HelmDocumentation) (result []lsp.CompletionItem) {
for _, item := range helmDocs {
result = append(result, functionCompletionItem(item))
}
return result
}

func functionCompletionItem(helmDocumentation HelmDocumentation) lsp.CompletionItem {
func functionCompletionItem(helmDocumentation helmdocs.HelmDocumentation) lsp.CompletionItem {
return lsp.CompletionItem{
Label: helmDocumentation.Name,
InsertText: helmDocumentation.Name,
Expand Down
Loading

0 comments on commit 2c253fc

Please sign in to comment.