LSP Support
Xtext supports the generation of language servers that comply to the Language Server Protocol (LSP).
Getting Started in Eclipse
Step 1: Create a new Xtext Project with language server support and Maven as the preferred build system.
Step 2: Implement the Domainmodel language following the Xtext - 15 Minutes Tutorial.
Step 3: Try out the Domainmodel language server in Eclipse based on LSP4E.
-
Configure a content type to the
*.dmodel
files:
-
Create a launch configuration that starts the Domainmodel language server:
-
Assign the Domainmodel content type to the Domainmodel language server launcher:
- LSP does not support syntax highlighting (usually it is done on the client side). The following TextMate json file adds syntax highlighting support for the keywords, single-line and multi-line comments of the Domainmodel language:
{ "name": "Domainmodel", "scopeName": "text.dmodel", "fileTypes": [ "dmodel" ], "repository": { "general": { "patterns": [ { "include": "#linecomment" }, { "include": "#blockcomment" }, { "include": "#keyword" } ] }, "linecomment": { "name": "comment.line.double-dash.dmodel", "begin": "(^[ \\t]+)?(?=//)", "end": "(?=$)" }, "blockcomment": { "name": "comment.block.dmodel", "begin": "/\\*(\\*)?(?!/)", "end": "\\*/" }, "keyword": { "name": "keyword.control.mydsl", "match": "\\b(entity|datatype)\\b|!" } }, "patterns": [ { "include": "#general" } ], "uuid": "8383e49a-fa0d-4bb5-827b-10e8abb294ca" }
You may save the content in a file and then add it to the TextMate grammar in Preferences.
- Open a
*.dmodel
file with the Generic Editor and explore the supported language features like syntax highlighting, content assist, validation, displaying code lenses, quickfixes, formatting, … :
Getting Started in IntelliJ IDEA
Step 1: Create a new Xtext Project with language server support and Gradle as the preferred build system.
Step 2: Implement the Domainmodel language following the Xtext - 15 Minutes Tutorial.
Step 3: Try out the Domainmodel language server in IntelliJ IDEA based on the gtache LSP plugin.
- Generate the Language Server jar file using the
gradle clean build
command. After the gradle build has been completed successfully, the generated jar file is located in the build folder of the ide project:org.example.domainmodel.ide\build\libs\org.example.domainmodel.ide-1.0.0-SNAPSHOT-ls.jar
-
Start IntelliJ IDEA and install the LSP plugin via
File - Settings... - Plugins - search for 'IntelliJ LSP Support'
:
-
Assign the Domainmodel language server to the
*.dmodel
extension viaFile - Settings... - Languages and Frameworks - Language Server Protocol - Server Definitions
:
-
While opening a Domainmodel file for the first time, the
*.dmodel
file pattern should be configured to be treated as text files and auto-detect the file type by content:
- After that, whenever a
*.dmodel
file is opened in IntelliJ IDEA, the Domainmodel Language Server is activated (indicated by the green circle at the bottom right) and provides the supported language features (e.g. code completion) to the user:
Further LSP Clients
Feel free to implement further LSP clients to the Domainmodel language server based on Atom, Eclipse Che, Eclipse Theia, Monaco Editor, VS Code, … The currenly supported LSP clients are available in the LSP clients
section of https://langserver.org/.
Language Features
Currently, Xtext supports the following LSP language features:
LSP 3.17.0 (released on 2022-10-05) LSP4J 0.21.0 (released on 2023-05-18) |
Xtext 2.31.0 (released on 2023-05-29) |
---|---|
Diagnostic (aka Validation) | |
Completion (aka Content Assist) | |
Snippets (aka Template Proposals) | |
Hover | |
Signature Help | |
Goto Declaration (aka Hyperlinking) | |
Goto Definition (aka Hyperlinking) | |
Goto Type Defintion (aka Hyperlinking) | |
Goto Implementation (aka Hyperlinking) | |
Find References | |
Document Highlight (aka Marking Occurrences) | |
Document Symbols (aka Model Elements, Outline View) | |
Code Action (aka QuickAssist, QuickFixes) | |
Code Lens (aka Code Mining) | |
Document Link (aka Hyperlinking) | |
Document Color | |
Document Formatting (aka Formatting) | |
Document Range Formatting (aka Formatting) | |
Document on Type Formatting (aka Auto Editing) | |
Rename (aka Rename Refactoring) | |
Folding Range (aka Folding) | |
Selection Range (aka Double Click Text Selection) | |
Call Hierarchy | |
Semantic Tokens (aka Semantic Highlighting) | |
Linked Editing Range (aka Rename Refactoring) |
Unit Testing
Automated tests are crucial for the maintainability and the quality of a software product. That is why it is strongly recommended to write unit tests for your language server, too. Xtext provides the org.eclipse.xtext.testing.AbstractLanguageServerTest base class that comes in handy when implementing automated LSP unit tests. The org.eclipse.xtext.ide.tests.server package contains JUnit test cases for almost all supported language features. Feel free to study them to get some inspirations on how to implement automated unit tests for your Xtext-based language server.