Differentiate between resources and data for terraform devdocs eldoc

This commit is contained in:
Jeremy Dormitzer 2024-05-10 12:31:41 -04:00
parent b0b5048176
commit 232f8053f0
2 changed files with 24 additions and 17 deletions

View File

@ -10,13 +10,20 @@
(defvar devdocs-entry-aliases nil
"Alist mapping expected DevDocs entry names to their actual names.")
(defun devdocs-as-string (entry doc)
(defun devdocs-as-string (entry doc &optional type-rx)
"Returns the devdocs for ENTRY in DOC as a string."
(let* ((entry (if (symbolp entry) (symbol-name entry) entry))
(entry (or (alist-get entry devdocs-entry-aliases nil nil #'equal) entry))
(docs (devdocs--doc-metadata doc))
(entries (devdocs--entries (list docs))))
(when-let ((entry-data (seq-find (lambda (e) (string-match entry e)) entries)))
(when-let ((entry-data (seq-find (lambda (e)
(let ((case-fold-search t)
(type (alist-get 'type (devdocs--get-data e))))
(if type-rx
(and (string-match type-rx type)
(string-match entry e))
(string-match entry e))))
entries)))
(with-current-buffer (devdocs--render (devdocs--get-data entry-data))
(buffer-string)))))
:general

View File

@ -4,13 +4,13 @@
(use-package terraform-mode
:mode "\\.tf\\'"
:config
(defun devdocs-terraform-resource-string (resource)
"Returns the devdocs for Terraform resource RESOURCE as a string."
(devdocs-as-string resource "terraform"))
(defun devdocs-terraform-resource-string (resource type)
"Returns the devdocs for Terraform resource RESOURCE of type TYPE (\"data\" or \"resource\")as a string."
(devdocs-as-string resource "terraform" type))
(add-to-list 'devdocs-entry-aliases '("aws_alb" . "aws_lb"))
(defun terraform-resource-name-at-pos (pos)
(defun terraform-resource-name-and-type-at-pos (pos)
(let* ((parser (treesit-parser-create 'hcl))
(current-node (treesit-node-at pos parser))
(block (treesit-node-top-level current-node "block"))
@ -18,20 +18,20 @@
(treesit-query-capture block
'((block (identifier) @id (:match "^\\(data\\|resource\\)$" @id)
(string_lit (_) (template_literal) @name (_)))))))
(name-node (when capture
(->> capture
(-filter (lambda (pair)
(eq (car pair) 'name)))
(-map #'cdr)
(first)))))
(when name-node (treesit-node-text name-node t))))
(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)))
(defun terraform-resource-name-at-point ()
(terraform-resource-name-at-pos (point)))
(defun terraform-resource-name-and-type-at-point ()
(terraform-resource-name-and-type-at-pos (point)))
(defun terraform-devdocs-eldoc-function (cb)
(when-let* ((resource-name (terraform-resource-name-at-point))
(doc (devdocs-terraform-resource-string resource-name))
(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))
(doc (devdocs-terraform-resource-string resource-name resource-type))
(summary (with-temp-buffer
(insert doc)
(goto-char (point-min))