-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JumpToMatchingBrace inconsistent with adjacent unpaired braces e.g. )[ or }( or )( #3308
Comments
Hi, On the subject of braces; it would be nice to bind a key so the cursor jumps to the matching brace (if the cursor is on a brace), or finds the next unpaired brace if the cursor is not on a brace. e.g. "Ctrl-J": "JumpToMatchingBrace | JumpToNextUnpairedBrace", A Kind Regards Gavin Holt |
I remember we were investigating the reasons for this "feature"... Here is a summary: #2876 (comment) |
Hi, Thank you for pointing me to the previous discussions. From my perspective I would really value a Not wanting to impose upon others - could these be written in Lua? The only drawback would be that a Lua function would not work well with multiple cursors so built-in actions may be better. Kind Regards Gavin Holt |
I don't quite understand what exact behavior of For example, if the text is |
Hi I really want a For your test string, (foo(bar)baz) a For example:
To achieve this I would first search for the next unpaired brace, save this location, then jump to matching brace, then select from this location to the saved location - excluding the limiting braces. Searching for the next unpaired brace would require counting opening/closing braces (by type), and finding the first closing brace without a preceding opening brace. If the cursor is initially on a closing brace then this is the found position and the cursor does not move forward. A Kind Regards Gavin Holt |
Hi I would plan to use all of these new actions, in key bindings: "CtrlJ": " JumpToMatchingBrace | JumpToNextUnpairedBrace ",
"CtrlShiftJ": "selectinner", With easy shortcuts to navigate braces - we might even attract some Lisp coders. 😊 Kind Regards Gavin Holt |
Got it. So, actually, it would be not just one new action
If we forget for a moment about those actions and focus just on your
I might try to code up some Lua code doing that for you, once I have some time... Or in the meantime, I might suggest you try using Loc's Move() method: the next character to the right of the given location:
or the next character to the left of it:
It automatically takes care of whether this next character is on the same line or on the next/previous line. You might also look how SelectWord() is implemented, and try to do something similar (unless you already did?). |
Ok... I think what you need for your But,
As a bonus, it would allow to fix the minor issue I described in #2876 (comment), since it would allow |
So... I've uploaded #3319 which improves
Then, use can also use these functions for finding the next "unpaired" brace to the left or to the right:
And you use them to implement the actual actions for jumping to the next "unpaired" brace:
Finally, you can implement
|
Hi, Many thanks, I will enjoy studying these new functions. function selectforwards(Current)
-- Select forwards from cursor using regex (set to ungreedy and case insensitive)
-- this does require escaping of regex metacharacter
-- this does not pollute the find history
-- this does not highlight all matches
local prompt = "SelectUntil : "
local seed = "(?sU-i).*"
local history = "SelectUntil"
micro.InfoBar():Prompt(prompt, seed, history,
function(input)
return
end,
function(input, canceled)
if input and not canceled then
local top = Current.Buf:Start()
local bottom = Current.Buf:End()
local searchLoc = -Current.Cursor.Loc
local down = true
local useRegex = true
local res, found = Current.Buf:FindNext(input, top, bottom, searchLoc, down, useRegex)
if found then
Current.Cursor:SetSelectionStart(searchLoc)
Current.Cursor:SetSelectionEnd(res[2])
Current.Cursor.OrigSelection[1] = -Current.Cursor.CurSelection[1]
Current.Cursor.OrigSelection[2] = -Current.Cursor.CurSelection[2]
Current.Cursor:GotoLoc(res[2])
Current:Relocate()
end
end
end
)
end
function selectbackwards(Current)
-- Select backwards from cursor using regex (set to ungreedy and case insensitive)
-- this does require escaping of regex metacharacter
-- this does not pollute the find history
-- this does not highlight all matches
local prompt = "SelectBack : "
local seed = "(?sU-i)"
local history = "SelectBack"
micro.InfoBar():Prompt(prompt, seed, history,
function(input)
return
end,
function(input, canceled)
if input and not canceled then
local top = Current.Buf:Start()
local bottom = Current.Buf:End()
local searchLoc = -Current.Cursor.Loc
local down = false
local useRegex = true
local res, found = Current.Buf:FindNext(input, top, bottom, searchLoc, down, useRegex)
if found then
Current.Cursor:SetSelectionStart(searchLoc)
Current.Cursor:SetSelectionEnd(res[1])
Current.Cursor.OrigSelection[1] = -Current.Cursor.CurSelection[1]
Current.Cursor.OrigSelection[2] = searchLoc
Current.Cursor:GotoLoc(res[1])
Current:Relocate()
end
end
end
)
end
function goforwards(Current)
-- Move forwards from cursor using regex (set to ungreedy and case insensitive)
-- this does require escaping of regex metacharacter
-- this does not pollute the find history
-- this does not highlight all matches
-- this will alight at the beginning of a multicharacter search
local prompt = "Go forwards : "
local seed = "(?sU-i)"
local history = "Goforwards"
micro.InfoBar():Prompt(prompt, seed, history,
function(input)
return
end,
function(input, canceled)
if input and not canceled then
local top = Current.Buf:Start()
local bottom = Current.Buf:End()
local searchLoc = -Current.Cursor.Loc
local down = true
local useRegex = true
local res, found = Current.Buf:FindNext(input, top, bottom, searchLoc, down, useRegex)
if found then
Current.Cursor:SetSelectionStart(res[1])
Current.Cursor:SetSelectionEnd(res[2])
Current.Cursor.OrigSelection[1] = -Current.Cursor.CurSelection[1]
Current.Cursor.OrigSelection[2] = -Current.Cursor.CurSelection[2]
Current.Cursor:GotoLoc(res[2])
Current:Relocate()
Current.Cursor:Deselect(true)
end
end
end
)
end
function gobackwards(Current)
-- Move backwards from cursor using regex (set to ungreedy and case insensitive)
-- this does require escaping of regex metacharacter
-- this does not pollute the find history
-- this does not highlight all matches
-- this will alight at the beginning of a multicharacter search
local prompt = "Go backwards : "
local seed = "(?sU-i)"
local history = "Gobackwards"
micro.InfoBar():Prompt(prompt, seed, history,
function(input)
return
end,
function(input, canceled)
if input and not canceled then
local top = Current.Buf:Start()
local bottom = Current.Buf:End()
local searchLoc = -Current.Cursor.Loc
local down = false
local useRegex = true
local res, found = Current.Buf:FindNext(input, top, bottom, searchLoc, down, useRegex)
if found then
Current.Cursor:SetSelectionStart(res[1])
Current.Cursor:SetSelectionEnd(res[2])
Current.Cursor.OrigSelection[1] = -Current.Cursor.CurSelection[1]
Current.Cursor.OrigSelection[2] = -Current.Cursor.CurSelection[2]
Current.Cursor:GotoLoc(res[2])
Current:Relocate()
Current.Cursor:Deselect(true)
end
end
end
)
end
I find these functions very useful, much quicker than reaching for the mouse. Kind Regards Gavin Holt PS I will leave this open as a point for discussion - until the PR is merged |
FYI #3319 merged. |
Hi, New binary + your functions work beautifully. Many Thanks Kind Regards Gavin Holt |
Hi,
I am having trouble with
JumpToMatchingBrace
action in my windows console. My console has a block cursor, I expect the cursor to bounce back and forth between one brace and its partner. At any point pressing delete should delete the brace i.e. the cursor is placed upon the brace.With the current implementation the cursor moves to the right of the found brace, and if this character happens to also be a brace, repeating the action does not return to the original brace - it goes off searching downstream.
For example in the following line of Lua, clicking upon the first
[
, and executingJumpToMatchingBrace
, will take my cursor to the first(
. ExecutingJumpToMatchingBrace
again will take the cursor past the first)
. From here further executions bounce between the first(
and a position just past the first)
.My guess is that the code has been written while watching an I beam cursor. It still malfunctions as above, but looks pretty with the cursors inside the braces. To function properly the I beam cursor always needs to locate on the left of the brace.
I have attempted to rewrite
JumpToMatchingBrace
in Lua, but manipulating the userdata locations is confusing.I am similarly frustrated after several attempts to write a
selectinner
Lua function to select the contents of matching braces.A nice tutorial about manipulating cursor/match location userdata would be greatly appreciated,
Kind Regards Gavin Holt
The text was updated successfully, but these errors were encountered: