Community
    • 登入

    Weird behavior with editor.rereplace function

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    12 貼文 2 Posters 6.4k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Eric ChungE
      Eric Chung
      最後由 編輯

      I have a text file with the following content:

      This is a not selected line !!!
      This is line one !!!
      Today is a beautiful day !!!
      This is line three !!!
      This is a not selected line !!!

      The text file contains 5 lines of text and I have Python Script installed within Notepad++ and I am executing the commands from the Python Script Console window.

      I’ve selected only the middle three lines (lines 2-4) for this test. Below is the output of the commands that I’ve executed.

      Python 2.7.6-notepad++ r2 (default, Apr 21 2014, 19:26:54) [MSC v.1600 32 bit (Intel)]
      Initialisation took 125ms
      Ready.
      >>> editor.targetFromSelection()
      >>> editor.getTargetStart()
      33
      >>> editor.getTargetEnd()
      107
      >>> editor.searchInTarget(r’beautiful’)
      66
      >>> editor.getTargetStart()
      66
      >>> editor.getTargetEnd()
      75
      >>> editor.replaceTargetRE(‘great’)
      5
      >>> editor.getTargetStart()
      0
      >>> editor.getTargetEnd()
      136

      My question is, after the replace command, shouldn’t the target range refer to the replacement text according to the documentation, which should be (start = 66, end = 71), since it replaced with 5 characters. But from the result above, I’ve gotten (start = 0, end = 136), can anyone explain to me what am I doing wrong? Or is this a bug?

      Thanks in advance.

      1 條回覆 最後回覆 回覆 引用 1
      • dailD
        dail
        最後由 dail 編輯

        It is possible that another plugin is changing the target behind the scene. Or possibly Notepad++ depending on what settings are enabled.

        However for me when I try it with the LuaScript plugin, it gives 66/71 as the target after doing the replace.

        1 條回覆 最後回覆 回覆 引用 0
        • Eric ChungE
          Eric Chung
          最後由 編輯

          I’ve just now realized that in the title I typed editor.rereplace but in the example I actually used editor.replaceTargetRE, but just for people who are wondering, I do get the same weird result no matter which command was used.

          1 條回覆 最後回覆 回覆 引用 1
          • Eric ChungE
            Eric Chung
            最後由 編輯

            @dail
            Thanks for the reply and I’ve also repeated the same commands with LuaScript plugin on my end…unfortunately I am still getting the weird result, see below for my LuaScript console log:

            Lua 5.3.2 Copyright © 1994-2015 Lua.org, PUC-Rio

            editor:TargetFromSelection()
            editor.TargetStart
            33
            editor.TargetEnd
            107
            editor:SearchInTarget(‘beautiful’)
            66
            editor.TargetStart
            66
            editor.TargetEnd
            75
            editor:ReplaceTargetRE(‘great’)
            5
            editor.TargetStart
            0
            editor.TargetEnd
            136

            Any other idea that I can try?

            1 條回覆 最後回覆 回覆 引用 0
            • dailD
              dail
              最後由 dail 編輯

              Try putting all that in a function and calling the function. That way it will execute all the code at once and not allow another plugin (or Notepad++) to possibly mess with the target.

              1 條回覆 最後回覆 回覆 引用 0
              • Eric ChungE
                Eric Chung
                最後由 編輯

                @dail
                I’ve just tried the same commands on a separate computer which doesn’t have Python Script or LuaScript installed before. So for the test, only LuaScript plugin is installed…but the result came back wrong as well (same weird result).

                1 條回覆 最後回覆 回覆 引用 0
                • Eric ChungE
                  Eric Chung
                  最後由 編輯

                  @dail
                  I’ve put all the command inside a function and with that function executed with Python Script plugin, I’ve gotten the same bad result.

                  Python 2.7.6-notepad++ r2 (default, Apr 21 2014, 19:26:54) [MSC v.1600 32 bit (Intel)]
                  Initialisation took 140ms
                  Ready.
                  getting selection
                  start = 33
                  end = 107
                  look for the word beautiful
                  start = 66
                  end = 75
                  replacing with the word great
                  start = 0
                  end = 136

                  1 條回覆 最後回覆 回覆 引用 0
                  • dailD
                    dail
                    最後由 編輯

                    I don’t have PythonScript installed however this is what I did within LuaScript.

                    Lua 5.3.2  Copyright (C) 1994-2015 Lua.org, PUC-Rio
                    
                    function f()
                    	editor.SearchFlags = SCFIND_REGEXP
                    	editor:TargetFromSelection()
                    	print(editor.TargetStart)
                    	print(editor.TargetEnd)
                    	print("look for the word beautiful")
                    	editor:SearchInTarget("beautiful")
                    	print(editor.TargetStart)
                    	print(editor.TargetEnd)
                    	print("replacing with the word great")
                    	editor:ReplaceTargetRE("great")
                    	print(editor.TargetStart)
                    	print(editor.TargetEnd)
                    end
                    
                    f()
                    33
                    107
                    look for the word beautiful
                    66
                    75
                    replacing with the word great
                    66
                    71
                    
                    

                    What version of Notepad++ are you using?

                    1 條回覆 最後回覆 回覆 引用 0
                    • Eric ChungE
                      Eric Chung
                      最後由 編輯

                      I am using Notepad++ v6.9.1.

                      1 條回覆 最後回覆 回覆 引用 0
                      • dailD
                        dail
                        最後由 dail 編輯

                        Same here. I’m honestly not sure what is going on then. As per the Scintilla documentation the target should be the start/end. My only guess is that there is some Notepad++ setting that is messing up the target range.

                        1 條回覆 最後回覆 回覆 引用 0
                        • Eric ChungE
                          Eric Chung
                          最後由 編輯

                          @dail
                          Good news, mystery solved. Now I can re-create this weird behavior of the replace function on your side as well. Simply go to Settings. Preferences, choose MISC., and under Clickable Link Settings, check the box next to Enable option.

                          Now run your Lua script again and you should get what I am getting. Thanks for all your help :)

                          1 條回覆 最後回覆 回覆 引用 0
                          • dailD
                            dail
                            最後由 編輯

                            I turned on that option and it still gave me the correct results. Tried it on v6.8.3, v6.9, and v6.9.1. But at least it is working correctly for you now.

                            1 條回覆 最後回覆 回覆 引用 0
                            • 第一個貼文
                              最後的貼文
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors