apommel / vscode-matlab-interactive-terminal

VS Code extension allowing for an interactive Matlab terminal and launching of scripts through Matlab Engine for Python
MIT License
46 stars 10 forks source link

Simple method to implement multiple-line inputs in interactive terminal (not in ml_selection) #20

Open RibomBalt opened 4 years ago

RibomBalt commented 4 years ago

Hello, in issue #3 you did a good job dealing with multiple-line inputs in selected scripts by cacheing them into a temp file. But in the interactive shell it's still not possible to run multiple-line scripts, especially those with for/if/while/try etc. (v0.3.2)

I have an idea: we can cache the inputs and execute them all when an empty line is input. To better explain this idea a python pseudo-code is presented below:

# what it used to be
def loop(inputs):
    run(inputs)
# what it can be better
def loop(inputs):
    global buffer
    if(inputs == ''):
        run(buffer)
        buffer=''
    else:
        buffer = '\n'.join([buffer, inputs]) # '\n' may be changed to ';'

With this method, we can run multiple-line script interactively just like in original MATLAB shells, e.g. like this:

>> for i=1:5
>> s=s+i
>> end
>> 
% protential outputs

I'm not familiar with Typescript so I can't do it myself, but I suppose this can be done by reusing the method and codes you dealt with issue #3.

Best Wishes!

apommel commented 3 years ago

If using your idea, that would mean someone would have to press enter twice in order to execute a single line instruction? I don't like the idea that much. I though about implementing that but there's really no clean way of doing it. Trying to detect when instructions blocks are "complete" would likely introduce a ton of bugs.