Al: regions in AL

Created on 28 Nov 2017  路  13Comments  路  Source: microsoft/AL

just a quick question: is there a way to use regions in AL? So something like

#region Write Log to File 
many many lines of code you would like to conveniently collapse
#endregion
al-language enhancement vscode-integration

Most helpful comment

I've also modified alsyntax.tmlanguage file to get better region colors by adding this based on c# language definition part:

        <dict>
            <key>begin</key>
            <string>(^\s*)?(?=//\s*#region\b)</string>
            <key>beginCaptures</key>
            <dict>
                <key>1</key>
                <dict>
                    <key>name</key>
                    <string>keyword.preprocessor.region.al</string>
                </dict>
            </dict>
            <key>end</key>
            <string>(?!\G)</string>
            <key>patterns</key>
            <array>
                <dict>
                    <key>begin</key>
                    <string>//\s*#region</string>
                    <key>beginCaptures</key>
                    <dict>
                        <key>0</key>
                        <dict>
                            <key>name</key>
                            <string>keyword.preprocessor.region.al</string>
                        </dict>
                    </dict>
                    <key>end</key>
                    <string>\n</string>
                    <key>name</key>
                    <string>string.unquoted.preprocessor.message.al</string>
                </dict>
            </array>
        </dict>

        <dict>
            <key>match</key>
            <string>^\s*//\s*#endregion\b</string>
            <key>name</key>
            <string>keyword.preprocessor.region.al</string>
        </dict>

and it looks much better now:

regions2

All 13 comments

AL doesn't have support for regions at this moment.
We have a general issue with adding code folding to the Visual Studios Code extension because of lack of support: https://github.com/Microsoft/vscode/issues/3422

@thpeder , Is it possible or are there any plans to make AL extension for VS Code available on "full visual studio" marketplace?

We do not have any plans to support full Visual Studio. Please see #932 for more details.

I've just manually added this section to al.configuration.json file in al extension folder:

"folding": {
        "markers": {
            "start": "^\\s*//\\s*#region\\b",
            "end": "^\\s*//\\s*#endregion\\b"
        }
    }

and I can use //#region and //#endregion in al files as in the attached picture now:

regions

Could you include this modification in AL extension?

I've also modified alsyntax.tmlanguage file to get better region colors by adding this based on c# language definition part:

        <dict>
            <key>begin</key>
            <string>(^\s*)?(?=//\s*#region\b)</string>
            <key>beginCaptures</key>
            <dict>
                <key>1</key>
                <dict>
                    <key>name</key>
                    <string>keyword.preprocessor.region.al</string>
                </dict>
            </dict>
            <key>end</key>
            <string>(?!\G)</string>
            <key>patterns</key>
            <array>
                <dict>
                    <key>begin</key>
                    <string>//\s*#region</string>
                    <key>beginCaptures</key>
                    <dict>
                        <key>0</key>
                        <dict>
                            <key>name</key>
                            <string>keyword.preprocessor.region.al</string>
                        </dict>
                    </dict>
                    <key>end</key>
                    <string>\n</string>
                    <key>name</key>
                    <string>string.unquoted.preprocessor.message.al</string>
                </dict>
            </array>
        </dict>

        <dict>
            <key>match</key>
            <string>^\s*//\s*#endregion\b</string>
            <key>name</key>
            <string>keyword.preprocessor.region.al</string>
        </dict>

and it looks much better now:

regions2

It would be even nicer if the two of them // would fall away and only # should be use.
A region is not a comment. In addition, a different color than blue would be good e. g. grey as it is not a command statement.

Thanks the both of you for your suggestions. It is on our backlog but we are discussion internally how we want this to look.

I am trying to find a solution that won't require any changes to AL compiler and //#region and //#endregion tags seem to be better as they are already used for regions definition in javascript files in Visual Studio Code:

jsregion

You can see that they have been defined by VS Code team at the end of this file:
https://github.com/Microsoft/vscode/blob/master/extensions/javascript/javascript-language-configuration.json

I agree with you, it might be good to use different color for region/endregion tags but in my example I was again trying to use something that has already been defined by VS Code team and I've decided to copy color settings from C# language definition from this file:
https://github.com/Microsoft/vscode/blob/master/extensions/csharp/syntaxes/csharp.tmLanguage.json

As you can see, VS Code team decided to use:

  • keyword.preprocessor.region.cs for "region" tag
  • keyword.preprocessor.endregion.cs for "endregion" tag
  • string.unquoted.preprocessor.message.cs for region description text
    so we could use keyword.preprocessor.region.al, keyword.preprocessor.endregion.al and string.unquoted.preprocessor.message.al

And that's how VS Code shows region for C# files:
regionscs

Thank you @thpeder

My two cents to add to this thread,

I use the options to collapse and expand some/all functions in the dev environment all the time. I'd love to be able to do the same thing in VS Code - and preferably without having to define regions for each function - but I'm open to how you think to best implement this sort of thing.

I got tired of manually adding the example code of anzwdev to AL every update so I created an extension that does it automatically.
https://marketplace.visualstudio.com/items?itemName=BartPermentier.al-toolbox

Oh splendid :-) Thanks @NKarolak for pointing this out here. I will close this evergreen feature request then :-)

I also love the code example...
image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

worldofthenavcraft picture worldofthenavcraft  路  3Comments

TinaMenezes picture TinaMenezes  路  3Comments

lvanvugt picture lvanvugt  路  3Comments

kine picture kine  路  3Comments

ThomasBrodkorb picture ThomasBrodkorb  路  3Comments