50 lines
2.1 KiB
EmacsLisp
50 lines
2.1 KiB
EmacsLisp
;; -*- lexical-binding: t; -*-
|
|
|
|
;; Configuration as code!
|
|
(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"))
|
|
|
|
(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)
|