Author Topic: Extract to local variable command  (Read 1480 times)

flethuseo

  • Senior Community Member
  • Posts: 176
  • Hero Points: 2
Extract to local variable command
« on: April 30, 2015, 12:27:38 am »
Hi all,

I want to work on a command that can simplify the following task for me.
The workflow is as follows:

I have some ruby code like so:
 
Code: [Select]
   File.open("C:\\#{@project}\\" + File.basename(__FILE__, '.rb').to_s + '.xml', 'w') do |ofile|
   #File.open(File.basename(__FILE__, '.rb').to_s + '.xml', 'w') do |ofile|
     header(ofile)

     main(ofile)

     footer(ofile)

   end

I select the following text: "C:\\#{@project}\\" + File.basename(__FILE__, '.rb').to_s + '.xml'

When I run the command I want to be able to prompt the user for a variable
name. And then it should place an assignment operation in the first blank
line above it.

The selected text should be replaced with the variable name. And an
assignment operation should be added for that variable. I've seen this
nice sophisticated behaviour in Eclipse as the "Extract Local Variable
here" operation. I can make one really simple for ruby to start.

Expected result would look like this:
Code: [Select]
   filepath = "C:\\#{@project}\\" + File.basename(__FILE__, '.rb').to_s + '.xml'
   File.open(filepath , 'w') do |ofile|
   #File.open(File.basename(__FILE__, '.rb').to_s + '.xml', 'w') do |ofile|
     header(ofile)

     main(ofile)

     footer(ofile)

   end

Anyone have a macro like this already?


hs2

  • Senior Community Member
  • Posts: 2736
  • Hero Points: 285
Re: Extract to local variable command
« Reply #1 on: April 30, 2015, 09:51:47 am »
get_string is your friend to get your variable set. The rest should be pretty straight forward.
You can simply use the var with '_insert_text', 'replace' etc. functions.
Good luck, HS2

flethuseo

  • Senior Community Member
  • Posts: 176
  • Hero Points: 2
Re: Extract to local variable command
« Reply #2 on: April 30, 2015, 03:50:39 pm »
HS2,
I have come up with the initial draft; it works ok, but I want to polish it
a little bit. I would like the prompt to be in the form of a dialog
instead of prompting me at the bottom.

Code: [Select]
#include "slick.sh"
_command extract_local_var() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   // Cut the selected text.
   cut();

   // Replace the cutted text with the  variable name.
   _str varName;
   get_string(varName, "Name of the variable: ");
   _insert_text(varName);

   // Add the variable assignment to the expression.
   insert_blankline_above();
   _insert_text(varName :+ " = ");
   paste();
}


hs2

  • Senior Community Member
  • Posts: 2736
  • Hero Points: 285
Re: Extract to local variable command
« Reply #3 on: April 30, 2015, 06:13:36 pm »
A dialog is of course possible but a bit more work. You need to do some SE GUI programming.
Unfortunately I've nothing related to post. Maybe someone else can provide a suitable example.
HS2

Graeme

  • Senior Community Member
  • Posts: 2125
  • Hero Points: 261
Re: Extract to local variable command
« Reply #4 on: May 02, 2015, 05:43:46 am »
SlickEdit provides textBoxDialog specifically for this kind of thing - it's described in the help with an example.  There's also an example in ProjectTbExtra.e I think I might have sent you a while ago.  It displays one or more text boxes, combo boxes, or check boxes on a modal dialog.

Code: [Select]
      int result = textBoxDialog(
         "Select folder", // Form caption
         TB_RETRIEVE,      // Flags
         11000,             // textbox width
         "",               // Help item
         "&New file,New from &template,New &folder," :+
         "&Explore,&Open file,Shel&l,&CD,&Browse," :+
         "OK:_ok,Cancel:_cancel\t" info1 "\n" info2" \n" info3 "\n",

         retrieve_name,
         "Path :" path,
         "-CHECKBOX Change current directory to here:"change_dir);