coc-extensions / coc-powershell

PowerShellEditorService integration for coc.vim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CocCommand powershell.execute spawns new terminal

JayDoubleu opened this issue · comments

commented

System Details

  • Vim or NeoVim?: Both
  • _Version of Vim : VIM - Vi IMproved 8.0, NVIM v0.3.7
  • _Version of coc-powershell : coc-powershell 0.0.18
  • Operating System: Ubuntu 18.04
  • _PowerShell version : 6.2.1

Issue Description

Running :CocCommand powershell.execute spawns new terminal on top of PowerShell Integrated Console and exits

Expected Behaviour

Code should run in existing terminal console

Actual Behaviour

Code runs in new terminal console on top of existing one.

powershell.evaluateLine and powershell.evaluateSelection runs as expected.

hi @JayDoubleu ,

It was intentional because the integrated console powers the language services, and carries an environment with evaluation. It's not the same as running the file from scratch.

What do you think?

commented

Well then I would suggest adding option to powershell.executeBuffer ?
Such option would not require file to be saved first and would execute entire buffer into the integrated console which is something I would expect from VSCode to do ?
Technically speaking I could just somehow make selection of entire buffer and then execute the executeSelection but its not exactly the same.

Got an idea -- maybe let integrated console spawn a child to execute that?

It's simple, first save the buffer as a tmp file, send pwsh -NoProfile -Interactive /tmp/xxxxxx.ps1<CR>

Technically speaking I could just somehow make selection of entire buffer and then execute the executeSelection but its not exactly the same.

Dragons ahead! You may be trapped in a ReadLine() in that case

In the ISE & VSCode extension, this is typically referred to as the F5 experience which would run the file you are currently looking at in the Integrated Console. It'd typically run:

& "/path/to/my/script.ps1"

However, the biggest difference between ISE and VSCode is that F5 in VSCode uses debugging through the Debug Adapter Protocol (something that coc.nvim doesn't support at the moment).

Perhaps the best way to go here is to do what the ISE does and just run:

& "/path/to/my/script.ps1"

In the Integrated Console.

Natually if you use Read-Host you're going to have to respond to it, but this would only negatively affect users who purposefully hide the Integrated Console

commented

Just an idea (probably a bad one), What if you would send whole buffer to base64 and execute it with pwsh -EncodedCommand in Integrated shell ?

The problem with that is variables like $PSScriptRoot won't be defined because they're no longer "in the file".

In theory, in the language server we can catch those and replace them with the absolute path of the file, but the user could still be using the Cmdlet class for something and if they are, that won't work.

I was playing with this myself. I'm getting error while running evaluateSelection. @JayDoubleu can you confirm it's working on your end? I'm running this in vim and the error is: "request error nvim_buf_get_mark - buf_get_mark support current buffer only"

commented

@mikeTWC1984 well i am not getting this error, you might want to try running CocUpdate command etc.

I personally don't use integrated shell now. Instead I am using Nuake plugin with autocmd FileType ps1 setlocal shell=pwsh

@JayDoubleu Thanks. Can you send vim buffer or selection to execute in Nuake terminal?

commented

@mikeTWC1984 well you can't however what I tend to do is just quick save + F12 to open nuake, run the file itself. If I don't exit the code buffer I can still undo changes

I tried evaluateSelection in nvim and it works, so I guess there might be a bug in vim version of it. Anyway, evaluateSelection do exactly what I was looking for (same F8 in ise), great work! Regarding "execute" command - the output disappear right after execution. Is that a bug or feature? @TylerLeonhardt Is there a way to generate command you mention (& "/path/to/my/script.ps1") and send it to integrated terminal (vs typing it manually)

commented

@mikeTWC1984
You can have a look at a quick script i've made here:
https://github.com/JayDoubleu/vim-pwsh-exec

It's able to execute entire buffer, current line and visual selection.

@JayDoubleu it's good, however I like the idea to execute lines within actual terminal context. Basically ISE's F8 function (I just use it all the time), evaluateSelection seemed to solve this request (once it's fixed in vim). To execute buffer we can just select all lines, although as was mentioned above it might be different from executing actual file. I think current file execution command should be added too. Something like :w | let c="term pwsh -f " . expand('%:p') | execute c , just execute in integrated terminal rather in the new window.

commented

in this case invoking powershell in terminal is not going to cut it. If you want ISE functionality the buffer/selection/line would need to be saved to tmpfile and then sourced within integrated shell. The plugin from above has most of the code to do that. There is also a way to find out the buffershell for coc-powershell and send source command to it using https://neovim.io/doc/user/eval.html#chansend() on neovim and jobstart() ? aka

chansend(buffnr, ". /tmp/tempfile.ps1\n")

@mikeTWC1984 the buffer mark problems is probably due to an upstream bug.
Could you provide the output of :CocInfo? I'll file a bug report for coc.nvim

@JayDoubleu Thanks, will check it out.
@yatli
vim version: VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 06 2019 17:31:41)
node version: v10.16.0
coc.nvim version: 0.0.73-5c385721a8
term: dumb
platform: linux

@TylerLeonhardt Tyler Leonhardt (POWERSHELL) FTE Is there a way to generate command you mention (& "/path/to/my/script.ps1") and send it to integrated terminal (vs typing it manually)

Yes! See this part of the code where we handle F8.

All that we need is to do is have powershell.execute run:

           
            const evaluateArgs: IEvaluateRequestArguments = {
                expression: `& ‘${ESCAPED_FILE_PATH_VARIABLE_HERE}’`,
            }
            client.sendRequest(EvaluateRequestMessage, evaluateArgs)

Escaped here means, if the path contains a we must replace it with two (which is how you escape single quotes in PowerShell)

image

Here we gooo #52