├── .github
└── workflows
│ └── tests.yml
├── .gitignore
├── Cask
├── Changes
├── LICENSE
├── Makefile
├── README.md
├── image
├── terraform-mode-imenu.png
└── terraform-mode.png
├── terraform-mode.el
└── test
├── fixtures
├── main.tf
└── provider.tf
├── test-command.el
├── test-helper.el
├── test-highlighting.el
└── test-indentation.el
/.github/workflows/tests.yml:
--------------------------------------------------------------------------------
1 | name: Tests
2 | on: [push, pull_request]
3 | jobs:
4 | run_tests:
5 | runs-on: ubuntu-latest
6 | strategy:
7 | matrix:
8 | emacs_version: ["26.3", "27.2", "28.2"]
9 | steps:
10 | # This command allows us to execute cask below. Refer to issue:
11 | # https://github.com/conao3/setup-cask/issues/6
12 | - run: echo "ACTIONS_ALLOW_UNSECURE_COMMANDS=true" >> $GITHUB_ENV
13 | - uses: purcell/setup-emacs@master
14 | with:
15 | version: ${{ matrix.emacs_version }}
16 | - uses: conao3/setup-cask@master
17 | with:
18 | version: '0.8.4'
19 | - uses: actions/checkout@v3
20 | - run: |
21 | cask install
22 | make test
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.cask/
--------------------------------------------------------------------------------
/Cask:
--------------------------------------------------------------------------------
1 | (source gnu)
2 | (source melpa)
3 |
4 | (package-file "terraform-mode.el")
5 |
6 | (development
7 | (depends-on "ert"))
8 |
--------------------------------------------------------------------------------
/Changes:
--------------------------------------------------------------------------------
1 | Revision history for terraform-mode.el
2 |
3 | Revision 0.06 2017/01/01 syohex
4 | - Update hcl-mode version for here document highlighting
5 | - Drop older Emacs support
6 |
7 | Revision 0.05 2016/05/03 syohex
8 | - Update hcl-mode version for here document highlighting
9 |
10 | Revision 0.04 2015/12/26 syohex
11 | - Fix indent size issue(#7, #8)
12 |
13 | Revision 0.03 2015/09/01 syohex
14 | - Change default indent size(Thanks svend)
15 | 2 indent size is recommended by official developpers
16 |
17 | Revision 0.02 2015/01/25 syohex
18 | - Improve for electric-mode
19 |
20 | Revision 0.01 2014/08/04 syohex
21 | - Initial Release
22 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY : test
2 |
3 | UNAME_S=$(shell uname -s)
4 | ifeq ($(UNAME_S),Darwin)
5 | EMACS ?= /Applications/Emacs.app/Contents/MacOS/Emacs
6 | else
7 | EMACS ?= emacs
8 | endif
9 |
10 | CASK ?= cask
11 |
12 | LOADPATH = -L .
13 | LOAD_HELPER = -l test/test-helper.el
14 |
15 | ELPA_DIR = $(shell EMACS=$(EMACS) $(CASK) package-directory)
16 |
17 | test: elpa
18 | $(CASK) exec $(EMACS) -Q -batch $(LOADPATH) $(LOAD_HELPER) \
19 | -l test/test-highlighting.el -l test/test-indentation.el \
20 | -l test/test-command.el \
21 | -f ert-run-tests-batch-and-exit
22 |
23 | test-highlighting: elpa
24 | $(CASK) exec $(EMACS) -Q -batch $(LOADPATH) $(LOAD_HELPER) \
25 | -l test/test-highlighting.el \
26 | -f ert-run-tests-batch-and-exi
27 |
28 | test-indentation: elpa
29 | $(CASK) exec $(EMACS) -Q -batch $(LOADPATH) $(LOAD_HELPER) \
30 | -l test/test-indentation.el \
31 | -f ert-run-tests-batch-and-exit
32 |
33 | test-command: elpa
34 | $(CASK) exec $(EMACS) -Q -batch $(LOADPATH) $(LOAD_HELPER) \
35 | -l test/test-command.el \
36 | -f ert-run-tests-batch-and-exit
37 |
38 | elpa: $(ELPA_DIR)
39 | $(ELPA_DIR): Cask
40 | $(CASK) install
41 | touch $@
42 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # terraform-mode.el  [![melpa badge][melpa-badge]][melpa-link] [![melpa stable badge][melpa-stable-badge]][melpa-stable-link]
2 |
3 | Major mode of [Terraform](http://www.terraform.io/) configuration file
4 |
5 | ## Screenshot
6 |
7 | 
8 |
9 | #### imenu(`helm-imenu`)
10 |
11 | 
12 |
13 |
14 | ## Installation
15 |
16 | You can install `terraform-mode.el` from [MELPA](https://melpa.org/) by `package.el`.
17 |
18 |
19 | ## Features
20 |
21 | - Syntax highlighting
22 | - Indentation
23 | - imenu
24 | - Formatting using `terraform fmt`
25 | - Block folding
26 | - easier access to Terraform resource documentation
27 |
28 | ### Block folding
29 |
30 | `terraform-mode` sets up `outline-mode` variables for block folding.
31 | To use `outline-mode` for block folding, enable `outline-minor-mode`
32 | in `terraform-mode-hook`:
33 |
34 | ``` emacs-lisp
35 | (add-hook 'terraform-mode-hook #'outline-minor-mode)
36 | ```
37 |
38 | You can use `outline-toggle-children` bound to `C-c C-f` to toggle
39 | visibility of a block at point.
40 |
41 | We also provide function `terraform-toggle-or-indent` which
42 | folds-or-indents. It is not bound by default, but you can bind it to
43 | `TAB` or any other key.
44 |
45 | ### Access to Terraform resource documentation
46 |
47 | Within a `resource` or a `data` block, type `C-c C-d C-w` to open a new
48 | browser tab with the resource or data documentation page.
49 |
50 | Type `C-c C-d C-c` to kill the URL (i.e. copy it to the clipboard) for the documentation page rather than directly open it in the browser.
51 |
52 | You can also type `C-c C-d C-r` to insert a comment containing a link to
53 | this documentation right above the resource or data block.
54 |
55 | This feature requires either:
56 |
57 | - a `required_provider` declaration in any `.tf` file in current directory
58 | (see [Terraform doc](https://developer.hashicorp.com/terraform/language/providers/requirements#requiring-providers))
59 | - a working `terraform providers` command. This command may require a
60 | valid token (at least for AWS).
61 |
62 | ## Customize Variables
63 |
64 | #### `terraform-indent-level`(Default: `2`)
65 |
66 | Indentation size. You need to call `revert-buffer` if you change this value outer of hook such as `eval-expression`.
67 |
68 | #### `terraform-format-on-save`(Default `nil`)
69 |
70 | Set to `t` to automatically format the buffer on save.
71 |
72 | ## Sample Configuration
73 |
74 | ```emacs-lisp
75 | (custom-set-variables
76 | '(terraform-indent-level 4))
77 | ```
78 |
79 | With `use-package`
80 |
81 | ``` emacs-lisp
82 | (use-package terraform-mode
83 | ;; if using straight
84 | ;; :straight t
85 |
86 | ;; if using package.el
87 | ;; :ensure t
88 | :custom (terraform-indent-level 4)
89 | :config
90 | (defun my-terraform-mode-init ()
91 | ;; if you want to use outline-minor-mode
92 | ;; (outline-minor-mode 1)
93 | )
94 |
95 | (add-hook 'terraform-mode-hook 'my-terraform-mode-init))
96 | ```
97 |
98 | ## See Also
99 |
100 | - [hcl-mode](https://github.com/syohex/emacs-hcl-mode)
101 |
102 | This major-mode inherits from hcl-mode. Most of syntax features, like highlighting, indentation are implemented in hcl-mode.
103 |
104 | [melpa-link]: https://melpa.org/#/terraform-mode
105 | [melpa-stable-link]: https://stable.melpa.org/#/terraform-mode
106 | [melpa-badge]: https://melpa.org/packages/terraform-mode-badge.svg
107 | [melpa-stable-badge]: https://stable.melpa.org/packages/terraform-mode-badge.svg
108 |
--------------------------------------------------------------------------------
/image/terraform-mode-imenu.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hcl-emacs/terraform-mode/80383ff42bd0047cde6e3a1dfb87bdb9e0340da3/image/terraform-mode-imenu.png
--------------------------------------------------------------------------------
/image/terraform-mode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hcl-emacs/terraform-mode/80383ff42bd0047cde6e3a1dfb87bdb9e0340da3/image/terraform-mode.png
--------------------------------------------------------------------------------
/terraform-mode.el:
--------------------------------------------------------------------------------
1 | ;;; terraform-mode.el --- Major mode for terraform configuration file -*- lexical-binding: t -*-
2 |
3 | ;; Copyright (C) 2017 by Syohei YOSHIDA
4 |
5 | ;; Author: Syohei YOSHIDA
6 | ;; URL: https://github.com/syohex/emacs-terraform-mode
7 | ;; Version: 0.06
8 | ;; Package-Requires: ((emacs "24.3") (hcl-mode "0.03") (dash "2.17.0"))
9 |
10 | ;; This program is free software; you can redistribute it and/or modify
11 | ;; it under the terms of the GNU General Public License as published by
12 | ;; the Free Software Foundation, either version 3 of the License, or
13 | ;; (at your option) any later version.
14 |
15 | ;; This program is distributed in the hope that it will be useful,
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 | ;; GNU General Public License for more details.
19 |
20 | ;; You should have received a copy of the GNU General Public License
21 | ;; along with this program. If not, see .
22 |
23 | ;;; Commentary:
24 |
25 | ;; Major mode of terraform configuration file. terraform-mode provides
26 | ;; syntax highlighting, indentation function and formatting.
27 |
28 | ;; Format the current buffer with terraform-format-buffer. To always
29 | ;; format terraform buffers when saving, use:
30 | ;; (setq terraform-format-on-save t)
31 |
32 | ;;; Code:
33 |
34 | (require 'cl-lib)
35 | (require 'rx)
36 | (require 'hcl-mode)
37 | (require 'dash)
38 | (require 'thingatpt)
39 | (require 'outline)
40 |
41 | (defgroup terraform nil
42 | "Major mode of Terraform configuration file."
43 | :group 'languages)
44 |
45 | (defcustom terraform-indent-level 2
46 | "The tab width to use when indenting."
47 | :type 'integer)
48 |
49 | (defcustom terraform-format-on-save nil
50 | "Format buffer on save"
51 | :type 'boolean
52 | :group 'terraform-mode)
53 |
54 | (defcustom terraform-command "terraform"
55 | "Command used to invoke terraform"
56 | :type 'string
57 | :group 'terraform-mode)
58 |
59 | (defface terraform-resource-type-face
60 | '((t :inherit font-lock-type-face))
61 | "Face for resource names."
62 | :group 'terraform-mode)
63 |
64 | (define-obsolete-face-alias 'terraform--resource-type-face 'terraform-resource-type-face "1.0.0")
65 |
66 | (defface terraform-resource-name-face
67 | '((t :inherit font-lock-function-name-face))
68 | "Face for resource names."
69 | :group 'terraform-mode)
70 |
71 | (define-obsolete-face-alias 'terraform--resource-name-face 'terraform-resource-name-face "1.0.0")
72 |
73 | (defface terraform-builtin-face
74 | '((t :inherit font-lock-builtin-face))
75 | "Face for builtins."
76 | :group 'terraform-mode)
77 |
78 | (define-obsolete-face-alias 'terraform--builtin-face 'terraform-builtin-face "1.0.0")
79 |
80 | (defface terraform-variable-name-face
81 | '((t :inherit font-lock-variable-name-face))
82 | "Face for varriables."
83 | :group 'terraform-mode)
84 |
85 | (defconst terraform--constants-regexp
86 | (concat "\\(?:^\\|[^.]\\)" (regexp-opt '("null") 'words)))
87 |
88 | (defconst terraform--block-builtins-without-name-or-type-regexp
89 | (rx line-start
90 | (zero-or-more space)
91 | (group-n 1 (or "terraform" "locals" "required_providers" "atlas" "connection"))
92 | (or (one-or-more space) "{")))
93 |
94 | (defconst terraform--block-builtins-with-type-only
95 | (rx (or "backend" "provider" "provisioner")))
96 |
97 | (defconst terraform--block-builtins-with-type-only--builtin-highlight-regexp
98 | (eval `(rx line-start
99 | (zero-or-more space)
100 | (group-n 1 (regexp ,(eval terraform--block-builtins-with-type-only)))
101 | (one-or-more space))))
102 |
103 | (defconst terraform--block-builtins-with-type-only--resource-type-highlight-regexp
104 | (eval `(rx (regexp ,(eval terraform--block-builtins-with-type-only--builtin-highlight-regexp))
105 | (group-n 2 (and (not (any "=")) (+? (not space))))
106 | (or (one-or-more space) "{"))))
107 |
108 | (defconst terraform--block-builtins-with-name-only
109 | (rx (or "variable" "module" "output")))
110 |
111 | (defconst terraform--block-builtins-with-name-only--builtin-highlight-regexp
112 | (eval `(rx line-start
113 | (zero-or-more space)
114 | (group-n 1 (regexp ,(eval terraform--block-builtins-with-name-only)))
115 | (one-or-more space))))
116 |
117 | (defconst terraform--block-builtins-with-name-only--name-highlight-regexp
118 | (eval `(rx (regexp ,(eval terraform--block-builtins-with-name-only--builtin-highlight-regexp))
119 | (group-n 2 (+? (not space)))
120 | (or (one-or-more space) "{"))))
121 |
122 | (defconst terraform--block-builtins-with-type-and-name
123 | (rx (or "data" "resource")))
124 |
125 | (defconst terraform--block-builtins-with-type-and-name--builtin-highlight-regexp
126 | (eval `(rx line-start
127 | (zero-or-more space)
128 | (group-n 1 (regexp ,(eval terraform--block-builtins-with-type-and-name)))
129 | (one-or-more space))))
130 |
131 | (defconst terraform--block-builtins-with-type-and-name--type-highlight-regexp
132 | (eval `(rx (regexp ,(eval terraform--block-builtins-with-type-and-name--builtin-highlight-regexp))
133 | (group-n 2 "\"" (+? (not space)) "\"")
134 | (one-or-more space))))
135 |
136 | (defconst terraform--block-builtins-with-type-and-name--name-highlight-regexp
137 | (eval `(rx (regexp ,(eval terraform--block-builtins-with-type-and-name--type-highlight-regexp))
138 | (group-n 3 (+? (not space)))
139 | (or (one-or-more space) "{"))))
140 |
141 | (defconst terraform--assignment-statement
142 | (rx line-start
143 | (zero-or-more space)
144 | (group-n 1 (minimal-match (one-or-more any)))
145 | (zero-or-more space)
146 | "="))
147 |
148 | (defvar terraform-font-lock-keywords
149 | `((,terraform--assignment-statement 1 'terraform-variable-name-face)
150 | (,terraform--block-builtins-without-name-or-type-regexp 1 'terraform-builtin-face)
151 | (,terraform--block-builtins-with-type-only--builtin-highlight-regexp 1 'terraform-builtin-face)
152 | (,terraform--block-builtins-with-type-only--resource-type-highlight-regexp
153 | (1 'terraform-builtin-face)
154 | (2 'terraform-resource-type-face t))
155 | (,terraform--block-builtins-with-name-only--builtin-highlight-regexp 1 'terraform-builtin-face)
156 | (,terraform--block-builtins-with-name-only--name-highlight-regexp
157 | (1 'terraform-builtin-face)
158 | (2 'terraform-resource-name-face t))
159 | (,terraform--block-builtins-with-type-and-name--builtin-highlight-regexp 1 'terraform-builtin-face)
160 | (,terraform--block-builtins-with-type-and-name--type-highlight-regexp 2 'terraform-resource-type-face t)
161 | (,terraform--block-builtins-with-type-and-name--name-highlight-regexp
162 | (1 'terraform-builtin-face)
163 | (2 'terraform-resource-type-face t)
164 | (3 'terraform-resource-name-face t))
165 | (,terraform--constants-regexp 1 'font-lock-constant-face)
166 | ,@hcl-font-lock-keywords))
167 |
168 | (defun terraform-format-buffer ()
169 | "Rewrite current buffer in a canonical format using terraform fmt."
170 | (interactive)
171 | (let ((buf (get-buffer-create "*terraform-fmt*")))
172 | (if (zerop (call-process-region (point-min) (point-max)
173 | terraform-command nil buf nil "fmt" "-no-color" "-"))
174 | (let ((point (point))
175 | (window-start (window-start)))
176 | (erase-buffer)
177 | (insert-buffer-substring buf)
178 | (when (/= terraform-indent-level 2)
179 | (indent-region (point-min) (point-max)))
180 | (goto-char point)
181 | (set-window-start nil window-start))
182 | (message "terraform fmt: %s" (with-current-buffer buf (buffer-string))))
183 | (kill-buffer buf)))
184 |
185 | (defun terraform-format-region ()
186 | "Rewrite current region in a canonical format using terraform fmt."
187 | (interactive)
188 | (let ((buf (get-buffer-create "*terraform-fmt*")))
189 | (when (use-region-p)
190 | (if (zerop (call-process-region (region-beginning) (region-end)
191 | terraform-command nil buf nil "fmt" "-"))
192 | (let ((point (region-end))
193 | (window-start (region-beginning)))
194 | (delete-region window-start point)
195 | (insert-buffer-substring buf)
196 | (goto-char point)
197 | (set-window-start nil window-start))
198 | (message "terraform fmt: %s" (with-current-buffer buf (buffer-string))))
199 | (kill-buffer buf))))
200 |
201 | (define-minor-mode terraform-format-on-save-mode
202 | "Run terraform-format-buffer before saving current buffer."
203 | :lighter ""
204 | (if terraform-format-on-save-mode
205 | (add-hook 'before-save-hook #'terraform-format-buffer nil t)
206 | (remove-hook 'before-save-hook #'terraform-format-buffer t)))
207 |
208 | (defun terraform--generate-imenu ()
209 | (let ((search-results (make-hash-table :test #'equal))
210 | (menu-list '()))
211 | (save-match-data
212 | (goto-char (point-min))
213 | (while (re-search-forward terraform--block-builtins-with-type-only--resource-type-highlight-regexp nil t)
214 | (let ((key (match-string 1))
215 | (location (match-beginning 2))
216 | (resource-type (replace-regexp-in-string "\"" "" (match-string 2))))
217 | (-if-let (matches (gethash key search-results))
218 | (puthash key (push `(,resource-type . ,location) matches) search-results)
219 | (puthash key `((,resource-type . ,location)) search-results))))
220 |
221 |
222 | (goto-char (point-min))
223 | (while (re-search-forward terraform--block-builtins-with-name-only--name-highlight-regexp nil t)
224 | (let ((key (match-string 1))
225 | (location (match-beginning 2))
226 | (resource-name (replace-regexp-in-string "\"" "" (match-string 2))))
227 | (-if-let (matches (gethash key search-results))
228 | (puthash key (push `(,resource-name . ,location) matches) search-results)
229 | (puthash key `((,resource-name . ,location)) search-results))))
230 |
231 | (goto-char (point-min))
232 | (while (re-search-forward terraform--block-builtins-with-type-and-name--name-highlight-regexp nil t)
233 | (let* ((key (match-string 1))
234 | (location (match-beginning 2))
235 | (type (match-string 2))
236 | (name (match-string 3))
237 | (resource-name (concat (replace-regexp-in-string "\"" "" type)
238 | "/"
239 | (replace-regexp-in-string "\"" "" name))))
240 | (-if-let (matches (gethash key search-results))
241 | (puthash key (push `(,resource-name . ,location) matches) search-results)
242 | (puthash key `((,resource-name . ,location)) search-results))))
243 |
244 | (maphash (lambda (k v) (push `(,k ,@v) menu-list)) search-results)
245 | menu-list)))
246 |
247 | (defun terraform--extract-provider (resource-name)
248 | "Return the provider associated with a RESOURCE-NAME."
249 | (car (split-string resource-name "_")))
250 |
251 | (defun terraform--extract-resource (resource-name)
252 | "Return the resource associated with a RESOURCE-NAME."
253 | (mapconcat #'identity (cdr (split-string resource-name "_")) "_"))
254 |
255 | (defun terraform--get-resource-provider-namespace (provider)
256 | "Return provider namespace for PROVIDER."
257 | (let ((provider-info (shell-command-to-string (concat terraform-command " providers"))))
258 | (with-temp-buffer
259 | (insert provider-info)
260 | (goto-char (point-min))
261 | (when (re-search-forward (concat "/\\(.*?\\)/" provider "\\]") nil t)
262 | (match-string 1)))))
263 |
264 | (defun terraform--get-resource-provider-source (provider &optional dir)
265 | "Return Terraform provider source for PROVIDER located in DIR.
266 | Terraform provider source is searched in `required_provider' declaration
267 | in current buffer or in other Terraform files located in the same directory
268 | of the file of current buffer. If still not found, the provider source is
269 | searched by running command `terraform providers'.
270 | The DIR parameter is optional and used only for tests."
271 | (goto-char (point-min))
272 | ;; find current directory if it's not specified in arguments
273 | (if (and (not dir) buffer-file-name) (setq dir (file-name-directory buffer-file-name)))
274 | (let (tf-files
275 | ;; try to find provider source in current buffer
276 | (provider-source (terraform--get-resource-provider-source-in-buffer provider)))
277 | ;; check if terraform provider-source was found
278 | (when (and (= (length provider-source) 0) dir)
279 | ;; find all terraform files of this project. One of them
280 | ;; should contain required_provider declaration
281 | (setq tf-files (directory-files dir nil "^[[:alnum:][:blank:]_.-]+\\.tf$")))
282 | ;; iterate on terraform files until a provider source is found
283 | (while (and (= (length provider-source) 0) tf-files)
284 | (with-temp-buffer
285 | (let* ((file (pop tf-files))
286 | (file-path (if dir (concat dir "/" file) file)))
287 | (insert-file-contents file-path)
288 | ;; look for provider source in a terraform file
289 | (setq provider-source (terraform--get-resource-provider-source-in-buffer provider)))))
290 | provider-source))
291 |
292 | (defun terraform--get-resource-provider-source-in-buffer (provider)
293 | "Search and return provider namespace for PROVIDER in current buffer.
294 | Return nil if not found."
295 | (goto-char (point-min))
296 | (if (and (re-search-forward "^terraform[[:blank:]]*{" nil t)
297 | (re-search-forward "^[[:blank:]]*required_providers[[:blank:]]*{" nil t)
298 | (re-search-forward (concat "^[[:blank:]]*" provider "[[:blank:]]*=[[:blank:]]*{") nil t)
299 | (re-search-forward "^[[:blank:]]*source[[:blank:]]*=[[:blank:]]*\"\\([a-z/]+\\)\"" nil t))
300 | (match-string 1)))
301 |
302 | (defun terraform--resource-url (resource doc-dir)
303 | "Return the url containing the documentation for RESOURCE using DOC-DIR."
304 | (let* ((provider (terraform--extract-provider resource))
305 | ;; search provider source in terraform files
306 | (provider-source (terraform--get-resource-provider-source provider))
307 | (resource-name (terraform--extract-resource resource)))
308 | (when (= (length provider-source) 0)
309 | ;; fallback to old method with terraform providers command
310 | (setq provider-source (concat
311 | (terraform--get-resource-provider-namespace provider)
312 | "/" provider)))
313 | (if (> (length provider-source) 0)
314 | (format "https://registry.terraform.io/providers/%s/latest/docs/%s/%s"
315 | provider-source doc-dir resource-name)
316 | (user-error "Can not determine the provider source for %s" provider))))
317 |
318 | (defun terraform--resource-url-at-point ()
319 | (save-excursion
320 | (goto-char (line-beginning-position))
321 | (unless (looking-at-p "^resource\\|^data")
322 | (re-search-backward "^resource\\|^data" nil t))
323 | (let ((doc-dir (if (equal (word-at-point) "data") "data-sources" "resources")))
324 | (forward-symbol 2)
325 | (terraform--resource-url (thing-at-point 'symbol) doc-dir))))
326 |
327 | (defun terraform-open-doc ()
328 | "Open a browser at the URL documenting the resource at point."
329 | (interactive)
330 | (browse-url (terraform--resource-url-at-point)))
331 |
332 | (defun terraform-kill-doc-url ()
333 | "Kill the URL documenting the resource at point (i.e. copy it to the clipboard)."
334 | (interactive)
335 | (let* ((url (substring-no-properties (terraform--resource-url-at-point))))
336 | (kill-new url)
337 | (message "Copied URL: %s" url)))
338 |
339 | (defun terraform-insert-doc-in-comment ()
340 | "Insert a comment containing an URL documenting the resource at point."
341 | (interactive)
342 | (let ((doc-url (terraform--resource-url-at-point)))
343 | (save-excursion
344 | (unless (looking-at-p "^resource\\|^data")
345 | (re-search-backward "^resource\\|^data" nil t))
346 | (insert (format "# %s\n" doc-url)))))
347 |
348 | (defun terraform--outline-level ()
349 | "Return the depth to which a statement is nested in the outline.
350 |
351 | See also `outline-level'."
352 | (or (cdr (assoc (match-string 1) outline-heading-alist))
353 | (- (match-end 1) (match-beginning 1))))
354 |
355 | (defun terraform--setup-outline-mode ()
356 | (set (make-local-variable 'outline-level) #'terraform--outline-level)
357 |
358 | (let ((terraform-keywords
359 | (list "terraform" "locals" "required_providers" "atlas" "connection"
360 | "backend" "provider" "provisioner"
361 | "variable" "module" "output"
362 | "data" "resource")))
363 | (set (make-local-variable 'outline-regexp)
364 | (concat
365 | "^"
366 | (regexp-opt terraform-keywords 'symbols)
367 | "[[:blank:]].*{[[:blank:]]*$"))
368 | (set (make-local-variable 'outline-heading-alist)
369 | (mapcar
370 | (lambda (item) (cons item 2))
371 | terraform-keywords))))
372 |
373 | (defun terraform-toggle-or-indent (&optional arg)
374 | "Toggle visibility of block under point or indent.
375 |
376 | If the point is not at the heading, call
377 | `indent-for-tab-command'."
378 | (interactive)
379 | (if (and outline-minor-mode (outline-on-heading-p))
380 | (outline-toggle-children)
381 | (indent-for-tab-command arg)))
382 |
383 | (defvar terraform-mode-map
384 | (let ((map (make-sparse-keymap)))
385 | (define-key map (kbd "C-c C-d C-w") #'terraform-open-doc)
386 | (define-key map (kbd "C-c C-d C-c") #'terraform-kill-doc-url)
387 | (define-key map (kbd "C-c C-d C-r") #'terraform-insert-doc-in-comment)
388 | (define-key map (kbd "C-c C-f") #'outline-toggle-children)
389 | map))
390 |
391 | ;;;###autoload
392 | (define-derived-mode terraform-mode hcl-mode "Terraform"
393 | "Major mode for editing terraform configuration file"
394 |
395 | (setq font-lock-defaults '((terraform-font-lock-keywords)))
396 | (when terraform-format-on-save
397 | (terraform-format-on-save-mode 1))
398 |
399 | ;; indentation
400 | (make-local-variable 'terraform-indent-level)
401 | (setq hcl-indent-level terraform-indent-level)
402 |
403 | ;; outline
404 | (terraform--setup-outline-mode)
405 |
406 | ;; imenu
407 | (set (make-local-variable 'imenu-sort-function) 'imenu--sort-by-name)
408 | (setq imenu-create-index-function 'terraform--generate-imenu)
409 | (imenu-add-to-menubar "Terraform"))
410 |
411 | ;;;###autoload
412 | (add-to-list 'auto-mode-alist '("\\.t\\(f\\(vars\\)?\\|ofu\\)\\'" . terraform-mode))
413 |
414 | (provide 'terraform-mode)
415 |
416 | ;;; terraform-mode.el ends here
417 |
--------------------------------------------------------------------------------
/test/fixtures/main.tf:
--------------------------------------------------------------------------------
1 | # blah blah
2 |
--------------------------------------------------------------------------------
/test/fixtures/provider.tf:
--------------------------------------------------------------------------------
1 | # blah blah
2 | terraform {
3 | required_providers {
4 | aws = {
5 | source = "hashicorp/aws"
6 | version = "~> 5"
7 | }
8 | azurerm = {
9 | source = "hashicorp/azurerm"
10 | version = "~> 3.47"
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/test/test-command.el:
--------------------------------------------------------------------------------
1 | ;;; test-command.el --- Test for terraform-mode commands
2 |
3 | ;; Copyright (C) 2017 by Syohei YOSHIDA
4 |
5 | ;; Author: Syohei YOSHIDA
6 |
7 | ;; This program is free software; you can redistribute it and/or modify
8 | ;; it under the terms of the GNU General Public License as published by
9 | ;; the Free Software Foundation, either version 3 of the License, or
10 | ;; (at your option) any later version.
11 |
12 | ;; This program is distributed in the hope that it will be useful,
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | ;; GNU General Public License for more details.
16 |
17 | ;; You should have received a copy of the GNU General Public License
18 | ;; along with this program. If not, see .
19 |
20 | ;;; Commentary:
21 |
22 | ;;; Code:
23 |
24 | (require 'ert)
25 | (require 'cl-lib)
26 | (require 'terraform-mode)
27 |
28 | (ert-deftest command--beginning-of-defun--from-within-block ()
29 | "Move to beginning-of-defun"
30 | (with-terraform-temp-buffer
31 | "
32 | variable \"ami\" {
33 | description = \"the AMI to use\"
34 | }
35 |
36 | resource \"aws_instance\" \"web\"{
37 | ami = ${variable.ami}
38 | count = 2
39 | }
40 | "
41 |
42 | (forward-cursor-on "use")
43 | (call-interactively 'hcl-beginning-of-defun)
44 | (should (looking-at "^variable"))))
45 |
46 | (ert-deftest command--beginning-of-defun--from-start-of-another-block ()
47 | "Move to beginning-of-defun"
48 | (with-terraform-temp-buffer
49 | "
50 | variable \"ami\" {
51 | description = \"the AMI to use\"
52 | }
53 |
54 | resource \"aws_instance\" \"web\"{
55 | ami = ${variable.ami}
56 | count = 2
57 | }
58 | "
59 |
60 | (forward-cursor-on "^resource")
61 | (call-interactively 'hcl-beginning-of-defun)
62 | (should (looking-at "^variable"))))
63 |
64 | (ert-deftest command--end-of-defun--from-within-block ()
65 | "Move to end-of-defun"
66 | (with-terraform-temp-buffer
67 | "
68 | variable \"ami\" {
69 | description = \"the AMI to use\"
70 | }
71 | # end1
72 | resource \"aws_instance\" \"web\"{
73 | ami = ${variable.ami}
74 | count = 2
75 | }
76 | "
77 |
78 | (forward-cursor-on "use")
79 | (call-interactively 'hcl-end-of-defun)
80 | (should (looking-at "^# end1"))))
81 |
82 | (ert-deftest command--end-of-defun--from-start-of-block ()
83 | "Move to end-of-defun"
84 | (with-terraform-temp-buffer
85 | "
86 | variable \"ami\" {
87 | description = \"the AMI to use\"
88 | }
89 | # end1
90 | resource \"aws_instance\" \"web\"{
91 | ami = ${variable.ami}
92 | count = 2
93 | }
94 | "
95 |
96 | (forward-cursor-on "^resource")
97 | (call-interactively 'hcl-end-of-defun)
98 | (should (eobp))))
99 |
100 | (ert-deftest command--open-doc--at-resource-def-line ()
101 | (with-terraform-temp-buffer
102 | "
103 | resource \"elasticstack_elasticsearch_security_user\" \"filebeat_writer\" {
104 | username = \"filebeat_writer\"
105 | password = random_password.filebeat_writer.result
106 | roles = [\"filebeat_writer\"]
107 | depends_on = [elasticstack_elasticsearch_security_role.filebeat_writer]
108 | }
109 | "
110 |
111 | (forward-cursor-on "security_user")
112 | (cl-letf (((symbol-function 'terraform--get-resource-provider-namespace) (lambda (prov) "elastic")))
113 | (should (equal (terraform--resource-url-at-point) "https://registry.terraform.io/providers/elastic/elasticstack/latest/docs/resources/elasticsearch_security_user")))))
114 |
115 | (ert-deftest command--open-doc--at-data-resource-def-line ()
116 | (with-terraform-temp-buffer
117 | "
118 | data \"aws_subnets\" \"example\" {
119 | filter {
120 | name = \"vpc-id\"
121 | values = [var.vpc_id]
122 | }
123 | }
124 | "
125 | (forward-cursor-on "subnets")
126 | (cl-letf (((symbol-function 'terraform--get-resource-provider-namespace) (lambda (prov) "hashicorp")))
127 | (should (equal (terraform--resource-url-at-point) "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets")))))
128 |
129 | (ert-deftest command--add-comment-doc--at-data-resource-def-line ()
130 | (with-terraform-temp-buffer
131 | "
132 | data \"aws_subnets\" \"example\" {
133 | filter {
134 | name = \"vpc-id\"
135 | values = [var.vpc_id]
136 | }
137 | }
138 | "
139 | (forward-cursor-on "filter")
140 | (cl-letf (((symbol-function 'terraform--get-resource-provider-namespace) (lambda (prov) "hashicorp")))
141 | (terraform-insert-doc-in-comment)
142 | (should (equal (search-backward "# https://registry") 2)))))
143 |
144 | (ert-deftest command--open-doc--in-body ()
145 | (with-terraform-temp-buffer
146 | "
147 | resource \"elasticstack_elasticsearch_security_user\" \"filebeat_writer\" {
148 | username = \"filebeat_writer\"
149 | password = random_password.filebeat_writer.result
150 | roles = [\"filebeat_writer\"]
151 | depends_on = [elasticstack_elasticsearch_security_role.filebeat_writer]
152 | }
153 | "
154 |
155 | (forward-cursor-on "random_password")
156 | (cl-letf (((symbol-function 'terraform--get-resource-provider-namespace) (lambda (prov) "elastic")))
157 | (should (equal (terraform--resource-url-at-point) "https://registry.terraform.io/providers/elastic/elasticstack/latest/docs/resources/elasticsearch_security_user")))))
158 |
159 | (ert-deftest command--terraform--get-resource-provider-source-in-buffer ()
160 | (with-terraform-temp-buffer
161 | "
162 | # blah blah
163 | terraform {
164 | required_providers {
165 | aws = {
166 | source = \"hashicorp/aws\"
167 | version = \"~> 5\"
168 | }
169 | azurerm = {
170 | source = \"hashicorp/azurerm\"
171 | version = \"~> 3.47\"
172 | }
173 | }
174 | }
175 | "
176 | (should (equal (terraform--get-resource-provider-source-in-buffer "azurerm") "hashicorp/azurerm"))
177 | ;;(should (equal (terraform--get-resource-provider-source-in-buffer "plop") nil))
178 | (should (equal (terraform--get-resource-provider-source-in-buffer "aws") "hashicorp/aws"))))
179 |
180 | ;; required_providers is defined in current buffer
181 | (ert-deftest command--terraform--get-resource-provider-source ()
182 | (with-terraform-temp-buffer
183 | "
184 | # blah blah
185 | terraform {
186 | required_providers {
187 | aws = {
188 | source = \"hashicorp/aws\"
189 | version = \"~> 5\"
190 | }
191 | azurerm = {
192 | source = \"hashicorp/azurerm\"
193 | version = \"~> 3.47\"
194 | }
195 | }
196 | }
197 | "
198 | (should (equal (terraform--get-resource-provider-source "aws" "test/fixtures") "hashicorp/aws"))))
199 |
200 | ;; required_providers is defined in another file
201 | (ert-deftest command--terraform--get-resource-provider-source-provider-in-file ()
202 | (should (equal (terraform--get-resource-provider-source "aws" "test/fixtures") "hashicorp/aws")))
203 |
204 | ;;; test-command.el ends here
205 |
--------------------------------------------------------------------------------
/test/test-helper.el:
--------------------------------------------------------------------------------
1 | ;;; test-helper.el --- helper for testing terraform-mode
2 |
3 | ;; Copyright (C) 2017 by Syohei YOSHIDA
4 |
5 | ;; Author: Syohei YOSHIDA
6 |
7 | ;; This program is free software; you can redistribute it and/or modify
8 | ;; it under the terms of the GNU General Public License as published by
9 | ;; the Free Software Foundation, either version 3 of the License, or
10 | ;; (at your option) any later version.
11 |
12 | ;; This program is distributed in the hope that it will be useful,
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | ;; GNU General Public License for more details.
16 |
17 | ;; You should have received a copy of the GNU General Public License
18 | ;; along with this program. If not, see .
19 |
20 | ;;; Commentary:
21 |
22 | ;;; Code:
23 |
24 | (require 'terraform-mode)
25 |
26 | (defmacro with-terraform-temp-buffer (code &rest body)
27 | "Insert `code' and enable `terraform-mode'. cursor is beginning of buffer"
28 | (declare (indent 0) (debug t))
29 | `(with-temp-buffer
30 | (insert ,code)
31 | (goto-char (point-min))
32 | (terraform-mode)
33 | (font-lock-fontify-buffer)
34 | ,@body))
35 |
36 | (defun forward-cursor-on (pattern)
37 | (let ((case-fold-search nil))
38 | (re-search-forward pattern))
39 | (goto-char (match-beginning 0)))
40 |
41 | (defun backward-cursor-on (pattern)
42 | (let ((case-fold-search nil))
43 | (re-search-backward pattern))
44 | (goto-char (match-beginning 0)))
45 |
46 | (defun face-at-cursor-p (face)
47 | (eq (face-at-point) face))
48 |
49 | ;;; test-helper.el ends here
50 |
--------------------------------------------------------------------------------
/test/test-highlighting.el:
--------------------------------------------------------------------------------
1 | ;;; test-highlighting.el --- test for highlighting
2 |
3 | ;; Copyright (C) 2017 by Syohei YOSHIDA
4 |
5 | ;; Author: Syohei YOSHIDA
6 |
7 | ;; This program is free software; you can redistribute it and/or modify
8 | ;; it under the terms of the GNU General Public License as published by
9 | ;; the Free Software Foundation, either version 3 of the License, or
10 | ;; (at your option) any later version.
11 |
12 | ;; This program is distributed in the hope that it will be useful,
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | ;; GNU General Public License for more details.
16 |
17 | ;; You should have received a copy of the GNU General Public License
18 | ;; along with this program. If not, see .
19 |
20 | ;;; Commentary:
21 |
22 | ;;; Code:
23 |
24 | (require 'ert)
25 | (require 'terraform-mode)
26 |
27 | (ert-deftest font-lock--boolean-keywords ()
28 | "Syntax highlight of `boolean' keywords"
29 |
30 | (dolist (keyword '("true" "false" "on" "off" "yes" "no"))
31 | (with-terraform-temp-buffer
32 | keyword
33 | (should (face-at-cursor-p 'font-lock-constant-face)))))
34 |
35 | (ert-deftest font-lock--constants-keywords ()
36 | "Syntax highlight of constant keywords"
37 |
38 | (dolist (keyword '("null"))
39 | (with-terraform-temp-buffer
40 | keyword
41 | (should (face-at-cursor-p 'font-lock-constant-face)))))
42 |
43 | (ert-deftest font-lock--provider-block--with-one-space ()
44 | "Syntax highlight of `provider' block."
45 |
46 | (with-terraform-temp-buffer
47 | "
48 | provider \"aws\" {
49 | access_key = \"foo\"
50 | }
51 | "
52 |
53 | (forward-cursor-on "provider")
54 | (should (face-at-cursor-p 'terraform-builtin-face))))
55 |
56 | (ert-deftest font-lock--provider-block--with-multiple-spaces ()
57 | (with-terraform-temp-buffer
58 | "
59 | provider \"aws\" {
60 | var = \"foo\"
61 | }
62 | "
63 |
64 | (forward-cursor-on "provider")
65 | (should (face-at-cursor-p 'terraform-builtin-face))))
66 |
67 | (ert-deftest font-lock--resource-block--with-one-space ()
68 | "Syntax highlight of `resource' block."
69 |
70 | (with-terraform-temp-buffer
71 | "
72 | resource \"aws_security_group\"\"default\" {
73 | name = \"terraform_example\"
74 | description = \"Used in terraform\"
75 | }
76 | "
77 |
78 | (forward-cursor-on "resource")
79 | (should (face-at-cursor-p 'terraform-builtin-face))))
80 |
81 | (ert-deftest font-lock--resource-block--with-multiple-spaces ()
82 | (with-terraform-temp-buffer
83 | "
84 | resource \"aws_security_group\" \"default\" {
85 | name = \"terraform_example\"
86 | description = \"Used in terraform\"
87 | }
88 | "
89 |
90 | (forward-cursor-on "resource")
91 | (should (face-at-cursor-p 'terraform-builtin-face))))
92 |
93 | (ert-deftest font-lock--data-block--with-one-space ()
94 | "Syntax highlight of 'data' block"
95 |
96 | (with-terraform-temp-buffer
97 | "
98 | data \"template_file\" \"userdata\" {
99 | source = \"foo\"
100 | }
101 | "
102 |
103 | (forward-cursor-on "data")
104 | (should (face-at-cursor-p 'terraform-builtin-face))))
105 |
106 | (ert-deftest font-lock--data-block--with-multiple-spaces ()
107 | (with-terraform-temp-buffer
108 | "
109 | data \"template_file\" \"userdata\" {
110 | source = \"foo\"
111 | }
112 | "
113 |
114 | (forward-cursor-on "data")
115 | (should (face-at-cursor-p 'terraform-builtin-face))))
116 |
117 | (ert-deftest font-lock--module-block--with-single-space ()
118 | "Syntax highlight of `module' block"
119 |
120 | (with-terraform-temp-buffer
121 | "
122 | module \"consul\" {
123 | source = \"foo\"
124 | }
125 | "
126 |
127 | (forward-cursor-on "module")
128 | (should (face-at-cursor-p 'terraform-builtin-face))))
129 |
130 | (ert-deftest font-lock--module-block--with-multiple-spaces ()
131 | (with-terraform-temp-buffer
132 | "
133 | module \"consul\" {
134 | source = \"foo\"
135 | }
136 | "
137 |
138 | (forward-cursor-on "module")
139 | (should (face-at-cursor-p 'terraform-builtin-face))))
140 |
141 | (ert-deftest font-lock--output-block--with-single-space ()
142 | "Syntax highlight of `output' block"
143 | (with-terraform-temp-buffer
144 | "
145 | output \"address\" {
146 | value = \"foobar\"
147 | }
148 | "
149 |
150 | (forward-cursor-on "output")
151 | (should (face-at-cursor-p 'terraform-builtin-face))))
152 |
153 | (ert-deftest font-lock--output-block--with-multiple-spaces ()
154 | (with-terraform-temp-buffer
155 | "
156 | output \"address\" {
157 | value = \"foobar\"
158 | }
159 | "
160 |
161 | (forward-cursor-on "output")
162 | (should (face-at-cursor-p 'terraform-builtin-face))))
163 |
164 | (ert-deftest font-lock--provisioner-block ()
165 | "Syntax highlight of `provisioner' block"
166 | (with-terraform-temp-buffer
167 | "
168 | resource \"aws_instance\" \"web\" {
169 | provisioner \"file\" {
170 | source = \"hoge.conf\"
171 | }
172 | }
173 | "
174 |
175 | (forward-cursor-on "provisioner")
176 | (should (face-at-cursor-p 'terraform-builtin-face))))
177 |
178 | (ert-deftest font-lock--atlas-block ()
179 | "Syntax highlight of `atlas' block"
180 | (with-terraform-temp-buffer
181 | "
182 | atlas {
183 | name = \"foo\"
184 | }
185 | "
186 | (forward-cursor-on "atlas")
187 | (should (face-at-cursor-p 'terraform-builtin-face))))
188 |
189 | (ert-deftest font-lock--assignment-statement--with-spaces-around-equal-sign ()
190 | "Syntax highlight of assignment statement"
191 | (with-terraform-temp-buffer
192 | "
193 | foo = \"var\"
194 | "
195 |
196 | (forward-cursor-on "foo")
197 | (should (face-at-cursor-p 'terraform-variable-name-face))))
198 |
199 |
200 | (ert-deftest font-lock--assignment-statement--without-spaces-around-equal-sign ()
201 | (with-terraform-temp-buffer
202 | "
203 | foo=\"var\"
204 | "
205 |
206 | (forward-cursor-on "foo")
207 | (should (face-at-cursor-p 'terraform-variable-name-face))))
208 |
209 | (ert-deftest font-lock--assignment-statement--with-spaces-after-equal-sign ()
210 | (with-terraform-temp-buffer
211 | "
212 | foo= \"var\"
213 | bar = local.baz == 1
214 | "
215 |
216 | (forward-cursor-on "foo")
217 | (should (face-at-cursor-p 'terraform-variable-name-face))))
218 |
219 | (ert-deftest font-lock--assignment-statement--with-eqeq-comparison-value ()
220 | (with-terraform-temp-buffer
221 | "
222 | foo= \"var\"
223 | bar = local.baz == 1
224 | "
225 |
226 | (forward-cursor-on "baz")
227 | (should-not (face-at-cursor-p 'terraform-variable-name-face))))
228 |
229 | (ert-deftest font-lock--assignment-statement--inside-a-map ()
230 | (with-terraform-temp-buffer
231 | "
232 | output \"name\" {
233 | bar = \"baz\"
234 | map {
235 | hoge = \"${bar}\"
236 | }
237 | }
238 | "
239 | (forward-cursor-on "hoge")
240 | (should (face-at-cursor-p 'terraform-variable-name-face))))
241 |
242 | (ert-deftest font-lock--assignment-statement--with-a-builtin-variable ()
243 | (with-terraform-temp-buffer
244 | "
245 | module \"test\" {
246 | backend = test
247 | }
248 | "
249 | (forward-cursor-on "backend")
250 | (should (face-at-cursor-p 'terraform-variable-name-face))
251 | (forward-cursor-on "=")
252 | (should (face-at-cursor-p nil))))
253 |
254 | (ert-deftest font-lock--string-interpolation ()
255 | "Syntax highlight of string interpolation"
256 | (with-terraform-temp-buffer
257 | "
258 | foo = \"hello world\"
259 | bar = \"${foo}\"
260 | "
261 |
262 | (forward-cursor-on "{foo}")
263 | (forward-char 1)
264 | (should (face-at-cursor-p 'font-lock-variable-name-face))))
265 |
266 | (ert-deftest font-lock--single-line-comment--at-bol ()
267 | "Syntax highlight of single line comment"
268 |
269 | (with-terraform-temp-buffer
270 | "# foo"
271 |
272 | (forward-cursor-on "foo")
273 | (should (face-at-cursor-p 'font-lock-comment-face))))
274 |
275 | (ert-deftest font-lock--single-line-comment--after-statement ()
276 | (with-terraform-temp-buffer
277 | " bar baz # foo "
278 |
279 | (forward-cursor-on "foo")
280 | (should (face-at-cursor-p 'font-lock-comment-face))))
281 |
282 | (ert-deftest font-lock--no-variable-assignment-in-comment--single-line ()
283 | (with-terraform-temp-buffer
284 | "# foo = \"bar\""
285 |
286 | (forward-cursor-on "foo")
287 | (should (face-at-cursor-p 'font-lock-comment-face))))
288 |
289 | (ert-deftest font-lock--no-variable-assignment-in-comment--backend-s3 ()
290 | (with-terraform-temp-buffer
291 | "# backend \"s3\" {
292 | # bucket = \"somestate\"
293 | # key = \"tfstates/terraform.tfstate\"
294 | # region = \"us-east-1\"
295 | # }
296 | "
297 | (forward-cursor-on "bucket")
298 | (should (face-at-cursor-p 'font-lock-comment-face))))
299 |
300 | (ert-deftest font-lock--map-statement ()
301 | "Syntax highlight of map"
302 | (with-terraform-temp-buffer
303 | "
304 | resource \"aws_security_group\" \"default\" {
305 | ingress {
306 | from_port = 22
307 | to_port = 22
308 | }
309 | }
310 | "
311 |
312 | (forward-cursor-on "ingress")
313 | (should (face-at-cursor-p 'font-lock-type-face))))
314 |
315 | (ert-deftest font-lock--multiple-line-comment--on-single-line ()
316 | "Syntax highlight of multiple line comment"
317 |
318 | (with-terraform-temp-buffer
319 | "/* foo */"
320 |
321 | (forward-cursor-on "foo")
322 | (should (face-at-cursor-p 'font-lock-comment-face))))
323 |
324 | (ert-deftest font-lock--multiple-line-comment--on-multiple-lines ()
325 | (with-terraform-temp-buffer
326 | "
327 | /*
328 | foo **
329 | bar **
330 | /////
331 | baz ##
332 | */"
333 |
334 | (forward-cursor-on "foo")
335 | (should (face-at-cursor-p 'font-lock-comment-face))
336 |
337 | (forward-cursor-on "bar")
338 | (should (face-at-cursor-p 'font-lock-comment-face))
339 |
340 | (forward-cursor-on "baz")
341 | (should (face-at-cursor-p 'font-lock-comment-face))))
342 |
343 | (ert-deftest font-lock--inner-block ()
344 | "Syntax highlight of special inner block"
345 |
346 | (with-terraform-temp-buffer
347 | "
348 | provisioner \"file\" {
349 | source = \"conf/myapp.conf\"
350 | destination = \"/etc/myapp.conf\"
351 | connection {
352 | type = \"ssh\"
353 | user = \"root\"
354 | password = \"${var.root_password}\"
355 | }
356 | }"
357 |
358 | (forward-cursor-on "connection")
359 | (should (face-at-cursor-p 'terraform-builtin-face))))
360 |
361 | ;;; test-highlighting ends here
362 |
--------------------------------------------------------------------------------
/test/test-indentation.el:
--------------------------------------------------------------------------------
1 | ;;; test-indentation.el --- test for indentation
2 |
3 | ;; Copyright (C) 2017 by Syohei YOSHIDA
4 |
5 | ;; Author: Syohei YOSHIDA
6 |
7 | ;; This program is free software; you can redistribute it and/or modify
8 | ;; it under the terms of the GNU General Public License as published by
9 | ;; the Free Software Foundation, either version 3 of the License, or
10 | ;; (at your option) any later version.
11 |
12 | ;; This program is distributed in the hope that it will be useful,
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | ;; GNU General Public License for more details.
16 |
17 | ;; You should have received a copy of the GNU General Public License
18 | ;; along with this program. If not, see .
19 |
20 | ;;; Commentary:
21 |
22 | ;;; Code:
23 |
24 | (require 'ert)
25 | (require 'terraform-mode)
26 |
27 | (ert-deftest indentation--no-indentation ()
28 | "No indentation case"
29 | (with-terraform-temp-buffer
30 | "
31 | foo = \"val1\"
32 | bar = \"val2\"
33 | "
34 |
35 | (forward-cursor-on "bar")
36 | (call-interactively 'indent-for-tab-command)
37 | (should (= (current-indentation) 0))))
38 |
39 | (ert-deftest indentation--no-indentation-with-empty-line ()
40 | "No indentation case with empty lines"
41 | (with-terraform-temp-buffer
42 | "
43 | foo = \"val1\"
44 | \t
45 | bar = \"val2\"
46 | "
47 |
48 | (forward-cursor-on "bar")
49 | (call-interactively 'indent-for-tab-command)
50 | (should (= (current-indentation) 0))))
51 |
52 | (ert-deftest indentation--no-indentation-into-comment ()
53 | "No indentation case into comment"
54 | (with-terraform-temp-buffer
55 | "
56 | foo = 10
57 | /*
58 | bar = 20
59 | */
60 | "
61 |
62 | (forward-cursor-on "bar")
63 | (let ((cur-indent (current-indentation)))
64 | (call-interactively 'indent-for-tab-command)
65 | (should (= (current-indentation) cur-indent)))))
66 |
67 | (ert-deftest indentation--indentation-into-block--in-provider-block ()
68 | "Indent into block"
69 | (with-terraform-temp-buffer
70 | "
71 | provider \"aws\" {
72 | foo = 10
73 | }
74 | "
75 |
76 | (forward-cursor-on "foo")
77 | (call-interactively 'indent-for-tab-command)
78 | (should (= (current-indentation) terraform-indent-level))))
79 |
80 |
81 | (ert-deftest indentation--indentation-into-block--in-variable-block ()
82 | (with-terraform-temp-buffer
83 | "
84 | variable \"aws_amis\" {
85 | foo = 10
86 | }
87 | "
88 |
89 | (forward-cursor-on "foo")
90 | (call-interactively 'indent-for-tab-command)
91 | (should (= (current-indentation) terraform-indent-level))))
92 |
93 | (ert-deftest indentation--indentation-into-block--in-resource-block ()
94 | (with-terraform-temp-buffer
95 | "
96 | resource \"aws_security_group\" \"group\" {
97 | foo = 10
98 | }
99 | "
100 |
101 | (forward-cursor-on "foo")
102 | (call-interactively 'indent-for-tab-command)
103 | (should (= (current-indentation) terraform-indent-level))))
104 |
105 | (ert-deftest indentation--indentation-into-nested-block ()
106 | "Indent into nested blocks"
107 | (with-terraform-temp-buffer
108 | "
109 | resource \"aws_security_group\" \"default\" {
110 | ingress {
111 | from_port = 22
112 | }
113 | }
114 | "
115 |
116 | (forward-cursor-on "ingress")
117 | (let ((cur-indent (current-indentation)))
118 | (forward-cursor-on "from_port")
119 | (call-interactively 'indent-for-tab-command)
120 | (should (= (current-indentation) (+ cur-indent terraform-indent-level))))))
121 |
122 | (ert-deftest indentation--map-indentation ()
123 | "Indent for map entry"
124 | (with-terraform-temp-buffer
125 | "
126 | map_var {
127 | key = val
128 | }
129 | "
130 |
131 | (forward-cursor-on "key")
132 | (call-interactively 'indent-for-tab-command)
133 | (should (= (current-indentation) terraform-indent-level))))
134 |
135 | (ert-deftest indentation--array-indentation ()
136 | "Indent for array element"
137 | (with-terraform-temp-buffer
138 | "
139 | array_var [
140 | \"foo\"
141 | ]
142 | "
143 |
144 | (forward-cursor-on "foo")
145 | (call-interactively 'indent-for-tab-command)
146 | (should (= (current-indentation) terraform-indent-level))))
147 |
148 | (ert-deftest indentation--change-indent-size ()
149 | "Indent for array element"
150 | (let ((terraform-indent-level 4))
151 | (with-terraform-temp-buffer
152 | "
153 | array_var [
154 | \"foo\"
155 | ]
156 | "
157 | (forward-cursor-on "foo")
158 | (call-interactively 'indent-for-tab-command)
159 | (should (= (current-indentation) 4)))))
160 |
161 | ;;; test-indentation.el ends here
162 |
--------------------------------------------------------------------------------