Skip to content

Commit

Permalink
Did go fmt, also fixed windows path.module issue
Browse files Browse the repository at this point in the history
  • Loading branch information
juliosueiras committed Feb 19, 2020
1 parent 5edc509 commit 71b6183
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 142 deletions.
10 changes: 5 additions & 5 deletions helper/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"github.com/hashicorp/terraform/configs"
"github.com/juliosueiras/terraform-lsp/hclstructs"
"github.com/juliosueiras/terraform-lsp/memfs"
log "github.com/sirupsen/logrus"
"github.com/sourcegraph/go-lsp"
"github.com/spf13/afero"
"github.com/zclconf/go-cty/cty"
log "github.com/sirupsen/logrus"
"reflect"
"regexp"
"strings"
"unicode/utf8"
)

func CheckAndGetConfig(parser *configs.Parser, originalFile afero.File, line int, character int) (*configs.File, hcl.Diagnostics, int, *hclsyntax.Body, bool) {
fileText, _ := afero.ReadFile(memfs.MemFs, originalFile.Name())
fileText, _ := afero.ReadFile(memfs.MemFs, originalFile.Name())
result := make([]byte, 1)
pos := FindOffset(string(fileText), line, character)

Expand Down Expand Up @@ -65,9 +65,9 @@ func CheckAndGetConfig(parser *configs.Parser, originalFile afero.File, line int

// credits: https://stackoverflow.com/questions/28008566/how-to-compute-the-offset-from-column-and-line-number-go
func FindOffset(fileText string, line, column int) int {
if column == 0 {
column = 1
}
if column == 0 {
column = 1
}

currentCol := 1
currentLine := 1
Expand Down
12 changes: 6 additions & 6 deletions langserver/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import (
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/lang"
lsp "github.com/sourcegraph/go-lsp"
"net/url"
"net/url"
"path/filepath"
"reflect"
"regexp"
"strings"
//"github.com/hashicorp/terraform/terraform"
"github.com/juliosueiras/terraform-lsp/hclstructs"
"github.com/juliosueiras/terraform-lsp/helper"
"github.com/juliosueiras/terraform-lsp/tfstructs"
"github.com/juliosueiras/terraform-lsp/memfs"
"github.com/spf13/afero"
"github.com/juliosueiras/terraform-lsp/tfstructs"
"github.com/spf13/afero"
)

func TextDocumentComplete(ctx context.Context, vs lsp.CompletionParams) (lsp.CompletionList, error) {
//log.Println(tfstructs.Clients)
//log.Println(tfstructs.Clients)
parser := configs.NewParser(memfs.MemFs)

fileURL := strings.Replace(string(vs.TextDocument.URI), "file://", "", 1)

decodedFileURL , _ := url.QueryUnescape(fileURL)
decodedFileURL, _ := url.QueryUnescape(fileURL)
fileDir := filepath.Dir(decodedFileURL)
res, _ := filepath.Glob(fileDir + "/*.tf")
var file *configs.File
Expand Down Expand Up @@ -220,7 +220,7 @@ func TextDocumentComplete(ctx context.Context, vs lsp.CompletionParams) (lsp.Com

if expr != nil {
helper.DumpLog("Found Expression")
helper.DumpLog(posHCL)
helper.DumpLog(posHCL)
helper.DumpLog(expr)
//.*for.*in\s+([^:]*)
//te, te2 := hclsyntax.ParseExpression([]byte("aws[0].test"), "test", hcl.Pos{
Expand Down
8 changes: 4 additions & 4 deletions langserver/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package langserver

import (
"context"
"os"
"github.com/juliosueiras/terraform-lsp/memfs"
"github.com/juliosueiras/terraform-lsp/memfs"
"os"

log "github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
lsp "github.com/sourcegraph/go-lsp"
)

Expand All @@ -15,7 +15,7 @@ func Exit(ctx context.Context, vs lsp.None) error {
return err
}

log.Info("Exited")
log.Info("Exited")
os.Exit(0)
return nil
}
6 changes: 3 additions & 3 deletions langserver/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package langserver

import (
"context"
"github.com/juliosueiras/terraform-lsp/memfs"
log "github.com/sirupsen/logrus"
lsp "github.com/sourcegraph/go-lsp"
"github.com/juliosueiras/terraform-lsp/memfs"
"github.com/spf13/afero"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

func Initialize(ctx context.Context, vs lsp.InitializeParams) (lsp.InitializeResult, error) {
Expand Down
6 changes: 3 additions & 3 deletions langserver/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package langserver

import (
"context"
"github.com/juliosueiras/terraform-lsp/memfs"
"github.com/juliosueiras/terraform-lsp/memfs"

log "github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
lsp "github.com/sourcegraph/go-lsp"
)

Expand All @@ -14,6 +14,6 @@ func Shutdown(ctx context.Context, vs lsp.None) error {
return err
}

log.Info("Shutdown")
log.Info("Shutdown")
return nil
}
2 changes: 1 addition & 1 deletion langserver/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package langserver

import (
"github.com/creachadair/jrpc2"
"github.com/spf13/afero"
lsp "github.com/sourcegraph/go-lsp"
"github.com/spf13/afero"
)

var tempFile afero.File
Expand Down
23 changes: 11 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"flag"
"fmt"
log "github.com/sirupsen/logrus"
oldLog "log"
"os"
"strings"
oldLog "log"
log "github.com/sirupsen/logrus"

"io/ioutil"
"github.com/creachadair/jrpc2/channel"
"github.com/juliosueiras/terraform-lsp/langserver"
"io/ioutil"
)

var location = flag.String("log-location", "", "Location of the lsp log")
Expand All @@ -26,9 +26,8 @@ var version = flag.Bool("version", false, "Show version")
func main() {
flag.Parse()

oldLog.SetOutput(ioutil.Discard)
oldLog.SetFlags(0)

oldLog.SetOutput(ioutil.Discard)
oldLog.SetFlags(0)

if *version {
fmt.Printf("v%s, commit: %s, build on: %s", strings.Trim(Version, "v"), GitCommit, Date)
Expand All @@ -37,13 +36,13 @@ func main() {

Server := langserver.CreateServer()

log.Infof("Log Level is Debug: %t", *debug)
log.Infof("Log Level is Debug: %t", *debug)

if *debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
if *debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}

if *enableLogFile {
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp.log", *location), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
Expand Down
127 changes: 70 additions & 57 deletions tfstructs/diags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@ package tfstructs

import (
"fmt"
"github.com/hashicorp/terraform/configs"
v2 "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/configs"
"github.com/zclconf/go-cty/cty"
"net/url"
"strings"
"unicode/utf8"
//"github.com/juliosueiras/terraform-lsp/helper"
"github.com/juliosueiras/terraform-lsp/memfs"
terragruntConfig "github.com/gruntwork-io/terragrunt/config"
terragruntOptions "github.com/gruntwork-io/terragrunt/options"
oldHCL2 "github.com/hashicorp/hcl2/hcl"
terragruntConfig "github.com/gruntwork-io/terragrunt/config"
terragruntOptions "github.com/gruntwork-io/terragrunt/options"
"github.com/juliosueiras/terraform-lsp/memfs"
"github.com/sourcegraph/go-lsp"
"github.com/spf13/afero"
"path/filepath"
)

func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {

parser := configs.NewParser(memfs.MemFs)
result := make([]lsp.Diagnostic, 0)
originalFileName := originalFile


originalFileNameDecoded, _ := url.QueryUnescape(originalFileName)

if strings.Contains(originalFileNameDecoded, ":/") {
s, i := utf8.DecodeRuneInString("/")
if []rune(originalFileNameDecoded)[0] == s {
// https://stackoverflow.com/questions/48798588/how-do-you-remove-the-first-character-of-a-string
originalFileNameDecoded = originalFileNameDecoded[i:]
}
}

if exist, _ := afero.Exists(memfs.MemFs, fileName); !exist {
return result
}
Expand All @@ -29,48 +42,48 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
originalFile = fileName
}

var hclDiags v2.Diagnostics
isTFVars := (filepath.Ext(originalFile) == ".tfvars")
isTerragrunt := (filepath.Base(originalFile) == "terragrunt.hcl")

var diagName string

if isTFVars {
_, hclDiags = parser.LoadValuesFile(fileName)
diagName = "TFVars"
} else if isTerragrunt {
fileContent, _ := afero.ReadFile(memfs.MemFs, fileName)

_, terragruntDiags := terragruntConfig.ParseConfigString(string(fileContent), &terragruntOptions.TerragruntOptions{}, &terragruntConfig.IncludeConfig{}, originalFile)

if terragruntDiags == nil {
return result
}

for _, diag := range terragruntDiags.(oldHCL2.Diagnostics) {
result = append(result, lsp.Diagnostic{
Severity: lsp.DiagnosticSeverity(diag.Severity),
Message: diag.Detail,
Range: lsp.Range{
Start: lsp.Position{
Line: diag.Subject.Start.Line - 1,
Character: diag.Subject.Start.Column - 1,
},
End: lsp.Position{
Line: diag.Subject.End.Line - 1,
Character: diag.Subject.End.Column - 1,
},
},
Source: "Terragrunt",
})
}

return result

} else {
_, hclDiags = parser.LoadHCLFile(fileName)
diagName = "HCL"
}
var hclDiags v2.Diagnostics
isTFVars := (filepath.Ext(originalFile) == ".tfvars")
isTerragrunt := (filepath.Base(originalFile) == "terragrunt.hcl")

var diagName string

if isTFVars {
_, hclDiags = parser.LoadValuesFile(fileName)
diagName = "TFVars"
} else if isTerragrunt {
fileContent, _ := afero.ReadFile(memfs.MemFs, fileName)

_, terragruntDiags := terragruntConfig.ParseConfigString(string(fileContent), &terragruntOptions.TerragruntOptions{}, &terragruntConfig.IncludeConfig{}, originalFile)

if terragruntDiags == nil {
return result
}

for _, diag := range terragruntDiags.(oldHCL2.Diagnostics) {
result = append(result, lsp.Diagnostic{
Severity: lsp.DiagnosticSeverity(diag.Severity),
Message: diag.Detail,
Range: lsp.Range{
Start: lsp.Position{
Line: diag.Subject.Start.Line - 1,
Character: diag.Subject.Start.Column - 1,
},
End: lsp.Position{
Line: diag.Subject.End.Line - 1,
Character: diag.Subject.End.Column - 1,
},
},
Source: "Terragrunt",
})
}

return result

} else {
_, hclDiags = parser.LoadHCLFile(fileName)
diagName = "HCL"
}

for _, diag := range hclDiags {
result = append(result, lsp.Diagnostic{
Expand All @@ -86,13 +99,13 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
Character: diag.Subject.End.Column - 1,
},
},
Source: diagName,
Source: diagName,
})
}

if isTFVars {
return result
}
if isTFVars {
return result
}

cfg, tfDiags := parser.LoadConfigFile(fileName)
parser.ForceFileSource(originalFileName, []byte(""))
Expand Down Expand Up @@ -120,17 +133,17 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {

variables := map[string]cty.Value{
"path": cty.ObjectVal(map[string]cty.Value{
"cwd": cty.StringVal(filepath.Dir(originalFile)),
"module": cty.StringVal(filepath.Dir(originalFile)),
"cwd": cty.StringVal(filepath.Dir(originalFileNameDecoded)),
"module": cty.StringVal(filepath.Dir(originalFileNameDecoded)),
}),
"var": cty.DynamicVal, // Need to check for undefined vars
"module": cty.DynamicVal,
"local": cty.DynamicVal,
"each": cty.DynamicVal,
"count": cty.DynamicVal,
"terraform": cty.ObjectVal(map[string]cty.Value{
"workspace": cty.StringVal(""),
}),
"terraform": cty.ObjectVal(map[string]cty.Value{
"workspace": cty.StringVal(""),
}),
}

for k, v := range resourceTypes {
Expand Down
Loading

0 comments on commit 71b6183

Please sign in to comment.