Code Monkey home page Code Monkey logo

lua-lsp's Introduction

lua-lsp

Build Status

A Language Server for Lua code, written in Lua.

It's still a work in progress, but it's usable for day-to-day. It currently supports:

  • Limited autocompletion
  • Goto definition
  • As you type linting and syntax checking
  • Code formatting
  • Supports Lua 5.1-5.3 and Luajit

Installation/Usage

lua-lsp can be installed using luarocks:

$ luarocks install --server=http://luarocks.org/dev lua-lsp

This will install the lua-lsp command. Language clients can then communicate with this process using stdio as a transport. See editors.md for more instructions specific to your editor of choice.

Plugins

lua-lsp automatically integrates with common lua packages, when they are installed. For linting, install luacheck:

$ luarocks install luacheck

For code formatting, we currently support Formatter and LCF. Formatter is 5.1 only, whereas lcf is 5.3 only. 5.1:

$ luarocks-5.1 install Formatter
$ luarocks-5.3 install lcf

If you have another package you'd like to see integrated, feel free to leave an issue/PR. Other plugins are always welcome, especially if they provide materially different results.

if lua-cjson is installed, we will optionally use it for a speed boost. It is unfortunately not supported by all versions of lua at the moment, but as soon as it is we will switch to making it mandatory.

$ luarocks install lua-cjson

Configuration

lua-lsp reads a few project-level configuration files to do its work.

To configure linting, we read your standard .luacheckrc file.

For autocomplete support, we reimplement the .luacompleterc format created by atom-autocomplete-lua. In particular, we need luaVersion to properly understand your code.

More LSP-specific configuration flags will hopefully be provided through your editor's configuration support.

TODO

The LSP spec is big, and we don't implement all of it. here's a quick wishlist ordered by roughly by priority/feasibility.

  • List references (textDocument/references)
  • Find symbols (workspace/symbol)
  • Function signature help (textDocument/signatureHelp)
  • Code links (textDocument/documentLink)
  • File events (workspace/didChangeWatchedFiles)

lua-lsp's People

Contributors

alloyed avatar doronbehar avatar kattrali avatar tzzh avatar vadi2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-lsp's Issues

multiple definition support

I just noticed the type sig of goto definition allows for multiple definition returns

Use cases, if we have a method name without a type we should return a list of all methods with that name

If we has a definition/declaration split or multiple potential definitions we should also return all of those

Return type inference

we can complete simple expressions, like

module.a.b:c()

but only when we know the type of each of the ids. We should be equally informative in these kinds of cases:

local a = { str = return_a_string() }
a.str:m---

or the inline equivalent, like for example

("string"):m
return_a_table().a
assert(module.table).a

These are all built around guessing what the type of a Call or Invoke expression will be and then storing it.

install command in documentation doesn't work

I tried installing following the instructions:
$ luarocks install --from=http://luarocks.org/dev lua-lsp

but i received an error:
Error: No results matching query were found.

it only found the download with:
$ luarocks install --from=http://luarocks.org/dev lua-lsp

enhance cursor knowledge

completion/goto definition works pretty okay when you're pointing it at a plain expression/lhs. it doesn't understand the following examples, though:

-- in comments
"in strings"
{ at_table_lhs = {}}

these suffer from the "changing scope" problem that regular scopes do.

Add lua-fmt as a formatter

lua-fmt is, at the moment, the most powerful and consistent formatter for Lua.

It works with multiple Lua versions, is well tested and is the only one so far that handles things like lines that are too long due to function calls - breaking arguments into multiple lines.

It has places that could be improved but the creator and maintainer, Trinx, is really open to contributions and feedback.

Assertion failed "in upvalue 'make_position'"

Not very familiar with Lua, so I don't know how to find much more info than this. It seems to start happening if I start using a variable without declaring it.

LSP :: Error from the Language Server: /usr/local/share/lua/5.3/lua-lsp/methods.lua:308: assertion failed!
stack traceback:
	[C]: in function 'assert'
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:308: in upvalue 'make_position'
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:324: in upvalue 'make_range'
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:334: in upvalue 'make_location'
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:711: in function 'lua-lsp.methods.textDocument/documentSymbol'
	/usr/local/share/lua/5.3/lua-lsp/loop.lua:57: in function </usr/local/share/lua/5.3/lua-lsp/loop.lua:56>
	[C]: in function 'xpcall'
	/usr/local/share/lua/5.3/lua-lsp/loop.lua:56: in function 'lua-lsp.loop'
	/usr/local/lib/luarocks/rocks/lua-lsp/scm-5/bin/lua-lsp:3: in main chunk
	[C]: in ? (Internal Error)

Interestingly, according to the line number, the assertion that's failing is assert(pos <= document.text:len()).

However, this error goes away if I restart the server, even without changing any code.

Let me know if (and how?) to get any extra info!

Error installing.

I am having this issue on Ubuntu 16.04.

When I run this:
./luarocks install --server=http://luarocks.org/dev lua-lsp
I get:
Error: Failed finding Lua header files. You may need to install them or configure LUA_INCDIR.

At that point of time, Lua has already been installed, and LUA_INCDIR env var has been set to Lua's src directory which contains the headers.

Any idea?

assertion failure, possibly on '...' functions?

experimenting with lua-lsp, got weird assertion failures when mousing over the name of a function with ... arguments:

/usr/local/share/lua/5.1/lua-lsp/methods.lua:175: assertion failed! stack traceback: [C]: in function 'assert' /usr/local/share/lua/5.1/lua-lsp/methods.lua:175: in function 'make_items' /usr/local/share/lua/5.1/lua-lsp/methods.lua:650: in function '?' /usr/local/share/lua/5.1/lua-lsp/loop.lua:55: in function </usr/local/share/lua/5.1/lua-lsp/loop.lua:54> [C]: in function 'xpcall' /usr/local/share/lua/5.1/lua-lsp/loop.lua:54: in function 'main' ...r/local/lib/luarocks/rocks/lua-lsp/scm-2/bin/lua-lsp:3: in main chunk [C]: ?

Haven't got a fix, might try to make one if you're interested and I get free time.

Infer self args

local module = { my_constant_field = "woah" }

function module:method()
    return self.my_
end

should autocomplete as imagined. This is not strictly true that self will always be module, but the intent is to make the common case helpful instead of correctly represent the language semantics. Hopefully we can also support simple setmetatable usage, too.

external typing

Native modules can't be easily parsed the way lua can: for one we might not even have the source. So it makes sense to provide a .luacompleterc-inspired format that can either be hand-made or generated, and then can be loaded when a user tries to use this kind of a module. imagined filesystem structure

: share/lua/5.1/dir/my_module.lua
: share/lua/5.1/dir/my_module.luacmp.json
: share/lua/5.1/native_module.luacmp.json
: lib/lua/5.1/native_module.so

accept more recent lpeglabel

hi,

Thanks for the nice package. Would be nice to accept more recent lpeglabel to prevent this:

-- Performing Test HAS_DIAG_COLOR_FLAG - Success
-- Found PkgConfig: /nix/store/50vxsm7w48l5mqa2f114c43vcyyixxn0-pkg-config-0.29.2/bin/pkg-config (found version "0.29.2")
Missing dependencies for lua-lsp scm-2:
   lpeglabel ~> 1.5 (1.6.0-1 installed)

lua-lsp scm-2 depends on lpeglabel ~> 1.5 (1.6.0-1 installed)

Also I package this program for nixos and it would be cool to have:

  • a rock available as well on luarocks.org
  • fill the rockspec description instead of "No summary";

project-level globals

if we find a global by parsing a file, we should probably assume that every other file has access to that global. this means the global table exists on a workspace level, not a document level

Error running with Emacs lsp-mode on M1 macOS

Here is the error output:

LSP :: Connected to [lsp-lua-lsp:83564].
LSP :: Error from the Language Server: .../stardiviner/.luarocks/share/lua/5.3/lua-lsp/methods.lua:703: attempt to get length of a nil value (field 'scopes')
stack traceback:
	.../stardiviner/.luarocks/share/lua/5.3/lua-lsp/methods.lua:703: in function 'lua-lsp.methods.textDocument/documentSymbol'
	/Users/stardiviner/.luarocks/share/lua/5.3/lua-lsp/loop.lua:57: in function </Users/stardiviner/.luarocks/share/lua/5.3/lua-lsp/loop.lua:56>
	[C]: in function 'xpcall'
	/Users/stardiviner/.luarocks/share/lua/5.3/lua-lsp/loop.lua:56: in function 'lua-lsp.loop'
	...uarocks/lib/luarocks/rocks-5.3/lua-lsp/scm-5/bin/lua-lsp:3: in main chunk
	[C]: in ? (Internal Error)
LSP :: Error from the Language Server: .../stardiviner/.luarocks/share/lua/5.3/lua-lsp/methods.lua:703: attempt to get length of a nil value (field 'scopes')

I use Homebrew installed Lua 5.3

  • macOS version: Big Sur
  • Lua version: 5.3, 5.4 (installed with Homebrew)

Goto definition is not working on field methods

I'm using neoclide/coc.nvim as language client support in my vim editor. However, when I move the cursor to self:foo() inside the module:bar() function, and try gd key, which is mapped to <Plug>(coc-definition), it will output the error about finding no definition.

local module = {}

function module:foo()
end

function module:bar()
    self:foo()
end

corner cases in function sig generation

At least, when looking at the builtins, there are many cases where returnTypes aren't provided, even when the function returns a value. This means that the void return type should be an empty returnTypes array, not the absence of one

Variants are now supported: implemented as multiple signatures for one item

Also, there does not seem to be a builtin example of an "error" return type, aka return nil, my_err. I think we will represent those like so function a() ->! bool, extra_ret, but it's unfortunate that it won't be available in the standard lib.

fill missing using package manager luarocks

file lua-lsp/json.lua actually not installed via command luarocks install --server=http://luarocks.org/dev lua-lsp ;

2020-10-21

$tree
tree
.
├── analyze.lua
├── data
│   ├── 5_1.lua
│   ├── 5_2.lua
│   ├── 5_3.lua
│   ├── love-completions.lua
│   ├── luajit-2_0.lua
│   └── _test.lua
├── formatting.lua
├── log.lua
├── loop.lua
├── lua-parser
│   ├── parser.lua
│   ├── scope.lua
│   └── validator.lua
├── methods.lua
├── rpc.lua
└── unicode.lua

2 directories, 16 files

autocomplete for /home/user/.luarocks

I edited $HOME/.luacompleterc

{
    "luaVersion": "5.1",
    "packagePath": "./?.lua;/home/user/.luarocks/share/lua/5.1/?.lua"
}

I did luarocks install --local lua-cjson
I added $HOME/.luarocks into package.path and package.cpath.

When I use vim I can autocomplete standard Lua functions(table.insert, io.open,etc), however I can not autocomplete anything from $HOME/.luarocks/share/lua/5.1, vim displays:

User defined completion(^U^N^P) Pattern not found.

So will lua-lsp be able to index all files under $HOME/.luarocks/share/lua/5.1 somehow? what about the C modules under package.cpath? I want to do auto-complete for all imported modules and my own lua files in addition to the working built-in Lua functions(e.g. os.open etc)

[Question] How to resolve specific require paths?

I'm using a library that use a specific require path like love2d, and it raise error when trying to jump to definitions from another file. How to configure to work with this?

I'm using neovim and the LanguageClient-neovim plugin, and setting g:LanguageClient_rootMarkers = ['main.lua',] doesn't work.

Can't install lua-lsp

If I'm executing this command:

luarocks --local install --server=http://luarocks.org/dev lua-lsp

than I'm getting this error message:

Error: No results matching query were found for Lua 5.4.
To check if it is available for other Lua versions, use --check-lua-versions.

and if I try this command:

luarocks --check-lua-versions

than this pops up:

Usage: luarocks [-h] [--version] [--dev] [--server <server>]
       [--only-server <server>] [--only-sources <url>]
       [--namespace <namespace>] [--lua-dir <prefix>]
       [--lua-version <ver>] [--tree <tree>] [--local] [--global]
       [--verbose] [--timeout <seconds>] [--pin] [<command>] ...

Error: unknown option '--check-lua-versions'

What am I doing wrong? I've installed lua from the official arch linux repositories and luarocks from the official arch linux repositories as well.

Starting lua-lsp fails

I installed lua-lsp with luarocks install --server=http://luarocks.org/dev lua-lsp. Starting lua-lsp in the command line results in the following error:

/usr/local/bin/lua5.3: /usr/local/share/lua/5.3/lua-lsp/rpc.lua:2: module 'dkjson' not found:Failed loading module dkjson in LuaRocks rock dkjson 2.5-2
	no field package.preload['dkjson']
	no file '/Users/dschuessler/.luarocks/share/lua/5.3/dkjson.lua'
	no file '/Users/dschuessler/.luarocks/share/lua/5.3/dkjson/init.lua'
	no file '/usr/local/share/lua/5.3/dkjson.lua'
	no file '/usr/local/share/lua/5.3/dkjson/init.lua'
	no file '/usr/local/lib/lua/5.3/dkjson.lua'
	no file '/usr/local/lib/lua/5.3/dkjson/init.lua'
	no file './dkjson.lua'
	no file './dkjson/init.lua'
	no file '/Users/dschuessler/.luarocks/lib/lua/5.3/dkjson.so'
	no file '/usr/local/lib/lua/5.3/dkjson.so'
	no file '/usr/local/lib/lua/5.3/loadall.so'
	no file './dkjson.so'
stack traceback:
	[C]: in function 'require'
	/usr/local/share/lua/5.3/lua-lsp/rpc.lua:2: in main chunk
	[C]: in function 'require'
	/usr/local/share/lua/5.3/lua-lsp/loop.lua:1: in main chunk
	[C]: in function 'require'
	/usr/local/lib/luarocks/rocks-5.3/lua-lsp/scm-2/bin/lua-lsp:2: in main chunk
	[C]: in ?

Thank you for having a look. Let me know if there is any further troubleshooting I can help with.

macOS 10.13.4
Lua 5.3

Update LuaRocks Package

The package over on luarocks.org is 2 years old, and there have definitely been commits since then. The version there is still missing the rockspec update to include the missing formatting module, so formatting is broken, which is how I noticed.

Sooooo publish master to luarocks.org, please?

How would I configure this with nvim built-in LSP client?

Any idea how I'd integrate this with the neovim built-in LSP?

I'm using lspconfig, but they only have a config for the sumneko Lua lsp, which IMHO is far too complex to set up just to get an LSP (you have to install several dependencies and build from source).

I'd much prefer to use this one, but I'm not sure how to configure it with the neovim built-in LSP?

I Meet A ERROR When Start LU-LSP.BAT

ensions/coc-lua-data/tools/share/lua/5.3/lua-lsp\rpc.lua:111: assertion failed!
stack traceback:
[C]: in function 'assert'
...ensions/coc-lua-data/tools/share/lua/5.3/lua-lsp\rpc.lua:111: in function 'lua-lsp.rpc.decode'
...nsions/coc-lua-data/tools/share/lua/5.3/lua-lsp\loop.lua:35: in function 'lua-lsp.loop'
...a\tools\lib\luarocks\rocks-5.3\lua-lsp\scm-2\bin\lua-lsp:3: in main chunk
[C]: in ?
How To Solve It?

Use ssh:// protocol to install - http "The unauthenticated git protocol on port 9418 is no longer supported."

Installation works by changing the protocol to SSH

$ sudo luarocks install --server=ssh://luarocks.org/dev lua-lsp   # works

Installation fails with HTTP URL which is currently suggested in lua-lsp README

$ sudo luarocks install --server=http://luarocks.org/dev lua-lsp   # fails
...
fatal: remote error:
  The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.

This change will be made permanent 2022-04-16. We're currently on brownout period, so the old command will work intermittently.

I'm not familiar with luarocks, simply providing the command that worked for me on Debian 11-Bullseye. I noticed the error while writing installation instructions for Micro editor's new LSP Plugin. It seems that Lua-lsp works to some extent with this Micro's LSP plugin, supporting linting (in the left side gutter) and alt-D jump to definition inside the same file.

ERROR with lua-lsp, lua version is 5.3

Error from the Language Server: /usr/local/share/lua/5.3/lua-lsp/methods.lua:517: attempt to index a nil value (local ’val’)
stack traceback:
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:517: in upvalue ’definition_of’
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:662: in function ’lua-lsp.methods.textDocument/hover’
	/usr/local/share/lua/5.3/lua-lsp/loop.lua:57: in function </usr/local/share/lua/5.3/lua-lsp/loop.lua:56>
	[C]: in function ’xpcall’
	/usr/local/share/lua/5.3/lua-lsp/loop.lua:56: in function ’lua-lsp.loop’
	/usr/local/lib/luarocks/rocks-5.3/lua-lsp/scm-2/bin/lua-lsp:3: in main chunk
	[C]: in ? (Internal Error)
Error from the Language Server: /usr/local/share/lua/5.3/lua-lsp/methods.lua:517: attempt to index a nil value (local ’val’)
stack traceback:
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:517: in upvalue ’definition_of’
	/usr/local/share/lua/5.3/lua-lsp/methods.lua:662: in function ’lua-lsp.methods.textDocument/hover’
	/usr/local/share/lua/5.3/lua-lsp/loop.lua:57: in function </usr/local/share/lua/5.3/lua-lsp/loop.lua:56>
	[C]: in function ’xpcall’
	/usr/local/share/lua/5.3/lua-lsp/loop.lua:56: in function ’lua-lsp.loop’
	/usr/local/lib/luarocks/rocks-5.3/lua-lsp/scm-2/bin/lua-lsp:3: in main chunk
	[C]: in ? (Internal Error)
Mark set

Error: write EOF

Getting

Error: write EOF
    at exports._errnoException (util.js:1050:11)
    at WriteWrap.afterWrite (net.js:813:14)
[Info  - 18:34:27] Connection to server got closed. Server will restart.
[Error - 18:34:36] Request textDocument/documentSymbol failed.
  Message: Request textDocument/documentSymbol failed with message: Cannot read property 'symbols' of undefined
  Code: -32603 
[Error - 18:34:37] Request textDocument/documentSymbol failed.
  Message: Request textDocument/documentSymbol failed with message: Cannot read property 'symbols' of undefined
  Code: -32603 

when playing around with super long files. https://github.com/WeakAuras/WeakAuras2/blob/master/.luacheckrc

Can't install

system: wsl ubuntu18.04

root@wj:~# luarocks install --server=http://luarocks.org/dev lua-lsp
Installing http://luarocks.org/dev/lua-lsp-scm-2.rockspec
Missing dependencies for lua-lsp scm-2:
   lpeglabel ~> 1.5 (not installed)
   inspect ~> 3.1 (not installed)

lua-lsp scm-2 depends on lpeglabel ~> 1.5 (not installed)
Installing https://luarocks.org/lpeglabel-1.5.0-1.src.rock
gcc -O2 -fPIC -I/usr/include/lua5.2 -c lpcap.c -o lpcap.o
lpcap.c:6:10: fatal error: lua.h: No such file or directory
 #include "lua.h"
          ^~~~~~~
compilation terminated.

Error: Failed installing dependency: https://luarocks.org/lpeglabel-1.5.0-1.src.rock - Build error: Failed compiling object lpcap.o

installation failure

$ luarocks install --local https://raw.githubusercontent.com/Alloyed/lua-lsp/master/lua-lsp-scm-1.rockspec
Cloning into 'lua-lsp'...
remote: Counting objects: 40, done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 40 (delta 6), reused 28 (delta 6), pack-reused 0
Receiving objects: 100% (40/40), 202.16 KiB | 433.00 KiB/s, done.
Resolving deltas: 100% (6/6), done.
cp: cannot stat 'lua-lsp.lua': No such file or directory

Error: Build error: Failed installing lua-lsp.lua in /home/user/.luarocks/lib/luarocks/rocks-5.3/lua-lsp/scm-1/lua/lua-lsp.lua: Failed copying lua-lsp.lua to /home/user/.luarocks/lib/luarocks/rocks-5.3/lua-lsp/scm-1/lua/lua-lsp.lua

I guess this is easy to fix, but i'm not proficient enough with luarocks to do it myself.

Update dkjson dependency

I am trying to bring some updates to nixpkgs (NixOS/nixpkgs#172096) and one of the problems is that lua-lsp requires dkjson ~> 2.5 but I would like to update dkjson to 2.6.

Is it possible for you to update the dependency? (As a small motivation: 2.5 was released 2014 and 2.6 was released in 2021).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.