Skip to content

Commit

Permalink
feat: templ component snippet (#714)
Browse files Browse the repository at this point in the history
Co-authored-by: joerdav <joe.davidson.21111@gmail.com>
  • Loading branch information
valentimarco and joerdav authored May 8, 2024
1 parent 27fc6a3 commit fd2c772
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
88 changes: 68 additions & 20 deletions cmd/templ/lspcmd/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// <div data-testid="count">{ fmt.Sprintf("%d", count) }</div>
Expand All @@ -111,8 +112,8 @@ func TestCompletion(t *testing.T) {
replacement: ` <div data-testid="count">{ `,
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
},
Expand All @@ -133,7 +134,7 @@ func TestCompletion(t *testing.T) {
replacement: ` <div data-testid="count">{ 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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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")

Expand Down
3 changes: 3 additions & 0 deletions cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 11 additions & 0 deletions cmd/templ/lspcmd/proxy/snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

0 comments on commit fd2c772

Please sign in to comment.