Skip to content

Commit

Permalink
Implement textDocument/formatting (#51)
Browse files Browse the repository at this point in the history
* Implement textDocument/formatting

* formatting: Return diff hunks instead of whole file content
  • Loading branch information
radeksimko authored Apr 22, 2020
1 parent 812c313 commit 6053e80
Show file tree
Hide file tree
Showing 22 changed files with 1,478 additions and 53 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/hashicorp/hcl/v2 v2.3.0
github.com/hashicorp/terraform-json v0.4.0
github.com/mitchellh/cli v1.0.0
github.com/pmezard/go-difflib v1.0.0
github.com/sourcegraph/go-lsp v0.0.0-20200117082640-b19bb38222e2
github.com/zclconf/go-cty v1.2.1
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
Expand Down
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
120 changes: 120 additions & 0 deletions internal/hcl/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package hcl

import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/source"
"github.com/pmezard/go-difflib/difflib"
)

type fileChange struct {
newText string
rng hcl.Range
opCode difflib.OpCode
}

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

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

const (
OpReplace = 'r'
OpDelete = 'd'
OpInsert = 'i'
OpEqual = 'e'
)

// Diff calculates difference between File's content
// and after byte sequence and returns it as filesystem.FileChanges
func Diff(f filesystem.File, after []byte) filesystem.FileChanges {
return diffLines(f.Filename(),
source.MakeSourceLines(f.Filename(), f.Text()),
source.MakeSourceLines(f.Filename(), after))
}

// diffLines calculates difference between two source.Lines
// and returns them as filesystem.FileChanges
func diffLines(filename string, beforeLines, afterLines source.Lines) filesystem.FileChanges {
context := 3

m := difflib.NewMatcher(
source.StringLines(beforeLines),
source.StringLines(afterLines))

changes := make(filesystem.FileChanges, 0)

for _, group := range m.GetGroupedOpCodes(context) {
for _, c := range group {
beforeStart, beforeEnd := c.I1, c.I2
afterStart, afterEnd := c.J1, c.J2

if c.Tag == OpEqual {
continue
}

if c.Tag == OpReplace {
var rng hcl.Range
var newBytes []byte

for i, line := range beforeLines[beforeStart:beforeEnd] {
if i == 0 {
rng = line.Range()
continue
}
rng.End = line.Range().End
}

for _, line := range afterLines[afterStart:afterEnd] {
newBytes = append(newBytes, line.Bytes()...)
}

changes = append(changes, &fileChange{
newText: string(newBytes),
rng: rng,
})
continue
}

if c.Tag == OpDelete {
var deleteRng hcl.Range
for i, line := range beforeLines[beforeStart:beforeEnd] {
if i == 0 {
deleteRng = line.Range()
continue
}
deleteRng.End = line.Range().End
}
changes = append(changes, &fileChange{
newText: "",
rng: deleteRng,
opCode: c,
})
continue
}

if c.Tag == OpInsert {
var insertRng hcl.Range
insertRng.Start = beforeLines[beforeStart-1].Range().End
insertRng.End = beforeLines[beforeStart-1].Range().End
var newBytes []byte

for _, line := range afterLines[afterStart:afterEnd] {
newBytes = append(newBytes, line.Bytes()...)
}

changes = append(changes, &fileChange{
newText: string(newBytes),
rng: insertRng,
})
continue
}

}
}

return changes
}
Loading

0 comments on commit 6053e80

Please sign in to comment.