Community
    • Login

    Terraform HCL syntax highlighting support

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 5 Posters 13.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • carlosvsilvaC
      carlosvsilva
      last edited by

      Hi guys thanks for the editor it works great but as I use it more and more often I notice missing things, like support for recognition of .tf files (Terraform files in HCL language). Terraform is becoming huge by the day (Infrastructure As Code) why don’t you add support for this language ?
      The spec is here in case anyone takes up the challenge:
      https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md

      Thanks!
      Best regards,
      Carlos Silva

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @carlosvsilva
        last edited by

        @carlosvsilva ,

        There are an unknowable number of possible languages for syntax highlighting. Notepad++ will never natively handle them all – that is why Notepad++ has the feature of User Defined Languages (UDL) which allow you to define keywords, folding, and other syntax highlighting for any language you want. The page I linked is the official top-level description of UDL in the official online Notepad++ user manual; there is another site which describes the technical aspects.

        You should try setting up a UDL for this language. Give it a go, and if you have specific questions that you need help, you can share what you’ve got so far (use the Export feature of the UDL, and paste the XML here, using the </> button to embed the text), and we can try help you figure out your issues.

        Once you’re done, you can submit it to the User Defined Languages Collection so that others can find it and use it, too.

        Good luck, and let us know if you have questions.

        1 Reply Last reply Reply Quote 3
        • carlosvsilvaC
          carlosvsilva
          last edited by

          Hi thanks for replying. As of right now I don’t have enough time to undertake this project, so I leave this as a challenge for someone who has free time on their hands.

          I noticed that if I select Java language for the .tf file it highlights well enough. Is there a way to associate .tf files with Java language by default so I don’t have to manually associate the language for every file? Thanks.

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @carlosvsilva
            last edited by

            @carlosvsilva said in Terraform HCL syntax highlighting support:

            Is there a way to associate .tf files with Java language by default so I don’t have to manually associate the language for every file?

            I believe going to the Settings menu and choosing Style Configurator… and setting this up will do the job:

            c64b3b55-94c9-4db2-8054-0b40d3daee7c-image.png

            carlosvsilvaC 1 Reply Last reply Reply Quote 2
            • carlosvsilvaC
              carlosvsilva @Alan Kilborn
              last edited by

              @Alan-Kilborn Thanks that worked. But how do I preload the Jenkinsfile UDL for all files named “Jenkinsfile” (note the lack of extension)? I would like to know that so I can also preload Makefile style for Makefile’s, etc

              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @carlosvsilva
                last edited by

                @carlosvsilva said in Terraform HCL syntax highlighting support:

                ut how do I preload the Jenkinsfile UDL for all files named “Jenkinsfile” (note the lack of extension)? I would like to know that so I can also preload Makefile style for Makefile’s, etc

                That’s a really good question, since this seems to work based upon actually having an extension to key off of.

                Without a UDL being involved, N++ knows that when you load a file called “makefile” (without a .mak or .mk extension) it should set the language to “Makefile”, but I don’t know if this is a hardcoded thing, or?

                EkopalypseE 1 Reply Last reply Reply Quote 1
                • EkopalypseE
                  Ekopalypse @Alan Kilborn
                  last edited by

                  @Alan-Kilborn said in Terraform HCL syntax highlighting support:

                  but I don’t know if this is a hardcoded thing, or?

                  Seems it is.

                  Alan KilbornA 1 Reply Last reply Reply Quote 2
                  • Alan KilbornA
                    Alan Kilborn @Ekopalypse
                    last edited by

                    @Ekopalypse

                    And being a Python person for a bit of time, I’ve never heard of these which are also hardcoded (but to load as Python without a .py extension):

                    • “SConstruct”
                    • “SConscript”
                    • “wscript”

                    I’m stunned and confused. :-)

                    EkopalypseE 1 Reply Last reply Reply Quote 1
                    • EkopalypseE
                      Ekopalypse @Alan Kilborn
                      last edited by

                      @Alan-Kilborn said in Terraform HCL syntax highlighting support:

                      I’m stunned and confused. :-)

                      Me too :-), especially the wscript one which reminds me to be the function to call vbs scripts.

                      Alan KilbornA 1 Reply Last reply Reply Quote 1
                      • Alan KilbornA
                        Alan Kilborn @Ekopalypse
                        last edited by

                        But back to the OP’s need:

                        I suppose a script plugin could watch for a file of the given extensionless filename to be opened, and then switch the “language” of the file’s tab.

                        Actually not 100% sure if this would work for a UDL (I’m simply unfamiliar because I don’t use any), but I think it would.

                        1 Reply Last reply Reply Quote 2
                        • carlosvsilvaC
                          carlosvsilva
                          last edited by carlosvsilva

                          That would work wonders, through a plug-in.
                          Notepad++ even could have that by default instead of hardcoded, since the list is subject to be updated with time.
                          The plug-in could allow to define extensionless filename and/or extension paired with UDL/native language to load.
                          Does anyone here know how to develop NPP plug-ins? :)

                          1 Reply Last reply Reply Quote 0
                          • carlosvsilvaC
                            carlosvsilva
                            last edited by

                            @Alan-Kilborn said in Terraform HCL syntax highlighting support:

                            Settings menu and choosing Style Configurator

                            Or better yet, allowing the Style Configurator in the Settings menu accept not only file extensions but also extensionless filenames as well. Thanks

                            PeterJonesP 1 Reply Last reply Reply Quote 1
                            • PeterJonesP
                              PeterJones @carlosvsilva
                              last edited by

                              @carlosvsilva said in Terraform HCL syntax highlighting support:

                              Or better yet, allowing the Style Configurator in the Settings menu accept not only file extensions but also extensionless filenames as well. Thanks

                              I like that idea. someone should make a feature request ;-)

                              1 Reply Last reply Reply Quote 0
                              • mmannionM
                                mmannion
                                last edited by

                                I know this thread is old, but it’s come up as the top hit in web searches periodically over the past year, so I figured I’d post a reply here.

                                I configured a user-defined language for HCL - at least a fledgling one - based on the documentation references that @PeterJones cites in the second post in this thread, as well as a cursory review of the HCL language spec. The relevant configuration is posted below, with the following caveats:

                                • I barely know HCL, this is my first Notepad++ UDL, and I’ve only spent about 2 hours on this so far, so set your expectations accordingly

                                  • I’m posting this draft UDL in case it might help others get started, but…

                                  • I’m not planning to maintain this as a project, so please don’t request fixes/enhancements

                                • I defined the new language using the Notepad++ 8.1.9.1 Language > User Defined Language > Define your language dialog

                                  • Accordingly, the configuration below was persisted in my %APPDATA%\Notepad++\userDefineLang.xml file

                                    • Another thread I read said that this is the old way of storing UDLs (the new way being the userDefinedLang directory) but the doc states that this is the method the configurator dialog uses, so voila
                                • I’m using dark mode with some global overrides, so you may need to change the colors just to make them readable if you’re using a light-colored background

                                • Two limits of which I’m aware are:

                                  • Folding doesn’t work. This seems to be due to the fact that I’ve defined curly braces as the folding delimiter, but also as operators and in another delimiter (I can’t explain it better than that at the moment).

                                  • Heredocs aren’t formatted specially, like you get with the built-in “shell” language, for example. I think that UDL just doesn’t support heredocs - but it’s equally likely that I just don’t know how to do it.

                                You should be able to add the following to your userDefineLang.xml file (creating the file, if necessary) and restart Notepad++. Once you do, you should see a new “HCL” choice at the bottom of the Language menu (note that this won’t show up under the “H” sub-menu along with the built-in languages):

                                npp_hcl.jpg

                                Here’s the content of my userDefineLang.xml file:

                                <?xml version="1.0" encoding="UTF-8" ?>
                                <NotepadPlus>
                                    <UserLang name="HCL" ext="tf" udlVersion="2.1">
                                        <Settings>
                                            <Global caseIgnored="no" allowFoldOfComments="yes" foldCompact="no" forcePureLC="0" decimalSeparator="0" />
                                            <Prefix Keywords1="no" Keywords2="no" Keywords3="no" Keywords4="no" Keywords5="no" Keywords6="no" Keywords7="no" Keywords8="no" />
                                        </Settings>
                                        <KeywordLists>
                                            <Keywords name="Comments">00# 00// 01 02 03/* 04*/</Keywords>
                                            <Keywords name="Numbers, prefix1"></Keywords>
                                            <Keywords name="Numbers, prefix2"></Keywords>
                                            <Keywords name="Numbers, extras1"></Keywords>
                                            <Keywords name="Numbers, extras2"></Keywords>
                                            <Keywords name="Numbers, suffix1"></Keywords>
                                            <Keywords name="Numbers, suffix2"></Keywords>
                                            <Keywords name="Numbers, range"></Keywords>
                                            <Keywords name="Operators1">+ - * / % &amp;&amp; || ! == != &lt; &gt; &lt;= &gt;= : ? { } = =&gt; [ ] ( ) . , ... ${ %{ ~</Keywords>
                                            <Keywords name="Operators2"></Keywords>
                                            <Keywords name="Folders in code1, open">{</Keywords>
                                            <Keywords name="Folders in code1, middle"></Keywords>
                                            <Keywords name="Folders in code1, close">}</Keywords>
                                            <Keywords name="Folders in code2, open"></Keywords>
                                            <Keywords name="Folders in code2, middle"></Keywords>
                                            <Keywords name="Folders in code2, close"></Keywords>
                                            <Keywords name="Folders in comment, open"></Keywords>
                                            <Keywords name="Folders in comment, middle"></Keywords>
                                            <Keywords name="Folders in comment, close"></Keywords>
                                            <Keywords name="Keywords1">data default dynamic locals module output provider resource terraform variable</Keywords>
                                            <Keywords name="Keywords2">false null true</Keywords>
                                            <Keywords name="Keywords3">else endfor endif for if in</Keywords>
                                            <Keywords name="Keywords4">abs abspath alltrue anytrue base64decode base64encode base64gzip base64sha256 base64sha512 basename bcrypt can ceil chomp chunklist cidrhost cidrnetmask cidrsubnet cidrsubnets coalesce coalescelist compact concat contains csvdecode defaults dirname distinct element file filebase64 filebase64sha256 filebase64sha512 fileexists filemd5 fileset filesha1 filesha256 filesha512 flatten floor format formatdate formatlist indent index join jsondecode jsonencode keys length list log lookup lower map matchkeys max md5 merge min nonsensitive one parseint pathexpand pow range regex regexall replace reverse rsadecrypt sensitive setintersection setproduct setsubtract setunion sha1 sha256 sha512 signum slice sort split strrev substr sum templatefile textdecodebase64 textencodebase64 timeadd timestamp title tobool tolist tomap tonumber toset tostring transpose trim trimprefix trimspace trimsuffix try upper urlencode uuid uuidv5 values yamldecode yamlencode zipmap</Keywords>
                                            <Keywords name="Keywords5"></Keywords>
                                            <Keywords name="Keywords6"></Keywords>
                                            <Keywords name="Keywords7"></Keywords>
                                            <Keywords name="Keywords8"></Keywords>
                                            <Keywords name="Delimiters">00${ 00%{ 01$${ 01%%[ 02} 02} 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21&apos; 21&quot; 22\ 22\ 23&apos; 23&quot;</Keywords>
                                        </KeywordLists>
                                        <Styles>
                                            <WordsStyle name="DEFAULT" fgColor="FFFFFF" bgColor="808080" fontStyle="0" nesting="0" />
                                            <WordsStyle name="COMMENTS" fgColor="008040" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="LINE COMMENTS" fgColor="008040" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="NUMBERS" fgColor="FF0000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS1" fgColor="FF00FF" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS2" fgColor="FFFF00" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS3" fgColor="00FFFF" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS4" fgColor="00FFFF" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS5" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS6" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS7" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="KEYWORDS8" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="OPERATORS" fgColor="00FFFF" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="FOLDER IN CODE1" fgColor="808080" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="FOLDER IN CODE2" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="FOLDER IN COMMENT" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="DELIMITERS1" fgColor="8080FF" bgColor="FFFFFF" fontStyle="0" nesting="16781440" />
                                            <WordsStyle name="DELIMITERS2" fgColor="FF0080" bgColor="FFFFFF" fontStyle="0" nesting="128" />
                                            <WordsStyle name="DELIMITERS3" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="DELIMITERS4" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="DELIMITERS5" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="DELIMITERS6" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="DELIMITERS7" fgColor="FF0000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
                                            <WordsStyle name="DELIMITERS8" fgColor="808080" bgColor="FFFFFF" fontStyle="0" nesting="1" />
                                        </Styles>
                                    </UserLang>
                                </NotepadPlus>
                                

                                Thanks to Ivan Radić for his excellent UDL 2.1 documentation; I wouldn’t have made it this far without it.

                                1 Reply Last reply Reply Quote 4
                                • First post
                                  Last post
                                The Community of users of the Notepad++ text editor.
                                Powered by NodeBB | Contributors