Skip to content

Commit

Permalink
lsp: provide whole document formatting, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed May 29, 2021
1 parent ba44064 commit 8905c85
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions cmd/lspcmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func (p *Proxy) Handle(ctx context.Context, conn *jsonrpc2.Conn, r *jsonrpc2.Req
case "textDocument/completion":
p.proxyCompletion(ctx, conn, r)
return
case "textDocument/formatting":
p.handleFormatting(ctx, conn, r)
return
default:
p.proxyCall(ctx, conn, r)
return
Expand Down Expand Up @@ -277,6 +280,52 @@ func (p *Proxy) rewriteCompletionRequest(params *lsp.CompletionParams) (err erro
return err
}

func (p *Proxy) handleFormatting(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
// Unmarshal the params.
var params lsp.DocumentFormattingParams
err := json.Unmarshal(*req.Params, &params)
if err != nil {
p.log.Error("handleFormatting: failed to unmarshal request params", zap.Error(err))
}
var resp []lsp.TextEdit
defer func() {
// Reply to the client, even if there's been a failure.
err = conn.Reply(ctx, req.ID, &resp)
if err != nil {
p.log.Error("handleFormatting: error sending response", zap.Error(err))
}
p.log.Info("handleFormatting: client -> textDocument.formatting -> client: complete", zap.Any("resp", resp))
}()
// Format the current document.
contents, _ := p.documentContents.Get(string(params.TextDocument.URI))
var lines int
for _, c := range contents {
if c == '\n' {
lines++
}
}
template, err := templ.ParseString(string(contents))
if err != nil {
p.sendParseErrorDiagnosticNotifications(params.TextDocument.URI, err)
return
}
p.sendDiagnosticClearNotification(params.TextDocument.URI)
w := new(strings.Builder)
err = template.Write(w)
if err != nil {
p.log.Error("handleFormatting: faled to write template", zap.Error(err))
return
}
// Replace everything.
resp = append(resp, lsp.TextEdit{
Range: lsp.Range{
Start: lsp.Position{},
End: lsp.Position{Line: lines + 1, Character: 0},
},
NewText: w.String(),
})
}

func (p *Proxy) rewriteDidOpenRequest(r *jsonrpc2.Request) (err error) {
// Unmarshal the params.
var params lsp.DidOpenTextDocumentParams
Expand Down

0 comments on commit 8905c85

Please sign in to comment.