Skip to content

Useful transforms

dcampos edited this page Nov 5, 2023 · 4 revisions

Tabstop transforms can give nice results when used jointly with Vimscript/Lua functions.

Let's say we have the following PHP class snippet, which creates a class named based on the current file's base name, the method getTableName() returning its name in snake case:

class ${1:`expand('%:t:r')`} {
  public function getTableName() {
    return '${2:${1/.*/\=ToSnakeCase(submatch(0))/g}}';
  }
}

We can define the ToSnakeCase() function like this:

function! ToSnakeCase(value) abort
    let result = tolower(substitute(a:value, '\l\zs\u', '_\L\0', 'g'))
    return result
endfunction

Lua can also be used instead of Vimscript, as long as you create a global function and use =v:lua.ToSnakeCase(...). Something like this should do the trick:

function ToSnakeCase(value)
    local result = string.lower(vim.fn.substitute(value, [[\l\zs\u]], [[_\L\0]], 'g'))
    return result
end
Clone this wiki locally