Linting and Code Navigation in the code editor

Today we’ve released a new version of the editor that can show lints in the code editor. The linting is implemented using Language Server Protocol, which hopefully will enable us to add more improvements to the code editor in the future.

To try out the linting, you first need to ensure you are running the latest version of the editor and then add this dependency in your game.project: https://github.com/defold/lua-language-server/releases/download/v0.0.5/release.zip

UPDATE #1: A newer version will allow the editor to lint the whole project when building it.

UPDATE #2: The editor now supports code navigation powered by Language Servers. Use F12 for Go to Definition and Shift F12 for Find References actions.

UPDATE #3: We now include code completions from language servers in the code completion popup.

UPDATE #4: Now Lua language server is built into the editor.

Once you add the dependency, you need to perform Project → Fetch Libraries and Project → Reload Editor Scripts actions.

Then, the editor will use our fork of sumneko’s lua-language-server that uses Luacheck to lint your edited files.

Additionally, we updated our editor scripts manual with new language servers section that describes how to add other language servers to do linting in the editor. For example, I experimented with using vscode’s JSON language server, to lint JSON files with JSON schema. I didn’t make it an extension though because it needs a nodeJS runtime to run which is like 80 Mb per platform. Another interesting language server to integrate into Defold would be a linter for our shader files, maybe based on glsl-language-server or something similar.

Try it out and tell us what you think!

43 Likes

This is great! Looking forward to trying it.

Out of curiosity, will this stay as an extension or will it be integrated with the editor directly?

4 Likes

Wow! :star_struck:

Is it for beta 1.4.2 or stable 1.4.1 release Editor?

3 Likes

Long-term: once we add support for code features like goto definition/find references, we will integrate the lua-language-server into the editor distribution.

12 Likes

Currently it’s in 1.4.1 stable, but it should be in the beta soon (I merged into beta awhile ago, it’s being built on CI).

3 Likes

Really good so far! Have tidied up my scripts quite a bit already.

7 Likes

Is there a window that shows all problems from linter?

1 Like

No, there is only highlighting in the code file

2 Likes

This is will be added in an upcoming release!

8 Likes

Gave it a try and it is working great.

I think this would be an excellent addition. For those wanting to explore and learn about shaders for the first time and experienced users as well.

3 Likes

Thank you so much! I thought that I was writing the perfect code (I’m kidding :grin: ) - the linter detected some really weird bugs.

Overall, it works great! Will it support .script_api from third-party extensions? Now the linter highlights the calls to native extensions as accessing undefined variable.

Plus, the console log syntax highlighting has been missing since the linter update - https://github.com/defold/defold/issues/7297

5 Likes

I wrote down in the linter issue that we want to pipe .script_api information to the language server, thanks for reminding me about it :slight_smile:

Regarding console syntax highlighting, as we discussed in the issue, the fix should be released soon.

I’m so glad linting is useful!!!

8 Likes

Is there a way to customize the LuaCheck options?

Create and edit the .luacheckrc file in the project root.

1 Like

I don’t understand how to make this work. I added the module as a project dependency and reloaded editor scripts but nothing changed or pops up while in the script editor.

What is your editor sha in Help → About? Are there errors in today’s log file (Help → Show Logs)?

It’s this file: Configuration file — luacheck 0.26.0 documentation

1 Like

sha: e0ad4769ad5b83c60b185b54ff889f4437b0d128

I do see an error here:

2023-01-20 20:09:19.895 25585 [async-dispatch-8] ERROR editor.lsp.server - {:line 226, :message "Language server process failed to start", :launcher {:command ["./build/plugins/lsp-lua-language-server/plugins/bin/x86_64-win32/bin/lua-language-server.exe" "--configpath=build/plugins/lsp-lua-language-server/plugins/share/config.json"]}}
java.io.IOException: Cannot run program "C:\Users\MyName\Documents\Game Development\Defold\Projects\Games\MyGame\.\build\plugins\lsp-lua-language-server\plugins\bin\x86_64-win32\bin\lua-language-server.exe" (in directory "C:\Users\MyName\Documents\Game Development\Defold\Projects\Games\MyGame"): CreateProcess error=2, The system cannot find the file specified
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
	at editor.lsp.server$fn__28420.invokeStatic(server.clj:77)
	at editor.lsp.server$fn__28420.invoke(server.clj:69)
	at editor.lsp.server$fn__28405$G__28400__28412.invoke(server.clj:66)
	at editor.lsp.server$make$fn__28803$state_machine__19501__auto____28820$fn__28824$fn__28832.invoke(server.clj:224)
	at clojure.core.async$thread_call$fn__19752.invoke(async.clj:484)
	at clojure.lang.AFn.run(AFn.java:22)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
	at java.base/java.lang.ProcessImpl.create(Native Method)
	at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:487)
	at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:154)
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
	... 10 common frames omitted
2023-01-20 20:09:19.919 25609 [clojure-agent-send-off-pool-2] INFO  editor.boot-open-project - {:line 408, :message "project loaded"}
2023-01-20 20:09:20.285 25975 [JavaFX Application Thread] INFO  editor.defold-project - {:line 890, :message "Cached save data in system cache.", :total 189, :retained 154, :unretained 35, :limit 20000}

Can you please check that path and see if it is missing the file or of it perhaps is a permission issue?

Could you try a path without a space ” ” in it?