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

lua-find-matching-token-word get wrong position #144

Open
twlz0ne opened this issue Aug 27, 2018 · 4 comments
Open

lua-find-matching-token-word get wrong position #144

twlz0ne opened this issue Aug 27, 2018 · 4 comments

Comments

@twlz0ne
Copy link

twlz0ne commented Aug 27, 2018

Steps to reproduce

(with-temp-buffer
  (insert "\
local x = function() return {{}} end

if true then
   if false then
      return 0
   end
   return 1
end")
  (lua-mode)
  (goto-char (point-max))
  (lua-find-matching-token-word "end" 'backward)
  (buffer-substring-no-properties (point-min) (point)))

Expected

"local x = function() return {{}} end
if true "

;; --- or ---

"local x = function() return {{}} end
"

Actual

"local x = "

Evironment

  • macOS 10.11.6
  • Emacs 27.0.50
  • lua-mode-20180323.1021
@immerrr
Copy link
Owner

immerrr commented Aug 27, 2018

Hi! Good catch, and an even better bugreport, thank you! I'll try to have a look this week.

@immerrr
Copy link
Owner

immerrr commented Jan 13, 2019

Apologies for the Valve-time-ish "this week" delay.

lua-find-matching-token-word function is meant to be internal-ish. It expects point to be at the beginning of the token whose pair you are trying to find to disambiguate situations like (with | being point)

{{    }|}   -- which close-brace are you trying to match, the first or the second one?

In this case point is beyond the closing end and thus the if-if-end-end block is skipped and then it tries to find a matching token that precedes them and fails to do so stopping at local for no specific reason.

I'll add the necessary documentation, but could you elaborate how did you end up using that lua-find-matching-token-word directly?

@twlz0ne
Copy link
Author

twlz0ne commented Jan 18, 2019

Currently, I can forward step by step from <0> to <1> to <2> by (lua-forward-sexp) / (lua-find-matching-token-word "function" 'forward):

local x = <0>function() return {{}} end<1>

function foobar() end<2>

But how can I backward step by step from <2> to <1> to <0>? The (lua-find-matching-token-word "end" 'backward) straight from <2> to <0>, the <1> was skipped.

@immerrr
Copy link
Owner

immerrr commented Jun 16, 2019

Sorry that this went under my radar for so long. To answer your question, as I said, lua-find-matching-token-word expects point to be at the beginning of the token whose counterpart you want to find.

In your example to get to point <A> and <B> from their respective end, you need your point to be at point <A'> and <B'> respectively

local x = <A>function() return {{}} <A'>end

<B>function foobar() <B'>end

I know it is a bit counter-intuitive. Back in the day I remembered it as "it starts at the beginning of the open/close token and goes past the corresponding close token". It is not very user-friendly that

<X>function foobar() <Y>end<Z>

forward from X means Z and to go backward to X you need to go from Y, but it was not meant as a direct replacement for forward/backward-sexp functions that maintain that nice stability when hopping forward and backward.

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

No branches or pull requests

2 participants