Skip to content

Commit

Permalink
add version for file system
Browse files Browse the repository at this point in the history
  • Loading branch information
njuCZ committed May 29, 2020
1 parent d02636e commit 939d38f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
13 changes: 11 additions & 2 deletions internal/filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type file struct {
content []byte
open bool

ls source.Lines
errs bool
version int
ls source.Lines
errs bool
}

func NewFile(fullPath string, content []byte) *file {
Expand All @@ -42,6 +43,14 @@ func (f *file) URI() string {
return URIFromPath(f.fullPath)
}

func (f *file) Version() int {
return f.version
}

func (f *file) IncrementVersion() {
f.version += 1
}

func (f *file) Lines() source.Lines {
if f.ls == nil {
f.ls = source.MakeSourceLines(f.Filename(), f.content)
Expand Down
3 changes: 3 additions & 0 deletions internal/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (fs *fsystem) Open(file File) error {
f = NewFile(file.FullPath(), file.Text())
}
f.open = true
f.version = file.Version()
d.files[file.Filename()] = f
return nil
}
Expand All @@ -54,6 +55,8 @@ func (fs *fsystem) Change(fh VersionedFileHandler, changes FileChanges) error {
for _, change := range changes {
f.applyChange(change)
}

f.IncrementVersion()
return nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/filesystem/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type File interface {
FileHandler
Text() []byte
Lines() source.Lines
Version() int
}

type FilePosition interface {
Expand Down
16 changes: 11 additions & 5 deletions internal/lsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ type File interface {
}

type file struct {
fh FileHandler
ls source.Lines
text []byte
fh FileHandler
ls source.Lines
text []byte
version int
}

func (f *file) URI() string {
Expand Down Expand Up @@ -50,9 +51,14 @@ func (f *file) lines() source.Lines {
return f.ls
}

func (f *file) Version() int {
return f.version
}

func FileFromDocumentItem(doc lsp.TextDocumentItem) *file {
return &file{
fh: FileHandler(doc.URI),
text: []byte(doc.Text),
fh: FileHandler(doc.URI),
text: []byte(doc.Text),
version: doc.Version,
}
}
52 changes: 50 additions & 2 deletions langserver/handlers/did_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,74 @@ package handlers

import (
"context"
"encoding/json"
"fmt"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/sourcegraph/go-lsp"
)

func TextDocumentDidChange(ctx context.Context, params lsp.DidChangeTextDocumentParams) error {
func TextDocumentDidChange(ctx context.Context, params DidChangeTextDocumentParams) error {
p := lsp.DidChangeTextDocumentParams{
TextDocument: lsp.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: lsp.TextDocumentIdentifier{
URI: params.TextDocument.URI,
},
Version: params.TextDocument.Version,
},
ContentChanges: params.ContentChanges,
}

fs, err := lsctx.Filesystem(ctx)
if err != nil {
return err
}

fh := ilsp.VersionedFileHandler(params.TextDocument)
fh := ilsp.VersionedFileHandler(p.TextDocument)
f, err := fs.GetFile(fh)
if err != nil {
return err
}

// old version change, just skip
if p.TextDocument.Version <= f.Version() {
return fmt.Errorf("skip old version %d change: %#v, current file version %d", p.TextDocument.Version, params, f.Version())
}

// missing version
if p.TextDocument.Version > f.Version()+1 {
return fmt.Errorf("missing version %d change: %#v, current file version %d", p.TextDocument.Version, params, f.Version())
}

changes, err := ilsp.FileChanges(params.ContentChanges, f)
if err != nil {
return err
}
return fs.Change(fh, changes)
}

type DidChangeTextDocumentParams struct {
TextDocument VersionedTextDocumentIdentifier `json:"textDocument"`
ContentChanges []lsp.TextDocumentContentChangeEvent `json:"contentChanges"`
}

type VersionedTextDocumentIdentifier struct {
URI lsp.DocumentURI `json:"uri"`
/**
* The version number of this document.
*/
Version int `json:"version"`
}

// UnmarshalJSON implements non-strict json.Unmarshaler.
func (v *DidChangeTextDocumentParams) UnmarshalJSON(b []byte) error {
type t DidChangeTextDocumentParams
return json.Unmarshal(b, (*t)(v))
}

// UnmarshalJSON implements non-strict json.Unmarshaler.
func (v *VersionedTextDocumentIdentifier) UnmarshalJSON(b []byte) error {
type t VersionedTextDocumentIdentifier
return json.Unmarshal(b, (*t)(v))
}

0 comments on commit 939d38f

Please sign in to comment.