Hi,
not sure if "folding" is the correct word here, but, given the following code is it possible to do this in TextMate:
let x = { number: 42 }
1. I want to place the cursor after the { and when I press return, I the closing bracket should too go on a new line
let x = { number: 42 }
2. If I put the cursor in front of the word something and press backspace, wrap it back to the original line.
Thanks Fabian
I'm progressing with this.
I tried to use a Command, unfortunately so far I haven't found the proper scope ;).
Is it possible to get "Edit->Select->Enclosing Typing Pairs" as input for a command?
I also see that some commands look like this:
"{command = 'copySelectionToFindPboard:'; },"
Is there a list of all these commands?
Thank you Fabian
On 21.08.2016, at 14:15 , Fabian Zeindl fabian.zeindl@gmail.com wrote:
Hi,
not sure if "folding" is the correct word here, but, given the following code is it possible to do this in TextMate:
let x = { number: 42 }
- I want to place the cursor after the { and when I press return, I the closing bracket should too go on a new line
let x = { number: 42 }
- If I put the cursor in front of the word something and press backspace, wrap it back to the original line.
Thanks Fabian
On 21 Aug 2016, at 14:15, Fabian Zeindl wrote:
[…] given the following code is it possible to do this in TextMate:
let x = { number: 42 }
- I want to place the cursor after the { and when I press return, I
the closing bracket should too go on a new line
let x = { number: 42 }
In the Ruby bundle there is a “Toggle ‘do … end’ / ‘{ … }’” command bound to ⌃{ which does something very similar, so for the implementation you can look at this command.
Though I took a quick look, and it seems surprisingly complex, and it even reads the selection or document (when there is no selection) in TextMate’s XML format (which include scope info). I am not sure why this is necessary.
For actually overloading return and backspace in these locations, this is possible if you make the grammar match them and assign a scope at the desired locations, but I think it would be better with a toggle key like the Ruby bundle.
Come to think of it, there is also _Fold/Unfold Code_ in the C bundle (also bound to ⌃{) which also does something similar.
On 22.08.2016, at 08:05 , Allan Odgaard mailinglist@textmate.org wrote:
Though I took a quick look, and it seems surprisingly complex, and it even reads the selection or document (when there is no selection) in TextMate’s XML format (which include scope info). I am not sure why this is necessary.
That's what I want to avoid.
For actually overloading return and backspace in these locations, this is possible if you make the grammar match them and assign a scope at the desired locations, but I think it would be better with a toggle key like the Ruby bundle.
I thought about overloading Cmd+Return anywhere inside the parentheses, but I'd need a selector for this first. Best would be If I could get the "match enclosing pairs" as a command input.
On 22 Aug 2016, at 10:50, Fabian Zeindl wrote:
I thought about overloading Cmd+Return anywhere inside the parentheses, but I'd need a selector for this first. Best would be If I could get the "match enclosing pairs" as a command input.
You can create a macro that first does the selection, then executes a command, e.g.:
( { command = 'selectBlock:'; }, { command = 'executeCommandWithOptions:'; argument = { name = 'Convert Block'; command = '#!/usr/bin/env ruby print "your code here…"'; input = 'selection'; output = 'replaceSelectedText'; }; }, )
This though leaves your insertion selected, there are other options, like asking for caret to be after insertion, but you probably want to keep the caret where it was.
You lose the caret when you do the selection, one way could be to insert a magic placeholder before doing the selection, then find this in your command, and produce a snippet where the placeholder is replaced by `${0}` (to indicate where caret should go).
On 22.08.2016, at 11:15 , Allan Odgaard mailinglist@textmate.org wrote: You can create a macro that first does the selection, then executes a command, e.g..
Awesome, this works.
This though leaves your insertion selected, there are other options, like asking for caret to be after insertion, but you probably want to keep the caret where it was.
Right now I'm outputting a snippet, since I need proper indentation and as far as I know that's only possible with a snippet? I'll set the cursor with $0 then.
What do I have to set in the macro to get the indentation?
Also: Is it possible to programmatically change the output type in ruby commands, like "exit_discard" and so on?
On 22 Aug 2016, at 11:33, Fabian Zeindl wrote:
Right now I'm outputting a snippet, since I need proper indentation and as far as I know that's only possible with a snippet? I'll set the cursor with $0 then.
Remember also to escape the content before/after the placeholder (caret), incase there is any snippet syntax. The `escape.rb` under `TM_BUNDLE_SUPPORT` has an `e_sn` function for this.
What do I have to set in the macro to get the indentation?
I assume you want `output = insertAsSnippet;`.
Also: Is it possible to programmatically change the output type in ruby commands, like "exit_discard" and so on?
Yes, you can use these. Though since that only affects the command, if you `exit_discard` then you leave the user with a selection, so it might be better to just output an “unchanged” snippet incase you want to abort.
TextMate detects when a command’s output matches what’s already in the document (it does prefix/suffix match) so there should be no visible flash when replacing content with what’s already there.
On 22.08.2016, at 11:39 , Allan Odgaard mailinglist@textmate.org wrote: Yes, you can use these. Though since that only affects the command, if you exit_discard then you leave the user with a selection, so it might be better to just output an “unchanged” snippet incase you want to abort.
TextMate detects when a command’s output matches what’s already in the document (it does prefix/suffix match) so there should be no visible flash when replacing content with what’s already there
That is the weird thing: My command (running from the Macro):
#!/usr/bin/env ruby18 -wKU require "#{ENV['TM_SUPPORT_PATH']}/lib/escape.rb" block = STDIN.read if block =~ /^{ [^\s]/ print e_sn("{\n#{block[2..-3]}\n}") else print e_sn(block) end
1. When I replace selected Text, or use "snippet" I get no indentation:
function() { const x = { number: 33 } }
->
function() { const x = { number: 33 } }
2. When I output with insertAsSnippet, I get almost correction indentation
function() { const x = { number: 33 } }
->
function() { const x = { number: 33 } }
3. When I output with insertAsSnippet, but just print the block again without changes, I get more indentation than I need…
function() { const x = { number: 33 } }
->
function() { const x = { number: 33 } }
On 22 Aug 2016, at 12:48, Fabian Zeindl wrote:
That is the weird thing: My command (running from the Macro):
TextMate doesn’t run the indent algorithm on the output but simply indent it to the “current level”.
So #2 is what you want, but you want to replace ^ with \t to increase the indent by one for all but the last line.
At least I think that should work…
On 22.08.2016, at 13:00 , Allan Odgaard mailinglist@textmate.org wrote: That is the weird thing: My command (running from the Macro): TextMate doesn’t run the indent algorithm on the output but simply indent it to the “current level”.
So #2 is what you want, but you want to replace ^ with \t to increase the indent by one for all but the last line.
ok, but I still got the problem: When I just output without modification: print e_sn(STDIN.read) it just indents the lines further.
On 22 Aug 2016, at 13:29, Fabian Zeindl wrote:
ok, but I still got the problem: When I just output without modification: print e_sn(STDIN.read) it just indents the lines further.
Yes, that is expected because your first line has indent, so that indent is added to every line of the command’s output.
On 22.08.2016, at 13:36 , Allan Odgaard mailinglist@textmate.org wrote:
On 22 Aug 2016, at 13:29, Fabian Zeindl wrote:
ok, but I still got the problem: When I just output without modification: print e_sn(STDIN.read) it just indents the lines further.
Yes, that is expected because your first line has indent, so that indent is added to every line of the command’s output.
Ok, so if I want to output a snippet to get correct indentation, but in some cases, not change anything, I need to change output type to discard.
How can I do that in ruby?
Again, thanks for your time. fabian
On 22 Aug 2016, at 14:47, Fabian Zeindl wrote:
Ok, so if I want to output a snippet to get correct indentation, but in some cases, not change anything, I need to change output type to discard.
How can I do that in ruby?
require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes" ⋮ TextMate.exit_discard
It is possible to disable the indent of snippet output, but in the normal case, I assume you do want the indent, as I don’t think it is possible for you to figure out what the current indent is (since your command gets a selection that doesn’t start at first column).
We could introduce a new `exit_insert_as_snippet_without_indent` so that a command would be able to just temporarily disable the indent.
On 22.08.2016, at 15:05 , Allan Odgaard mailinglist@textmate.org wrote: require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes" ⋮ TextMate.exit_discard It is possible to disable the indent of snippet output, but in the normal case, I assume you do want the indent, as I don’t think it is possible for you to figure out what the current indent is (since your command gets a selection that doesn’t start at first column).
We could introduce a new exit_insert_as_snippet_without_indent so that a command would be able to just temporarily disable the indent.
Why not just run the normal indentation on the commands output?
On 22 Aug 2016, at 16:47, Fabian Zeindl wrote:
We could introduce a new exit_insert_as_snippet_without_indent so that a command would be able to just temporarily disable the indent.
Why not just run the normal indentation on the commands output?
I fear that could break some existing snippets, so it would have to be opt-in, but in theory (I would think), a snippet always “knows best”.
E.g. a snippet could insert partial code that would not indent properly by the indenter, or it could be a language where TextMate’s indenter is just not up to getting the indent 100% correct (e.g. most functional languages).
On 22.08.2016, at 11:39 , Allan Odgaard <mailinglist@textmate.org mailto:mailinglist@textmate.org> wrote: Yes, you can use these. Though since that only affects the command, if you exit_discard then you leave the user with a selection, so it might be better to just output an “unchanged” snippet incase you want to abort.
TextMate detects when a command’s output matches what’s already in the document (it does prefix/suffix match) so there should be no visible flash when replacing content with what’s already there
That is the weird thing: My command (running from the Macro):
#!/usr/bin/env ruby18 -wKU require "#{ENV['TM_SUPPORT_PATH']}/lib/escape.rb" block = STDIN.read if block =~ /^{ [^\s]/ print e_sn("{\n#{block[2..-3]}\n}") else print e_sn(block) end
1. When I replace selected Text, or use "snippet" I get no indentation:
function() { const x = { number: 33 } }
->
function() { const x = { number: 33 } }
2. When I output with insertAsSnippet, I get almost correction indentation
function() { const x = { number: 33 } }
->
function() { const x = { number: 33 } }
3. When I output with insertAsSnippet, but just print the block again without changes, I get more indentation than I need…
function() { const x = { number: 33 } }
->
function() { const x = { number: 33 } }