Skip to content

Commit

Permalink
feat: Language server with validation (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Jeffery <[email protected]>
  • Loading branch information
d-jeffery and d-jeffery authored Aug 4, 2023
1 parent aa16faa commit ebd4b72
Show file tree
Hide file tree
Showing 9 changed files with 1,041 additions and 13 deletions.
21 changes: 18 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,38 @@
"type": "extensionHost",
"request": "launch",
"name": "Launch Client",
"trace": true,
"runtimeExecutable": "${execPath}",
"sourceMaps": true,
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
"outFiles": ["${workspaceRoot}/client/out/**/*.js"],
"preLaunchTask": "npm: webpack"
},
{
"type": "node",
"request": "attach",
"name": "Attach to Server",
"port": 6012, // Port number chosen and set in client/src/extnesion.ts upon startup
"restart": true,
"sourceMaps": true
},
{
"name": "Language Server E2E Test",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"sourceMaps": true,
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"--extensionTestsPath=${workspaceRoot}/client/out/test/index",
"${workspaceRoot}/client/testFixture"
],
"outFiles": ["${workspaceRoot}/client/out/test/**/*.js"],
"preLaunchTask": "npm: compile"
}
]
],
"compounds": [
{
"name": "Client/Server",
"configurations": ["Launch Client", "Attach to Server"]
}
]
}
49 changes: 44 additions & 5 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as path from 'path';
import { window, workspace, ExtensionContext, commands, Range } from 'vscode';
import { friendlySyntaxToApiSyntax } from '@openfga/syntax-transformer';

import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';

let client: LanguageClient;

export function activate(context: ExtensionContext) {

const module = path.join(__dirname, '..', '..', 'server', 'dist', 'server.js');
const debugOptions = { execArgv: ['--nolazy', '--inspect=6012'] };
const serverOptions: ServerOptions = {
run: { module, transport: TransportKind.ipc },
debug: { module, transport: TransportKind.ipc, options: debugOptions}
};

// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for plain text documents
documentSelector: [{ scheme: 'file', language: 'openfga' }],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
}
};

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

// Start the client. This will also launch the server
client.start();

const transformCommand = commands.registerCommand('openfga.commands.transformToJson', async () => {
const activeEditor = window.activeTextEditor;
if (!activeEditor) {
Expand All @@ -28,3 +60,10 @@ export function activate(context: ExtensionContext) {

context.subscriptions.push(transformCommand);
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
"package": "webpack --mode production --devtool hidden-source-map",
"compile": "rm -rf client/out && tsc -b",
"test": "sh ./scripts/e2e.sh",
"lint": "eslint ./client/src --ext .ts,.tsx",
"postinstall": "cd client && npm install && cd .."
"lint": "eslint ./client/src ./server/src --ext .ts,.tsx",
"postinstall": "cd client && npm install && cd ../server && npm install && cd .."
},
"devDependencies": {
"@types/mocha": "^10.0.1",
Expand Down
Loading

0 comments on commit ebd4b72

Please sign in to comment.