Beyond LSP Getting Your Language into Theia and VS Code Dr. Jan Köhnlein @jankoehnlein
The Goal • Custom domain-specific language • Rich text editor support • With diagram support as VS Code Extension! 2
VS Code... Isn't this EclipseCon ??? 3
Why VS Code Extension • >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 4
VS Code Extension Extension Config Language Server Manifest Editor Config Language Server Language Server CLI 🐠 🐠 Syntax Highlighting Diagram Language Snippets Diagram Generator Grammar Server LSP Sprotty JSON Java/Xtend Extension Code Diagram Webview 🐠 🐠 (De-)activation Language Client DI config Views JSON LSP Extension 🐠 🐠 Sprotty VS Code VS Code Diagram Server TS TS TS VSIX 5
VS Code Extension Extension Config Manifest Editor Config Syntax Highlighting Snippets Grammar JSON VSIX 6
Extension Manifest // package.json { "name": "states-extension", • NPM package.json "displayName": "States Example", "description": "An Xtext-based DSL with Sprotty diagrams for statemachines", • extension metadata "publisher": "TypeFox", "repository": { • dependencies "type": "git", "url": "https://github.com/TypeFox/sprotty-vscode" • scripts }, "version": "0.0.20", • VS Code specific extensions "files": [ "syntaxes" ], "scripts": { • commands, keybindings, menu entries... "publish": "vsce publish" } • activation events "engines": { "vscode": "^1.46.0" }, "categories": [ • entry module "Programming Languages" ], • language registration "contributes": { "languages": [{ • file extensions "id": "states", "aliases": [ "states", "sm" ], • editor configuration "extensions": [ ".sm" ], "configuration": "./language-configuration.json" • highlighting grammar }], "grammars": [{ • snippets "language": "states", "scopeName": "source.sm", "path": "./syntaxes/states.tmLanguage.json" }] } } 7
TextMate Grammar • grammar für syntax highlighting • uses regular expressions to map to styles // 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" } } }, .... 8
Demo: Declarative Language Services 9
Language Server Protocol Language Server Language Server LSP Java/Xtend Extension Code Language Client 10
Using Xtext to Generate a LS // States.xtext grammar io.typefox.examples.theia.states.States with org.eclipse.xtext.common.Terminals Language Server generate states "http://www.typefox.io/States" Language Server Start Scripts StateMachine: 'statemachine' name=ID (states+=State | events+=Event)*; State: 'state' name=ID transitions+=Transition*; Java/Xtend Event: 'event' name=ID; Transition: event=[Event] '=>' state=[State]; 11
VS Code Extension Extension Config Language Server Manifest Editor Config Language Server Start Scripts Syntax Highlighting Snippets Grammar LSP JSON Java/Xtend Extension Code (De-)activation Language Client TS VSIX 12
Extension Activation // package.json • Registration (JSON) • activation events { "name": "states-extension", • entry module ... "activationEvents": [ "onLanguage:states" ], "main": "./pack/states-extension", ... } // states-extension.ts • Code (TypeScript) export function activate(context: vscode.ExtensionContext) { • (de-)activation functions // start the language server and connect it } export function deactivate(): Thenable<void> { // disconnect and stop the language server } 13
Demo 14
VS Code Webview • 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 15
Extension Code Diagram Webview JSON TS 16
Sprotty • web-based diagramming framework Sprotty Client • reactive architecture Command • client server separation CommandStack • We use Actions • sprotty: diagram client code SModel ActionDispatcher • sprotty-xtext: extend Xtext LSPs with diagrams • sprotty-vscode: glue code to integrate with VS Code Action Viewer 17
Sprotty – SModel • diagram model SGraph • serializable as JSON • cross-references via IDs • extensible SNode SNode SEdge SPort SCompartment SLabel SLabel 18
Sprotty – Webview application • DI container defines // di.config.ts • custom services / configs • mapping of SModel to views const statesDiagramModule = new ContainerModule(() => { rebind(TYPES.ILogger).to(ConsoleLogger).inSingletonScope(); • behavior using features 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); ... }); 19
Diagram Webview 🐠 🐠 DI config Views TS 20
Language Server 🐠 🐠 Diagram Language Diagram Generator Server LSP Sprotty Java/Xtend Extension Code Diagram Webview 🐠 🐠 Language Client DI config Views TS TS TS 21
sprotty-xtext – Language Server Integration • extends LSP Diagram Webview • adds LSP notifications Command • accept(Action) CommandStack • dispatch(Action) Xtext LS • LanguageServer replaced by Actions DiagramLanguageServer SModel Diagram Server ActionDispatcher • hooks a DiagramGenerator into Xtext model lifecycles Diagram Generator Action Viewer 22
sprotty-xtext – DiagramGenerator • maps Xtext model elements to Sprotty model • on text changes StateMachine SGraph • Xtext model is reparsed • new Sprotty model is created State SNode Transition SEdge Event SLabel 23
Language Server 🐠 🐠 Diagram Language Diagram Generator Server LSP Sprotty Java/Xtend Extension Code Diagram Webview 🐠 🐠 Language Client DI config Views LSP Extension 🐠 Diagram Server 🐠 Sprotty VS Code JSON VS Code TS TS TS VSIX 24
sprotty-vscode – Wiring it Up • 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 25
Language Server generates new SModel 🐠 🐠 Diagram Language Diagram Generator Server LSP Sprotty Extension Code Diagram Webview Language Client JSON LSP Extension 🐠 Diagram Server 🐠 Sprotty VS Code VS Code TS TS TS VSIX 26
Language Server Create UpdateModelAction 🐠 🐠 Diagram Language Diagram Generator Server LSP Sprotty Extension Code Diagram Webview Language Client JSON LSP Extension 🐠 Diagram Server 🐠 Sprotty VS Code VS Code TS TS TS VSIX 27
Language Server LSP notify dispatch(UpdateModelAction) 🐠 🐠 Diagram Language Diagram Generator Server LSP Sprotty Extension Code Diagram Webview Language Client JSON LSP Extension 🐠 Diagram Server 🐠 Sprotty VS Code VS Code TS TS TS VSIX 28
Language Server onNotify dispatch(UpdateModelAction) 🐠 🐠 Diagram Language Diagram Generator Server LSP Sprotty Extension Code Diagram Webview Language Client JSON LSP Extension 🐠 Diagram Server 🐠 Sprotty VS Code VS Code TS TS TS VSIX 29
Recommend
More recommend