Use an alist to encapsulate terraform block metadata

This commit is contained in:
Jeremy Dormitzer 2024-05-10 23:40:36 -04:00
parent 3ba2b5940a
commit 9f4e0f88ad

View File

@ -10,27 +10,26 @@
(add-to-list 'devdocs-entry-aliases '("aws_alb" . "aws_lb"))
(defun terraform-resource-name-and-type-at-pos (pos)
(defun terraform-block-metadata-at-pos (pos)
(let* ((parser (treesit-parser-create 'hcl))
(current-node (treesit-node-at pos parser))
(block (treesit-node-top-level current-node "block"))
(capture (when block
(treesit-query-capture block
'((block (identifier) @id (:match "^\\(data\\|resource\\)$" @id)
(string_lit (_) (template_literal) @name (_)))))))
(name-node (alist-get 'name capture))
(id-node (alist-get 'id capture))
(name (when name-node (treesit-node-text name-node t)))
(id (when id-node (treesit-node-text id-node t))))
(cons name id)))
(string_lit (_) (template_literal) @name (_))))))))
(->> (-take 2 capture)
(-map (lambda (r)
(let ((key (if (eq (car r) 'id) 'type (car r))))
(cons key (treesit-node-text (cdr r) t))))))))
(defun terraform-resource-name-and-type-at-point ()
(terraform-resource-name-and-type-at-pos (point)))
(defun terraform-block-metadata-at-point ()
(terraform-block-metadata-at-pos (point)))
(defun terraform-devdocs-eldoc-function (cb)
(when-let* ((resource-name-and-type (terraform-resource-name-and-type-at-point))
(resource-name (car resource-name-and-type))
(resource-type (cdr resource-name-and-type))
(when-let* ((metadata (terraform-block-metadata-at-point))
(resource-name (alist-get 'name metadata))
(resource-type (alist-get 'type metadata))
(doc (devdocs-terraform-resource-string resource-name resource-type))
(summary (with-temp-buffer
(insert doc)