Skip to content
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

WIP: Send code to REPL #2

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"Snippets"
],
"activationEvents": [
"onCommand:language-julia.openPackageDirectory"
"onCommand:language-julia.openPackageDirectory",
"onCommand:language-julia.executeJuliaCodeInREPL"
],
"main": "./out/src/extension",
"contributes": {
Expand Down Expand Up @@ -60,8 +61,17 @@
{
"command": "language-julia.openPackageDirectory",
"title": "julia: Open Package Directory in New Window"
},
{
"command": "language-julia.executeJuliaCodeInREPL",
"title": "julia: Execute Code"
}
]
],
"keybindings": [{
"command": "language-julia.executeJuliaCodeInREPL",
"key": "ctrl+Enter",
"when": "editorTextFocus"
}]
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
Expand Down
34 changes: 34 additions & 0 deletions scripts/server.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module _vscodeserver
import JSON

@async begin
server = listen("\\\\.\\pipe\\vscode-language-julia-server")
while true
sock = accept(server)
@async while isopen(sock)
msg = ""
process_message = false
try
msg = JSON.parse(sock)
process_message = true
catch e
if isa(e, EOFError)
else
rethrow()
end
end

if process_message
if msg["command"] == "run"
command_string = msg["body"]
command_eval = parse(command_string)
eval(Main, command_eval)
else
error("Unknown command")
end
end
end
end
end

end
39 changes: 38 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as vscode from 'vscode';
import * as fs from 'fs'
import * as path from 'path'
import * as net from 'net'

let diagnosticCollection: vscode.DiagnosticCollection;

Expand All @@ -19,14 +20,50 @@ export function activate(context: vscode.ExtensionContext) {
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('language-julia.openPackageDirectory', openPackageDirectoryCommand);

context.subscriptions.push(disposable);

let disposable2 = vscode.commands.registerCommand('language-julia.executeJuliaCodeInREPL', executeJuliaCodeInREPL);
context.subscriptions.push(disposable2);
}

// this method is called when your extension is deactivated
export function deactivate() {
}

function executeJuliaCodeInREPL() {
var editor = vscode.window.activeTextEditor;
if(!editor) {
return;
}

var selection = editor.selection;

var text = selection.isEmpty ? editor.document.lineAt(selection.start.line).text : editor.document.getText(selection);

// If no text was selected, try to move the cursor to the end of the next line
if (selection.isEmpty) {
for (var line = selection.start.line+1; line < editor.document.lineCount; line++) {
if (!editor.document.lineAt(line).isEmptyOrWhitespace) {
var newPos = selection.active.with(line, editor.document.lineAt(line).range.end.character);
var newSel = new vscode.Selection(newPos, newPos);
editor.selection = newSel;
break;
}
}
}

var namedPipe = '\\\\.\\pipe\\vscode-language-julia-server';
var client = net.connect(namedPipe,
() => {
var msg = {
"command": "run",
"body": text
};
client.write(JSON.stringify(msg));
});

}

// This method implements the language-julia.openPackageDirectory command
function openPackageDirectoryCommand() {
const optionsVersion: vscode.QuickPickOptions = {
Expand Down