Love2D LSP in NeoVim


I was struggling to figure out how to enable Love2D Language Server Protocol (LSP) within NeoVim, and after searching everywhere for a solution, I came across this. Big shout out to Bjerb!


Prerequisites

  brew install lua-language-server

Next, Make sure that the language server is executable from the command line. (you may have to add it to $PATH)

  lua-language-server

Now, let’s get started

If you don’t already have a plugins directory in your nvim config, add one.

cd .config/nvim/lua
mkdir plugins
  ├── .congif
      ├── nvim
          ├── lua
              ├── plugins

Now we will create a new Lua file in your plugins directory.

nvim lsp.lua

Then add the following, and make sure to save it after:

return {

  {
    "williamboman/mason.nvim",
    cmd = "Mason",
    keys = { { "<leader>cm", "<cmd>Mason<cr>", desc = "Mason" } },
    build = ":MasonUpdate",
    opts_extend = { "ensure_installed" },
    opts = {
      ensure_installed = {
        "stylua",
        "shfmt",
      },
    },
    ---@param opts MasonSettings | {ensure_installed: string[]}
    config = function(_, opts)
      require("mason").setup(opts)
      local mr = require("mason-registry") mr:on("package:install:success", function()
        vim.defer_fn(function()
          -- trigger FileType event to possibly load this newly installed LSP server
          require("lazy.core.handler.event").trigger({
            event = "FileType",
            buf = vim.api.nvim_get_current_buf(),
          })
        end, 100)
      end)

      mr.refresh(function()
        for _, tool in ipairs(opts.ensure_installed) do
          local p = mr.get_package(tool)
          if not p:is_installed() then
            p:install()
          end
        end
      end)
    end,
  },

  -- lsp servers
  {
    "neovim/nvim-lspconfig",
    opts = {
      inlay_hints = { enabled = flase },
      ---@type lspconfig.options
      servers = {
        lua_ls = {
          --enabled = false
          sinble_file_support = true,
          settings = {
            Lua = {
              workspace = {
                library = {
                  -- add Love2D support
                  "${3rd}/love2d/library",
                },
              },
            },
          },
        },
      },
    },
  },

}

Conclusion

Assuming all has been setup correctly (you may need to restart your terminal), you should be up and running with Love2D language support (as seen in the clip above).

Happy Coding!