Thank you for taking an interest in helping improve the language server!
Debugging can be performed in a few ways. You can do a quick print()
, write to the log file, or attach a debugger to get all the info you need.
You can quickly print()
to the OUTPUT
panel (Ctrl + Shift + U) in Visual Studio Code.
Below is an example of how a plugin can be debugged.
local util = require 'utility'
local client = require 'client'
function OnSetText(uri, text)
print(uri, #text, util.dump(client.info.clientInfo))
end
You can add an entry to the log file. Below is an example of how a plugin can be debugged.
local util = require 'utility'
local client = require 'client'
function OnSetText(uri, text)
log.debug(uri, #text, util.dump(client.info.clientInfo))
end
This is the most advanced method, but provides all kinds of useful info and is the most "proper" way to debug the language server.
You will need two Visual Studio Code instances open:
- The Debug Host
- This instance has the language server open which can be found in one of these locations:
%USERPROFILE%\.vscode\extensions\sumneko.lua-VERSION\server
~/.vscode/extensions/sumneko.lua-VERSION/server
~/.vscode/extensions/sumneko.lua-VERSION/server
- This instance has the language server open which can be found in one of these locations:
- The Debug Target
- This instance is where you will test the language server. It should have a folder opened where you can write Lua to test the various features of the language server and use it as normal.
The below steps guide you through setting up the above:
- Install
actboy168.lua-debug
in Visual Studio Code. - Copy
.vscode/launch.json
into Debug Host at.vscode/launch.json
- Copy the below settings into your
settings.json
for the Debug TargetJSON"Lua.misc.parameters": [ "--develop=true", "--dbgport=11428" ],
- In the Debug Host, open the
Run and Debug
sidepanel (Ctrl + Shift + D) and select🍄 attach
from the dropdown - Press F5 to begin debugging
- Restart the Debug Target (F1 ->
Reload Window
)
If you got the server through Git you will need to change the debug port in
settings.json
to "--dbgport=XXXXX"
and address to "address": "127.0.0.1:XXXXX"
in launch.json
.
The server has supported multi-root workspaces since v2.6.0
.
Before v3.5.1
, the server did not support dynamically adding/removing
workspaces.
The server creates a <fallback>
scope by default. If you start the server while in "single file mode", this <fallback>
scope is what is being used.
Should the server be started in "workspace mode", each workspace will be given its own scope.
Linking to other files/directories outside the scope of your workspace(s) is also possible. You can specify additional files to include using workspace.library
.
When a Lua file is opened/created, the server will check all workspace scopes. If the file belongs to the working directory or a linked directory of the scope, it will be assigned to this scope. If all workspace scopes are not suitable, the file will be assigned to the <fallback>
scope.
Every scope has an independent environment for separating global variables, classes, settings, requires, etc.
lua-language-server/
.github/
Github-specific files
.vscode/
VS Code-specific files for development
3rd/
Git submodule dependencies
bin/
Build binaries
doc/
Documentation
locale/
Translations
log/
Default log location
make/
Files for building
meta/
Lua definition files
3rd/
Built-in addons for popular libraries. To be removed .
template/
Templates for the built-in Lua libraries that will be generated according to the requested Lua version, language ID, and file encoding
Lua ${LUA_VERSION} ${LANGUAGE_ID} ${ENCODING}/
The generated definition files for built-in libraries. Generated from the templates in the template/
directory. There will be a directory for each variation.
script/
brave/
Sub-thread workers that read protocol from standard input, read file content, and regularly wake up the main thread.
cli/
Provide CLI support for arguments like --check
config/
Configuration file handling
core/
Provides core language features. Files are named the same as the feature they implement.
encoder/
Convert encodings between ANSI, UTF8, and UTF16
glob/
Resolves glob patterns
parser/
Parses Lua code into an abstract syntax tree
guide.lua
Provide utility functions, for example getVisibleLocals(source, position)
, getParentFunction(source)
and positionToOffset(state, position)
luadoc.lua
Parses LuaCATS annotations from comments
newparser.lua
Parses Lua code into an AST then wraps it into state
.
local state = {
version = 'Lua 5.4',
lua = [[local x = 1]],
ast = { ... },
errs = { ... }, -- syntax errors
comms = { ... }, -- comments
lines = { ... }, -- map of offset and position
}
tokens.lua
Parses strings into tokens. Uses sqmedeiros/lpeglabel .
proto/
Code for Language Server Protocol (LSP)
converter.lua
Convert AST values into something the LSP can use. For example; 50003 -> { line = 5, character = 3 }
define.lua
Definitions of constants
proto.lua
Communication with the client
provider/
Bridges LSP requests with core features
diagnostic.lua
Manages the diagnostic push service
provider.lua
Registers the server's capabilities with the client so that it knows what is supported
pub/
Host for subthreads
service/
Server runtime and event loop
test/
Unit tests
tools/
Various tools for development
~/
Semantic analysis of the AST and binding status according to the workspace.
Turns:
---@class myClass
local mt
into:
vm.compileNode('mt')
-->
node: {
[1] = {
type = 'local',
[1] = 'mt',
},
[2] = {
type = 'global',
cate = 'type',
name = 'myClass',
},
}
compiler.lua
Provides vm.compileNode(source) --> node
doc.lua
Provides annotation features
field.lua
Provides vm.getFields(source) --> source[]
generic.lua
Resolve generics by proto, sign, and call args
global.lua
Manages global variables and types
infer.lua
Provides infer class for inferring types of sources
local-id.lua
Manages local variables
node.lua
Provides node class
ref.lua
Provides vm.getRefs(source) --> source[]
runner.lua
Provides vm.compileNode(source) --> node. Process analysis and tracking of local variables
---@type number|nil
local x
if x then
print(x) --> `x` is number here
end
sign.lua
Create generic instance
workspace/
Manages workspaces
loading.lua
Workspace loading process
require-path.lua
Computer require filename
scope.lua
Provides scope class. Adds support for multiple workspaces
workspace.lua
Provides workspace features
await.lua
Simple coroutine library
client.lua
Contains wrapped request from server to client. Modifies configuration file
files.lua
Manages files
language.lua
Provide support for multiple languages
lclient.lua
Fake client for CLI and tests
library.lua
library and definition file features
plugin.lua
Adds support for plugins
debugger.lua
Used when attaching debugger
test.lua
Entry file for testing
main.lua
Entry file
Here you can find the names of the various tokens in Visual Studio Code to highlight and colour the various semantic items of Lua.
These tokens are being previewed in Dark+
of Visual Studio Code as it has great support for the various tokens used by the extension.
token | preview |
---|---|
keyword.local.lua | |
keyword.control.lua | |
entity.name.class.lua | |
entity.name.function.lua | |
punctuation.definition.parameters.begin.lua | |
punctuation.definition.parameters.finish.lua | |
variable.parameter.function.lua | |
punctuation.separator.arguments.lua | |
constant.numeric.integer.lua | |
constant.numeric.float.lua | |
constant.numeric.integer.hexadecimal.lua | |
constant.numeric.float.hexadecimal.lua | |
punctuation.definition.string.begin.lua | |
punctuation.definition.string.end.lua | |
string.quoted.single.lua | |
string.quoted.double.lua | |
string.quoted.other.multiline.lua | |
constant.character.escape.lua | |
constant.character.escape.byte.lua | |
constant.character.escape.unicode.lua | |
invalid.illegal.character.escape.lua | |
punctuation.definition.comment.lua | |
comment.line.double-dash.lua | |
punctuation.definition.comment.begin.lua | |
punctuation.definition.comment.end.lua | |
comment.block.lua | |
keyword.control.goto.lua | |
string.tag.lua | |
punctuation.section.embedded.begin.lua | |
punctuation.section.embedded.end.lua | |
variable.language.self.lua | |
support.function.lua | |
support.function.library.lua | |
keyword.operator.lua | |
variable.other.lua |
semantic token | fallen syntax token | preview |
---|---|---|
namespace.static | support.function.lua | |
namespace.readonly | constant.language.lua | |
namespace.deprecated | entity.name.label | |
parameter.declaration | variable.parameter | |
property.declaration | entity.other.attribute | |
variable | variable.other.lua | |
interface.declaration | entity.name.function.lua |