From 6373a2edc160547fb93588c573a531d6b3f4e70d Mon Sep 17 00:00:00 2001 From: gak Date: Fri, 14 Jun 2024 09:33:24 +1000 Subject: [PATCH] refactor(lsp): join help and completion (#1776) This should make it easier to add new completions. --- lsp/completion.go | 75 ++++++++-------------------- lsp/markdown/completion/enumType.md | 8 +++ lsp/markdown/completion/enumValue.md | 6 +++ lsp/markdown/completion/verb.md | 8 +++ 4 files changed, 43 insertions(+), 54 deletions(-) diff --git a/lsp/completion.go b/lsp/completion.go index 08a138e0e8..5f18195669 100644 --- a/lsp/completion.go +++ b/lsp/completion.go @@ -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 { diff --git a/lsp/markdown/completion/enumType.md b/lsp/markdown/completion/enumType.md index 9a733298d0..3a9b83b7df 100644 --- a/lsp/markdown/completion/enumType.md +++ b/lsp/markdown/completion/enumType.md @@ -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}" +) diff --git a/lsp/markdown/completion/enumValue.md b/lsp/markdown/completion/enumValue.md index f8684eed17..3566d1fa31 100644 --- a/lsp/markdown/completion/enumValue.md +++ b/lsp/markdown/completion/enumValue.md @@ -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}() {} diff --git a/lsp/markdown/completion/verb.md b/lsp/markdown/completion/verb.md index 68fc2f8e66..a16bbc5d1b 100644 --- a/lsp/markdown/completion/verb.md +++ b/lsp/markdown/completion/verb.md @@ -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 +} \ No newline at end of file