kylebarron / vscode-jupyter-python

Run automatically-inferred Python code blocks in the VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=kylebarron.vscode-jupyter-python
MIT License
37 stars 3 forks source link

Illegal value for `line` #6

Open kylebarron opened 1 year ago

kylebarron commented 1 year ago

this warning happens when running shift+enter on the last line of a text editor. Presumably I'm trying to set a line one after what exists in the editor

ThisUserIsSuperCool commented 1 year ago

found the same problem. cmd+enter works well for the last line. anyway, please fix this, this extension is awesome.

xclrr commented 1 year ago

Hey, I'm also a fan of Atom Hydrogen. Yesterday I found this extension and found the same problem. I've fixed it by editting the run-code.js. And here's my code. Replace this function in run-code.js with my code below. It is expected to work.

function runInferredCodeBlockAndMoveDown() {
    const textEditor = vscode.window.activeTextEditor;
    if (!textEditor) {
        return;
    }
    const expandedCodeRange = inferCodeBlock(textEditor);
    const inferredBlockText = textEditor.document.getText(expandedCodeRange);
    console.log("inferredBlockText", inferredBlockText);
    var empty_line_count = 0;
    for(var i = expandedCodeRange.end.line; i >= expandedCodeRange.start.line; i--){
        if(textEditor.document.lineAt(i).text.match(/^\s*$/)){
            empty_line_count += 1;
        }
        else{
            break;
        }
    }
    console.log("empty_line_count", empty_line_count);
    executeText(inferredBlockText);
    //add a new line to the end of the code block
    var endPosition;
    var newSelection;
    if(textEditor.document.lineCount <= expandedCodeRange.end.line + 1){
        console.log("the last block")
        if(expandedCodeRange.start.line == expandedCodeRange.end.line-empty_line_count){
            console.log("the last block is a single line")
            if(empty_line_count == 0){
                console.log("need to add a new line")
                vscode.commands.executeCommand("editor.action.insertLineAfter");
            }
            endPosition = new vscode_1.Position(expandedCodeRange.end.line + 1 - empty_line_count, 0);
            newSelection = new vscode_1.Selection(endPosition, endPosition);
            setSelectionAndMoveDown(textEditor, newSelection);
        }
        else{
            console.log("the last block is a multi-line block")
            endPosition = new vscode_1.Position(expandedCodeRange.end.line + 1 - empty_line_count, 0);
            newSelection = new vscode_1.Selection(endPosition, endPosition);
            setSelectionAndMoveDown(textEditor, newSelection);
            if(empty_line_count == 0){
                console.log("need to add a new line")
                endPosition = new vscode_1.Position(expandedCodeRange.end.line + 1, 0);
                newSelection = new vscode_1.Selection(endPosition, endPosition);
                vscode.commands.executeCommand("editor.action.insertLineAfter");
                setSelectionAndMoveDown(textEditor, newSelection);
            }
        }
    } 
    else {
        endPosition = new vscode_1.Position(expandedCodeRange.end.line + 1, 0);
        newSelection = new vscode_1.Selection(endPosition, endPosition);
        setSelectionAndMoveDown(textEditor, newSelection);
    }
}
andycraig commented 1 year ago

@kylebarron Hi, is there any chance that this solution from @xclrr could be integrated in? I love this extension and I run into this issue multiple times a day. If there's anything I could do to help, please let me know.

kylebarron commented 1 year ago

A PR is welcome. Some sort of test for this would be ideal, though I never figured out the initial setup for how to do tests on vscode

andycraig commented 1 year ago

@xclrr Would you like to do a PR? If so, great! If not, would you mind if I did a PR based on your code?

@kylebarron I think a test for this would be trickier than ones I've done in the past but I'll have a look into it.

andycraig commented 1 year ago

@kylebarron I'm afraid I still don't have a solution for testing but would it be possible to merge @xclrr's PR in the meantime?