You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

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

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.

  1. 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 be Kotlin.tmLanguage.json and for Rust rust.tmLanguage.json.
  2. 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.

  3. 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.

  1. 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.
  2. Add dependencies to "LSP Client" and "MIME Lookup API" modules to your module, as described in the previous chapter:
  3. 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

Other resources

  1. Nadeeshaan G. & Nipuna M. (2022), Language Server Protocol and Implementation: Supporting Language-Smart Editing and Programming Tools, APress.
  2. Stalla A. (2021), "Go to Definition in the Language Server Protocol", Strumenta.
  3. Watt D.A. & Brown D. F. (2000), Programming Language Processors in Java, Prentice Hall.
  • No labels