Skip to content

Commit dcd8d79

Browse files
committed
terraform/lang: Extract types into a separate file
1 parent d089964 commit dcd8d79

File tree

6 files changed

+48
-34
lines changed

6 files changed

+48
-34
lines changed

internal/terraform/lang/config_block.go

-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import (
1010
lsp "github.com/sourcegraph/go-lsp"
1111
)
1212

13-
type ConfigBlock interface {
14-
CompletionItemsAtPos(pos hcl.Pos) (lsp.CompletionList, error)
15-
Name() string
16-
BlockType() string
17-
}
18-
1913
type configBlockFactory interface {
2014
New(*hclsyntax.Block) (ConfigBlock, error)
2115
}

internal/terraform/lang/hcl_attribute.go

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import (
66
tfjson "github.com/hashicorp/terraform-json"
77
)
88

9-
type Attribute struct {
10-
schema *tfjson.SchemaAttribute
11-
hclAttribute *hcl.Attribute
12-
}
13-
149
func (a *Attribute) Schema() *tfjson.SchemaAttribute {
1510
return a.schema
1611
}

internal/terraform/lang/hcl_block_type.go

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import (
66
tfjson "github.com/hashicorp/terraform-json"
77
)
88

9-
type BlockType struct {
10-
BlockList []Block
11-
schema *tfjson.SchemaBlockType
12-
}
13-
149
func (b *BlockType) Schema() *tfjson.SchemaBlockType {
1510
return b.schema
1611
}

internal/terraform/lang/hcl_parser.go

-11
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ import (
88
tfjson "github.com/hashicorp/terraform-json"
99
)
1010

11-
// Block implements an abstraction above HCL block
12-
// and both tfjson's SchemaBlock & SchemaBlockType
13-
type Block interface {
14-
BlockAtPos(pos hcl.Pos) (Block, bool)
15-
Range() hcl.Range
16-
PosInBody(pos hcl.Pos) bool
17-
PosInAttribute(pos hcl.Pos) bool
18-
Attributes() map[string]*Attribute
19-
BlockTypes() map[string]*BlockType
20-
}
21-
2211
// ParseBlock parses HCL configuration based on tfjson's SchemaBlock
2312
// and keeps hold of all tfjson schema details on block or attribute level
2413
func ParseBlock(block *hclsyntax.Block, schema *tfjson.SchemaBlock) Block {

internal/terraform/lang/parser.go

-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ import (
2323
// (e.g. meta-parameters)
2424
const parserVersionConstraint = ">= 0.12.0"
2525

26-
type Parser interface {
27-
SetLogger(*log.Logger)
28-
SetCapabilities(lsp.TextDocumentClientCapabilities)
29-
SetSchemaReader(schema.Reader)
30-
ParseBlockFromHCL(*hcl.Block) (ConfigBlock, error)
31-
}
32-
3326
type parser struct {
3427
logger *log.Logger
3528
caps lsp.TextDocumentClientCapabilities

internal/terraform/lang/types.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package lang
2+
3+
import (
4+
"log"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
tfjson "github.com/hashicorp/terraform-json"
8+
"github.com/hashicorp/terraform-ls/internal/terraform/schema"
9+
lsp "github.com/sourcegraph/go-lsp"
10+
)
11+
12+
// Parser implements a parser which can turn raw HCL block
13+
// into ConfigBlock with the help of a schema reader
14+
type Parser interface {
15+
SetLogger(*log.Logger)
16+
SetCapabilities(lsp.TextDocumentClientCapabilities)
17+
SetSchemaReader(schema.Reader)
18+
ParseBlockFromHCL(*hcl.Block) (ConfigBlock, error)
19+
}
20+
21+
// ConfigBlock implements an abstraction above HCL block
22+
// which provides any LSP capabilities (e.g. completion)
23+
type ConfigBlock interface {
24+
CompletionItemsAtPos(pos hcl.Pos) (lsp.CompletionList, error)
25+
Name() string
26+
BlockType() string
27+
}
28+
29+
// Block represents a decoded HCL block (by a Parser)
30+
// which keeps track of the related schema
31+
type Block interface {
32+
BlockAtPos(pos hcl.Pos) (Block, bool)
33+
Range() hcl.Range
34+
PosInBody(pos hcl.Pos) bool
35+
PosInAttribute(pos hcl.Pos) bool
36+
Attributes() map[string]*Attribute
37+
BlockTypes() map[string]*BlockType
38+
}
39+
40+
type BlockType struct {
41+
BlockList []Block
42+
schema *tfjson.SchemaBlockType
43+
}
44+
45+
type Attribute struct {
46+
schema *tfjson.SchemaAttribute
47+
hclAttribute *hcl.Attribute
48+
}

0 commit comments

Comments
 (0)