2

I want to use yasnippets to generate control flow statements in Lua.

-- a control flow statement in Lua
if foo then
   return "foo"
elseif bar then
   return "bar"
else 
   return "baz"
end

First I make a yasnippet for if.

# -*- mode: snippet; require-final-newline: nil -*-
# name: if
# key: if
# binding: direct-keybinding
# --
if $1 then
  $0
end

No problem. Now I make a yasnippet for elseif

# -*- mode: snippet; require-final-newline: nil -*-
# name: elseif
# key: elseif
# binding: direct-keybinding
# --
elseif $1 then
  $0

...and one for else.

# -*- mode: snippet; require-final-newline: nil -*-
# name: else
# key: else
# binding: direct-keybinding
# --
else
  $0

However, using these snippets to generate the target control flow statement yields the following:

-- a misformatted control flow statement in Lua
if foo then
   return "foo"
   elseif bar then
     return "bar"
     else 
       return "baz"
end

I want to have the statements automatically align themselves correctly, just as if I pressed Tab on each line. yas-indent-line is already set to the default value of 'auto, and of course setting it to 'fixed is not the solution. I could accomplish the reindentation by customizing yas-after-exit-snippet-hook, but that seems too heavyweight. Is there an option in yasnippets to automatically indent each line according to the mode's default indentation of the line?

3

Is there an option in yasnippets to automatically indent each line according to the mode's default indentation of the line?

This is what setting yas-indent-line to 'auto is for, except that you also want to set yas-also-auto-indent-first-line to t. I guess it defaults to nil because it causes problems in some cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.