Writing indentation plugins in Lua

From Yzis Wiki

Jump to: navigation, search

Writing Lua indentation plugins

Writing indentation plugins in Lua is quite easy. This files shows how to do it.

Everywhere in the code of this file you need to replace FILETYPE with the actual file type, for example "lua", "cpp", "java" ...)


Indent on ENTER

  -- this function will be called everytime <ENTER> is pressed
  -- nbNextTabs : number of initial tabs of the next line
  -- nbNextSpaces : number of initial spaces of the next line
  -- nbPrevTabs : number of initial tabs of the previous line (the one where <ENTER> was pressed)
  -- nbPrevSpaces : number of initial spaces of the previous line
  -- prevLine : the previous line itself
  -- nextLine : the next line itself</p>
  function Indent_FILETYPE (nbNextTabs,nbNextSpaces,nbPrevTabs,nbPrevSpaces,prevLine,nextLine) 
  {
     -- calculate the indentation you want
     -- even more calucation
     return a_string_to_be_prepended_on_the_new_line


Indent on other keys

   -- this function will be called whenever a key which was put in the "indentkeys" option was pressed
   -- nbCurTabs : number of initial tabs of the current line
   -- nbCurSpaces : number of initial spaces of the current line
   -- nbNextTabs : number of initial tabs of the next line
   -- nbNextSpaces : number of initial spaces of the next line
   -- nbPrevTabs : number of initial tabs of the previous line (the one where <ENTER> was pressed)
   -- nbPrevSpaces : number of initial spaces of the previous line
   -- curLine : the current line itself
   -- prevLine : the previous line itself
   -- nextLine : the next line itself
   -- char : the character which trigger the call to this function (so this character is in the indentkeys option ;)
   function Indent_OnKey_FILETYPE (char, nbPrevTabs, nbPrevSpaces, nbCurTabs, nbCurSpaces, nbNextTabs, nbNextSpaces, curLine, prevLine, nextLine) 
   {
       -- do whatever you want on the buffer text
       -- and don't return anything

Informing Yzis about indentation functions
Then, all you need to do is to connect your Lua functions defined here to Yzis events :

  -- connect the required events to call these new functions
  connect("INDENT_ON_ENTER", Indent_FILETYPE)
  connect("INDENT_ON_KEY", Indent_OnKey_FILETYPE)
  setlocal("indentkeys=}")