Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

source: FormatGop #30

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gopls/internal/lsp/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormat
return source.Format(ctx, snapshot, fh)
case source.Work:
return work.Format(ctx, snapshot, fh)
case source.Gop: // goxls: format Go+ files
return source.FormatGop(ctx, snapshot, fh)
}
return nil, nil
}
26 changes: 17 additions & 9 deletions gopls/internal/lsp/source/format_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ import (
"fmt"
"strings"

"path/filepath"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/format"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/token"
"golang.org/x/tools/gopls/internal/goxls/goputil"
"golang.org/x/tools/gopls/internal/goxls/imports"
"golang.org/x/tools/gopls/internal/goxls/parserutil"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/safetoken"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/tokeninternal"
)

/*
import (
"bytes"
"context"
"fmt"
"path/filepath"

"github.com/goplus/gop/format"
"golang.org/x/tools/gopls/internal/goxls/goputil"
"golang.org/x/tools/gopls/internal/goxls/parserutil"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/tokeninternal"
)

*/
// FormatGop formats a file with a given range.
func FormatGop(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.TextEdit, error) {
ctx, done := event.Start(ctx, "gop.Format")
Expand All @@ -56,7 +57,7 @@ func FormatGop(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protoco
if err != nil {
return nil, err
}
return computeGopTextEdits(ctx, snapshot, pgf, string(formatted))
return gopComputeTextEdits(ctx, snapshot, pgf, string(formatted))
}

// format.Node changes slightly from one release to another, so the version
Expand All @@ -72,7 +73,7 @@ func FormatGop(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protoco

// Apply additional formatting, if any is supported. Currently, the only
// supported additional formatter is gofumpt.
if format := snapshot.Options().GofumptFormat; snapshot.Options().Gofumpt && format != nil {
if format := snapshot.View().Options().GofumptFormat; snapshot.View().Options().Gofumpt && format != nil {
// gofumpt can customize formatting based on language version and module
// path, if available.
//
Expand Down Expand Up @@ -113,7 +114,6 @@ func isClass(fh FileHandle) bool {
fext := filepath.Ext(fh.URI().Filename())
return goputil.FileKind(fext) == goputil.FileGopClass
}
*/

// GopAllImportsFixes formats f for each possible fix to the imports.
// In addition to returning the result of applying all edits,
Expand Down Expand Up @@ -300,3 +300,11 @@ func gopImportPrefix(src []byte) (string, error) {
}
return string(src[:importEnd]), nil
}

func gopComputeTextEdits(ctx context.Context, snapshot Snapshot, pgf *ParsedGopFile, formatted string) ([]protocol.TextEdit, error) {
_, done := event.Start(ctx, "source.computeTextEdits")
defer done()

edits := snapshot.View().Options().ComputeEdits(string(pgf.Src), formatted)
return ToProtocolEdits(pgf.Mapper, edits)
}