This How-To tutorial explains how to add support for a programming language to Apache NetBeans using the LSP protocol.
What is the Language Server Protocol (LSP)?
How to add support in NetBeans to a programming language using LSP
You will need Apache NetBeans source code. Please download or clone it from here and build it.
1. File Type Recognition
See Adding New Language Support.
2. Custom project types
See Adding New Language Support.
3. Semantic Syntax highlighting and braces matching
To add syntax highlighting to your source file, follow the instructions provided in LSP Client demo - (ba)sh language server tutorial.
- Download the TextMate json file for your language. A list of Textmate syntaxes is provided here and here but in other places, too. Let's call it
my-grammar.json
for the purposes of this example. E.g. for Kotlin this should beKotlin.tmLanguage.json
and for Rustrust.tmLanguage.json
. Right-click on Libraries folder of your module project and select Add Module Dependency. Enter "TextMate Lexer" inside Filter and add the module found in the Module list as a dependency to your module.
In the DataObject that you created in the previous chapter, add this annotation:
@GrammarRegistration(grammar="my-grammar.json", mimeType="text/sh")
and import it:
import org.netbeans.modules.textmate.lexer.api.GrammarRegistration;
If you open your source file in NetBeans, as described in the previous chapter, you should be able to see the various parts of the language (e.g. keywords) to be coloured as defined in the TextMate grammar file. Braces matching is also there. Take a look at java/kotlin.editor/src/org/netbeans/modules/kotlin/editor/KtDataObject.java
as an example.
4. 5. Code Completion and Navigation
To add support for language-specific features like code completion, navigation, etc. you need the help of a Language Protocol Server. Again, follow the instructions provided in LSP Client demo - (ba)sh language server tutorial.
- Download the LSP implementation for the language you wish to add support for. See a list of Language Server implementations and another list of Language Server Implementations.For example, to add support for Kotlin programming language, download and build kotlin-language-server.
- Add dependencies to "LSP Client" and "MIME Lookup API" modules to your module, as described in the previous chapter:
- Create a new class that implements
LanguageServerProvider
.
See for example, ShellClient
or java/kotlin.editor/src/org/netbeans/modules/kotlin/editor/
lsp/KotlinLSPServer
. E.g. for Kotlin, you will need to provide the path to kotlin-language-server/server/build/install/server/bin/kotlin-language-server
inside the overriden startServer()
method of this class:
public class KotlinLSPServer implements LanguageServerProvider {
private static final Logger LOG = Logger.getLogger(KotlinLSPServer.class.getName());
@Override
public LanguageServerDescription startServer(Lookup lookup) {
File server = new File("kotlin-language-server/server/build/install/server/bin/kotlin-language-server");
try {
Process p = new ProcessBuilder(new String[] {server.getAbsolutePath()})
.directory(server.getParentFile().getParentFile())
.redirectError(ProcessBuilder.Redirect.INHERIT).start();
return LanguageServerDescription.create(p.getInputStream(), p.getOutputStream(), p);
} catch (IOException ex) {
LOG.log(Level.FINE, null, ex);
return null;
}
}
}
Important note! It is yet to be decided where to install and how to access the (pre-)built language servers.
Open the source file again and try code completion, navigation (e.g. jump to definition, peek definition), find all references, symbol search etc.
6. Types and documentation on hover
7. Code formatting
8. Configuration
See Adding New Language Support.
9. Refactoring
10. Error squiggles and apply suggestions from errors
11. Debugging
12. Snippets
13. Build tasks
NetBeans Specific Resources
Rich Client Programming: Plugging into the NetBeans Platform https://www.amazon.com/Rich-Client-Programming-Plugging-NetBeans/dp/0132354802
Apache NetBeans Platform for Beginners https://leanpub.com/nbp4beginners
- Lahoda J. (2019), "LSP Client demo - (ba)sh language server", ASF.
- Cardona J.R. (2018), "Quick Start: Creating Language Tools In NetBeans IDE", DZone.
- NetBeans Platform Learning Trail https://netbeans.apache.org/kb/docs/platform/index.html
- Kostaras I. et al. (2020), Pro Apache NetBeans, APress, Chapter 11, "Writing a Plugin for NetBeans".
Other resources
- Nadeeshaan G. & Nipuna M. (2022), Language Server Protocol and Implementation: Supporting Language-Smart Editing and Programming Tools, APress.
- Stalla A. (2021), "Go to Definition in the Language Server Protocol", Strumenta.
- Watt D.A. & Brown D. F. (2000), Programming Language Processors in Java, Prentice Hall.