Skip to content

Commit

Permalink
Implement textDocument/formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Apr 22, 2020
1 parent 812c313 commit 1f6733c
Show file tree
Hide file tree
Showing 14 changed files with 399 additions and 18 deletions.
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -52,13 +51,16 @@ github.com/mitchellh/cli v1.0.0 h1:iGBIsUe3+HZ/AD/Vd7DErOt5sU9fa8Uj7A2s1aggv1Y=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
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=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/radeksimko/go-lsp v0.1.0 h1:evTibJ/0G2QtamSdA6mi+Xov7t5RpoGmMcTK60bWp+E=
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/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
Expand All @@ -74,9 +76,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82 h1:vsphBvatvfbhlb4PO1BYSr9dzugGxJ/SQHoNufZJq1w=
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 h1:L2auWcuQIvxz9xSEqzESnV/QN/gNRXNApHi3fYwl2w0=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
8 changes: 8 additions & 0 deletions internal/filesystem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package filesystem

import (
"testing"

"github.com/hashicorp/hcl/v2"
)

func TestFile_ApplyChange_fullUpdate(t *testing.T) {
Expand All @@ -23,3 +25,9 @@ type fileChange struct {
func (fc *fileChange) Text() string {
return fc.text
}

func (fc *fileChange) Range() hcl.Range {
return hcl.Range{
// TODO: Implement partial updates
}
}
7 changes: 7 additions & 0 deletions internal/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/source"
)

Expand Down Expand Up @@ -203,3 +204,9 @@ type testChange struct {
func (ch *testChange) Text() string {
return ch.text
}

func (ch *testChange) Range() hcl.Range {
return hcl.Range{
// TODO: Implement partial updates
}
}
1 change: 1 addition & 0 deletions internal/filesystem/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type FilePosition interface {

type FileChange interface {
Text() string
Range() hcl.Range
}

type VersionedFileHandler interface {
Expand Down
34 changes: 34 additions & 0 deletions internal/hcl/hcl.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package hcl

import (
"fmt"

"github.com/hashicorp/hcl/v2"
hcllib "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform-ls/internal/filesystem"
)

type File interface {
BlockAtPosition(filesystem.FilePosition) (*hcllib.Block, hcllib.Pos, error)
Diff([]byte) (filesystem.FileChanges, error)
}

type file struct {
Expand Down Expand Up @@ -45,6 +49,36 @@ func (f *file) BlockAtPosition(filePos filesystem.FilePosition) (*hcllib.Block,
return b, pos, nil
}

func (f *file) Diff(target []byte) (filesystem.FileChanges, error) {
var changes filesystem.FileChanges

ast, _ := f.ast()
body, ok := ast.Body.(*hclsyntax.Body)
if !ok {
return nil, fmt.Errorf("invalid configuration format: %T", ast.Body)
}

changes = append(changes, &fileChange{
newText: target,
rng: body.Range(),
})

return changes, nil
}

type fileChange struct {
newText []byte
rng hcl.Range
}

func (ch *fileChange) Text() string {
return string(ch.newText)
}

func (ch *fileChange) Range() hcl.Range {
return ch.rng
}

func (f *file) blockAtPosition(pos hcllib.Pos) (*hcllib.Block, error) {
ast, _ := f.ast()

Expand Down
44 changes: 44 additions & 0 deletions internal/hcl/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,50 @@ func TestFile_BlockAtPosition(t *testing.T) {
}
}

func TestFile_diff(t *testing.T) {
targetCfg := []byte(`
provider "aws" {
first = "test"
very_long_attr = "xyz"
}`)
fsFile := filesystem.NewFile("/tmp/test.tf", []byte(`
provider "aws" {
first = "test"
very_long_attr = "xyz"
}`))
f := NewFile(fsFile)
changes, err := f.Diff(targetCfg)
if err != nil {
t.Fatal(err)
}

var expectedChanges filesystem.FileChanges
expectedChanges = append(expectedChanges, &fileChange{
newText: []byte(targetCfg),
rng: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Column: 1,
Line: 1,
Byte: 0,
},
End: hcl.Pos{
Column: 2,
Line: 5,
Byte: 62,
},
},
})

opts := cmp.Options{
cmp.AllowUnexported(fileChange{}),
}

if diff := cmp.Diff(expectedChanges, changes, opts...); diff != "" {
t.Fatalf("Changes don't match: %s", diff)
}
}

type testPosition struct {
filesystem.FileHandler
pos hcl.Pos
Expand Down
32 changes: 32 additions & 0 deletions internal/lsp/file_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package lsp
import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/sourcegraph/go-lsp"
)

type fileChange struct {
text string
rng hcl.Range
}

func FileChange(chEvent lsp.TextDocumentContentChangeEvent, f File) (*fileChange, error) {
Expand All @@ -33,6 +35,36 @@ func FileChanges(events []lsp.TextDocumentContentChangeEvent, f File) (filesyste
return changes, nil
}

func TextEdits(changes filesystem.FileChanges) []lsp.TextEdit {
edits := make([]lsp.TextEdit, len(changes))

for i, change := range changes {
edits[i] = lsp.TextEdit{
Range: hclRangeToLSP(change.Range()),
NewText: change.Text(),
}
}

return edits
}

func hclRangeToLSP(hclRng hcl.Range) lsp.Range {
return lsp.Range{
Start: lsp.Position{
Character: hclRng.Start.Column - 1,
Line: hclRng.Start.Line - 1,
},
End: lsp.Position{
Character: hclRng.End.Column - 1,
Line: hclRng.End.Line - 1,
},
}
}

func (fc *fileChange) Text() string {
return fc.text
}

func (fc *fileChange) Range() hcl.Range {
return fc.rng
}
Loading

0 comments on commit 1f6733c

Please sign in to comment.