Skip to content

feat(vscode): Add run button #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions editor-extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@
],
"main": "./out/extension",
"contributes": {
"menus": {
"editor/title/run": [
{
"command": "grain.runEditorFile",
"when": "resourceLangId == grain",
"group": "navigation@1"
}
]
},
"commands": [
{
"command": "grain.restart",
"title": "Grain: Restart Language Server"
},
{
"command": "grain.runEditorFile",
"title": "Grain: Run File",
"icon": "$(play)"
}
],
"languages": [
Expand Down Expand Up @@ -89,6 +103,12 @@
"type": "string",
"description": "Absolute path to the grain CLI (detected in PATH if not specified)"
},
"grain.runCommandLayout": {
"scope": "resource",
"type": "string",
"default": "${cliPath} ${activeFile} ${cliFlags}",
"description": "The layout of the run command"
},
"grain.enableLSP": {
"scope": "resource",
"type": "boolean",
Expand Down
47 changes: 46 additions & 1 deletion editor-extensions/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ function getLspCommand(uri: Uri) {
return [command, args] as const;
}

function getRunCommand(uri: Uri) {
let config = workspace.getConfiguration("grain", uri);

let command = config.get<string | undefined>("cliPath") || findGrain();
// For some reason, if you specify a capitalized EXE extension for our pkg binary,
// it crashes the LSP so we just lowercase any .EXE ending in the command
command = command.replace(/\.EXE$/, ".exe");

let flags = config.get<string | undefined>("cliFlags") || "";

let args = flags.split(" ");

return [command, args] as const;
}

async function startFileClient(uri: Uri) {
let [command, args] = getLspCommand(uri);

Expand Down Expand Up @@ -268,6 +283,34 @@ async function restartAllClients() {
await client.restart();
}
}
async function runEditorFile(resource: Uri) {
let targetResource = resource;
if (!targetResource && window.activeTextEditor) {
targetResource = window.activeTextEditor.document.uri;
}
if (targetResource) {
const [command, args] = getRunCommand(targetResource);

const config = workspace.getConfiguration();
const grainRunCommand = config
.get<string>(
"grain.runCommandLayout",
"${cliPath} ${activeFile} ${cliFlags}"
)
.replaceAll("${cliPath}", command)
.replaceAll("${activeFile}", targetResource.fsPath)
.replaceAll("${cliFlags}", args.join(" "));

// Create a new terminal instance
const terminal = window.createTerminal("Grain");

// Send a command to the terminal
terminal.sendText(grainRunCommand);

// Show the terminal
terminal.show();
}
}

async function didOpenTextDocument(
document: TextDocument
Expand Down Expand Up @@ -359,11 +402,13 @@ export async function activate(context: ExtensionContext): Promise<void> {
didChangeWorkspaceFolders
);
let restart$ = commands.registerCommand("grain.restart", restartAllClients);
const run$ = commands.registerCommand("grain.runEditorFile", runEditorFile);

context.subscriptions.push(
didOpenTextDocument$,
didChangeWorkspaceFolders$,
restart$
restart$,
run$
);

for (let doc of workspace.textDocuments) {
Expand Down
Loading