Skip to content

Commit

Permalink
Merge branch 'master' into fix-uri-scheme-stripping
Browse files Browse the repository at this point in the history
  • Loading branch information
juliosueiras authored Feb 22, 2020
2 parents ef4774b + 3a7608c commit 4bd8b99
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 59 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/creack/pty v1.1.9 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-language-server/uri v0.2.0 // indirect
github.com/go-language-server/uri v0.2.0
github.com/gogo/protobuf v1.2.1 // indirect
github.com/gruntwork-io/terragrunt v0.21.13
github.com/hashicorp/go-hclog v0.9.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions langserver/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/lang"
lsp "github.com/sourcegraph/go-lsp"
"net/url"
"path/filepath"
"reflect"
"regexp"
Expand All @@ -31,14 +30,13 @@ func TextDocumentComplete(ctx context.Context, vs lsp.CompletionParams) (lsp.Com
}
fileURL := uri.Filename()

decodedFileURL, _ := url.QueryUnescape(fileURL)
fileDir := filepath.Dir(decodedFileURL)
fileDir := filepath.Dir(fileURL)
res, _ := filepath.Glob(fileDir + "/*.tf")
var file *configs.File
var resultFiles []*configs.File

for _, v := range res {
if strings.ToLower(decodedFileURL) == strings.ToLower(v) {
if strings.ToLower(fileURL) == strings.ToLower(v) {
continue
}

Expand Down
43 changes: 43 additions & 0 deletions langserver/document_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package langserver

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

type documentLinkParams struct {
TextDocument lsp.TextDocumentItem `json:"textDocument"`
}

type DocumentLink struct {
Range lsp.Range `json:"range"`

/**
* The uri this link points to. If missing a resolve request is sent later.
*/
Target string `json:"target"`

Tooltip string `json:"tooltip"`
}

func TextDocumentDocumentLink(ctx context.Context, vs documentLinkParams) ([]DocumentLink, error) {
log.Info(vs)

return []DocumentLink{
DocumentLink{
Range: lsp.Range{
Start: lsp.Position{
Line: 1,
Character: 1,
},
End: lsp.Position{
Line: 1,
Character: 10,
},
},
Target: "https://github.com",
Tooltip: "https://github.com",
},
}, nil
}
24 changes: 21 additions & 3 deletions langserver/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@ import (
"github.com/spf13/afero"
)

func Initialize(ctx context.Context, vs lsp.InitializeParams) (lsp.InitializeResult, error) {
type DocumentLinkOptions struct {
ResolveProvider bool `json:"resolveProvider,omitempty"`
}

type ExtendedServerCapabilities struct {
TextDocumentSync *lsp.TextDocumentSyncOptionsOrKind `json:"textDocumentSync,omitempty"`
CompletionProvider *lsp.CompletionOptions `json:"completionProvider,omitempty"`
HoverProvider bool `json:"hoverProvider,omitempty"`
DocumentLinkProvider *DocumentLinkOptions `json:"documentLinkProvider,omitempty"`
}

type ExtendedInitializeResult struct {
Capabilities ExtendedServerCapabilities `json:"capabilities"`
}

func Initialize(ctx context.Context, vs lsp.InitializeParams) (ExtendedInitializeResult, error) {
file, err := afero.TempFile(memfs.MemFs, "", "tf-lsp-")
if err != nil {
log.Fatal(err)
}
//defer os.Remove(file.Name())
tempFile = file

return lsp.InitializeResult{
Capabilities: lsp.ServerCapabilities{
return ExtendedInitializeResult{
Capabilities: ExtendedServerCapabilities{
TextDocumentSync: &lsp.TextDocumentSyncOptionsOrKind{
Options: &lsp.TextDocumentSyncOptions{
OpenClose: true,
Expand All @@ -29,6 +44,9 @@ func Initialize(ctx context.Context, vs lsp.InitializeParams) (lsp.InitializeRes
TriggerCharacters: []string{"."},
},
HoverProvider: false,
DocumentLinkProvider: &DocumentLinkOptions{
ResolveProvider: false,
},
},
}, nil
}
1 change: 1 addition & 0 deletions langserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func CreateServer() *jrpc2.Server {
"textDocument/didChange": handler.New(TextDocumentDidChange),
"textDocument/didOpen": handler.New(TextDocumentDidOpen),
"textDocument/didClose": handler.New(TextDocumentDidClose),
"textDocument/documentLink": handler.New(TextDocumentDocumentLink),
//"textDocument/hover": handler.New(TextDocumentHover),
//"textDocument/references": handler.New(TextDocumentReferences),
//"textDocument/codeLens": handler.New(TextDocumentCodeLens),
Expand Down
34 changes: 7 additions & 27 deletions tfstructs/diags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
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"
terragruntConfig "github.com/gruntwork-io/terragrunt/config"
terragruntOptions "github.com/gruntwork-io/terragrunt/options"
Expand All @@ -26,16 +23,6 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
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 Down Expand Up @@ -133,15 +120,8 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
resourceTypes[v.Type][v.Name] = cty.DynamicVal
}

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:]
}
}
targetDir := filepath.Dir(originalFileName)

targetDir := filepath.Dir(originalFileNameDecoded)
resultedDir := ""
searchLevel := 4
for dir := targetDir; dir != "" && searchLevel != 0; dir = filepath.Dir(dir) {
Expand All @@ -154,8 +134,8 @@ 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(originalFileNameDecoded)),
"module": cty.StringVal(filepath.Dir(originalFileNameDecoded)),
"cwd": cty.StringVal(filepath.Dir(originalFileName)),
"module": cty.StringVal(filepath.Dir(originalFileName)),
"root": cty.StringVal(resultedDir),
}),
"var": cty.DynamicVal, // Need to check for undefined vars
Expand Down Expand Up @@ -219,7 +199,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
}

for _, local := range cfg.Locals {
diags := GetLocalsForDiags(*local, filepath.Dir(originalFileNameDecoded), variables)
diags := GetLocalsForDiags(*local, filepath.Dir(originalFileName), variables)

if diags != nil {
for _, diag := range diags {
Expand Down Expand Up @@ -269,7 +249,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
for _, v := range cfg.ProviderConfigs {
providerType := v.Name

tfSchema := GetProviderSchemaForDiags(providerType, v.Config, filepath.Dir(originalFileNameDecoded), variables)
tfSchema := GetProviderSchemaForDiags(providerType, v.Config, filepath.Dir(originalFileName), variables)

if tfSchema != nil {
for _, diag := range tfSchema.Diags {
Expand Down Expand Up @@ -316,7 +296,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
providerType = v.ProviderConfigRef.Name
}

tfSchema := GetResourceSchemaForDiags(resourceType, v.Config, filepath.Dir(originalFileNameDecoded), providerType, variables)
tfSchema := GetResourceSchemaForDiags(resourceType, v.Config, filepath.Dir(originalFileName), providerType, variables)

if tfSchema != nil {
for _, diag := range tfSchema.Diags {
Expand Down Expand Up @@ -362,7 +342,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
providerType = v.ProviderConfigRef.Name
}

tfSchema := GetDataSourceSchemaForDiags(resourceType, v.Config, filepath.Dir(originalFileNameDecoded), providerType, variables)
tfSchema := GetDataSourceSchemaForDiags(resourceType, v.Config, filepath.Dir(originalFileName), providerType, variables)

if tfSchema != nil {
for _, diag := range tfSchema.Diags {
Expand Down
9 changes: 0 additions & 9 deletions tfstructs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tfstructs

import (
"fmt"
"unicode/utf8"
log "github.com/sirupsen/logrus"
"go/build"
"io/ioutil"
Expand Down Expand Up @@ -189,14 +188,6 @@ func pluginDirs(targetDir string) ([]string, error) {

searchLevel := 4

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

for dir := targetDir; dir != "" && searchLevel != 0; dir = filepath.Dir(dir) {

log.Debug("[DEBUG] search .terraform dir in %s", dir)
Expand Down
19 changes: 4 additions & 15 deletions tfstructs/vars.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package tfstructs

import (
"unicode/utf8"
"strings"
"net/url"
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand All @@ -29,16 +26,9 @@ type GetVarAttributeRequest struct {

func GetVarAttributeCompletion(request GetVarAttributeRequest) []lsp.CompletionItem {
scope := lang.Scope{}
fileDir, _ := url.QueryUnescape(request.FileDir)
if strings.Contains(fileDir, "\\") {
s, i := utf8.DecodeRuneInString("\\")
if []rune(fileDir)[0] == s {
// https://stackoverflow.com/questions/48798588/how-do-you-remove-the-first-character-of-a-string
fileDir = fileDir[i:]
}
}

targetDir := filepath.Dir(fileDir)
targetDir := request.FileDir

resultedDir := ""
searchLevel := 4
for dir := targetDir; dir != "" && searchLevel != 0; dir = filepath.Dir(dir) {
Expand All @@ -49,13 +39,12 @@ func GetVarAttributeCompletion(request GetVarAttributeRequest) []lsp.CompletionI
searchLevel -= 1
}

helper.DumpLog(fileDir)
helper.DumpLog(targetDir)

variables := map[string]cty.Value{
"path": cty.ObjectVal(map[string]cty.Value{
"cwd": cty.StringVal(fileDir),
"module": cty.StringVal(fileDir),
"cwd": cty.StringVal(request.FileDir),
"module": cty.StringVal(request.FileDir),
"root": cty.StringVal(resultedDir),
}),
"var": cty.DynamicVal, // Need to check for undefined vars
Expand Down

0 comments on commit 4bd8b99

Please sign in to comment.