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")) (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)) (let* ((parser (treesit-parser-create 'hcl))
(current-node (treesit-node-at pos parser)) (current-node (treesit-node-at pos parser))
(block (treesit-node-top-level current-node "block")) (block (treesit-node-top-level current-node "block"))
(capture (when block (capture (when block
(treesit-query-capture block (treesit-query-capture block
'((block (identifier) @id (:match "^\\(data\\|resource\\)$" @id) '((block (identifier) @id (:match "^\\(data\\|resource\\)$" @id)
(string_lit (_) (template_literal) @name (_))))))) (string_lit (_) (template_literal) @name (_))))))))
(name-node (alist-get 'name capture)) (->> (-take 2 capture)
(id-node (alist-get 'id capture)) (-map (lambda (r)
(name (when name-node (treesit-node-text name-node t))) (let ((key (if (eq (car r) 'id) 'type (car r))))
(id (when id-node (treesit-node-text id-node t)))) (cons key (treesit-node-text (cdr r) t))))))))
(cons name id)))
(defun terraform-resource-name-and-type-at-point () (defun terraform-block-metadata-at-point ()
(terraform-resource-name-and-type-at-pos (point))) (terraform-block-metadata-at-pos (point)))
(defun terraform-devdocs-eldoc-function (cb) (defun terraform-devdocs-eldoc-function (cb)
(when-let* ((resource-name-and-type (terraform-resource-name-and-type-at-point)) (when-let* ((metadata (terraform-block-metadata-at-point))
(resource-name (car resource-name-and-type)) (resource-name (alist-get 'name metadata))
(resource-type (cdr resource-name-and-type)) (resource-type (alist-get 'type metadata))
(doc (devdocs-terraform-resource-string resource-name resource-type)) (doc (devdocs-terraform-resource-string resource-name resource-type))
(summary (with-temp-buffer (summary (with-temp-buffer
(insert doc) (insert doc)