Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

How to add support in NetBeans to

...

a programming language using LSP

...

  1. Create a new NetBeans module. There are two kinds of NetBeans modules, ant-based and maven-based.
    1. To add a new ant-based NetBeans module, click on File → New Project → Java with Ant → NetBeans modules category and select the Module project.
      1. Give a name to your module, e.g. Kotlin-editor and provide a location to it. Select Standalone Module. Click Next.
      2. Provide a Code Name Base; this is the package name, e.g. org.netbeans.modules.kotlin.editor. Click Finish.
    2. To add a new maven-based NetBeans module, click on File → New Project → Java with Maven category and select the NetBeans Module project.
      1. Provide a Project Name, e.g. kotlin-editor as well as the maven coordinates, e.g. org.netbeans.modules as the Group Id.
      2. Select the NetBeans version (usually the same as the one of the NetBeans IDE you are developing in) and click on Finish.

Next you need to provide support for:

  • syntax highlighting
  • code completion
  • 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

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

...

  1. See a list of Language Server implementations

...

  1. and another list of Language Server Implementations

...

  1. .

...

  1. For example, to add support for Kotlin programming language, download and build kotlin-language-server

...

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

...

This tutorial is an LSP Client demo to provide support for the (ba)sh language.

File Type Recognition

...

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

See the File Type Integration Tutorial for more details on how to add File Type recognition support.

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" available at https://netbeans.apache.org/tutorials/nbm-projecttype.html is probably a good starting point.

Adding editor features

...

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

...