Menu

Find and replace text in a block then surround said block with other text

Tug
2014-08-06
2014-08-27
  • Tug

    Tug - 2014-08-06

    tl;dr: How to do what the title describes with a macro ? I tried solutions but none works :/

    Hi,

    I am trying to simplify the process described in the title with Practically Macro, however, I can't quite find a solution (example given at the end).

    Let me describe it more precisely:
    - A block of code, containing "/ /" comments, is selected;
    - A find and replace regex to modify all the comments;
    - The initial block is surrounded with some text.

    However, I am being confronted to a major problem as the initial selection is lost during the second step (F&R process) and only the last comment replaced is selected. I believe the solution lies in marks but unfortunately Eclipse only has one mark which does not even work as I would like it to.

    I tried to work around the one mark problem by creating two different macros between which I would move my cursor to an appropriate position but it still does not work (seems like the mark is reset between the macros):

    New process:

    Macro 1:
    - Mark selection start (PractMacro defined)
    - F&R in selection

    Manually move the cursor to the end of the inital block

    Macro 2
    - Extend selection to mark (so it should go from my cursor to mark and I get my inital selection back)
    - F&R the whole selection with itself + the surrounded text

    However, the "Extend selection to mark" selects everything from my cursor until the beginning of the document.
    When I try it in the same macro, the selection goes until the beginning of the initial block just fine, except the end of it is wrong. Does Practically Macro purposely reset the mark at the beginning of every macro ?

    If anyone has a better solution, I'd be willing to take it :)

    Thank you in advance,
    Tug

    Example of before/after:

    BEFORE:
    int a;
    / My common comment /
    a = 1;
    / More commenting /
    a += a;

    AFTER:
    / TEXT to warn below has been modified /
    int a;
    / My better comment /
    a = 1;
    / Even better commenting /
    a += a;
    / TEXT to say modifications stop here/

     

    Last edit: Tug 2014-08-06
  • Ernest

    Ernest - 2014-08-06

    The mark is part of the plugin, not Eclipse. It is part of each macro, so it doesn't persist across macros. :(

    It seems that aspect probably isn't an issue, however; you just want to be able to capture a range so that you can select range, set begin/end marks, F&R, move to begin mark, insert comment, move to end mark, insert comment. Is that correct?

     
  • Tug

    Tug - 2014-08-07

    Yes I think you understood well. My problem is having only one mark available to remember two position (beginning and end of selection). Do you have a work-around for this ?

    EDIT: The range is selected manually before starting the macro btw

     

    Last edit: Tug 2014-08-07
  • Tug

    Tug - 2014-08-07

    Note:
    I juste tried selecting text manually then starting the macro "Mark selection start" but the debug console indicate that the mark is set at the end of the selection (cursor position) Is that normal ? How come the selection isn't detected for marks but it works for F&R ?

    Console:
    --Executing command: Mark selection start
    --After command, cursor at: Line 682, Column 1
    --After command, there is no selection

     
    • Ernest

      Ernest - 2014-08-07

      Sure seems like that's a bug, but I'd have to step through the code to check.

       
  • Ernest

    Ernest - 2014-08-07

    No workaround that I know of...I'd have to think about whether I could add a second mark or something. Not sure how to do that in a clean way. I knew when I added mark support that it would be limited, but no one has actually come to me before now with an actual use case.

     
  • Tug

    Tug - 2014-08-08

    I might be interested in peeking into your code and try to understand how the whole plugin works (never dev'ed for eclipse plugins before). That's be off-work hours so I'll see how it goes :)

    If it is simple enough and I manage to find the time, (didn't look very complex concepts from the two glances I gave it) I'd be glad to help design a solution for this. The kind of macro I'm trying to develop would benefit (slightly) quite a pack of developers here.

     
  • Ernest

    Ernest - 2014-08-10

    I put in a 2nd set of mark commands (seemed like the only real option since it would be awkward to provide input when recording a macro). So now there are "Move cursor to mark #1" and "Move cursor to mark #2" etc. Thus, you can use one mark or two, as appropriate. A version 0.4.7 jar is here:
    https://sourceforge.net/projects/practicalmacro/files/experimental/

    You'd need to put this in the dropins directory to deploy it. If you get a chance to try it, let me know how it works for you. I've also pushed the code to the CVS repo if you are downloading the source.

     
  • Tug

    Tug - 2014-08-11

    Oh wow awesome !

    Still have the "--After command, there is no selection" bug though, so I can't test it yet :/

    I tried with Eclipse CDT 4.4, PMv0.4.7, and Eclipse JEE Kepler, PMv0.4.6 and I can't get to set a mark at the beginning of the (non existent) selection.
    EDIT: Should I make a ticket for this ?

    On a side note: where are the macro stored ? I had Eclipse standard Kepler installed in program files (yeah wrong i know), I then installed Eclipse standard Luna in my documents and there was already some tests macro from Kepler installed when I started PM in the new eclipse. how come ? How do I properly clean this ?

     

    Last edit: Tug 2014-08-11
    • Ernest

      Ernest - 2014-08-11

      Okay, I didn't actually look at the bug directly, but I did something I thought was similar to you and it seemed to work. I'll have to take a closer look. I would say don't open a ticket until I look a little further. So the "mark" command is actually erasing the selection?

      It looks like I stored the macro defintions in the preferences (which is part of the workspace). Look in your metadata directory here:
      .metadata.plugins\org.eclipse.core.runtime.settings

       
    • Ernest

      Ernest - 2014-08-11

      Can you just send me macro text (just export it from the preferences page). I'm confused about what you're seeing. The mark selection start/end always captures the left/right points of the selection, not the selection anchor/caret, because I felt it was much more common to just want the selected text, not the actual caret.

      I did just find a bug with the macro debugger where it was getting the selection start/length rather than start/end and was reporting no selection incorrectly. I've pushed a fix for that.

       
  • Tug

    Tug - 2014-08-13

    Duh -__- I'm so stupid.
    This was actually only a debugger display bug and I was dumb enough to believe the display and not test fully/didnt realize my macro was working. (I was often stopping the macro after seeing at the first steps there was no selection >_< )

    So YEAH IT WORKS NOW !
    Thank you so much for all the efforts you put into helping me out !

    There still seems to be a minor bug (probably only related to my macro) which I'm reporting here for informational purpose but doesn't actually needs to be solved:

    • Select anything containing a comment but NOT starting by said comment and apply my macro => everything works fine

    • Do the same selection but start it at the very beginning of the comment (ie right before "/* blabla") and the insert text I'm doing after my F&R is inserted after the comment, not before. Anyway, I don't need this to be corrected as all you need to do is select one more line before the comment and it's fine.

    Another minor bug a little more annoying though is that "Run macro as a compound" does not seem to work anymore under Luna. Ctrl-Z'ing undoes the steps one by one.
    Anyway, you've done enough for me, I'll just be careful about what I'm macro'ing on :)

    EDIT: Long week end ahead, probably won't answer before next mon/tuesday. Just let me know whenever you make 4.7 an official release :)

     

    Last edit: Tug 2014-08-13
  • Ernest

    Ernest - 2014-08-14

    Great! I have fixed the debug display to be correct.
    You'll need to send me your macro if you want me to figure out why it matters that you start out on the open slash of your comment. I did fix a bug where the mark position moved if text was inserted at the mark position. I decided it makes more sense to treat the mark as being before the character at that position.

    In any case, I've pushed out 0.4.8.

     
  • Tug

    Tug - 2014-08-18

    As of 0.4.8, the macro is indeed behaving correctly. Same for "Run as compound" which works fine. I had to undo one by one because of the debug mode :)

    Thanks again for your great support on this case !

    EDIT: I get it :) The weird behaviour is exlained by the fact that F&R is similar, functionnally to inserting text, and since I'm replacing comments by improved ones, if the end of my selection is indeed a comment, the better one inserted by F&R is re-inserted AFTER the mark, which is why my own text insertion afterward is misplaced, because it is at the mark. But this is a very specific case and I suppose you are right considering inserting text should not move the mark (did not fully think this through though)

     

    Last edit: Tug 2014-08-18
    • Tug

      Tug - 2014-08-18

      Update:

      Actually, it appears that I also need to add a character at the beginning of each line of my selection (I'm commenting out the whole block) for clarity reasons but the bottom mark is being moved away by each of my insert, even if the cursor is not at the mark . I'll do further investigating tomorrow

       
      • Tug

        Tug - 2014-08-19

        My bottom mark is definitely moving away while using:

        <searchstring><![CDATA[(^(.)*\n)|^[\n\r]]]]]><![CDATA[></searchstring>

        <replacestring><![CDATA[ * $1]]]]><![CDATA[></replacestring>

        EDIT: I am actually working around this by inserting my bottom text before anything that will be inserted above but this behaviour is still weird and probably unwanted in most case (the mark seems to shift as many chars and lines as I insert above it)

         

        Last edit: Tug 2014-08-19
        • Ernest

          Ernest - 2014-08-20

          Okay, sorry, I'm still confused. It looks like that XML is messed up (an extra CDATA tag with no end); must be some problem generating the xml.

          Regardless, this is just a single find command, right? So where in the macro do you set a mark?

          The marks should move as you insert items before them so that they keep their relative positions in the text...I'm still not quite understanding what you are seeing.

           
  • Tug

    Tug - 2014-08-27

    Hmmm, well, in my Editor (C/C++) the mark is shifted beyond the relative movement of the text (seems it shifts three times farther).

    Here is an execution trace with the attached macro:
    Before:
    this is my test sentence to expose the mark bug I encounter

    After: (Selection from "sentence" to "the" included):
    this is my test testsentence to expose the mark butest2g I encounter

    As you can see, the mark is shifted 8 char right (which is twice the length of "test")

    Maybe this is only an Eclipse CDT bug.

    EDIT: How do you properly delete a macro ? The permanent command stays in the command list and the id is unusable :/

     

    Last edit: Tug 2014-08-28

Anonymous
Anonymous

Add attachments
Cancel