1
0
mirror of https://github.com/golang/go synced 2024-11-18 21:05:02 -07:00
go/internal/lsp/imports.go

39 lines
935 B
Go
Raw Normal View History

// 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.
package lsp
import (
"context"
"fmt"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
func organizeImports(ctx context.Context, v source.View, s span.Span) ([]protocol.TextEdit, error) {
f, m, err := newColumnMap(ctx, v, s.URI())
if err != nil {
return nil, err
}
rng, err := s.Range(m.Converter)
if err != nil {
return nil, err
}
if rng.Start == rng.End {
// If we have a single point, assume we want the whole file.
tok := f.GetToken(ctx)
if tok == nil {
return nil, fmt.Errorf("no file information for %s", f.URI())
}
rng.End = tok.Pos(tok.Size())
}
edits, err := source.Imports(ctx, f, rng)
if err != nil {
return nil, err
}
return toProtocolEdits(m, edits)
}