Skip to content
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

FindNextSubmatch: return submatches when searching #3552

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matthias314
Copy link
Contributor

This PR complements FindNext by FindNextSubmatch. In addition to the start and end of the match as a whole this function returns start and end of each submatch, analogous to Regexp.FindSubmatchIndex. I think this is very useful for certain non-trivial search operations, for example for finding the current environment of some position in a LaTeX file. One could also think of other search functions, for example in the spirit of Regexp.FindAllSubmatchIndex. In any case, this PR is at least a start.

This is the first time I work with Go (and Lua). So please check that everything is OK. I've played around with the new function, and it seems to work well. The only issue I have found is possibly due to the way the Go-Lua interface handles slices as opposed to arrays. When accessing a slice element from Lua, I seem to get a pointer instead of a struct. For example, the Lua function

function f3(bp)
    local buf = bp.Buf
    match, found = buf:FindNextSubmatch("a(x|y)b", buf:Start(), buf:End(), buf:Start(), true)
    if found then
    	local cur = bp.Cursor
    	cur:SetSelectionStart(buffer.Loc(match[1].X, match[1].Y)) -- works
     	cur:SetSelectionEnd(match[2]) -- errors
    end
end

gives

bad argument #2 to SetSelectionEnd (cannot use &{3 2} (type *buffer.Loc) as type buffer.Loc)

Is there a way to avoid this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant