Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/filesystem: Integrate spf13/afero #249

Merged
merged 2 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions commands/completion_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,21 @@ func (c *CompletionCommand) Run(args []string) int {

fs := filesystem.NewFilesystem()
fs.SetLogger(logger)
fs.Open(ilsp.FileFromDocumentItem(lsp.TextDocumentItem{
URI: fh.DocumentURI(),
Text: string(content),
Version: 0,
}))
fs.CreateAndOpenDocument(fh, content)

file, err := fs.GetFile(fh)
file, err := fs.GetDocument(fh)
if err != nil {
c.Ui.Error(err.Error())
return 1
}

hclFile := ihcl.NewFile(file)
text, err := file.Text()
if err != nil {
c.Ui.Error(err.Error())
return 1
}

hclFile := ihcl.NewFile(file, text)
fPos, err := ilsp.FilePositionFromDocumentPosition(lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{
URI: fh.DocumentURI(),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/mitchellh/mapstructure v1.3.2
github.com/pmezard/go-difflib v1.0.0
github.com/sourcegraph/go-lsp v0.0.0-20200117082640-b19bb38222e2
github.com/spf13/afero v1.3.2
github.com/zclconf/go-cty v1.2.1
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/creachadair/jrpc2 v0.8.1 h1:YX27WgtXa9v7g0kn3oYe7gmxt7e5M3miVgEgTXoTqcY=
github.com/creachadair/jrpc2 v0.8.1/go.mod h1:yMFI6NwXGITWoWDEEdmZESIHp13SAuSkGCTBz9bSGBg=
github.com/creachadair/staticfile v0.1.2/go.mod h1:a3qySzCIXEprDGxk6tSxSI+dBBdLzqeBOMhZ+o2d3pM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
Expand Down Expand Up @@ -45,6 +46,7 @@ github.com/hashicorp/terraform-json v0.5.0 h1:7TV3/F3y7QVSuN4r9BEXqnWqrAyeOtON8f
github.com/hashicorp/terraform-json v0.5.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136 h1:81Dg7SK6Q5vzqFItO8e1iIF2Nj8bLXV23NXjEgbev/s=
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -64,6 +66,8 @@ github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzC
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1 h1:ccV59UEOTzVDnDUEFdT95ZzHVZ+5+158q8+SJb2QV5w=
Expand All @@ -72,16 +76,21 @@ github.com/radeksimko/go-lsp v0.1.0 h1:evTibJ/0G2QtamSdA6mi+Xov7t5RpoGmMcTK60bWp
github.com/radeksimko/go-lsp v0.1.0/go.mod h1:tpps84QRlOVVLYk5QpKYX8Tr289D1v/UTWDLqeguiqM=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/afero v1.3.2 h1:GDarE4TJQI52kYSbSAmLiId1Elfj+xgSDqrUZxFhxlU=
github.com/spf13/afero v1.3.2/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.2.1 h1:vGMsygfmeCl4Xb6OA5U5XVAaQZ69FvoG7X2jUtQujb8=
github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -109,4 +118,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
12 changes: 6 additions & 6 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (k *contextKey) String() string {
}

var (
ctxFs = &contextKey{"filesystem"}
ctxDs = &contextKey{"document storage"}
ctxClientCapsSetter = &contextKey{"client capabilities setter"}
ctxClientCaps = &contextKey{"client capabilities"}
ctxTfExecPath = &contextKey{"terraform executable path"}
Expand All @@ -39,14 +39,14 @@ func missingContextErr(ctxKey *contextKey) *MissingContextErr {
return &MissingContextErr{ctxKey}
}

func WithFilesystem(ctx context.Context, fs filesystem.Filesystem) context.Context {
return context.WithValue(ctx, ctxFs, fs)
func WithDocumentStorage(ctx context.Context, fs filesystem.DocumentStorage) context.Context {
return context.WithValue(ctx, ctxDs, fs)
}

func Filesystem(ctx context.Context) (filesystem.Filesystem, error) {
fs, ok := ctx.Value(ctxFs).(filesystem.Filesystem)
func DocumentStorage(ctx context.Context) (filesystem.DocumentStorage, error) {
fs, ok := ctx.Value(ctxDs).(filesystem.DocumentStorage)
if !ok {
return nil, missingContextErr(ctxFs)
return nil, missingContextErr(ctxDs)
}

return fs, nil
Expand Down
11 changes: 0 additions & 11 deletions internal/filesystem/dir.go

This file was deleted.

7 changes: 7 additions & 0 deletions internal/filesystem/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Package filesystem implements a virtual filesystem which reflects
// the needs of both the language server and the HCL parser.
//
// - creates in-memory files based on data received from the language client
// - allows updating in-memory files via diffs received from the language client
// - maintains file metadata (e.g. version, or whether it's open by the client)
package filesystem
78 changes: 78 additions & 0 deletions internal/filesystem/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package filesystem

import (
"bytes"
"io/ioutil"
"path/filepath"

"github.com/hashicorp/terraform-ls/internal/source"
"github.com/spf13/afero"
)

type fileOpener interface {
Open(name string) (afero.File, error)
}

type document struct {
meta *documentMetadata
fo fileOpener
}

func (d *document) Text() ([]byte, error) {
f, err := d.fo.Open(d.meta.dh.FullPath())
if err != nil {
return nil, err
}

return ioutil.ReadAll(f)
}

func (d *document) FullPath() string {
return d.meta.dh.FullPath()
}

func (d *document) Dir() string {
return filepath.Dir(d.meta.dh.FullPath())
}

func (d *document) Filename() string {
return filepath.Base(d.meta.dh.FullPath())
}

func (d *document) URI() string {
return URIFromPath(d.meta.dh.FullPath())
}

func (d *document) Lines() source.Lines {
return d.meta.Lines()
}

func (d *document) Version() int {
return d.meta.Version()
}

func (d *document) IsOpen() bool {
return d.meta.IsOpen()
}

func (d *document) Equal(doc *document) bool {
if d.URI() != doc.URI() {
return false
}
if d.IsOpen() != doc.IsOpen() {
return false
}
if d.Version() != doc.Version() {
return false
}

leftB, err := d.Text()
if err != nil {
return false
}
rightB, err := doc.Text()
if err != nil {
return false
}
return bytes.Equal(leftB, rightB)
}
60 changes: 60 additions & 0 deletions internal/filesystem/document_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package filesystem

import (
"sync"

"github.com/hashicorp/terraform-ls/internal/source"
)

type documentMetadata struct {
dh DocumentHandler

mu *sync.RWMutex
isOpen bool
version int
lines source.Lines
}

func NewDocumentMetadata(dh DocumentHandler, content []byte) *documentMetadata {
return &documentMetadata{
dh: dh,
mu: &sync.RWMutex{},
lines: source.MakeSourceLines(dh.Filename(), content),
}
}

func (d *documentMetadata) setOpen(isOpen bool) {
d.mu.Lock()
defer d.mu.Unlock()
d.isOpen = isOpen
}

func (d *documentMetadata) setVersion(version int) {
d.mu.Lock()
defer d.mu.Unlock()
d.version = version
}

func (d *documentMetadata) updateLines(content []byte) {
d.mu.Lock()
defer d.mu.Unlock()
d.lines = source.MakeSourceLines(d.dh.Filename(), content)
}

func (d *documentMetadata) Lines() source.Lines {
d.mu.RLock()
defer d.mu.RUnlock()
return d.lines
}

func (d *documentMetadata) Version() int {
d.mu.RLock()
defer d.mu.RUnlock()
return d.version
}

func (d *documentMetadata) IsOpen() bool {
d.mu.RLock()
defer d.mu.RUnlock()
return d.isOpen
}
Loading