2018-12-14 15:00:24 -07: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.
|
|
|
|
|
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
)
|
|
|
|
|
2018-12-18 14:18:03 -07:00
|
|
|
func organizeImports(ctx context.Context, v source.View, uri protocol.DocumentURI) ([]protocol.TextEdit, error) {
|
2019-02-04 15:44:35 -07:00
|
|
|
sourceURI, err := fromProtocolURI(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := v.GetFile(ctx, sourceURI)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
tok := f.GetToken(ctx)
|
2018-12-14 15:00:24 -07:00
|
|
|
r := source.Range{
|
|
|
|
Start: tok.Pos(0),
|
|
|
|
End: tok.Pos(tok.Size()),
|
|
|
|
}
|
2019-02-13 14:08:29 -07:00
|
|
|
edits, err := source.Imports(ctx, f, r)
|
2018-12-14 15:00:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
return toProtocolEdits(ctx, f, edits), nil
|
2018-12-14 15:00:24 -07:00
|
|
|
}
|