pkulchenko / ZeroBraneStudio

Lightweight Lua-based IDE for Lua with code completion, syntax highlighting, live coding, remote debugger, and code analyzer; supports Lua 5.1, 5.2, 5.3, 5.4, LuaJIT and other Lua interpreters on Windows, macOS, and Linux
http://studio.zerobrane.com/
Other
2.6k stars 519 forks source link

Move cursor between words in snake case #1121

Closed hollunder closed 2 years ago

hollunder commented 2 years ago

Maybe there is an option that allows this but I looked and did not find any. The idea is that Ctrl+right/left would move the cursor through a variable like this_is_fine just like it would move through this.is.fine.

pkulchenko commented 2 years ago

You can try setting SetWordChars("_") in every editor with something like this in user.lua:

package {
  onEditorNew =  function(self, editor) editor:SetWordChars("_") end,
  onEditorLoad =  function(self, editor) editor:SetWordChars("_") end,
}

See the words section in the Scintilla documentation.

hollunder commented 2 years ago

Thanks Paul, that wasn't quite the solution and it took me a while of fiddling to debug this but I worked it out eventually with the help of ide:Print().

The solution is to use SetPunctuationChars("_"), which is a misnomer as it appends the character rather than setting it.

package {
    onEditorNew = function(self, editor) editor:SetPunctuationChars("_") end,
    onEditorLoad = function(self, editor) editor:SetPunctuationChars("_") end,
}

Now it would be nice if I could also get it to handle these characters a bit better but if I understand the documentation correctly this can not be done using a config change. Please correct me if I'm wrong. As it stands the cursor stops before and after every group of punctuation characters. I think it would make more sense if it stopped only after every group of punctuation characters. Examples: this|.|is|.....-_|the|.|current|___|behavior this.|would.....-_|be.|nicer___|behavior

moteus commented 2 years ago

I think you can handle onEditorKeyDown event in your plugin like you want.

Package.onEditorKeyDown = function (self, editor, event)
  if event:GetModifiers() ~= wx.wxMOD_ALT or event:GetKeyCode() ~= wx.WXK_RIGHT then
    return true
  end

  -- find new position you want
  local pos = ....

  -- move cursor
  editor:SetSelection(pos, pos)

  -- disable default handler
  return false
end
pkulchenko commented 2 years ago

this|.|is|.....-_|the|.|current|_|behavior this.|would.....-|be.|nicer|behavior

I did observe the same thing and I found it strange as well. Try setting SetWhitespaceChars("._"), as it may have the desired effect. If not, you can always follow @moteus'es suggestion and implement any logic you want.

hollunder commented 2 years ago

Thanks a lot Paul, that option seem to do the trick. I still need to test it more for sideeffects. To recap, this is what I have so far:

package {
    onEditorNew = function(self, editor)
        editor:SetPunctuationChars("_")
        editor:SetWhitespaceChars("._")
    end,
    onEditorLoad = function(self, editor)
        editor:SetPunctuationChars("_")
        editor:SetWhitespaceChars("._")
    end,
}