Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide correct URL for data sources #61

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ folds-or-indents. It is not bound by default, but you can bind it to

### Access to Terraform resource documentation

Within a `resource` or a `data` block, type `C-c C-h` to open a new
Within a `resource` or a `data` block, type `C-c C-d C-w` to open a new
browser tab with the resource or data documentation page.

## Customize Variables
Expand Down
18 changes: 10 additions & 8 deletions terraform-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,27 @@
(when (re-search-forward (concat "/\\(.*?\\)/" provider "\\]") nil t)
(match-string 1)))))

(defun terraform--resource-url (resource)
"Return the url containing the documentation for RESOURCE."
(defun terraform--resource-url (resource doc-dir)
"Return the url containing the documentation for RESOURCE using DOC-DIR."
(let* ((provider (terraform--extract-provider resource))
(provider-ns (terraform--get-resource-provider-namespace provider))
(resource-name (terraform--extract-resource resource)))
(if provider-ns
(format "https://registry.terraform.io/providers/%s/%s/latest/docs/resources/%s"
(format "https://registry.terraform.io/providers/%s/%s/latest/docs/%s/%s"
provider-ns
provider
doc-dir
resource-name)
(user-error "Can not determine the provider namespace for %s" provider))))

(defun terraform--resource-url-at-point ()
(save-excursion
(goto-char (line-beginning-position))
(unless (looking-at-p "^resource")
(re-search-backward "^resource" nil t))
(forward-symbol 2)
(terraform--resource-url (thing-at-point 'symbol))))
(unless (looking-at-p "^resource\\|^data")
(re-search-backward "^resource\\|^data" nil t))
(let ((doc-dir (if (equal (word-at-point) "data") "data-sources" "resources")))
(forward-symbol 2)
(terraform--resource-url (thing-at-point 'symbol) doc-dir))))

(defun terraform-open-doc ()
"Open a browser at the URL documenting the resource at point."
Expand Down Expand Up @@ -314,7 +316,7 @@ If the point is not at the heading, call

(defvar terraform-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-h") #'terraform-open-doc)
(define-key map (kbd "C-c C-d C-w") #'terraform-open-doc)
(define-key map (kbd "C-c C-f") #'outline-toggle-children)
map))

Expand Down
14 changes: 14 additions & 0 deletions test/test-command.el
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ resource \"elasticstack_elasticsearch_security_user\" \"filebeat_writer\" {
(cl-letf (((symbol-function 'terraform--get-resource-provider-namespace) (lambda (prov) "elastic")))
(should (equal (terraform--resource-url-at-point) "https://registry.terraform.io/providers/elastic/elasticstack/latest/docs/resources/elasticsearch_security_user")))))

(ert-deftest command--open-doc--at-data-resource-def-line ()
(with-terraform-temp-buffer
"
data \"aws_subnets\" \"example\" {
filter {
name = \"vpc-id\"
values = [var.vpc_id]
}
}
"
(forward-cursor-on "subnets")
(cl-letf (((symbol-function 'terraform--get-resource-provider-namespace) (lambda (prov) "hashicorp")))
(should (equal (terraform--resource-url-at-point) "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets")))))

(ert-deftest command--open-doc--in-body ()
(with-terraform-temp-buffer
"
Expand Down