2018-11-02 16:10:49 -06: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 source
|
|
|
|
|
|
|
|
import (
|
2018-11-05 15:54:12 -07:00
|
|
|
"fmt"
|
2018-11-02 16:10:49 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
"io/ioutil"
|
2018-11-07 13:21:31 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages"
|
2018-11-02 16:10:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// File holds all the information we know about a file.
|
|
|
|
type File struct {
|
2018-11-05 15:54:12 -07:00
|
|
|
URI URI
|
2018-11-02 16:10:49 -06:00
|
|
|
view *View
|
|
|
|
active bool
|
|
|
|
content []byte
|
|
|
|
ast *ast.File
|
|
|
|
token *token.File
|
2018-11-05 15:54:12 -07:00
|
|
|
pkg *packages.Package
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range represents a start and end position.
|
|
|
|
// Because Range is based purely on two token.Pos entries, it is not self
|
|
|
|
// contained. You need access to a token.FileSet to regain the file
|
|
|
|
// information.
|
|
|
|
type Range struct {
|
|
|
|
Start token.Pos
|
|
|
|
End token.Pos
|
2018-11-02 16:10:49 -06:00
|
|
|
}
|
|
|
|
|
2018-11-07 10:58:55 -07:00
|
|
|
// TextEdit represents a change to a section of a document.
|
|
|
|
// The text within the specified range should be replaced by the supplied new text.
|
|
|
|
type TextEdit struct {
|
|
|
|
Range Range
|
|
|
|
NewText string
|
|
|
|
}
|
|
|
|
|
2018-11-02 16:10:49 -06:00
|
|
|
// SetContent sets the overlay contents for a file.
|
|
|
|
// Setting it to nil will revert it to the on disk contents, and remove it
|
|
|
|
// from the active set.
|
|
|
|
func (f *File) SetContent(content []byte) {
|
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
|
|
|
f.content = content
|
|
|
|
// the ast and token fields are invalid
|
|
|
|
f.ast = nil
|
|
|
|
f.token = nil
|
2018-11-05 15:54:12 -07:00
|
|
|
f.pkg = nil
|
2018-11-02 16:10:49 -06:00
|
|
|
// and we might need to update the overlay
|
|
|
|
switch {
|
|
|
|
case f.active && content == nil:
|
|
|
|
// we were active, and want to forget the content
|
|
|
|
f.active = false
|
2018-11-05 15:54:12 -07:00
|
|
|
if filename, err := f.URI.Filename(); err == nil {
|
2018-11-02 16:10:49 -06:00
|
|
|
delete(f.view.Config.Overlay, filename)
|
|
|
|
}
|
|
|
|
f.content = nil
|
|
|
|
case content != nil:
|
|
|
|
// an active overlay, update the map
|
|
|
|
f.active = true
|
2018-11-05 15:54:12 -07:00
|
|
|
if filename, err := f.URI.Filename(); err == nil {
|
2018-11-02 16:10:49 -06:00
|
|
|
f.view.Config.Overlay[filename] = f.content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read returns the contents of the file, reading it from file system if needed.
|
|
|
|
func (f *File) Read() ([]byte, error) {
|
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
|
|
|
return f.read()
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:54:12 -07:00
|
|
|
func (f *File) GetToken() (*token.File, error) {
|
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
|
|
|
if f.token == nil {
|
|
|
|
if err := f.view.parse(f.URI); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if f.token == nil {
|
|
|
|
return nil, fmt.Errorf("failed to find or parse %v", f.URI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f.token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) GetAST() (*ast.File, error) {
|
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
|
|
|
if f.ast == nil {
|
|
|
|
if err := f.view.parse(f.URI); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f.ast, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) GetPackage() (*packages.Package, error) {
|
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
|
|
|
if f.pkg == nil {
|
|
|
|
if err := f.view.parse(f.URI); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f.pkg, nil
|
|
|
|
}
|
|
|
|
|
2018-11-02 16:10:49 -06:00
|
|
|
// read is the internal part of Read that presumes the lock is already held
|
|
|
|
func (f *File) read() ([]byte, error) {
|
|
|
|
if f.content != nil {
|
|
|
|
return f.content, nil
|
|
|
|
}
|
|
|
|
// we don't know the content yet, so read it
|
2018-11-05 15:54:12 -07:00
|
|
|
filename, err := f.URI.Filename()
|
2018-11-02 16:10:49 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.content = content
|
|
|
|
return f.content, nil
|
|
|
|
}
|