The language server has a built-in code formatter, courtesy of CppCXY/EmmyLuaCodeStyle , that allows you to easily format your code. It also offers code style checking

Note

Please create issues related to the formatter in its repository .

Configuration

The configuration file must be called .editorconfig and uses YAML syntax. This file must be in the project root, however, you can also have multiple in different directories if you want the various directories to format differently.

A template .editorconfig can be found at CppCXY/EmmyLuaCodeStyle/lua.template.editorconfig .

For more info, refer to CppCXY/EmmyLuaCodeStyle .

Default Configuration

To set a default global configuration across projects, navigate to your configuration file and perform the below:

This format is used by Visual Studio Code's settings.json and .luarc.json files.

JSON
"Lua.format.defaultConfig": {
    "indent_style": "space",
    "indent_size": "2"
}

This format is used by custom configuration files, often used with Neovim.

Lua
Lua = {
  format = {
      enable = true,
      -- Put format options here
      -- NOTE: the value should be String!
      defaultConfig = {
        indent_style = "space",
        indent_size = "2",
      }
  },
}
Warning

Although indentation is used in the examples above, it may not actually be effective, as the editor's native indentation settings will be used instead, when available.

Code Style Checking

To enable code style checking, you need to add the following entry to diagnostics.neededFileStatus:

JSON
["codestyle-check"] = "Any"

Last Modified: