Skip to content

Commit

Permalink
First commands to call CML generators via LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-ka committed May 6, 2020
1 parent 3fd0c09 commit fcd7b28
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 29 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ group 'org.contextmapper'
version '1.0-SNAPSHOT'

repositories {
mavenLocal()
mavenCentral()
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cmlVersion=5.11.0
cmlVersion=5.11.1-SNAPSHOT
32 changes: 25 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "The Context Mapper VS Code Extension",
"version": "0.0.1",
"publisher": "ContextMapper",
"icon": "images/contextmapper.png",
"icon": "resources/contextmapper.png",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand All @@ -28,6 +28,8 @@
{
"id": "cml",
"aliases": [
"Context Mapper DSL",
"CML",
"cml"
],
"extensions": [
Expand All @@ -45,14 +47,30 @@
],
"commands": [
{
"command": "cml.a.proxy",
"title": "CML Command A"
"command": "cml.generate.puml.proxy",
"title": "Generate PlantUML Diagrams",
"when": "editorLangId == cml"
},
{
"command": "cml.b",
"title": "CML Command B"
"command": "cml.generate.mdsl.proxy",
"title": "Generate MDSL Contracts",
"when": "editorLangId == cml"
}
]
],
"menus": {
"editor/context": [
{
"when": "resourceLangId == cml",
"command": "cml.generate.puml.proxy",
"group": "cmlGenerators@1"
},
{
"when": "resourceLangId == cml",
"command": "cml.generate.mdsl.proxy",
"group": "cmlGenerators@2"
}
]
}
},
"devDependencies": {
"vscode": "^1.1.33",
Expand All @@ -69,4 +87,4 @@
"watch": "tsc -w -p ./src",
"update-vscode": "node ./node_modules/vscode/bin/install"
}
}
}
Binary file added resources/cml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
36 changes: 36 additions & 0 deletions src/commands/generators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* CML generator commands
*/

import { commands, ExtensionContext, window, workspace, Uri } from "vscode";

type CommandType = (...args: any[]) => any;

export function generatePlantUML(): CommandType {
return generate('cml.generate.puml', 'The PlantUML diagrams have been generated into the src-gen folder.');
}

export function generateMDSL(): CommandType {
return generate('cml.generate.mdsl', 'The MDSL contracts have been generated into to src-gen folder.');
}

function generate(command: string, successMessage: string): CommandType {
return async () => {
if (editorIsNotCMLEditor())
return;

if (documentInEditorHasURI()) {
await commands.executeCommand(command, window.activeTextEditor.document.uri.toString());
window.showInformationMessage(successMessage);
}
};
}

function editorIsNotCMLEditor(): boolean {
let activeEditor = window.activeTextEditor;
return !activeEditor || !activeEditor.document || activeEditor.document.languageId !== 'cml';
}

function documentInEditorHasURI(): boolean {
return window.activeTextEditor.document.uri instanceof Uri;
}
37 changes: 16 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,48 @@
import * as path from 'path';
import * as os from 'os';

import {Trace} from 'vscode-jsonrpc';
import { commands, window, workspace, ExtensionContext, Uri } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient';
import { Trace } from 'vscode-jsonrpc';
import { commands, window, workspace, ExtensionContext, Uri, InputBoxOptions } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, VersionedTextDocumentIdentifier } from 'vscode-languageclient';
import * as generators from "./commands/generators";

export function activate(context: ExtensionContext) {
// The server is a locally installed in lsp
let launcher = os.platform() === 'win32' ? 'context-mapper-lsp.bat' : 'context-mapper-lsp';
let script = context.asAbsolutePath(path.join('lsp', 'bin', launcher));

let serverOptions: ServerOptions = {
run : { command: script },
debug: { command: script, args: [], options: { env: createDebugEnv() } }
run: { command: script },
debug: { command: script, args: ['-log'], options: { env: createDebugEnv() } }
};

let clientOptions: LanguageClientOptions = {
documentSelector: ['cml'],
synchronize: {
fileEvents: workspace.createFileSystemWatcher('**/*.*')
}
};

// Create the language client and start the client.
let lc = new LanguageClient('CML Language Server', serverOptions, clientOptions);

var disposable2 =commands.registerCommand("cml.a.proxy", async () => {
let activeEditor = window.activeTextEditor;
if (!activeEditor || !activeEditor.document || activeEditor.document.languageId !== 'cml') {
return;
}

if (activeEditor.document.uri instanceof Uri) {
commands.executeCommand("cml.a", activeEditor.document.uri.toString());
}
})
context.subscriptions.push(disposable2);
// Register generator commands
context.subscriptions.push(
commands.registerCommand("cml.generate.puml.proxy", generators.generatePlantUML()),
commands.registerCommand("cml.generate.mdsl.proxy", generators.generateMDSL())
);

// enable tracing (.Off, .Messages, Verbose)
lc.trace = Trace.Verbose;
let disposable = lc.start();

// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disposable);
}

function createDebugEnv() {
return Object.assign({
JAVA_OPTS:"-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n,quiet=y"
JAVA_OPTS: "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n,quiet=y"
}, process.env)
}

0 comments on commit fcd7b28

Please sign in to comment.