Skip to content

Commit

Permalink
Add support for expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 25, 2021
1 parent 368303e commit 148328d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 32 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ require (
github.com/google/uuid v1.2.0
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/hcl-lang v0.0.0-20210213170001-bd00c3f68680
github.com/hashicorp/hcl-lang v0.0.0-20210225100447-6bec52147206
github.com/hashicorp/hcl/v2 v2.9.0
github.com/hashicorp/terraform-exec v0.13.0
github.com/hashicorp/terraform-json v0.8.0
github.com/hashicorp/terraform-schema v0.0.0-20210213170531-2b07abee4703
github.com/hashicorp/terraform-schema v0.0.0-20210225081637-ef7973ce1ac4
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.2
github.com/mitchellh/go-homedir v1.1.0
Expand Down
21 changes: 10 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYU
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -184,10 +183,11 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20210213170001-bd00c3f68680 h1:QjPFluG2uGJCBKIp5sEFx+UZ98QnuxmVsnLIuHz8a2Y=
github.com/hashicorp/hcl-lang v0.0.0-20210213170001-bd00c3f68680/go.mod h1:TZ5tpvmgJSHfmIndN4WP9SpZvyWK8tHPBY8LDRyU+pI=
github.com/hashicorp/hcl-lang v0.0.0-20210225075938-01f0b453dd63 h1:ixUQf+oecfFaMpg6gUNc8LymYKQf+OfmRpxtGL34XtU=
github.com/hashicorp/hcl-lang v0.0.0-20210225075938-01f0b453dd63/go.mod h1:jVqv6gpR2SOPIC6j5HFG/KavqCdfLZED5QitQuV7JTU=
github.com/hashicorp/hcl-lang v0.0.0-20210225100447-6bec52147206 h1:5AuophNCp8CefXiO9MJvl5yWYG+Mwcdqbc6MdYX+zJA=
github.com/hashicorp/hcl-lang v0.0.0-20210225100447-6bec52147206/go.mod h1:jVqv6gpR2SOPIC6j5HFG/KavqCdfLZED5QitQuV7JTU=
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.6.0/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/hcl/v2 v2.9.0 h1:7kJiMiKBqGHASbDJuFAMlpRMJLyhuLg/IsU/3EzwniA=
github.com/hashicorp/hcl/v2 v2.9.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand All @@ -198,11 +198,10 @@ github.com/hashicorp/terraform-config-inspect v0.0.0-20201102131242-0c45ba392e51
github.com/hashicorp/terraform-config-inspect v0.0.0-20201102131242-0c45ba392e51/go.mod h1:Z0Nnk4+3Cy89smEbrq+sl1bxc9198gIP4I7wcQF6Kqs=
github.com/hashicorp/terraform-exec v0.13.0 h1:1Pth+pdWJAufJuWWjaVOVNEkoRTOjGn3hQpAqj4aPdg=
github.com/hashicorp/terraform-exec v0.13.0/go.mod h1:SGhto91bVRlgXQWcJ5znSz+29UZIa8kpBbkGwQ+g9E8=
github.com/hashicorp/terraform-json v0.7.0/go.mod h1:3defM4kkMfttwiE7VakJDwCd4R+umhSQnvJwORXbprE=
github.com/hashicorp/terraform-json v0.8.0 h1:XObQ3PgqU52YLQKEaJ08QtUshAfN3yu4u8ebSW0vztc=
github.com/hashicorp/terraform-json v0.8.0/go.mod h1:3defM4kkMfttwiE7VakJDwCd4R+umhSQnvJwORXbprE=
github.com/hashicorp/terraform-schema v0.0.0-20210213170531-2b07abee4703 h1:u1otHGsvYAQcQ3aJl+TJjkFRdxJ1I8n7O9uGOlFw6oE=
github.com/hashicorp/terraform-schema v0.0.0-20210213170531-2b07abee4703/go.mod h1:6UFjY3ZeFEExipVkJOLzMJzBBosso6MoAyLRHgDIllw=
github.com/hashicorp/terraform-schema v0.0.0-20210225081637-ef7973ce1ac4 h1:HYVGsGlyKHWPVqmb3t4ynllAqOAmySKxUps0ruF0jLc=
github.com/hashicorp/terraform-schema v0.0.0-20210225081637-ef7973ce1ac4/go.mod h1:YXVdD7QkOH56Sihp7n5nlhgZg8zJY+W6lXWCMJ7vCaU=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
Expand Down Expand Up @@ -257,8 +256,9 @@ github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceT
github.com/mitchellh/cli v1.1.1/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
github.com/mitchellh/cli v1.1.2 h1:PvH+lL2B7IQ101xQL63Of8yFS2y+aDlsFcsqNc+u/Kw=
github.com/mitchellh/cli v1.1.2/go.mod h1:6iaV0fGdElS6dPBx0EApTxHrcWvmJphyh2n8YBLPPZ4=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/copystructure v1.1.1 h1:Bp6x9R1Wn16SIz3OfeDr0b7RnCG2OB66Y7PQyC/cvq4=
github.com/mitchellh/copystructure v1.1.1/go.mod h1:EBArHfARyrSWO/+Wyr9zwEkc6XMFB9XyNgFNmRkZZU4=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand All @@ -273,8 +273,9 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE=
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down Expand Up @@ -367,8 +368,6 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.6.1/go.mod h1:VDR4+I79ubFBGm1uJac1226K5yANQFHeauxPBoP54+o=
github.com/zclconf/go-cty v1.7.1-0.20201110003513-1338293a79a9/go.mod h1:VDR4+I79ubFBGm1uJac1226K5yANQFHeauxPBoP54+o=
github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI=
Expand Down
12 changes: 12 additions & 0 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ func toCompletionItem(candidate lang.Candidate, caps lsp.CompletionClientCapabil
kind = lsp.ClassCompletion
case lang.LabelCandidateKind:
kind = lsp.FieldCompletion
case lang.BoolCandidateKind:
kind = lsp.EnumMemberCompletion
case lang.StringCandidateKind:
kind = lsp.TextCompletion
case lang.NumberCandidateKind:
kind = lsp.ValueCompletion
case lang.KeywordCandidateKind:
kind = lsp.KeywordCompletion
case lang.ListCandidateKind, lang.SetCandidateKind, lang.TupleCandidateKind:
kind = lsp.EnumCompletion
case lang.MapCandidateKind, lang.ObjectCandidateKind:
kind = lsp.StructCompletion
}

// TODO: Omit item which uses kind unsupported by the client
Expand Down
67 changes: 48 additions & 19 deletions internal/lsp/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lsp

import (
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/zclconf/go-cty/cty"
)
Expand All @@ -10,45 +11,73 @@ func DocumentSymbols(sbs []decoder.Symbol, caps lsp.DocumentSymbolClientCapabili
symbols := make([]lsp.DocumentSymbol, len(sbs))

for i, s := range sbs {
kind, ok := symbolKind(s, caps.SymbolKind.ValueSet)
symbol, ok := documentSymbol(s, caps)
if !ok {
// skip symbol not supported by client
continue
}

symbols[i] = lsp.DocumentSymbol{
Name: s.Name(),
Kind: kind,
Range: HCLRangeToLSP(s.Range()),
SelectionRange: HCLRangeToLSP(s.Range()),
}

if caps.HierarchicalDocumentSymbolSupport {
symbols[i].Children = DocumentSymbols(s.NestedSymbols(), caps)
}
symbols[i] = symbol
}
return symbols
}

func symbolKind(symbol decoder.Symbol, supported []lsp.SymbolKind) (lsp.SymbolKind, bool) {
func documentSymbol(symbol decoder.Symbol, caps lsp.DocumentSymbolClientCapabilities) (lsp.DocumentSymbol, bool) {
supported := caps.SymbolKind.ValueSet

var kind lsp.SymbolKind
var ok bool

switch s := symbol.(type) {
case *decoder.BlockSymbol:
return supportedSymbolKind(supported, lsp.Class)
kind, ok = supportedSymbolKind(supported, lsp.Class)
if !ok {
return lsp.DocumentSymbol{}, false
}

case *decoder.AttributeSymbol:
// Only primitive types are supported at this point
switch s.Type {
kind, ok = exprSymbolKind(s.ExprKind, supported)
if !ok {
return lsp.DocumentSymbol{}, false
}
case *decoder.ExprSymbol:
kind, ok = exprSymbolKind(s.ExprKind, supported)
if !ok {
return lsp.DocumentSymbol{}, false
}
}

ds := lsp.DocumentSymbol{
Name: symbol.Name(),
Kind: kind,
Range: HCLRangeToLSP(symbol.Range()),
SelectionRange: HCLRangeToLSP(symbol.Range()),
}
if caps.HierarchicalDocumentSymbolSupport {
ds.Children = DocumentSymbols(symbol.NestedSymbols(), caps)
}
return ds, true
}

func exprSymbolKind(symbolKind lang.SymbolExprKind, supported []lsp.SymbolKind) (lsp.SymbolKind, bool) {
switch k := symbolKind.(type) {
case lang.LiteralTypeKind:
switch k.Type {
case cty.Bool:
return supportedSymbolKind(supported, lsp.Boolean)
case cty.String:
return supportedSymbolKind(supported, lsp.String)
case cty.Number:
return supportedSymbolKind(supported, lsp.Number)
}

return supportedSymbolKind(supported, lsp.Variable)
case lang.TraversalExprKind:
return supportedSymbolKind(supported, lsp.Constant)
case lang.TupleConsExprKind:
return supportedSymbolKind(supported, lsp.Array)
case lang.ObjectConsExprKind:
return supportedSymbolKind(supported, lsp.Struct)
}

return lsp.SymbolKind(0), false
return supportedSymbolKind(supported, lsp.Variable)
}

func supportedSymbolKind(supported []lsp.SymbolKind, kind lsp.SymbolKind) (lsp.SymbolKind, bool) {
Expand Down
11 changes: 11 additions & 0 deletions internal/lsp/token_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ func (te *TokenEncoder) encodeTokenOfIndex(i int) []float64 {
tokenType = TokenTypeString
case lang.TokenAttrName:
tokenType = TokenTypeProperty
case lang.TokenBool:
tokenType = TokenTypeKeyword
case lang.TokenNumber:
tokenType = TokenTypeNumber
case lang.TokenString:
tokenType = TokenTypeString
case lang.TokenObjectKey:
tokenType = TokenTypeParameter
case lang.TokenMapKey:
tokenType = TokenTypeParameter

default:
return []float64{}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/lsp/token_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ var (
TokenTypeType,
TokenTypeString,
TokenTypeProperty,
TokenTypeKeyword,
TokenTypeNumber,
TokenTypeParameter,
}
serverTokenModifiers = TokenModifiers{
TokenModifierDeprecated,
Expand Down

0 comments on commit 148328d

Please sign in to comment.