Debian Codegraph @

gopls/internal/golang/add_import.go

raw symbols SCIP
1 // Copyright 2020 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 golang
6
7 import (
8 "context"
9
10 "golang.org/x/tools/gopls/internal/cache"
11 "golang.org/x/tools/gopls/internal/cache/parsego"
12 "golang.org/x/tools/gopls/internal/file"
13 "golang.org/x/tools/gopls/internal/protocol"
14 "golang.org/x/tools/internal/imports"
15 )
16
17 // AddImport adds a single import statement to the given file
18 func AddImport(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, importPath string) ([]protocol.TextEdit, error) {
19 pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full)
20 if err != nil {
21 return nil, err
22 }
23 return ComputeImportFixEdits(snapshot.Options().Local, pgf.Src, &imports.ImportFix{
24 StmtInfo: imports.ImportInfo{
25 ImportPath: importPath,
26 },
27 FixType: imports.AddImport,
28 })
29 }