[txmt-dev] Working with indentation rules

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Working with indentation rules

Jacob Carlborg
I'm working on updating the D bundle with the latest language changes
and making sure it works properly for TM2.

One thing I noticed is that the indentation rules don't work at all.
They currently work in TM1 but not in TM2.

This is how they currently look like:

<key>decreaseIndentPattern</key>
<string>^(.*\*/)?\s*\}([^}{"']*\{)?[;\s]*(//.*|/\*.*\*/\s*)?$|^\s*(public|private|protected):\s*$</string>

<key>increaseIndentPattern</key>
<string>^.*\{[^}"']*$|^\s*(public|private|protected):\s*$</string>

<key>indentNextLinePattern</key>
<string>^(?!(.*[};:])?\s*(//|/\*.*\*/\s*$)).*[^\s;:{}]\s*$</string>

If a type a function declaration (same syntax as in C), like this:

void foo ()
     {|}

When I've typed the opening brace it will look like the above (the pipe
being the position of the cursor). But setting the language to C and
doing the same will decrease the indentation, as it should. Also,
pressing enter the indention will increase, if the language is set to C.

All this behave a bit differently compared to how it does in TM1. In TM1
it will not indent the cursor after pressing enter after the closing
parenthesis. Also, pressing enter after typing the opening brace will
correctly increase the indentation with the language set to D in TM1.

Is there anyone that can help me fixing these indention rules to make it
work properly in TM2?

--
/Jacob Carlborg

_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Allan Odgaard-4
On 17 Sep 2013, at 9:38, Jacob Carlborg wrote:

> […] If a type a function declaration (same syntax as in C), like
> this:
>
> void foo ()
> {|}
>
> When I've typed the opening brace it will look like the above (the
> pipe being the position of the cursor). But
> setting the language to C and doing the same will decrease the
> indentation, as it should.

In the C bundle this was fixed by
https://github.com/textmate/c.tmbundle/commit/776da04eda90a8af9ae4e0d20381765caaca5655

Basically the indent patterns must deal with the user having typed
‘{}’ and decrease indent for that case, because of ‘indent as you
type’ and auto-pairing of characters.

> Also, pressing enter the indention will increase, if the language is
> set to C.

You mean pressing return here ‘{‸}’ will insert ‘\n\t‸\n’?

In 2.0 this is done by a snippet scoped to “between empty pairs”.
For this to work, we require the grammar to assign punctuation scopes to
braces (and other paired characters where it should work), but I’ve
been wanting to inject a rule just for empty braces, so that the 1.x
behavior works even if the grammar has not been updated.

This is basically what I wish to inject into all scopes, sans the D
suffix:

     {   match = '(\{)(\})';
         captures = {
             1 = { name = 'punctuation.section.block.begin.d'; };
             2 = { name = 'punctuation.section.block.end.d'; };
         };
     },

If the grammar alredy match (stand-alone) braces, it doesn’t need the
rule, instead it can assign the above begin/end scopes to these matches.
_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Timothy Bates
On 17 Sep 2013, at 10:17, "Allan Odgaard" <[hidden email]> wrote:

> This is basically what I wish to inject into all scopes, sans the D suffix:
>
>    {   match = '(\{)(\})';
>        captures = {
>            1 = { name = 'punctuation.section.block.begin.d'; };
>            2 = { name = 'punctuation.section.block.end.d'; };
>        };
>    },
>
> If the grammar alredy match (stand-alone) braces, it doesn’t need the rule, instead it can assign the above begin/end scopes to these matches.

Go ahead :-)

_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Jacob Carlborg
In reply to this post by Allan Odgaard-4
On 2013-09-17 11:17, Allan Odgaard wrote:

> In the C bundle this was fixed by
> https://github.com/textmate/c.tmbundle/commit/776da04eda90a8af9ae4e0d20381765caaca5655
>
>
> Basically the indent patterns must deal with the user having typed ‘{}’
> and decrease indent for that case, because of ‘indent as you type’ and
> auto-pairing of characters.

Ok, thanks.

> You mean pressing return here ‘{‸}’ will insert ‘\n\t‸\n’?

Yes, exactly.

> In 2.0 this is done by a snippet scoped to “between empty pairs”. For
> this to work, we require the grammar to assign punctuation scopes to
> braces (and other paired characters where it should work),

I can't find that snippet. Could you like to it please. Or do you mean
it will automatically work if I assign punctuation scopes to braces?

--
/Jacob Carlborg

_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Allan Odgaard-4
On 18 Sep 2013, at 8:59, Jacob Carlborg wrote:

>> In 2.0 this is done by a snippet scoped to “between empty pairs”.
>> For
>> this to work, we require the grammar to assign punctuation scopes to
>> braces (and other paired characters where it should work),
>
> I can't find that snippet. Could you like to it please. Or do you mean
> it will automatically work if I assign punctuation scopes to braces?

It will automatically work, if you assign the scopes to the braces.

The snippet is in the Source bundle, to find it, enter {‸} in a C
file, then use Bundles → Select Bundle Item… (⌃⌘T) switch to key
equivalent (⌘4) and press return (↩): this should find the snippet,
you can inspect it by pressing the link arrow or ⌥↩, here you can
see that it is scoped to:

     source & (
         (L:punctuation.section.*.begin    &
R:punctuation.section.*.end)
       | (L:punctuation.definition.*.begin &
R:punctuation.definition.*.end)
     ) - string
_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Jacob Carlborg
On 2013-09-18 12:21, Allan Odgaard wrote:

> It will automatically work, if you assign the scopes to the braces.

Nice.

> The snippet is in the Source bundle, to find it, enter {‸} in a C file,
> then use Bundles → Select Bundle Item… (⌃⌘T) switch to key equivalent
> (⌘4) and press return (↩): this should find the snippet, you can inspect
> it by pressing the link arrow or ⌥↩, here you can see that it is scoped to:
>
>      source & (
>          (L:punctuation.section.*.begin    & R:punctuation.section.*.end)
>        | (L:punctuation.definition.*.begin &
> R:punctuation.definition.*.end)
>      ) - string

I couldn't find it using Select Bundle Item. I'm guessing that's because
it's not in the Bundle -> Source menu. But I can see it if I go to
Bundles -> Edit Bundles -> Source -> Other Actions.

--
/Jacob Carlborg

_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Jacob Carlborg
In reply to this post by Allan Odgaard-4
On 2013-09-18 12:21, Allan Odgaard wrote:

> It will automatically work, if you assign the scopes to the braces.

I tried to copy what's in the C bundle but I couldn't get it to work. I
added the following:

<key>block</key>
<dict>
     <key>patterns</key>
     <array>
         <dict>
             <key>begin</key>
             <string>\{</string>
             <key>beginCaptures</key>
             <dict>
                 <key>0</key>
                 <dict>
                     <key>name</key>
                     <string>punctuation.section.block.begin.d</string>
                 </dict>
             </dict>
             <key>end</key>
             <string>\}</string>
             <key>endCaptures</key>
             <dict>
                 <key>0</key>
                 <dict>
                     <key>name</key>
                     <string>punctuation.section.block.end.d</string>
                 </dict>
             </dict>
         </dict>
     </array>
</dict>

Below this line, just before the "comments" key.

https://github.com/textmate/d.tmbundle/blob/master/Syntaxes/D.tmLanguage#L560

--
/Jacob Carlborg

_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Allan Odgaard-4
On 18 Sep 2013, at 20:54, Jacob Carlborg wrote:

> […] I tried to copy what's in the C bundle but I couldn't get it to
> work. I added the following […]
> Below this line, just before the "comments" key.

That would add it to the ‘repository’ so that it can be included as
#block, but it wouldn’t make the rule active.

Use the bundle editor (instead of editing the XML) and add the rule I
quoted in a previous make to the bottom of the root patterns array.

[ other email about snippet ]
> I couldn't find it using Select Bundle Item. I'm guessing that's
> because it's not in the Bundle -> Source menu. But I can see it if I
> go to Bundles -> Edit Bundles -> Source -> Other Actions.

I can find it via ⌃⌘T if I switch to key equivalent search and press
enter, you must however have the caret placed between empty braces in a
language that has the proper scope, or you can change the search to All
Scopes.
_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Stephen Bannasch-3
very interesting thread -- inspired me to try making some changes to the JavaScript grammar.

Learning about ctrl-command-T and them command-4 to find out what snippet is run was interesting.

I'd like to have similar indentation behavior when entering return in a pair of square braces in JavaScript. I found a solution (listed below) -- but then I wanted a similar indentation behavior when pressing return after an open square brace followed by content as I get when I enter return after an open curly brace followed by content.

Using comand-ctrl-T and then command-4 and entering return when my cursor was after an opening curly brace produced empty results.

How do I find the code that produces the indentation after entering return when my cursor is after an opening curly brace produced empty results.

--- more details ---

When I am editing in JavaScript and have a document that contains content like this""

   "destinations": [
     {
       "type": "timeSeries",
       "componentIds": [
         "angle-graph"
       ],
       "properties": [
         "currentAngle"
       ]
     },
     {
       "type": "timeSeries",
       "componentIds": [
         "last-run-table"
       ],
       "properties": [
         "rodLength",
         "ballMass",
         "startingAngle",
         "period"
       ]
     }
   ]

If I put my cursor here (just after "{" char) and press return

   "destinations": [
     {

I get a return and approriate indentation:

   "destinations": [
     {
       <=cursor-is-here

I often also find myself wanted to add another item to the array of items and would like to have a similar indentation rule applied when entering return at the end of this line:

   "destinations": [


I started with something simpler -- enable an indentation to appear when entering return with the cursor in the middle of two square brackets.

So this:
[]

Changes to this after entering return
[
   <=cursor-is-here
]

By adding this to the JavaScript Language Grammers:

                { comment = 'Allows the special return snippet to fire.';
                        match = '(\[)(\])';
                        captures = {
                                1 = { name = 'punctuation.section.array.begin.js'; };
                                2 = { name = 'punctuation.section.array.end.js'; };
                        };
                },


So I tried to use the same technique to find out what textmate was doing when my cursor is at the beginning of a curly brace and I get an indentation when entering return -- when the opening brace is followed by more content before the ending brace.

Using comand-ctrl-T and then command-4 and entering return produced empty results.

_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Jacob Carlborg
In reply to this post by Allan Odgaard-4
On 2013-09-18 22:36, Allan Odgaard wrote:

> That would add it to the ‘repository’ so that it can be included as
> #block, but it wouldn’t make the rule active.

Aha, now I see.

> Use the bundle editor (instead of editing the XML) and add the rule I
> quoted in a previous make to the bottom of the root patterns array.

Thanks, that work.

> I can find it via ⌃⌘T if I switch to key equivalent search and press
> enter, you must however have the caret placed between empty braces in a
> language that has the proper scope, or you can change the search to All
> Scopes.

I can find it now (using another computer). Don't know that happened.

--
/Jacob Carlborg

_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev
Reply | Threaded
Open this post in threaded view
|

[txmt-dev] Re: Working with indentation rules

Allan Odgaard-4
In reply to this post by Stephen Bannasch-3
On 19 Sep 2013, at 0:06, Stephen Bannasch wrote:

> […] I wanted a similar indentation behavior when pressing return
> after an open square brace followed by content as I get when I enter
> return after an open curly brace followed by content.
> […] How do I find the code that produces the indentation after
> entering return when my cursor is after an opening curly brace […]

By default, no bundle item is executed when you press return at random
places. How much indent is inserted is estimated via the indent rules,
this is explained here:
http://manual.macromates.com/en/appendix#indentation_rules

So if you want only a change to indent, then it’s the indentation
rules that should be changed.

If however you want more complex behavior, you can overload the key
binding with a snippet or command.

For JavaScript, square brackets are assigned the
“meta.brace.square.js” scope.

If we work with the grammar as-is, then we can make a snippet fire on
return only when a square bracket is to the left of the caret by using a
scope selector of:

     (L:meta.brace.square.js)

Here the scope selector targets the “left scope” via the (L:…)
syntax, meaning the scope to the left of the caret. You can view the
scope to the right of the caret by pressing ⌃⇧P (so move it one to
the left to view the left scope).

But if your desire is simply to increase the indent, when pressing
return after ‘[’, then it should be enough to edit the indent
patterns (which already target squigly braces).
_______________________________________________
textmate-dev mailing list
[hidden email]
http://lists.macromates.com/listinfo/textmate-dev