github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/source/add_import.go (about)

     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 source
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/powerman/golang-tools/internal/imports"
    11  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    12  )
    13  
    14  // AddImport adds a single import statement to the given file
    15  func AddImport(ctx context.Context, snapshot Snapshot, fh VersionedFileHandle, importPath string) ([]protocol.TextEdit, error) {
    16  	_, pgf, err := GetParsedFile(ctx, snapshot, fh, NarrowestPackage)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	return ComputeOneImportFixEdits(snapshot, pgf, &imports.ImportFix{
    21  		StmtInfo: imports.ImportInfo{
    22  			ImportPath: importPath,
    23  		},
    24  		FixType: imports.AddImport,
    25  	})
    26  }