lua-mode icon indicating copy to clipboard operation
lua-mode copied to clipboard

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

Open twlz0ne opened this issue 6 years ago • 4 comments

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

twlz0ne avatar Aug 27 '18 12:08 twlz0ne

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

immerrr avatar Aug 27 '18 12:08 immerrr

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?

immerrr avatar Jan 13 '19 10:01 immerrr

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.

twlz0ne avatar Jan 18 '19 05:01 twlz0ne

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.

immerrr avatar Jun 16 '19 09:06 immerrr