-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/filesystem: Integrate spf13/afero (#249)
* internal/filesystem: Integrate spf13/afero * go mod vendor
- Loading branch information
1 parent
b9df7a4
commit 7e25849
Showing
55 changed files
with
4,815 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.