Beyond LSP Getting Your Language into Theia and VS Code Dr. Jan - - PowerPoint PPT Presentation

beyond lsp
SMART_READER_LITE
LIVE PREVIEW

Beyond LSP Getting Your Language into Theia and VS Code Dr. Jan - - PowerPoint PPT Presentation

Beyond LSP Getting Your Language into Theia and VS Code Dr. Jan Khnlein @jankoehnlein The Goal Custom domain-specific language Rich text editor support With diagram support as VS Code Extension! 2 VS Code... Isn't this


slide-1
SLIDE 1
  • Dr. Jan Köhnlein

@jankoehnlein

Beyond LSP

Getting Your Language into Theia and VS Code

slide-2
SLIDE 2

The Goal

2

  • Custom domain-specific language
  • Rich text editor support
  • With diagram support

as VS Code Extension!

slide-3
SLIDE 3

VS Code... Isn't this EclipseCon ???

3

slide-4
SLIDE 4

Why VS Code Extension

4

  • >50% IDE market share
  • VS Code extensions run in Eclipse Theia
  • new default providing language support
  • Theia is the base for all the fancy new web-IDEs
slide-5
SLIDE 5

VS Code Extension

5

Extension Config

Manifest Editor Config Syntax Highlighting Grammar Snippets JSON TS

Diagram Webview

JSON DI config Views VS Code Diagram Server

🐠 🐠 🐠

TS LSP Sprotty

Language Server

Language Server Language Server CLI Diagram Language Server Diagram Generator

🐠 🐠

Java/Xtend VSIX

Extension Code

(De-)activation Language Client Sprotty VS Code LSP Extension🐠 TS

slide-6
SLIDE 6

VS Code Extension

6

Extension Config

Manifest Editor Config Syntax Highlighting Grammar Snippets JSON VSIX

slide-7
SLIDE 7

Extension Manifest

  • NPM package.json
  • extension metadata
  • dependencies
  • scripts
  • VS Code specific extensions
  • commands, keybindings, menu entries...
  • activation events
  • entry module
  • language registration
  • file extensions
  • editor configuration
  • highlighting grammar
  • snippets

7 // package.json { "name": "states-extension", "displayName": "States Example", "description": "An Xtext-based DSL with Sprotty diagrams for statemachines", "publisher": "TypeFox", "repository": { "type": "git", "url": "https://github.com/TypeFox/sprotty-vscode" }, "version": "0.0.20", "files": [ "syntaxes" ], "scripts": { "publish": "vsce publish" } "engines": { "vscode": "^1.46.0" }, "categories": [ "Programming Languages" ], "contributes": { "languages": [{ "id": "states", "aliases": [ "states", "sm" ], "extensions": [ ".sm" ], "configuration": "./language-configuration.json" }], "grammars": [{ "language": "states", "scopeName": "source.sm", "path": "./syntaxes/states.tmLanguage.json" }] } }

slide-8
SLIDE 8

TextMate Grammar

  • grammar für syntax highlighting
  • uses regular expressions to map to styles

8 // states.tmLanguage.json { "name": "States DSL", "scopeName": "source.sm", "fileTypes": [ "sm" ], "patterns": [ { "include": "#comments" }, { "name": "keyword.control.states", "match": "\\b(statemachine|state|event|=>)\\b"}, { "name": "keyword.operator.states", "match": "\\=\\>" }, { "name": "string.quoted.double.states", "begin": "\"", "end": "\""}, ], "repository": { "comments": { "patterns": [{ "name": "comment.block.states", "begin": "/\\*", "beginCaptures": { "0": { "name": "punctuation.definition.comment.states" } }, "end": "\\*/", "endCaptures": { "0": { "name": "punctuation.definition.comment.states" } } }, ....

slide-9
SLIDE 9

Demo: Declarative Language Services

9

slide-10
SLIDE 10

Language Server Protocol

10

LSP

Language Server

Language Server Java/Xtend

Extension Code

Language Client

slide-11
SLIDE 11

Using Xtext to Generate a LS

11

// States.xtext grammar io.typefox.examples.theia.states.States with org.eclipse.xtext.common.Terminals generate states "http://www.typefox.io/States" StateMachine: 'statemachine' name=ID (states+=State | events+=Event)*; State: 'state' name=ID transitions+=Transition*; Event: 'event' name=ID; Transition: event=[Event] '=>' state=[State];

Language Server

Language Server Start Scripts Java/Xtend

slide-12
SLIDE 12

VS Code Extension

12

Extension Config

Manifest Editor Config Syntax Highlighting Grammar Snippets JSON LSP

Language Server

Language Server Start Scripts Java/Xtend VSIX

Extension Code

(De-)activation TS Language Client

slide-13
SLIDE 13

Extension Activation

13

  • Code (TypeScript)
  • (de-)activation functions

// states-extension.ts export function activate(context: vscode.ExtensionContext) { // start the language server and connect it } export function deactivate(): Thenable<void> { // disconnect and stop the language server }

  • Registration (JSON)
  • activation events
  • entry module

// package.json { "name": "states-extension", ... "activationEvents": [ "onLanguage:states" ], "main": "./pack/states-extension", ... }

slide-14
SLIDE 14

Demo

14

slide-15
SLIDE 15

VS Code Webview

15

  • Generic VS Code GUI component
  • Separate IFRAME in the DOM
  • can use arbitrary web frameworks
  • runs a separate web-application
  • communicates with extension via

JSON objects

  • Runs the diagram client app
slide-16
SLIDE 16

16

Diagram Webview

TS

Extension Code

JSON

slide-17
SLIDE 17

Sprotty

17

  • web-based diagramming framework
  • reactive architecture
  • client server separation
  • We use
  • sprotty: diagram client code
  • sprotty-xtext: extend Xtext LSPs with diagrams
  • sprotty-vscode: glue code to integrate with VS Code

CommandStack Viewer

Command Action SModel

ActionDispatcher Sprotty Client

Actions

slide-18
SLIDE 18

Sprotty – SModel

18

SGraph SNode SCompartment SEdge SNode SLabel SLabel SPort

  • diagram model
  • serializable as JSON
  • cross-references via IDs
  • extensible
slide-19
SLIDE 19

Sprotty – Webview application

19

  • DI container defines
  • custom services / configs
  • mapping of SModel to views
  • behavior using features

// di.config.ts const statesDiagramModule = new ContainerModule(() => { rebind(TYPES.ILogger).to(ConsoleLogger).inSingletonScope(); rebind(TYPES.LogLevel).toConstantValue(LogLevel.warn); rebind(TYPES.IModelFactory).to(StatesModelFactory); ... configureModelElement(context, 'graph', SGraph, SGraphView, { enable: [hoverFeedbackFeature, popupFeature] }); configureModelElement(context, 'node', SNode, RectangularNodeView); configureModelElement(context, 'label', SLabel, SLabelView); configureModelElement(context, 'edge', SEdge, PolylineArrowEdgeView); configureModelElement(context, 'html', HtmlRoot, HtmlRootView); ... });

slide-20
SLIDE 20

20

Diagram Webview

DI config Views

🐠 🐠

TS

slide-21
SLIDE 21

21

TS LSP

Sprotty

Language Server

Diagram Language Server Diagram Generator

🐠 🐠

Java/Xtend

Extension Code

Language Client TS

Diagram Webview

DI config Views

🐠 🐠

TS

slide-22
SLIDE 22

sprotty-xtext – Language Server Integration

22

  • extends LSP
  • adds LSP notifications
  • accept(Action)
  • dispatch(Action)
  • LanguageServer replaced by

DiagramLanguageServer

  • hooks a DiagramGenerator into

Xtext model lifecycles

Xtext LS Diagram Generator CommandStack Viewer

Command Action SModel

ActionDispatcher Diagram Webview

Actions

Diagram Server

slide-23
SLIDE 23

sprotty-xtext – DiagramGenerator

23

  • maps Xtext model elements to Sprotty model
  • on text changes
  • Xtext model is reparsed
  • new Sprotty model is created

State Transition SGraph SNode SLabel SEdge StateMachine Event

slide-24
SLIDE 24

24

TS

Diagram Webview

JSON VS Code Diagram Server🐠 TS LSP Sprotty

Language Server

VSIX

Extension Code

Language Client Sprotty VS Code LSP Extension🐠 TS Diagram Language Server Diagram Generator

🐠 🐠

Java/Xtend DI config Views

🐠 🐠

slide-25
SLIDE 25

sprotty-vscode – Wiring it Up

25

  • Glue code for using Sprotty in VS Code
  • abstracts internal communication
  • VscodeDiagramServer
  • webview side
  • exchanges actions with the extension
  • Sprotty VS Code Extension
  • webview management
  • extension side
  • exchanges LSP messages with the LanguageClient
  • exchanges Actions with the webview
  • converts between Sprotty LSP messages and Actions
slide-26
SLIDE 26

26

TS

Diagram Webview

JSON VS Code Diagram Server🐠 TS LSP Sprotty

Language Server

VSIX

Extension Code

Language Client Sprotty VS Code LSP Extension🐠 TS Diagram Language Server Diagram Generator

🐠 🐠

generates new SModel

slide-27
SLIDE 27

27

TS

Diagram Webview

JSON VS Code Diagram Server🐠 TS LSP Sprotty

Language Server

VSIX

Extension Code

Language Client Sprotty VS Code LSP Extension🐠 TS Diagram Language Server Diagram Generator

🐠 🐠

Create UpdateModelAction

slide-28
SLIDE 28

28

TS

Diagram Webview

JSON VS Code Diagram Server🐠 TS LSP Sprotty

Language Server

VSIX

Extension Code

Language Client Sprotty VS Code LSP Extension🐠 TS Diagram Language Server Diagram Generator

🐠 🐠

LSP notify dispatch(UpdateModelAction)

slide-29
SLIDE 29

29

TS

Diagram Webview

JSON VS Code Diagram Server🐠 TS LSP Sprotty

Language Server

VSIX

Extension Code

Language Client Sprotty VS Code LSP Extension🐠 TS Diagram Language Server Diagram Generator

🐠 🐠

  • nNotify

dispatch(UpdateModelAction)

slide-30
SLIDE 30

30

TS

Diagram Webview

JSON VS Code Diagram Server🐠 TS LSP Sprotty

Language Server

VSIX

Extension Code

Language Client Sprotty VS Code LSP Extension🐠 TS Diagram Language Server Diagram Generator

🐠 🐠

sendToWebview (UpdateModelAction)

slide-31
SLIDE 31

31

TS

Diagram Webview

JSON VS Code Diagram Server🐠 TS LSP Sprotty

Language Server

VSIX

Extension Code

Language Client Sprotty VS Code LSP Extension🐠 TS Diagram Language Server Diagram Generator

🐠 🐠

Execute UpdateModelAction as a smooth animation

slide-32
SLIDE 32

Demo: Diagram View

32

slide-33
SLIDE 33

Development Process

33

slide-34
SLIDE 34

Debugging

34

  • Extension code
  • launch extension from VS Code
  • use host VS Code debugger
  • Language Server
  • run language server form Eclipse in socket mode
  • use Eclipse debugger
  • Webview
  • in the runtime VS Code hit F1 > Developer: Open Developer Tools
  • also shows error console
slide-35
SLIDE 35

webpack – Packaging JS Applications

35

  • standard npm tool
  • package multiple JavaScript modules, CSS files, images etc. into a single JavaScript file
  • minimizes result
  • removes whitespace
  • tree shakes dependencies
  • configuration can be tricky
  • use tsconfig.json and webpack.config.json as in the example
  • configures source maps correctly for debugging
slide-36
SLIDE 36

Publishing

36

  • VS Code Marketplace
  • ~21300 Extensions (Oct 2020),
  • closed source
  • terms of service only allow access via Microsoft products
  • No on-premise installs
  • openVSX
  • Eclipse project
  • can be installed on-premise
  • public instance is run by Eclipse Foundation
  • default for VS Codium, Gitpod, Theia
slide-37
SLIDE 37

Demo: Diagram Editing

37

slide-38
SLIDE 38

Thanks for Your Attention

slide-39
SLIDE 39

Links

https://insights.stackoverflow.com/survey/2019#development-environments-and-tools https://theia-ide.org https://code.visualstudio.com/api https://microsoft.github.io/language-server-protocol https://www.eclipse.org/Xtext https://github.com/eclipse/sprotty https://github.com/eclipse/sprotty-xtext https://github.com/eclipse/sprotty-vscode https://github.com/TypeFox/vscode-xtext-sprotty-example https://open-vsx.org/

39