diff --git a/cmd/templ/lspcmd/lsp_test.go b/cmd/templ/lspcmd/lsp_test.go index 0803183d3..c0538c70f 100644 --- a/cmd/templ/lspcmd/lsp_test.go +++ b/cmd/templ/lspcmd/lsp_test.go @@ -88,13 +88,14 @@ func TestCompletion(t *testing.T) { Text: string(templFile), }, }) - if err != nil { t.Errorf("failed to register open file: %v", err) return } log.Info("Calling completion") + globalSnippetsLen := 1 + // Edit the file. // Replace: //
{ fmt.Sprintf("%d", count) }
@@ -111,8 +112,8 @@ func TestCompletion(t *testing.T) { replacement: `
{ `, cursor: ` ^`, assert: func(t *testing.T, actual *protocol.CompletionList) (msg string, ok bool) { - if diff := lspdiff.CompletionList(nil, actual); diff != "" { - return fmt.Sprintf("unexpected completion: %v", diff), false + if actual != nil && len(actual.Items) != globalSnippetsLen { + return "expected completion list to be empty", false } return "", true }, @@ -133,7 +134,7 @@ func TestCompletion(t *testing.T) { replacement: `
{ fmt.Sprintf("%d",`, cursor: ` ^`, assert: func(t *testing.T, actual *protocol.CompletionList) (msg string, ok bool) { - if actual != nil && len(actual.Items) != 0 { + if actual != nil && len(actual.Items) != globalSnippetsLen { return "expected completion list to be empty", false } return "", true @@ -388,27 +389,42 @@ func (tc TestClient) Progress(ctx context.Context, params *protocol.ProgressPara return nil } -func (tc TestClient) WorkDoneProgressCreate(ctx context.Context, params *protocol.WorkDoneProgressCreateParams) (err error) { +func (tc TestClient) WorkDoneProgressCreate( + ctx context.Context, + params *protocol.WorkDoneProgressCreateParams, +) (err error) { tc.log.Info("client: Received WorkDoneProgressCreate", zap.Any("params", params)) return nil } -func (tc TestClient) LogMessage(ctx context.Context, params *protocol.LogMessageParams) (err error) { +func (tc TestClient) LogMessage( + ctx context.Context, + params *protocol.LogMessageParams, +) (err error) { tc.log.Info("client: Received LogMessage", zap.Any("params", params)) return nil } -func (tc TestClient) PublishDiagnostics(ctx context.Context, params *protocol.PublishDiagnosticsParams) (err error) { +func (tc TestClient) PublishDiagnostics( + ctx context.Context, + params *protocol.PublishDiagnosticsParams, +) (err error) { tc.log.Info("client: Received PublishDiagnostics", zap.Any("params", params)) return nil } -func (tc TestClient) ShowMessage(ctx context.Context, params *protocol.ShowMessageParams) (err error) { +func (tc TestClient) ShowMessage( + ctx context.Context, + params *protocol.ShowMessageParams, +) (err error) { tc.log.Info("client: Received ShowMessage", zap.Any("params", params)) return nil } -func (tc TestClient) ShowMessageRequest(ctx context.Context, params *protocol.ShowMessageRequestParams) (result *protocol.MessageActionItem, err error) { +func (tc TestClient) ShowMessageRequest( + ctx context.Context, + params *protocol.ShowMessageRequestParams, +) (result *protocol.MessageActionItem, err error) { return nil, nil } @@ -417,44 +433,70 @@ func (tc TestClient) Telemetry(ctx context.Context, params interface{}) (err err return nil } -func (tc TestClient) RegisterCapability(ctx context.Context, params *protocol.RegistrationParams) (err error) { +func (tc TestClient) RegisterCapability( + ctx context.Context, + params *protocol.RegistrationParams, +) (err error) { tc.log.Info("client: Received RegisterCapability", zap.Any("params", params)) return nil } -func (tc TestClient) UnregisterCapability(ctx context.Context, params *protocol.UnregistrationParams) (err error) { +func (tc TestClient) UnregisterCapability( + ctx context.Context, + params *protocol.UnregistrationParams, +) (err error) { tc.log.Info("client: Received UnregisterCapability", zap.Any("params", params)) return nil } -func (tc TestClient) ApplyEdit(ctx context.Context, params *protocol.ApplyWorkspaceEditParams) (result *protocol.ApplyWorkspaceEditResponse, err error) { +func (tc TestClient) ApplyEdit( + ctx context.Context, + params *protocol.ApplyWorkspaceEditParams, +) (result *protocol.ApplyWorkspaceEditResponse, err error) { tc.log.Info("client: Received ApplyEdit", zap.Any("params", params)) return nil, nil } -func (tc TestClient) Configuration(ctx context.Context, params *protocol.ConfigurationParams) (result []interface{}, err error) { +func (tc TestClient) Configuration( + ctx context.Context, + params *protocol.ConfigurationParams, +) (result []interface{}, err error) { tc.log.Info("client: Received Configuration", zap.Any("params", params)) return nil, nil } -func (tc TestClient) WorkspaceFolders(ctx context.Context) (result []protocol.WorkspaceFolder, err error) { +func (tc TestClient) WorkspaceFolders( + ctx context.Context, +) (result []protocol.WorkspaceFolder, err error) { tc.log.Info("client: Received WorkspaceFolders") return nil, nil } -func Setup(ctx context.Context, log *zap.Logger) (clientCtx context.Context, appDir string, client protocol.Client, server protocol.Server, teardown func(t *testing.T), err error) { +func Setup( + ctx context.Context, + log *zap.Logger, +) (clientCtx context.Context, appDir string, client protocol.Client, server protocol.Server, teardown func(t *testing.T), err error) { wd, err := os.Getwd() if err != nil { - return ctx, appDir, client, server, teardown, fmt.Errorf("could not find working dir: %w", err) + return ctx, appDir, client, server, teardown, fmt.Errorf( + "could not find working dir: %w", + err, + ) } moduleRoot, err := modcheck.WalkUp(wd) if err != nil { - return ctx, appDir, client, server, teardown, fmt.Errorf("could not find local templ go.mod file: %v", err) + return ctx, appDir, client, server, teardown, fmt.Errorf( + "could not find local templ go.mod file: %v", + err, + ) } appDir, err = createTestProject(moduleRoot) if err != nil { - return ctx, appDir, client, server, teardown, fmt.Errorf("failed to create test project: %v", err) + return ctx, appDir, client, server, teardown, fmt.Errorf( + "failed to create test project: %v", + err, + ) } var wg sync.WaitGroup @@ -549,13 +591,19 @@ func Setup(ctx context.Context, log *zap.Logger) (clientCtx context.Context, app log.Error("Failed to init", zap.Error(err)) } if ir.ServerInfo.Name != "templ-lsp" { - return ctx, appDir, client, server, teardown, fmt.Errorf("expected server name to be templ-lsp, got %q", ir.ServerInfo.Name) + return ctx, appDir, client, server, teardown, fmt.Errorf( + "expected server name to be templ-lsp, got %q", + ir.ServerInfo.Name, + ) } // Confirm initialization. log.Info("Confirming initialization...") if err = server.Initialized(ctx, &protocol.InitializedParams{}); err != nil { - return ctx, appDir, client, server, teardown, fmt.Errorf("failed to confirm initialization: %v", err) + return ctx, appDir, client, server, teardown, fmt.Errorf( + "failed to confirm initialization: %v", + err, + ) } log.Info("Initialized") diff --git a/cmd/templ/lspcmd/proxy/server.go b/cmd/templ/lspcmd/proxy/server.go index f993e2a07..ccc69684c 100644 --- a/cmd/templ/lspcmd/proxy/server.go +++ b/cmd/templ/lspcmd/proxy/server.go @@ -410,6 +410,9 @@ func (p *Server) Completion(ctx context.Context, params *lsp.CompletionParams) ( } result.Items[i] = item } + + //Add snippet for templ + result.Items = append(result.Items, snippet...) return } diff --git a/cmd/templ/lspcmd/proxy/snippets.go b/cmd/templ/lspcmd/proxy/snippets.go index 22ada7265..d1313a333 100644 --- a/cmd/templ/lspcmd/proxy/snippets.go +++ b/cmd/templ/lspcmd/proxy/snippets.go @@ -98,3 +98,14 @@ var htmlSnippets = []lsp.CompletionItem{ InsertTextFormat: lsp.InsertTextFormatSnippet, }, } + +var snippet = []lsp.CompletionItem{ + { + Label: "templ", + InsertText: `templ ${2:TemplateName}() { + ${0} +}`, + Kind: lsp.CompletionItemKind(lsp.CompletionItemKindSnippet), + InsertTextFormat: lsp.InsertTextFormatSnippet, + }, +}