golang.org/x/tools/gopls@v0.15.3/internal/test/integration/fake/edit.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 fake
     6  
     7  import (
     8  	"golang.org/x/tools/gopls/internal/protocol"
     9  	"golang.org/x/tools/internal/diff"
    10  )
    11  
    12  // NewEdit creates an edit replacing all content between the 0-based
    13  // (startLine, startColumn) and (endLine, endColumn) with text.
    14  //
    15  // Columns measure UTF-16 codes.
    16  func NewEdit(startLine, startColumn, endLine, endColumn uint32, text string) protocol.TextEdit {
    17  	return protocol.TextEdit{
    18  		Range: protocol.Range{
    19  			Start: protocol.Position{Line: startLine, Character: startColumn},
    20  			End:   protocol.Position{Line: endLine, Character: endColumn},
    21  		},
    22  		NewText: text,
    23  	}
    24  }
    25  
    26  // applyEdits applies the edits to a file with the specified lines,
    27  // and returns a new slice containing the lines of the patched file.
    28  // It is a wrapper around diff.Apply; see that function for preconditions.
    29  func applyEdits(mapper *protocol.Mapper, edits []protocol.TextEdit, windowsLineEndings bool) ([]byte, error) {
    30  	diffEdits, err := protocol.EditsToDiffEdits(mapper, edits)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	patched, err := diff.ApplyBytes(mapper.Content, diffEdits)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	if windowsLineEndings {
    39  		patched = toWindowsLineEndings(patched)
    40  	}
    41  	return patched, nil
    42  }