Ever find that you have a whole lot of:

.. sourcecode:: py

  rec[f['analyte']] ... rec[f['sample_type']] ...

expressions in your code, and now things have changed and you want them
all to be:

.. sourcecode:: py

  row.Analyte ... row.Sample_Type ...

basically if str variable s was::

  rec[f['analyte']]

then you want to perform:

.. sourcecode:: py

  s = "row."+s.split("'")[1].title()

on each one.  In general it would be nice to be able to use a python
expression when search and replace doesn't cut it.

The button code below creates a button, ``fac``, which, when pressed,
creates another button, with some name you choose, which, when pressed,
executes some python code to fix the selected text in the body.

You can define the code to be executed in two ways, either in its own
node:

 - insert a new node with a headline which describes the refactor
 - enter code in the node which modifies the string variable ``s``, 
   which is initially set to the selected text in the body
 - press the ``fac`` button, which creates a new button named
   after this code node
 - select each offending piece of text and press the button created
   in the previous step to fix

or

 - type some code modifying ``s`` right in the body you're working on
 - press the ``fac`` button, which creates a new button named "fix"
 - select each offending piece of text and press the button created
   in the previous step to fix

Note:

 - unlike regular button nodes, changing the code after the
   button's created (first option above) doesn't change the code
   executed by the button
 - replacing selection text makes Leo reposition the insert point at
   the top of the window, this is annoying but unrelated to this code

Here's the button code:

.. sourcecode:: py

  @button fac
    from leo.plugins.mod_scripting import scriptingController
    
    sc = scriptingController(c)
    
    if c.frame.body.hasSelection():
        code = c.frame.body.getSelectedText()
        heading = 'fix'
    else:
        code = p.b
        heading = p.h
    
    def transform(c=c, code=code):
        s = c.frame.body.getSelectedText()
        g.es(s)
        exec code
        g.es(s)
        c.frame.body.deleteTextSelection()
        i = c.frame.body.getInsertPoint()
        c.frame.body.insert(i, s)
        p.b = c.frame.body.getAllText()
        c.frame.body.setInsertPoint(i)
    
    b = sc.createIconButton(
        heading,
        command = transform,
        shortcut = None,
        statusLine = 'Make filter button',
        bg = "LightBlue"
    )

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en.

Reply via email to