Author Topic: Ruby enhancement.  (Read 5186 times)

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Ruby enhancement.
« on: January 25, 2015, 10:46:20 PM »
Hi all,

I would like to add the following enhancement to ruby, but I don't know how to go about it. Let me explain it in a workflow:

1. Select word or phrase within a quoted string. i.e. "hello world count" (glowing text is selected text).
2. Press shift+3 on the keyboard to type in '#'
3. The selected phrase now becomes enclosed in #{}. i.e. "hello #{world count}".

Another desirable simpler workflow would be as follows:
1. I press Shift+3 on the keyboard to type in '#' while I'm within a quoted string.
2. I get "#{%\c}" where the cursor, %\c, is between the two brackets where I can type ruby code.

I have to do this in ruby very often, and I saw this really useful feature in TextMate and would like to replicate it in SE.

Any help appreciated,
Ted
« Last Edit: January 25, 2015, 10:52:17 PM by flethuseo »

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Ruby enhancement.
« Reply #1 on: January 26, 2015, 06:04:21 AM »
The 1st feature can be done with a 'surround-with' alias. You could add a wrapper macro bound to a key directly calling e.g. 'surround-with strvar' where strvar is the surround-with alias you've added. 'surround-with' command without args shows a selection list of the current (Ruby) aliases.
I think the 2nd one requires macro code but it's doable.
HS2

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Ruby enhancement.
« Reply #2 on: January 27, 2015, 11:31:45 AM »
Here's some code you could try.  I don't know if I'm doing everything correctly here so use at your own risk.

Code: [Select]
#include "slick.sh"

#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)


_command void ruby_hash() name_info(','VSARG2_MULTI_CURSOR|VSARG2_CMDLINE|VSARG2_REQUIRES_EDITORCTL|VSARG2_LASTKEY)
{
   if (command_state()) {
      call_root_key('#');
      return;
   }
   
   if (!_LanguageInheritsFrom("ruby")) {
      call_root_key('#');
      return;
   }

   typeless pos1, pos2;
   if (select_active()) {
      save_pos(pos1);
      _begin_select();
      if (!_in_string()) {
         restore_pos(pos1);
         call_root_key('#');
         return;
      }
      _save_pos2(pos2);
      _end_select();
      deselect();
      _insert_text("}");
      _restore_pos2(pos2);
      _insert_text("#{");
      return;
   }

   if (!_in_string()) {
      call_root_key('#');
      return;
   }

   _insert_text("#{}");
   cursor_left();
}

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Re: Ruby enhancement.
« Reply #3 on: February 02, 2015, 04:16:41 PM »
Hi Graeme,

Sorry for the late reply. It almost works perfectly! (Workflow 2 works right) The only problem is that when I select a word or phrase, and then call ruby_hash it will not put that word between the brackets, but instead replace the text with #{}. In other words,

The expected behavior is:
<sel>Hello world</sel>
#{Hello world}

The current behavior is:
<sel>Hello world</sel>
#{}

Thanks so much for this macro, it will be tremendously useful and save me quite a few keystrokes :)

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Ruby enhancement.
« Reply #4 on: February 03, 2015, 05:28:16 AM »
Hi,
I thought this was working before I posted it.  It seems that the call to select_active() is returning false most of the time.  I have no idea why that is but I'll try and investigate.

Graeme

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Ruby enhancement.
« Reply #5 on: February 03, 2015, 05:35:31 AM »
It seems to work if there are no name_info options - you could try removing them and see if it helps in the meantime.

_command void ruby_hash() name_info(',')

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Re: Ruby enhancement.
« Reply #6 on: February 03, 2015, 04:06:49 PM »
It works when I remove the options like you specified.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Ruby enhancement.
« Reply #7 on: February 06, 2015, 01:29:32 AM »
Hi Ted
SlickEdit support have told me that I need VSARG2_MARK in the list of name_info options otherwise the selection gets cleared before the command is executed.  I love it when I learn something.

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Re: Ruby enhancement.
« Reply #8 on: February 13, 2015, 12:54:11 AM »
Just for closure, I tried what you mentioned last, and it works great. I also have added a binding for # in ruby mode so that it calls ruby_hash(). It works great.

Leaving code here in case someone wants to use this in the future.

Code: [Select]
// SlickEdit support have told me that I need VSARG2_MARK in the list of name_info options otherwise the selection gets cleared before the command is executed.
_command void ruby_hash() name_info(','VSARG2_MULTI_CURSOR|VSARG2_CMDLINE|VSARG2_REQUIRES_EDITORCTL|VSARG2_LASTKEY|VSARG2_MARK)
{
   if (command_state()) {
      call_root_key('#');
      return;
   }
   
   if (!_LanguageInheritsFrom("ruby")) {
      call_root_key('#');
      return;
   }

   typeless pos1, pos2;
   if (select_active()) {
      save_pos(pos1);
      _begin_select();
      if (!_in_string()) {
         restore_pos(pos1);
         call_root_key('#');
         return;
      }
      _save_pos2(pos2);
      _end_select();
      deselect();
      _insert_text("}");
      _restore_pos2(pos2);
      _insert_text("#{");
      return;
   }

   if (!_in_string()) {
      call_root_key('#');
      return;
   }

   _insert_text("#{}");
   cursor_left();
}