in internal/parser/parser_utils.go [24:68]
func BuildHCLContext(docContent, fileName string, position protocol.Position) (*HCLContext, hcl.Diagnostics, error) {
file, diags := hclsyntax.ParseConfig([]byte(docContent), fileName, hcl.InitialPos)
if diags != nil && diags.HasErrors() {
return nil, diags, fmt.Errorf("failed to parse HCL: %v", diags)
}
body, ok := file.Body.(*hclsyntax.Body)
if !ok {
return nil, nil, fmt.Errorf("file is not valid HCL body")
}
hclPos := lsp.LSPPosToHCL(position)
block := BlockAtPos(body, hclPos)
if block == nil || len(block.Labels) == 0 {
return nil, nil, fmt.Errorf("no valid block found")
}
resource := block.Labels[0]
if !strings.HasPrefix(resource, schema.AzureRMPrefix) {
return nil, nil, fmt.Errorf("not an AzureRM resource")
}
subBlock := BlockAtPos(block.Body, hclPos)
attr := AttributeAtPos(block, hclPos)
if attr == nil && subBlock != nil {
attr = AttributeAtPos(subBlock, hclPos)
}
var path string
if attr != nil {
path = buildNestedPath(block, attr)
} else if subBlock != nil {
path = buildNestedPath(block, subBlock)
}
return &HCLContext{
File: file,
Body: body,
Block: block,
SubBlock: subBlock,
Attribute: attr,
Resource: resource,
ParsedPath: path,
}, nil, nil
}