golang.org/x/tools/gopls@v0.15.3/internal/server/format.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package server
     6  
     7  import (
     8  	"context"
     9  
    10  	"golang.org/x/tools/gopls/internal/file"
    11  	"golang.org/x/tools/gopls/internal/golang"
    12  	"golang.org/x/tools/gopls/internal/mod"
    13  	"golang.org/x/tools/gopls/internal/protocol"
    14  	"golang.org/x/tools/gopls/internal/work"
    15  	"golang.org/x/tools/internal/event"
    16  	"golang.org/x/tools/internal/event/tag"
    17  )
    18  
    19  func (s *server) Formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
    20  	ctx, done := event.Start(ctx, "lsp.Server.formatting", tag.URI.Of(params.TextDocument.URI))
    21  	defer done()
    22  
    23  	fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	defer release()
    28  
    29  	switch snapshot.FileKind(fh) {
    30  	case file.Mod:
    31  		return mod.Format(ctx, snapshot, fh)
    32  	case file.Go:
    33  		return golang.Format(ctx, snapshot, fh)
    34  	case file.Work:
    35  		return work.Format(ctx, snapshot, fh)
    36  	}
    37  	return nil, nil // empty result
    38  }