diff --git a/langserver/did_change.go b/langserver/did_change.go index 693795b..a113bca 100644 --- a/langserver/did_change.go +++ b/langserver/did_change.go @@ -19,9 +19,9 @@ func TextDocumentDidChange(ctx context.Context, vs lsp.DidChangeTextDocumentPara DiagsFiles[fileURL] = tfstructs.GetDiagnostics(tempFile.Name(), fileURL) - TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{ - URI: vs.TextDocument.URI, - Diagnostics: DiagsFiles[fileURL], - }) + TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{ + URI: vs.TextDocument.URI, + Diagnostics: DiagsFiles[fileURL], + }) return nil } diff --git a/langserver/did_open.go b/langserver/did_open.go index 9859321..e2089e5 100644 --- a/langserver/did_open.go +++ b/langserver/did_open.go @@ -15,10 +15,10 @@ func TextDocumentDidOpen(ctx context.Context, vs lsp.DidOpenTextDocumentParams) DiagsFiles[fileURL] = tfstructs.GetDiagnostics(fileURL, fileURL) - TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{ - URI: vs.TextDocument.URI, - Diagnostics: DiagsFiles[fileURL], - }) + TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{ + URI: vs.TextDocument.URI, + Diagnostics: DiagsFiles[fileURL], + }) tempFile.Write([]byte(vs.TextDocument.Text)) return nil } diff --git a/langserver/initialized.go b/langserver/initialized.go index 5410054..0a402ab 100644 --- a/langserver/initialized.go +++ b/langserver/initialized.go @@ -4,8 +4,8 @@ import ( "context" ) -type InitializedParams struct {} +type InitializedParams struct{} func Initialized(ctx context.Context, vs InitializedParams) error { - return nil + return nil } diff --git a/langserver/main.go b/langserver/main.go index 4af1fed..f007f95 100644 --- a/langserver/main.go +++ b/langserver/main.go @@ -2,23 +2,23 @@ package langserver import ( "context" - "fmt" + "fmt" "github.com/creachadair/jrpc2" "github.com/creachadair/jrpc2/channel" "github.com/creachadair/jrpc2/handler" "github.com/creachadair/jrpc2/server" log "github.com/sirupsen/logrus" - oldLog "log" + oldLog "log" "net" "os" ) func RunStdioServer(oldLogInstance *oldLog.Logger) { - isTCP = false + isTCP = false StdioServer = jrpc2.NewServer(ServiceMap, &jrpc2.ServerOptions{ AllowPush: true, - Logger: oldLogInstance, + Logger: oldLogInstance, }) StdioServer.Start(channel.Header("")(os.Stdin, os.Stdout)) @@ -34,7 +34,7 @@ func RunStdioServer(oldLogInstance *oldLog.Logger) { } func RunTCPServer(address string, port int, oldLogInstance *oldLog.Logger) { - isTCP = true + isTCP = true lst, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port)) if err != nil { @@ -51,9 +51,9 @@ func RunTCPServer(address string, port int, oldLogInstance *oldLog.Logger) { if err := server.Loop(lst, ServiceMap, &server.LoopOptions{ Framing: newChan, ServerOptions: &jrpc2.ServerOptions{ - AllowPush: true, - Logger: oldLogInstance, - }, + AllowPush: true, + Logger: oldLogInstance, + }, }); err != nil { log.Errorf("Loop: unexpected failure: %v", err) cancelFunc() @@ -76,7 +76,7 @@ func RunTCPServer(address string, port int, oldLogInstance *oldLog.Logger) { func InitializeServiceMap() { ServiceMap = handler.Map{ "initialize": handler.New(Initialize), - "initialized": handler.New(Initialized), + "initialized": handler.New(Initialized), "textDocument/completion": handler.New(TextDocumentComplete), "textDocument/didChange": handler.New(TextDocumentDidChange), "textDocument/didOpen": handler.New(TextDocumentDidOpen), diff --git a/langserver/publish_diagnostics.go b/langserver/publish_diagnostics.go index 7f67af9..af51205 100644 --- a/langserver/publish_diagnostics.go +++ b/langserver/publish_diagnostics.go @@ -8,13 +8,13 @@ import ( func TextDocumentPublishDiagnostics(ctx context.Context, vs lsp.PublishDiagnosticsParams) error { - var resultedError error + var resultedError error - if isTCP { - resultedError = jrpc2.ServerPush(ctx, "textDocument/publishDiagnostics", vs) - } else { - resultedError = StdioServer.Push(ctx, "textDocument/publishDiagnostics", vs) - } + if isTCP { + resultedError = jrpc2.ServerPush(ctx, "textDocument/publishDiagnostics", vs) + } else { + resultedError = StdioServer.Push(ctx, "textDocument/publishDiagnostics", vs) + } return resultedError } diff --git a/main.go b/main.go index f1b9dfe..1d90172 100644 --- a/main.go +++ b/main.go @@ -59,25 +59,25 @@ func main() { log.SetOutput(f) } - var oldLogInstance *oldLog.Logger - - if *debugJRPC2 { - if !*tcp && !*enableLogFileJRPC2 { - log.Fatal("Debug for JRPC2 has to be set for log file location if is set to use stdio") - } - - oldLogInstance = oldLog.New(os.Stdout, "", 0) - if *enableLogFileJRPC2 { - f, err := os.OpenFile(fmt.Sprintf("%stf-lsp-jrpc2.log", *locationJRPC2), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - log.Fatalf("error opening file: %v", err) - } - defer f.Close() - oldLogInstance.SetOutput(f) - } + var oldLogInstance *oldLog.Logger + + if *debugJRPC2 { + if !*tcp && !*enableLogFileJRPC2 { + log.Fatal("Debug for JRPC2 has to be set for log file location if is set to use stdio") + } + + oldLogInstance = oldLog.New(os.Stdout, "", 0) + if *enableLogFileJRPC2 { + f, err := os.OpenFile(fmt.Sprintf("%stf-lsp-jrpc2.log", *locationJRPC2), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + log.Fatalf("error opening file: %v", err) + } + defer f.Close() + oldLogInstance.SetOutput(f) + } } - langserver.InitializeServiceMap() + langserver.InitializeServiceMap() if *tcp { langserver.RunTCPServer(*address, *port, oldLogInstance) diff --git a/tfstructs/diags.go b/tfstructs/diags.go index 4af2453..5f9a036 100644 --- a/tfstructs/diags.go +++ b/tfstructs/diags.go @@ -2,10 +2,10 @@ package tfstructs import ( "fmt" - "os" v2 "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform/configs" "github.com/zclconf/go-cty/cty" + "os" //"github.com/juliosueiras/terraform-lsp/helper" terragruntConfig "github.com/gruntwork-io/terragrunt/config" terragruntOptions "github.com/gruntwork-io/terragrunt/options" @@ -120,13 +120,13 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic { resourceTypes[v.Type][v.Name] = cty.DynamicVal } - targetDir := filepath.Dir(originalFileName) + targetDir := filepath.Dir(originalFileName) - resultedDir := "" + resultedDir := "" searchLevel := 4 for dir := targetDir; dir != "" && searchLevel != 0; dir = filepath.Dir(dir) { if _, err := os.Stat(filepath.Join(dir, ".terraform")); err == nil { - resultedDir = dir + resultedDir = dir break } searchLevel -= 1 @@ -136,7 +136,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic { "path": cty.ObjectVal(map[string]cty.Value{ "cwd": cty.StringVal(filepath.Dir(originalFileName)), "module": cty.StringVal(filepath.Dir(originalFileName)), - "root": cty.StringVal(resultedDir), + "root": cty.StringVal(resultedDir), }), "var": cty.DynamicVal, // Need to check for undefined vars "module": cty.DynamicVal, @@ -198,7 +198,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic { }) } - for _, local := range cfg.Locals { + for _, local := range cfg.Locals { diags := GetLocalsForDiags(*local, filepath.Dir(originalFileName), variables) if diags != nil { @@ -220,7 +220,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic { }) } } - } + } // cfg, diags := configload.NewLoader(&configload.Config{ // ModulesDir: ".terraform/modules", diff --git a/tfstructs/main.go b/tfstructs/main.go index 20e8578..2b25689 100644 --- a/tfstructs/main.go +++ b/tfstructs/main.go @@ -40,14 +40,14 @@ func GetModuleVariables(moduleAddr string, config hcl.Body, targetDir string) (m func GetLocalsForDiags(local configs.Local, targetDir string, variables map[string]cty.Value) hcl.Diagnostics { scope := lang.Scope{} - //_, diags := scope.EvalExpr(local, cty.DynamicPseudoType) - _, diags := local.Expr.Value( - &hcl.EvalContext{ - // Build Full Tree - Variables: variables, - Functions: scope.Functions(), - }, - ) + //_, diags := scope.EvalExpr(local, cty.DynamicPseudoType) + _, diags := local.Expr.Value( + &hcl.EvalContext{ + // Build Full Tree + Variables: variables, + Functions: scope.Functions(), + }, + ) //res, _, diags := hcldec.PartialDecode(config, nil, &hcl.EvalContext{ // // Build Full Tree // Variables: variables, diff --git a/tfstructs/vars.go b/tfstructs/vars.go index a617670..eff55c9 100644 --- a/tfstructs/vars.go +++ b/tfstructs/vars.go @@ -6,14 +6,14 @@ import ( "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/configs" + "github.com/hashicorp/terraform/lang" "github.com/juliosueiras/terraform-lsp/hclstructs" - "github.com/zclconf/go-cty/cty" "github.com/juliosueiras/terraform-lsp/helper" - "github.com/hashicorp/terraform/lang" "github.com/sourcegraph/go-lsp" + "github.com/zclconf/go-cty/cty" + "os" + "path/filepath" "reflect" - "path/filepath" - "os" ) type GetVarAttributeRequest struct { @@ -27,36 +27,35 @@ type GetVarAttributeRequest struct { func GetVarAttributeCompletion(request GetVarAttributeRequest) []lsp.CompletionItem { scope := lang.Scope{} - targetDir := request.FileDir + targetDir := request.FileDir - resultedDir := "" + resultedDir := "" searchLevel := 4 for dir := targetDir; dir != "" && searchLevel != 0; dir = filepath.Dir(dir) { if _, err := os.Stat(filepath.Join(dir, ".terraform")); err == nil { - resultedDir = dir + resultedDir = dir break } searchLevel -= 1 } - helper.DumpLog(targetDir) - - variables := map[string]cty.Value{ - "path": cty.ObjectVal(map[string]cty.Value{ - "cwd": cty.StringVal(request.FileDir), - "module": cty.StringVal(request.FileDir), - "root": cty.StringVal(resultedDir), - }), - "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(""), - }), - } - + helper.DumpLog(targetDir) + + variables := map[string]cty.Value{ + "path": cty.ObjectVal(map[string]cty.Value{ + "cwd": cty.StringVal(request.FileDir), + "module": cty.StringVal(request.FileDir), + "root": cty.StringVal(resultedDir), + }), + "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(""), + }), + } if request.Variables.RootName() == "var" { vars := request.Variables @@ -72,18 +71,17 @@ func GetVarAttributeCompletion(request GetVarAttributeRequest) []lsp.CompletionI } } + testVal, _ := found.Expr.Value( + &hcl.EvalContext{ + // Build Full Tree + Variables: variables, + Functions: scope.Functions(), + }, + ) - testVal, _ := found.Expr.Value( - &hcl.EvalContext{ - // Build Full Tree - Variables: variables, - Functions: scope.Functions(), - }, - ) - - helper.DumpLog(testVal) + helper.DumpLog(testVal) - origType := reflect.TypeOf(found.Expr) + origType := reflect.TypeOf(found.Expr) if origType == hclstructs.ObjectConsExpr() { items := found.Expr.(*hclsyntax.ObjectConsExpr).Items @@ -109,9 +107,9 @@ func GetVarAttributeCompletion(request GetVarAttributeRequest) []lsp.CompletionI } } - helper.DumpLog(request.Variables[2:]) - helper.DumpLog(testVal.Type()) - request.Result = append(request.Result, helper.ParseOtherAttr(request.Variables[2:], testVal.Type(), request.Result)...) + helper.DumpLog(request.Variables[2:]) + helper.DumpLog(testVal.Type()) + request.Result = append(request.Result, helper.ParseOtherAttr(request.Variables[2:], testVal.Type(), request.Result)...) return request.Result