Skip to content

Commit

Permalink
refactor(lsp): join help and completion (#1776)
Browse files Browse the repository at this point in the history
This should make it easier to add new completions.
  • Loading branch information
gak authored Jun 13, 2024
1 parent 63fb100 commit 6373a2e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 54 deletions.
75 changes: 21 additions & 54 deletions lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,35 @@ var enumTypeCompletionDocs string
//go:embed markdown/completion/enumValue.md
var enumValueCompletionDocs string

var (
snippetKind = protocol.CompletionItemKindSnippet
insertTextFormat = protocol.InsertTextFormatSnippet

verbCompletionItem = protocol.CompletionItem{
Label: "ftl:verb",
Kind: &snippetKind,
Detail: stringPtr("FTL Verb"),
InsertText: stringPtr(`type ${1:Request} struct {}
type ${2:Response} struct{}
//ftl:verb
func ${3:Name}(ctx context.Context, req ${1:Request}) (${2:Response}, error) {
return ${2:Response}{}, nil
}`),
Documentation: &protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: verbCompletionDocs,
},
InsertTextFormat: &insertTextFormat,
}
var completionItems = []protocol.CompletionItem{
completionItem("ftl:verb", "FTL Verb", verbCompletionDocs),
completionItem("ftl:enum (sum type)", "FTL Enum (sum type)", enumTypeCompletionDocs),
completionItem("ftl:enum (value)", "FTL Enum (value type)", enumValueCompletionDocs),
}

enumTypeCompletionItem = protocol.CompletionItem{
Label: "ftl:enum (sum type)",
Kind: &snippetKind,
Detail: stringPtr("FTL Enum (sum type)"),
InsertText: stringPtr(`//ftl:enum
type ${1:Enum} string
const (
${2:Value1} ${1:Enum} = "${2:Value1}"
${3:Value2} ${1:Enum} = "${3:Value2}"
)`),
Documentation: &protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: enumTypeCompletionDocs,
},
InsertTextFormat: &insertTextFormat,
}
func completionItem(label, detail, markdown string) protocol.CompletionItem {
snippetKind := protocol.CompletionItemKindSnippet
insertTextFormat := protocol.InsertTextFormatSnippet

enumValueCompletionItem = protocol.CompletionItem{
Label: "ftl:enum (value)",
Kind: &snippetKind,
Detail: stringPtr("FTL enum (value type)"),
InsertText: stringPtr(`//ftl:enum
type ${1:Type} interface { ${2:interface}() }
// Split markdown by "---"
// First half is completion docs, second half is insert text
parts := strings.Split(markdown, "---")
if len(parts) != 2 {
panic("invalid markdown. must contain exactly one '---' to separate completion docs from insert text")
}

type ${3:Value} struct {}
func (${3:Value}) ${2:interface}() {}
`),
insertText := strings.TrimSpace(parts[1])
return protocol.CompletionItem{
Label: label,
Kind: &snippetKind,
Detail: &detail,
InsertText: &insertText,
Documentation: &protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: enumValueCompletionDocs,
Value: strings.TrimSpace(parts[0]),
},
InsertTextFormat: &insertTextFormat,
}
)

var completionItems = []protocol.CompletionItem{
verbCompletionItem,
enumTypeCompletionItem,
enumValueCompletionItem,
}

func (s *Server) textDocumentCompletion() protocol.TextDocumentCompletionFunc {
Expand Down
8 changes: 8 additions & 0 deletions lsp/markdown/completion/enumType.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ const (
Value2 MyEnum = "Value2"
)
```
---
//ftl:enum
type ${1:Enum} string

const (
${2:Value1} ${1:Enum} = "${2:Value1}"
${3:Value2} ${1:Enum} = "${3:Value2}"
)
6 changes: 6 additions & 0 deletions lsp/markdown/completion/enumValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ type Animal interface { animal() }
type Cat struct {}
func (Cat) animal() {}
```
---
//ftl:enum
type ${1:Type} interface { ${2:interface}() }

type ${3:Value} struct {}
func (${3:Value}) ${2:interface}() {}
8 changes: 8 additions & 0 deletions lsp/markdown/completion/verb.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ Snippet for defining a verb function.
//ftl:verb
func Name(ctx context.Context, req Request) (Response, error) {}
```
---
type ${1:Request} struct {}
type ${2:Response} struct {}

//ftl:verb
func ${3:Name}(ctx context.Context, req ${1:Request}) (${2:Response}, error) {
return ${2:Response}{}, nil
}

0 comments on commit 6373a2e

Please sign in to comment.