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

Compare with Current View Page History

« Previous Version 3 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

You will need Apache NetBeans source code. Please download or clone it from here and build it.

1. File Type Recognition

The first thing to do is for NetBeans to be able to recognize the file type. E.g. if you are adding support for the kotlin programming language you would like NetBeans editor to be able to recognize .kt source files.

Use the New File → Module Development → File Type wizard. A MIME type must be specified. This MIME type will be the key under which other services will be looked up.

See the File Type Integration Tutorial for more details on how to add File Type recognition support. A DataObject file will be created with a number of annotations.

Important Note! Make sure to create your module inside your cloned Apache NetBeans source code. Your module will need some files from nbbuild folder.

Take a look at java/kotlin.editor/src/org/netbeans/modules/kotlin/editor/KtDataObject.java and rust/rust.sources/src/org/netbeans/modules/rust/sources/rs/RustFileDataObject.java as examples of DataObjects.

Hint! To open a source file easily, click on Window → Favorites and navigate to the source file (e.g. a .kt file if you are adding support for kotlin, or a .rs source file for Rust)

2. Syntax highlighting

Without syntax highlighting, NetBeans opens the source file as a text file. 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. Into 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 now open your source file in NetBeans again, as described in the previous chapter, you should be able to see the various parts of the languages 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.

3. Navigation and Code Completion

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

4. Custom project types

You may want to create a new "Project Type" for your specific language. For instance, "Rust" projects usually have a folder structure defined by the "cargo" tool.

The NetBeans Project Type Tutorial is a good starting point.

5. Refactoring


6.  Error Hints/Fixes/Suggestions


7. Debugging

---------


Other support you may need to provide:

  • jump to definition, peek definition, find all references, symbol search
  • types and documentation on hover
  • code formatting
  • refactoring (e.g. rename, move)
  • error squiggles and apply suggestions from errors
  • snippets
  • build tasks


Language Feature Support
File type recognition
Project type
Semantic syntax highlighting
Formatting
Braces matching
Error Hints/Fixes/Suggestions
Code completion
Code templates
Refactoring
Debugging

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