## tree-sitter-luau Luau grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter)
deploying in helix editor 1. create the `languages.toml` file if it doesn't already exist ([docs](https://docs.helix-editor.com/languages.html)) 2. append two entries inside `languages.toml`: ```toml [[language]] name = "luau" scope = "source.luau" injection-regex = "^luau$" file-types = ["luau", "server.lua", "client.lua"] comment-token = "--" indent = { tab-width = 2, unit = " "} # language-server = { command = "luau-lsp", args = ["lsp", "--definitions="] } roots = [ "aftman.toml", "default.project.json", "wally.toml" ] [[grammar]] name = "luau" source = { git = "https://github.com/polychromatist/tree-sitter-luau" } ``` 3. run `.\scripts\clone_helix_queries.ps1` (or manually clone from `.\helix-queries\` into `\runtime\queries\luau`) 4. run `hx --grammar fetch` && `hx --grammar build`
### Note on the Neovim case There is now a Luau parser packaged in nvim-treesitter which will cause a conflict if naively overwritten. [See the issue](https://github.com/polychromatist/tree-sitter-luau/issues/2#issuecomment-1540723013). Due to this, the script `scripts/clone_queries.ps1` is now incorrect and has been removed. As it stands (5/13/2023), the parser packaged in nvim-treesitter is somewhat OK, but [not fully correct](https://i.imgur.com/FSqjBjK.png). This is not my parser. Here is the approach I used to overwrite it.
deploying in neovim, v2
  1. have a c/c++ compiler and nodejs
  2. install nvim-treesitter, and make sure it's configured
  3. clone or download this repository somewhere
  4. register parser in init.lua file (or equivalent) with this code fragment:
    
    local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
    
    local myhome = os.getenv "USERPROFILE" or os.getenv "HOME" -- user profile path, if needed
    
    local mypath = "CHANGE_THIS"
    
    -- Example:
    -- local mypath = myhome .. "/techstuff/hunter2/cloned_repos"
    
    parser_config.luau = {
    install_info = {
    url = mypath .. "/tree-sitter-luau",
    files = {"src/parser.c", "src/scanner.c"},
    -- generate_requires_npm = false,
    -- requires_generate_from_grammar = false
    },
    }
    
    
  5. issue Ex command :TSInstall luau
  6. in Neovim config directory (e.g. `%LOCALAPPDATA%\nvim`):
  7. overwrite queries by appending a second code segment to the nvim init.lua file:
    
    -- set queries for luau. parser_config segment should be above
    do
      for i, v in pairs{"highlights", "indents", "folds", "injections", "locals"} do
        local fd = io.open(mypath .. "/tree-sitter-luau/nvim-queries/" .. v .. ".scm")
        local txt = fd:read"*a"
        fd:close()
        vim.treesitter.query.set("luau", v, txt)
      end
    end
    
Deploying luau-lsp for high quality linting
  1. download or compile luau-lsp: https://github.com/JohnnyMorganz/luau-lsp/releases
  2. make sure you have either an `aftman.toml`, `wally.toml`, or `default.project.json` file in the project root
  3. modify this lsp config skeleton & put in init.lua file:
  4. 
    local MY_LUAU_LSP_PATH = "C:\\bin\\luau-lsp.exe"
    local MY_DIAGNOSTIC_KEY = "" -- ctrl N
    local MY_LOOKUP_KEY = "K" -- shift K
    
    -- LSP Diagnostics Options Setup 
    local sign = function(opts)
    vim.fn.sign_define(opts.name, {
      texthl = opts.name,
      text = opts.text,
      numhl = ''
    })
    end
    
    sign({name = 'DiagnosticSignError', text = ''})
    sign({name = 'DiagnosticSignWarn', text = ''})
    sign({name = 'DiagnosticSignHint', text = ''})
    sign({name = 'DiagnosticSignInfo', text = ''})
    
    vim.diagnostic.config({
      virtual_text = false,
      signs = true,
      update_in_insert = true,
      underline = true,
      severity_sort = false,
      float = {
          border = 'rounded',
          source = 'always',
          header = '',
          prefix = '',
      },
    })
    
    -- overwrite keymap on LSP enabled buffers
    vim.api.nvim_create_autocmd('LspAttach', {
    callback = function(args)
      vim.keymap.set('n', MY_LOOKUP_KEY, vim.lsp.buf.hover, { buffer = args.buf })
      vim.keymap.set('n', MY_DIAGNOSTIC_KEY, function()
        vim.diagnostic.open_float(nil, { focusable = false })
      end, {buffer = true})
    end
    })
    
    -- autocmd-event LSP Server Start Callback
    function _G.start_luau_lsp()
    vim.lsp.start({
      name = 'nvim-luau-lsp',
      cmd = {MY_LUAU_LSP_PATH, 'lsp'},
      root_dir = vim.fs.dirname(vim.fs.find({'aftman.toml', 'wally.toml', 'default.project.json'}, { upward = true })[1])
    })
    end
    
    -- enable signcolumn and register autocmd-event
    vim.cmd([[
    set signcolumn=yes
    au BufRead,BufNewFile *.luau lua _G.start_luau_lsp()
    ]])