Something went wrong. Try again.
A Neovim plugin for interactive OpenTofu applies
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123--- Terraform HCL parser for resource extraction
local M = {}
--- Parse a line to extract Terraform resource information--- @param line string The line to parse--- @return table|nil resource_info { type = string, resource_type = string, name = string }local function parse_resource_line(line) -- Match patterns like: -- resource "aws_instance" "example" { -- data "aws_ami" "ubuntu" { -- module "vpc" {
local patterns = { -- resource "type" "name" '^%s*(resource)%s+"([^"]+)"%s+"([^"]+)"', -- data "type" "name" '^%s*(data)%s+"([^"]+)"%s+"([^"]+)"', -- module "name" '^%s*(module)%s+"([^"]+)"', }
for _, pattern in ipairs(patterns) do local matches = { line:match(pattern) } if #matches > 0 then local block_type = matches[1]
if block_type == 'module' then return { type = 'module', name = matches[2], address = string.format('module.%s', matches[2]), } else return { type = block_type, resource_type = matches[2], name = matches[3], address = string.format('%s.%s.%s', block_type, matches[2], matches[3]), } end end end
return nilend
--- Get the resource under the cursor--- @return table|nil resource_info { type = string, resource_type = string|nil, name = string, address = string }function M.get_resource_under_cursor() local cursor = vim.api.nvim_win_get_cursor(0) local current_line = cursor[1] local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
-- Search backwards from cursor to find the resource declaration for i = current_line, 1, -1 do local line = lines[i] local resource = parse_resource_line(line)
if resource then -- Verify we're still within this resource block by finding the closing brace -- Count opening and closing braces from resource line to cursor local brace_count = 0 for j = i, current_line do local l = lines[j] -- Count braces for c in l:gmatch('[{}]') do if c == '{' then brace_count = brace_count + 1 elseif c == '}' then brace_count = brace_count - 1 -- If we hit zero, we've closed the block if brace_count == 0 and j < current_line then -- Cursor is outside this resource block goto continue end end end end
-- If we're here, cursor is within this resource if brace_count > 0 then return resource end end
::continue:: end
return nilend
--- Get all resources in the current buffer--- @return table[] resources Array of resource_info tablesfunction M.get_all_resources_in_buffer() local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local resources = {}
for i, line in ipairs(lines) do local resource = parse_resource_line(line) if resource then resource.line = i table.insert(resources, resource) end end
return resourcesend
--- Check if current buffer is a Terraform file--- @return booleanfunction M.is_terraform_file() local filetype = vim.bo.filetype local filename = vim.fn.expand('%:t')
return filetype == 'terraform' or filetype == 'tf' or filename:match('%.tf$') ~= nil or filename:match('%.tfvars$') ~= nilend
return M