Community
    • Login

    Search in target is sometimes failing

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    46 Posts 3 Posters 3.7k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EkopalypseE
      Ekopalypse @Peter Goddard
      last edited by

      @Peter-Goddard

      Sorry, but there is a small, but unfortunately insurmountable problem,
      I cannot program in C++, I am a python junkie :-)

      Peter GoddardP 1 Reply Last reply Reply Quote 0
      • Peter GoddardP
        Peter Goddard @Ekopalypse
        last edited by

        @Ekopalypse No problem. Maybe you could just confirm that this is a memory issue for me? After restarting notepad my plugin seems to work and I can use it over and over again until it fails and then won’t work until I restart notepad. I’m pretty sure this means it’s a memory problem, and I don’t really know where to go from here

        EkopalypseE 1 Reply Last reply Reply Quote 0
        • EkopalypseE
          Ekopalypse @Peter Goddard
          last edited by Ekopalypse

          @Peter-Goddard

          until it fails and then won’t work until I restart notepad

          yes, that could mean that you have an overflow.
          There are several ways to find out what is causing it.
          Old school, use print statements in your code and check certain parameters.
          More modern, use Visual Studios features like Analyzer and Debugger to see what is going on.
          I assume you are using Visual Studio for development,
          there is one setting I would suggest to activate, it is to treat every warning as an error.
          (Project->Properties->Configuration Properties->C/C+±>General->Treat warnings as errors->Yes)

          Peter GoddardP 1 Reply Last reply Reply Quote 1
          • Peter GoddardP
            Peter Goddard @Ekopalypse
            last edited by

            @Ekopalypse

            Thanks, I’ll try that.

            There is another thing I have noticed and I wonder if you could help me with it. The text file I am working on is around 40’000 lines long, and as I said before I am formatting small parts of that file and eventually it will start to mess up. What I have noticed though, is that when I copy and paste a part of the original text file to a separate notepad tab and try to format it, then once it’s formatted it, delete it and then repeat the process, the formatting process never messes up. Could it be that the length of the text file has something to do with how many times I can use my plugin on it?

            EkopalypseE 1 Reply Last reply Reply Quote 0
            • EkopalypseE
              Ekopalypse @Peter Goddard
              last edited by Ekopalypse

              @Peter-Goddard

              yes, could be but to me it sounds more like an issue with the “new” content.
              For example, let’s say we have a text like this

              This is a sampl of a text which has two wrong words named sampl.
              

              Now let’s assume your plugin is used to auto correct wrong words.
              If your code logic would be

              1. find all instances of the wrong word
              2. Loop over the results and replace with the correct word

              then it would fail as you invalidate the position of the second sampl
              word by replacing the first one.
              In such a scenario replacing from bottom to top would avoid this.
              Of course, there are multiple other ways to solve this issue as well.

              Peter GoddardP 1 Reply Last reply Reply Quote 1
              • Peter GoddardP
                Peter Goddard @Ekopalypse
                last edited by

                @Ekopalypse

                I see what you mean, but I don’t think that applies to my situation as when my plugin fails, I can simply reopen notepad and continue to format some more content until it fails again. If this issue was happening to every instance of content all of the time then I could see why this makes sense.

                EkopalypseE 1 Reply Last reply Reply Quote 1
                • EkopalypseE
                  Ekopalypse @Peter Goddard
                  last edited by Ekopalypse

                  @Peter-Goddard

                  yes, it could be that this is not the issue, but a new npp start also means that your data is not the same as before,
                  but you certainly have more information to evaluate this than I do,
                  I am fishing here more or less in the dark.
                  Can you describe in pseudocode what you are doing, maybe this could be helpful to find the issue.

                  Peter GoddardP 1 Reply Last reply Reply Quote 1
                  • Peter GoddardP
                    Peter Goddard @Ekopalypse
                    last edited by

                    @Ekopalypse

                    Sure. This plugin just places a “\n” infront every case of “<” in the selected area.

                    function(){

                    Set the target search area to the area that has been selected.

                    Look for the first case of “<” in the targeted area.

                    While a, “<” can be found, get the placement of, “<”, insert a “\n” at that spot. Set the beginning of the target search area just ahead of where “<” is now found and increase the end of the target search area by 1. Search again for the next “<” in the target search area

                    }

                    EkopalypseE 1 Reply Last reply Reply Quote 0
                    • EkopalypseE
                      Ekopalypse @Peter Goddard
                      last edited by

                      @Peter-Goddard

                      From your explanation this doesn’t work.
                      You need to advance the start by 2, correct?
                      Assuming the following, 0123… are positions made visible.

                      0123456789
                      ab<def<h
                      

                      position of < is 2 correct?
                      Insert c at that position results in

                      0123456789
                      abc<def<h
                      

                      and if you set the start now to position+1 (=3) it will re-find the same <

                      Sure this is what you do?
                      My pseudocode would look like this

                      target = selected text
                      target_end = last position of selected text
                      position = search in target for '<'
                      while position > -1:
                          insert char '\n' at position 
                          reset target_start to position+2 and target_end to target_end+1
                          position = search in target for '<'
                      
                      Peter GoddardP 2 Replies Last reply Reply Quote 0
                      • Peter GoddardP
                        Peter Goddard @Ekopalypse
                        last edited by

                        @Ekopalypse

                        Sorry, I am increasing the start by 2, I just didn’t clarify that. I meant that when I said, “Set the beginning of the target search area just ahead of where “<” is now found”. My mistake. I know my code works because when I select the text,

                        <<<<

                        it results in

                        <
                        <
                        <
                        <

                        Sorry for the confusion

                        EkopalypseE 1 Reply Last reply Reply Quote 0
                        • Peter GoddardP
                          Peter Goddard @Ekopalypse
                          last edited by Peter Goddard

                          @Ekopalypse Of course, my code works, until it breaks liked we talked about before

                          1 Reply Last reply Reply Quote 0
                          • EkopalypseE
                            Ekopalypse @Peter Goddard
                            last edited by

                            @Peter-Goddard

                            ok, then I would say it is time to debug/print some info about
                            target_start, target_end and position found.
                            Make a debug build, use OutputDebugStringW and tools like DebugView
                            to capture the output. Of course only, if you can’t run within Visual Studio.

                            1 Reply Last reply Reply Quote 1
                            • EkopalypseE
                              Ekopalypse
                              last edited by

                              Actually, it doesn’t have to be a debug build, a release build should do it as well.

                              Peter GoddardP 1 Reply Last reply Reply Quote 0
                              • Peter GoddardP
                                Peter Goddard @Ekopalypse
                                last edited by

                                @Ekopalypse

                                Okay, I’m trying that now. I am using visual studios and I have added in my code a OutputDebugStringW, now where would I find the values that it outputs when I use my plugin in notepad?

                                EkopalypseE 1 Reply Last reply Reply Quote 0
                                • EkopalypseE
                                  Ekopalypse @Peter Goddard
                                  last edited by

                                  @Peter-Goddard

                                  Did you start npp from within Visual Studio or did you attach (from Debug menu) npp to visual studio? If so, those should appear in
                                  View->Output window.

                                  Peter GoddardP 1 Reply Last reply Reply Quote 0
                                  • Peter GoddardP
                                    Peter Goddard @Ekopalypse
                                    last edited by

                                    @Ekopalypse

                                    I think I have made a very obvious mistake. I forgot to specify my search flag as you mentioned in my other post. After switching it to Regexp, it seems to be working. I feel like that was very obvious and you have taken a lot of your time to help me, so thank you again for all your help.

                                    If anything else happens, I’ll post again. Thanks.

                                    1 Reply Last reply Reply Quote 3
                                    • First post
                                      Last post
                                    The Community of users of the Notepad++ text editor.
                                    Powered by NodeBB | Contributors