diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2021-05-09 17:12:30 +0200 |
|---|---|---|
| committer | DanielGavin <danielgavin5@hotmail.com> | 2021-05-09 17:12:30 +0200 |
| commit | 87d4464e91f8b5784ba20e6ef78c818092afb9a9 (patch) | |
| tree | cd9307540daeebaad68e28ba552c523bf3a41f29 /editors | |
| parent | 3f154beb5bb774dc93f0a5e2450e1880b35c307c (diff) | |
move commands to it seperate file
Diffstat (limited to 'editors')
| -rw-r--r-- | editors/vscode/src/commands.ts | 104 | ||||
| -rw-r--r-- | editors/vscode/src/ctx.ts | 64 | ||||
| -rw-r--r-- | editors/vscode/src/extension.ts | 105 | ||||
| -rw-r--r-- | editors/vscode/src/run.ts | 4 | ||||
| -rw-r--r-- | editors/vscode/src/watch.ts | 13 |
5 files changed, 191 insertions, 99 deletions
diff --git a/editors/vscode/src/commands.ts b/editors/vscode/src/commands.ts new file mode 100644 index 0000000..27c986b --- /dev/null +++ b/editors/vscode/src/commands.ts @@ -0,0 +1,104 @@ +import * as vscode from 'vscode'; +import * as lc from 'vscode-languageclient'; + +import { Ctx, Cmd } from './ctx'; +import { execFile, spawnSync } from 'child_process'; +import { LanguageClient } from 'vscode-languageclient/node'; +import path = require('path'); +import { getDebugConfiguration } from './debug'; + + +export function runDebugTest(ctx: Ctx): Cmd { + + return async(debugConfig: any) => { + + const fn = debugConfig.function; + const cwd = debugConfig.cwd; + const pkg = path.basename(cwd); + + var args = []; + + args.push("test"); + args.push(cwd); + args.push(`-test-name:${fn}`); + args.push("-debug"); + + for(var i = 0; i < ctx.config.collections.length; i++) { + const name = ctx.config.collections[i].name; + const path = ctx.config.collections[i].path; + if(name === "core") { + continue; + } + args.push(`-collection:${name}=${path}`); + } + + const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath; + + if(workspaceFolder === undefined) { + return; + } + + const odinExecution = execFile("odin", args, {cwd : workspaceFolder}, (err, stdout, stderr) => { + if (err) { + vscode.window.showErrorMessage(err.message); + } + }); + + const executableName = path.join(workspaceFolder, pkg); + + odinExecution.on("exit", (code) => { + + if(code !== 0) { + throw Error("Odin test failed!"); + } + + vscode.debug.startDebugging(undefined, getDebugConfiguration(ctx.config, executableName)).then(r => console.log("Result", r)); + }); + + }; + +} + +export function runTest(ctx: Ctx): Cmd { + + return async(debugConfig: any) => { + const fn = debugConfig.function; + const cwd = debugConfig.cwd; + const pkg = path.basename(cwd); + + var args = []; + + args.push("test"); + args.push(cwd); + args.push(`-test-name:${fn}`); + + for(var i = 0; i < ctx.config.collections.length; i++) { + const name = ctx.config.collections[i].name; + const path = ctx.config.collections[i].path; + if(name === "core") { + continue; + } + args.push(`-collection:${name}=${path}`); + } + + const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath; + + if(workspaceFolder === undefined) { + return; + } + + var definition = { + type: "shell", + command: "run", + args: args, + cwd: workspaceFolder, + }; + + const shellExec = new vscode.ShellExecution("odin", args, { cwd: workspaceFolder}); + + const target = vscode.workspace.workspaceFolders![0]; + var task = new vscode.Task(definition, target, "Run Test", "odin", shellExec); + + vscode.tasks.executeTask(task); + }; +}
\ No newline at end of file diff --git a/editors/vscode/src/ctx.ts b/editors/vscode/src/ctx.ts new file mode 100644 index 0000000..7abf9c4 --- /dev/null +++ b/editors/vscode/src/ctx.ts @@ -0,0 +1,64 @@ +import * as vscode from 'vscode'; +import * as lc from 'vscode-languageclient/node'; + +import { Config } from './config'; +import { isOdinEditor, OdinEditor } from './util'; + +//modified from https://github.com/rust-analyzer/rust-analyzer/blob/master/editors/code/src/ctx.ts - 09.05.2021 + +export class Ctx { + private constructor( + readonly config: Config, + private readonly extCtx: vscode.ExtensionContext, + readonly client: lc.LanguageClient, + readonly serverPath: string, + ) { + + } + + static async create( + config: Config, + client: lc.LanguageClient, + extCtx: vscode.ExtensionContext, + serverPath: string, + cwd: string, + ): Promise<Ctx> { + const res = new Ctx(config, extCtx, client, serverPath); + return res; + } + + get activeOdinEditor(): OdinEditor | undefined { + const editor = vscode.window.activeTextEditor; + return editor && isOdinEditor(editor) + ? editor + : undefined; + } + + get visibleOdinEditors(): OdinEditor[] { + return vscode.window.visibleTextEditors.filter(isOdinEditor); + } + + registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { + const fullName = `ols.${name}`; + const cmd = factory(this); + const d = vscode.commands.registerCommand(fullName, cmd); + this.pushCleanup(d); + } + + get globalState(): vscode.Memento { + return this.extCtx.globalState; + } + + get subscriptions(): Disposable[] { + return this.extCtx.subscriptions; + } + + pushCleanup(d: Disposable) { + this.extCtx.subscriptions.push(d); + } +} + +export interface Disposable { + dispose(): void; +} +export type Cmd = (...args: any[]) => unknown;
\ No newline at end of file diff --git a/editors/vscode/src/extension.ts b/editors/vscode/src/extension.ts index 0c0c6eb..eabf6bb 100644 --- a/editors/vscode/src/extension.ts +++ b/editors/vscode/src/extension.ts @@ -6,8 +6,6 @@ import * as vscode from 'vscode'; import * as path from "path"; import * as os from "os"; import { promises as fs, PathLike, constants } from "fs"; -import { execFile } from 'child_process'; -import { trace } from 'console'; var AdmZip = require('adm-zip'); @@ -22,14 +20,13 @@ import { RunnableCodeLensProvider } from "./run"; import { PersistentState } from './persistent_state'; import { Config } from './config'; import { fetchRelease, download } from './net'; -import { getDebugConfiguration } from './debug'; import { isOdinInstalled } from './toolchain'; -import { tasks } from 'vscode'; - +import { Ctx } from './ctx'; +import { runDebugTest, runTest } from './commands'; const onDidChange: vscode.EventEmitter<void> = new vscode.EventEmitter<void>(); -let client: LanguageClient; +let ctx: Ctx | undefined; export async function activate(context: vscode.ExtensionContext) { @@ -106,7 +103,7 @@ export async function activate(context: vscode.ExtensionContext) { outputChannel: vscode.window.createOutputChannel("Odin Language Server") }; - client = new LanguageClient( + var client = new LanguageClient( 'odinLanguageClient', 'Odin Language Server Client', serverOptions, @@ -115,96 +112,10 @@ export async function activate(context: vscode.ExtensionContext) { client.start(); - //Temp - //Move commands to somewhere else(probably do it like rust-analyzer does it) - vscode.commands.registerCommand("extension.debug", debugConfig => { - const fn = debugConfig.function; - const cwd = debugConfig.cwd; - const pkg = path.basename(cwd); - - var args = []; - - args.push("test"); - args.push(cwd); - args.push(`-test-name:${fn}`); - args.push("-debug"); - - for(var i = 0; i < config.collections.length; i++) { - const name = config.collections[i].name; - const path = config.collections[i].path; - if(name === "core") { - continue; - } - args.push(`-collection:${name}=${path}`); - } - - const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath; - - if(workspaceFolder === undefined) { - return; - } - - const odinExecution = execFile("odin", args, {cwd : workspaceFolder}, (err, stdout, stderr) => { - if (err) { - vscode.window.showErrorMessage(err.message); - } - }); - - const executableName = path.join(workspaceFolder, pkg); - - odinExecution.on("exit", (code) => { - - if(code !== 0) { - throw Error("Odin test failed!"); - } - - vscode.debug.startDebugging(undefined, getDebugConfiguration(config, executableName)).then(r => console.log("Result", r)); - }); - - - }); - - vscode.commands.registerCommand("extension.run", debugConfig => { - const fn = debugConfig.function; - const cwd = debugConfig.cwd; - const pkg = path.basename(cwd); - - var args = []; - - args.push("test"); - args.push(cwd); - args.push(`-test-name:${fn}`); - - for(var i = 0; i < config.collections.length; i++) { - const name = config.collections[i].name; - const path = config.collections[i].path; - if(name === "core") { - continue; - } - args.push(`-collection:${name}=${path}`); - } - - const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath; - - if(workspaceFolder === undefined) { - return; - } - - var definition = { - type: "shell", - command: "run", - args: args, - cwd: workspaceFolder, - }; - - const shellExec = new vscode.ShellExecution("odin", args, { cwd: workspaceFolder}); - - const target = vscode.workspace.workspaceFolders![0]; - var task = new vscode.Task(definition, target, "Run Test", "odin", shellExec); - - tasks.executeTask(task); - }); + ctx = await Ctx.create(config, client, context, serverPath, workspaceFolder.uri.fsPath); + ctx.registerCommand("runDebugTest", runDebugTest); + ctx.registerCommand("runTest", runTest); vscode.commands.registerCommand("ols.start", () => { client.start(); @@ -410,5 +321,5 @@ async function queryForGithubToken(state: PersistentState): Promise<void> { } export function deactivate(): Thenable<void> { - return client.stop(); + return ctx!.client.stop(); } diff --git a/editors/vscode/src/run.ts b/editors/vscode/src/run.ts index 3698596..c00b27b 100644 --- a/editors/vscode/src/run.ts +++ b/editors/vscode/src/run.ts @@ -96,7 +96,7 @@ export class RunnableCodeLensProvider implements CodeLensProvider { return new CodeLens(range, { title: "Debug", - command: "extension.debug", + command: "ols.runDebugTest", tooltip: "Debug", arguments: [{ function: fn, @@ -114,7 +114,7 @@ export class RunnableCodeLensProvider implements CodeLensProvider { return new CodeLens(range, { title: "Run", - command: "extension.run", + command: "ols.runTest", tooltip: "Run", arguments: [{ function: fn, diff --git a/editors/vscode/src/watch.ts b/editors/vscode/src/watch.ts new file mode 100644 index 0000000..0f960c0 --- /dev/null +++ b/editors/vscode/src/watch.ts @@ -0,0 +1,13 @@ +import * as vscode from "vscode"; + +export function watchOlsConfigFile() +{ + var olsWatcher = vscode.workspace.createFileSystemWatcher("ols.json"); + + olsWatcher.onDidCreate((uri) => { + + }); + + + +}
\ No newline at end of file |