2019-04-17 13:37:20 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-12-05 15:00:36 -07:00
|
|
|
)
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-20 14:38:43 -07:00
|
|
|
snapshot := view.Snapshot()
|
2019-12-17 16:57:54 -07:00
|
|
|
fh, err := snapshot.GetFile(ctx, uri)
|
2019-06-27 14:29:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-10 09:51:34 -07:00
|
|
|
var edits []protocol.TextEdit
|
2019-12-17 16:57:54 -07:00
|
|
|
switch fh.Identity().Kind {
|
2019-12-10 09:51:34 -07:00
|
|
|
case source.Go:
|
2019-12-17 16:57:54 -07:00
|
|
|
edits, err = source.Format(ctx, snapshot, fh)
|
2019-12-10 09:51:34 -07:00
|
|
|
case source.Mod:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return edits, nil
|
2019-04-08 07:22:58 -06:00
|
|
|
}
|