diff --git a/emacs/.emacs.d/config/init-devdocs.el b/emacs/.emacs.d/config/init-devdocs.el index ca28075..ade747a 100644 --- a/emacs/.emacs.d/config/init-devdocs.el +++ b/emacs/.emacs.d/config/init-devdocs.el @@ -6,6 +6,19 @@ devdocs-install devdocs-delete devdocs-update-all) + :config + (defvar devdocs-entry-aliases nil + "Alist mapping expected DevDocs entry names to their actual names.") + + (defun devdocs-as-string (entry doc) + "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))) + (with-current-buffer (devdocs--render (devdocs--get-data entry-data)) + (buffer-string))))) :general (help-map "D" #'devdocs-lookup) ('normal devdocs-mode-map diff --git a/emacs/.emacs.d/config/init-terraform.el b/emacs/.emacs.d/config/init-terraform.el index c8bdc3d..bc86976 100644 --- a/emacs/.emacs.d/config/init-terraform.el +++ b/emacs/.emacs.d/config/init-terraform.el @@ -2,6 +2,48 @@ ;; Configuration as code! (use-package terraform-mode - :mode "\\.tf\\'") + :mode "\\.tf\\'" + :config + (defun devdocs-terraform-resource-string (resource) + "Returns the devdocs for Terraform resource RESOURCE as a string." + (devdocs-as-string resource "terraform")) + + (add-to-list 'devdocs-entry-aliases '("aws_alb" . "aws_lb")) + + (defun terraform-resource-name-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 (when capture + (->> capture + (-filter (lambda (pair) + (eq (car pair) 'name))) + (-map #'cdr) + (first))))) + (when name-node (treesit-node-text name-node t)))) + + (defun terraform-resource-name-at-point () + (terraform-resource-name-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)) + (summary (with-temp-buffer + (insert doc) + (goto-char (point-min)) + (forward-line) + (while (and (looking-at (rx space) t) (not (eobp))) + (forward-line)) + (buffer-substring-no-properties (line-beginning-position) (line-end-position))))) + (funcall cb doc :echo summary))) + + (defun terraform-setup-eldoc () + (add-hook 'eldoc-documentation-functions #'terraform-devdocs-eldoc-function nil t)) + + (add-hook 'terraform-mode-hook #'terraform-setup-eldoc)) (provide 'init-terraform)