Skip to content

Commit

Permalink
feat(completions): complete template blocks in text node
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Dec 25, 2023
1 parent efbd123 commit 6e128ed
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 6 deletions.
34 changes: 34 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,37 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


Part of the documentation that is included in helm-ls is copied from
the Go standard library. So the following license applies:

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright

notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above

copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
- Neither the name of Google Inc. nor the names of its

contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
lsp "go.lsp.dev/protocol"
)

func (yamllsConnector Connector) CallCompletion(params lsp.CompletionParams) *lsp.CompletionList {
func (yamllsConnector Connector) CallCompletion(ctx context.Context, params lsp.CompletionParams) *lsp.CompletionList {
if yamllsConnector.Conn == nil {
return &lsp.CompletionList{}
}

logger.Println("Calling yamlls for completions")
var response = reflect.New(reflect.TypeOf(lsp.CompletionList{})).Interface()
_, err := (*yamllsConnector.Conn).Call(context.Background(), lsp.MethodTextDocumentCompletion, params, response)
_, err := (*yamllsConnector.Conn).Call(ctx, lsp.MethodTextDocumentCompletion, params, response)
if err != nil {
logger.Error("Error Calling yamlls for completions", err)
return &lsp.CompletionList{}
Expand Down
35 changes: 31 additions & 4 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
var (
emptyItems = make([]lsp.CompletionItem, 0)
functionsCompletionItems = make([]lsp.CompletionItem, 0)
textCompletionsItems = make([]lsp.CompletionItem, 0)
)

func init() {
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(helmFuncs)...)
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(builtinFuncs)...)
functionsCompletionItems = append(functionsCompletionItems, getFunctionCompletionItems(sprigFuncs)...)
textCompletionsItems = append(textCompletionsItems, getTextCompletionItems(textSnippets)...)
}

func (h *langHandler) handleTextDocumentCompletion(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) (err error) {
Expand All @@ -48,7 +50,10 @@ func (h *langHandler) handleTextDocumentCompletion(ctx context.Context, reply js
word, isTextNode := completionAstParsing(doc, params.Position)

if isTextNode {
return yamllsCompletions(ctx, err, h, params, reply)
var result = make([]lsp.CompletionItem, 0)
result = append(result, textCompletionsItems...)
result = append(result, yamllsCompletions(ctx, h, params)...)
return reply(ctx, result, err)
}

var (
Expand Down Expand Up @@ -97,10 +102,10 @@ func (h *langHandler) handleTextDocumentCompletion(ctx context.Context, reply js
return reply(ctx, items, err)
}

func yamllsCompletions(ctx context.Context, err error, h *langHandler, params lsp.CompletionParams, reply jsonrpc2.Replier) error {
response := *h.yamllsConnector.CallCompletion(params)
func yamllsCompletions(ctx context.Context, h *langHandler, params lsp.CompletionParams) []lsp.CompletionItem {
response := *h.yamllsConnector.CallCompletion(ctx, params)
logger.Debug("Got completions from yamlls", response)
return reply(ctx, response, err)
return response.Items
}

func completionAstParsing(doc *lsplocal.Document, position lsp.Position) (string, bool) {
Expand Down Expand Up @@ -241,6 +246,13 @@ func getFunctionCompletionItems(helmDocs []HelmDocumentation) (result []lsp.Comp
return result
}

func getTextCompletionItems(helmSnippet []HelmSnippet) (result []lsp.CompletionItem) {
for _, item := range helmSnippet {
result = append(result, textCompletionItem(item))
}
return result
}

func functionCompletionItem(helmDocumentation HelmDocumentation) lsp.CompletionItem {
return lsp.CompletionItem{
Label: helmDocumentation.Name,
Expand All @@ -250,3 +262,18 @@ func functionCompletionItem(helmDocumentation HelmDocumentation) lsp.CompletionI
Kind: lsp.CompletionItemKindFunction,
}
}

func textCompletionItem(helmSnippet HelmSnippet) lsp.CompletionItem {
return lsp.CompletionItem{
Label: helmSnippet.Name,
TextEdit: &lsp.TextEdit{
Range: lsp.Range{},
NewText: helmSnippet.Snippet,
},
Detail: helmSnippet.Detail,
Documentation: helmSnippet.Doc,
Kind: lsp.CompletionItemKindText,
InsertTextFormat: lsp.InsertTextFormatSnippet,
FilterText: helmSnippet.Filter,
}
}
107 changes: 107 additions & 0 deletions internal/handler/helm_snippet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// The content of the Snippet was taken from https://pkg.go.dev/text/template
// So the following license applies to them:

// Copyright (c) 2009 The Go Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// - Redistributions of source code must retain the above copyright
//
// notice, this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above
//
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// - Neither the name of Google Inc. nor the names of its
//
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package handler

type HelmSnippet struct {
Name string
Detail string
Doc string
Snippet string
Filter string
}

var (
textSnippets = []HelmSnippet{
{
Name: "comment",
Detail: "{{- /* a comment with white space trimmed from preceding and following text */ -}}",
Doc: "A comment; discarded. May contain newlines. Comments do not nest and must start and end at the delimiters, as shown here.",
Snippet: "{{- /* $1 */ -}}",
},
{
Name: "{{ }}",
Detail: "template",
Doc: "",
Snippet: "{{- $0 }}",
Filter: "{}", // TODO: is this useful?
},
{
Name: "if",
Detail: "{{if pipeline}} T1 {{end}}",
Doc: "If the value of the pipeline is empty, no output is generated; otherwise, T1 is executed. The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero. Dot is unaffected.",
Snippet: "{{- if $1 }}\n $0 \n{{- end }}",
},
{
Name: "if else",
Detail: "{{if pipeline}} T1 {{else}} T0 {{end}}",
Doc: "If the value of the pipeline is empty, T0 is executed; otherwise, T1 is executed. Dot is unaffected.",
Snippet: "{{- if $1 }}\n $2 \n{{- else }}\n $0 \n{{- end }}",
},
{
Name: "if else if",
Detail: "{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}",
Doc: "To simplify the appearance of if-else chains, the else action of an if may include another if directly; the effect is exactly the same as writing {{if pipeline}} T1 {{else}}{{if pipeline}} T0 {{end}}{{end}}",
Snippet: "{{- if $1 }}\n $2 \n{{- else if $4 }}\n $0 \n{{- end }}",
},
{
Name: "range",
Detail: "{{range pipeline}} T1 {{end}}",
Doc: "The value of the pipeline must be an array, slice, map, or channel. If the value of the pipeline has length zero, nothing is output; otherwise, dot is set to the successive elements of the array, slice, or map and T1 is executed. If the value is a map and the keys are of basic type with a defined order, the elements will be visited in sorted key order.",
Snippet: "{{- range $1 }}\n $0 \n{{- end }}",
},
{
Name: "range else",
Detail: "{{range pipeline}} T1 {{else}} T0 {{end}}",
Doc: "The value of the pipeline must be an array, slice, map, or channel. If the value of the pipeline has length zero, dot is unaffected and T0 is executed; otherwise, dot is set to the successive elements of the array, slice, or map and T1 is executed.",
Snippet: "{{- range $1 }}\n $2 {{- else }}\n $0 \n{{- end }}",
},
{
Name: "block",
Detail: "{{block \"name\" pipeline}} T1 {{end}}",
Doc: "A block is shorthand for defining a template {{define \"name\"}} T1 {{end}} and then executing it in place {{template \"name\" pipeline}} The typical use is to define a set of root templates that are then customized by redefining the block templates within.",
Snippet: "{{- block $1 }}\n $0 \n{{- end }}",
},
{
Name: "with",
Detail: "{{with pipeline}} T1 {{end}}",
Doc: "If the value of the pipeline is empty, no output is generated; otherwise, dot is set to the value of the pipeline and T1 is executed.",
Snippet: "{{- with $1 }}\n $0 \n{{- end }}",
},
{
Name: "with else",
Detail: "{{with pipeline}} T1 {{else}} T0 {{end}}",
Doc: "If the value of the pipeline is empty, dot is unaffected and T0 is executed; otherwise, dot is set to the value of the pipeline and T1 is executed",
Snippet: "{{- with $1 }}\n $2 {{- else }}\n $0 \n{{- end }}",
},
}
)

0 comments on commit 6e128ed

Please sign in to comment.