File: lsp-terraform-ls.md

package info (click to toggle)
lsp-mode 9.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,472 kB
  • sloc: lisp: 27,961; makefile: 62; java: 34; cpp: 33; javascript: 31; xml: 23; python: 14; sh: 2
file content (146 lines) | stat: -rw-r--r-- 3,831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
---
author: psibi
template: comment.html
root_file: docs/manual-language-docs/lsp-terraform-ls.md
---

## Server note

This page documents the official language server for Terraform by
Hashicorp.

Note that currently lsp-mode supports two terraform language
servers. This [FAQ entry](https://emacs-lsp.github.io/lsp-mode/page/faq/#i-have-multiple-language-servers-registered-for-language-foo-which-one-will-be-used-when-opening-a-project) shows how to choose a specific one. If
you would want to go with the official Hashicorp's language server,
set this:

``` emacs-lisp
(setq lsp-disabled-clients '(tfls))
```

## terraform-ls

### Commands

#### `lsp-terraform-ls-validate`

Runs terraform validate on project root. All the violations are
published back in the buffer.

![](../examples/lsp-terraform-validate.png)

#### `lsp-terraform-ls-init`

Runs `terraform init` using terraform available from `$PATH`. You have
to make sure that that proper credentials are there.

Note that this is a synchronous action and will timeout after a
certain amount of time.

#### `lsp-terraform-ls-version`

This feature is only available on terraform-ls version >= 0.29.0.

Provides information about the terraform binary version for the
current workspace. This is how it will look like in the minibuffer:

``` shellsession
Required: 1.1.9, Current: 1.0.5
```

Note that if you have changed the version in your `$PATH` meanwhile,
you would have to restart the lsp session using
`lsp-workspace-restart` for it to pick up the newer version.

### Code Lens

This is an experimental feature which can be enabled via the option
`lsp-terraform-ls-enable-show-reference`:

``` emacs-lisp
(setq lsp-terraform-ls-enable-show-reference t)
```

This gif demonstrates how this feature is used:

![](../examples/lsp-terraform-code-lens-refs.gif)

### Semantic token support

Make sure to enable these two variables to ensure that you have
semantic token support for terraform mode:

``` emacs-lisp
(setq lsp-semantic-tokens-enable t)
(setq lsp-semantic-tokens-honor-refresh-requests t)
```

This is how the code looks without semantic tokens support:

![](../examples/lsp-terraform-without-semantic-token.png)

And with semantic token support you get more contextual information
via different faces:

![](../examples/lsp-terraform-with-semantic-token.png)

### Link to Documentation

Link to documentation from module sources for registry modules is
available. Make sure you have this enabled:

``` emacs-lisp
(setq lsp-enable-links t)
```

Note that the default value of `lsp-enable-links` is `t`. So enabling
it isn't strictly required, but you would require this feature for it
to properly work.

This gif demonstrates on how to use it:

![](../examples/lsp-terraform-open-link.gif)

There are two ways to open the link:

- Key binding: Alt + Enter
- Clicking the middle button of your mouse

### Prefill required fields

This is an experimental feature which can be enabled via the option
`lsp-terraform-ls-prefill-required-fields`:

``` emacs-lisp
(setq lsp-terraform-ls-prefill-required-fields t)
```

This gif demonstrates how this feature is used:

![](../examples/terraform-ls-prefill.gif)

### Treeview controls

For this feature to work, make sure that you have [lsp-treemacs](https://github.com/emacs-lsp/lsp-treemacs)
installed.

#### Providers widget

This widget can be invoked by `lsp-terraform-ls-providers`.

![](../examples/lsp-terraform-providers-treemacs.png)

#### Module calls widget

This widget can be invoked by `lsp-terraform-ls-module-calls`.

If the modules data is empty, you might need to do `terraform init`
for the project.

The widget has a minor mode named
`lsp-terraform-modules-mode`. Following keybinding are available
within the mode:

- `g`: Refresh modules view

![](../examples/lsp-terraform-modules-treemacs.png)