Lua Language Server

repository·master·Indexed 26 days ago

https://github.com/luals/lua-language-server

An LSP-compliant language server for Lua providing advanced development features including type checking, autocompletion, diagnostics, and code formatting. It supports Lua versions 5.1 through 5.5 and LuaJIT. Key capabilities include code intelligence (go to definition, find references), dynamic type checking, semantic highlighting, inlay hints, and extensibility via custom plugins and libraries.

Tokens
7.4K
Snippets
7
Records
61
Agent score
86%

What's inside lua-language-server

  1. Overview of Lua Language Server features

    master

    The Lua language server provides a wide range of development features for Lua, including support for multiple Lua versions and advanced IDE capabilities.

    Supported Lua Versions

    • Lua 5.5
    • Lua 5.4
    • Lua 5.3
    • Lua 5.2
    • Lua 5.1
    • LuaJIT

    Key Features

    • Code Intelligence: Go to definition, Find references, Autocompletion, and Hover to view details.
    • Analysis: Dynamic type checking, Diagnostics/Warnings, Syntax checking, and Spell checking.
    • Refactoring & Formatting: Element renaming and Code formatting.
    • Documentation: Support for over 20 annotations and Documentation Generation.
    • Extensibility: Support for libraries and Custom plugins.
  2. Configure Type checking behavior

    master

    Adjust how the type system validates assignments and unions.

    • type.castNumberToInteger: Allows assigning number to integer.
    • type.weakNilCheck: If false, a union type like number|nil cannot be assigned to number. If true, it can.
    • type.weakUnionCheck: If false, a union type like number|boolean cannot be assigned to number. If true, it can.
  3. Configure diagnostic severity via diagnostics.severity

    master

    Modify the severity level of specific diagnostics.

    To override the global group setting diagnostics.groupSeverity, append an exclamation mark ! to the value.

    Available values:

    • "Error"
    • "Warning"
    • "Information"
    • "Hint"
    • "Error!", "Warning!", "Information!", "Hint!" (overrides group setting)
  4. Configure inlay hints

    master

    Enable and customize inlay hints to provide extra context in the editor.

    • hint.enable: Enable/disable inlay hints (default: false).
    • hint.arrayIndex: Control array index hints:
      • "Enable": Show in all tables.
      • "Auto": Show for tables > 3 items or mixed tables (default).
      • "Disable": Turn off.
    • hint.await: If true, prompts await when calling a function marked ---@async (default: true).
    • hint.paramName: Show parameter names at function calls:
      • "All": Show all types (default).
      • "Literal": Show only literal type parameters.
      • "Disable": Turn off.
    • hint.paramType: Show type hints at function parameters (default: true).
    • hint.semicolon: Display virtual semicolons:
      • "All": Display for all statements.
      • "SameLine": Display between statements on the same line (default).
      • "Disable": Turn off.
    • hint.setType: Show type hints during assignment operations (default: false).
  5. Configure workspace diagnostics behavior

    master

    Control how and when workspace-wide diagnostics are triggered to balance CPU usage and feedback latency.

    • diagnostics.workspaceDelay: Latency in milliseconds before diagnostics run (default: 3000).
    • diagnostics.workspaceEvent: Trigger mechanism:
      • "OnChange": Trigger when the file changes.
      • "OnSave": Trigger when the file is saved (default).
      • "None": Disable workspace diagnostics.
    • diagnostics.workspaceRate: Run rate percentage (0-100). Lowering this reduces CPU usage (default: 100).
  6. Configure Runtime environment

    master

    Adjust how the language server simulates the Lua runtime.

    • runtime.builtin: Adjust the enabled state of built-in libraries (e.g., math, table, os). Options: "default", "enable", "disable".
    • runtime.fileEncoding: Set file encoding. Options: "utf8", "ansi" (Windows only), "utf16le", "utf16be".
    • runtime.meta: Format of the directory name for meta files.
    • runtime.nonstandardSymbol: List of supported non-standard symbols (e.g., "//", "continue", "|lambda|").
    • runtime.path: Defines how require finds files. Example: ["?.lua", "?/init.lua"].
    • runtime.pathStrict: If true, runtime.path only searches the first level of directories.
    • runtime.plugin: Path to plugins.
    • runtime.pluginArgs: Additional arguments for plugins.
    • runtime.special: Map custom global variables to special built-in behaviors (e.g., mapping include to require).
    • runtime.unicodeName: Allows Unicode characters in names.
    • runtime.version: Set the Lua runtime version. Options: "Lua 5.1", "Lua 5.2", "Lua 5.3", "Lua 5.4", "LuaJIT".
  7. Configure code formatting

    master

    Enable the built-in code formatter and provide custom configuration.

    • format.enable: Set to true to enable the formatter (default: true).
    • format.defaultConfig: An object containing default format configuration. Note that .editorconfig files in the workspace take higher priority.
  8. Configure diagnostic file status via diagnostics.neededFileStatus

    master

    Control when specific diagnostics are triggered based on the file's state. You can specify whether diagnostics should run for all files, only opened files, or be disabled entirely.

    To override the global group setting diagnostics.groupFileStatus, append an exclamation mark ! to the value.

    Available values:

    • "Any": Diagnose all files.
    • "Opened": Only diagnose opened files.
    • "None": Disable this diagnostic.
    • "Any!", "Opened!", "None!": Overrides the group setting.
    {
        "ambiguity-1": "Any",
        "assign-type-mismatch": "Opened",
        "await-in-sync": "None"
    }
  9. Configure Workspace and Libraries

    master

    Manage how the language server interacts with your project files and external libraries.

    • workspace.checkThirdParty: Automatic detection for libraries like OpenResty, LÖVE, skynet, etc. Options: "Ask", "Apply", "ApplyInMemory", "Disable".
    • workspace.ignoreDir: Files/directories to ignore (uses .gitignore grammar).
    • workspace.ignoreSubmodules: Whether to ignore submodules.
    • workspace.library: Directories to load as external code libraries. Files here are treated as read-only (e.g., renaming fields won't modify them).
    • workspace.maxPreload: Maximum number of preloaded files.
    • workspace.preloadFileSize: Skip files larger than this value (KB) when preloading.
    • workspace.useGitIgnore: Whether to use .gitignore for the ignore list.
    • workspace.userThirdParty: Paths to private third-party library configuration files.
  10. Configure diagnostic rules and severity

    master

    You can customize the behavior and severity of various Lua diagnostics (e.g., Warning, Hint, Information) by configuring the diagnostic keys in your settings. This allows you to enable or disable specific checks like unused-local, undefined-global, or spell-check.

    {
        "ambiguity-1": "Warning",
        "unused-local": "Hint",
        "spell-check": "Information",
        "unreachable-code": "Hint"
    }
  11. Configure Type Formatting

    master

    Set the behavior for automatic code formatting while typing.

    Use the typeFormat.config object to control:

    • auto_complete_end: Automatically complete end at suitable positions.
    • auto_complete_table_sep: Automatically append a separator at the end of table declarations.
    • format_line: Whether a line is formatted at all.
    "typeFormat.config" : {
        "auto_complete_end": "true",
        "auto_complete_table_sep": "true",
        "format_line": "true"
    }