From df10a54fa1e3fdfdfac2488a684cfe689e0ef77b Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sat, 18 May 2019 08:43:22 -0700 Subject: [PATCH 1/3] allow ability to change powershell version --- autoload/coc/powershell.vim | 4 +- autoload/coc/powershell/utils.vim | 81 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 autoload/coc/powershell/utils.vim diff --git a/autoload/coc/powershell.vim b/autoload/coc/powershell.vim index ed7d1e9..aa85387 100644 --- a/autoload/coc/powershell.vim +++ b/autoload/coc/powershell.vim @@ -1,8 +1,8 @@ let s:root = expand(':h:h:h') let s:is_win = has('win32') || has('win64') let s:is_mac = !s:is_win && !has('win32unix') - \ && (has('mac') || has('macunix') || has('gui_macvim') || - \ (!isdirectory('/proc') && executable('sw_vers'))) + \ && (has('mac') || has('macunix') || has('gui_macvim') || + \ (!isdirectory('/proc') && executable('sw_vers'))) let s:is_vim = !has('nvim') let s:install_script = s:root.'/install.ps1' diff --git a/autoload/coc/powershell/utils.vim b/autoload/coc/powershell/utils.vim new file mode 100644 index 0000000..7764edf --- /dev/null +++ b/autoload/coc/powershell/utils.vim @@ -0,0 +1,81 @@ +let s:is_win = has('win32') || has('win64') +let s:is_mac = !s:is_win && !has('win32unix') + \ && (has('mac') || has('macunix') || has('gui_macvim') || + \ (!isdirectory('/proc') && executable('sw_vers'))) + +let s:linuxExe = { "versionName": "PowerShell Core", "executablePath": "/usr/bin/pwsh" } +let s:linuxPreviewExe = { "versionName": "PowerShell Core Preview", "executablePath": "/usr/bin/pwsh-preview" } +let s:snapExe = { "versionName": "PowerShell Core Snap", "executablePath": "/snap/bin/pwsh" } +let s:snapPreviewExe = { "versionName": "PowerShell Core Preview Snap", "executablePath": "/snap/bin/pwsh-preview" } +let s:macOSExe = { "versionName": "PowerShell Core", "executablePath": "/usr/local/bin/pwsh" } +let s:macOSPreviewExe = { "versionName": "PowerShell Core Preview", "executablePath": "/usr/local/bin/pwsh-preview" } + +function! s:getAvailablePowerShellExecutables () + let paths = [] + if(s:is_win) + call add(paths, { + versionName: "Windows PowerShell", + executablePath: "C:\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" + + }) + + let psCoreInstallPath = "C:\\System32\\PowerShell\\" + if(isdirectory(psCoreInstallPath)) + let psCoreExePaths = split(glob(psCoreInstallPath . "**\\pwsh.exe"), "\n") + if(!empty(psCoreExePaths)) + call add(paths, { + versionName: "PowerShell Core", + executablePath: psCoreExePaths[0] + }) + endif + endif + + return paths + endif + + " macOS and Linux + let powershellExecutables = [ s:linuxExe, s:linuxPreviewExe, s:snapExe, s:snapPreviewExe ] + if(s:is_mac) + let powershellExecutables = [ s:macOSExe, s:macOSPreviewExe ] + endif + + for powershellExecutable in powershellExecutables + if(filereadable(powershellExecutable.executablePath)) + call add(paths, powershellExecutable) + endif + endfor + + return paths +endfunction + +function! coc#powershell#utils#switch_powershell_executable (...) + let s:choice = "" + if(!exists("a:1")) + let powershellExecutables = s:getAvailablePowerShellExecutables() + let index = 1 + let choiceStr = "" + while(index <= len(powershellExecutables)) + let choiceStr = choiceStr . index . " " . powershellExecutables[index - 1].versionName + if(index != len(powershellExecutables)) + let choiceStr = choiceStr . "\n" + endif + let index = index + 1 + endwhile + let s:choice = powershellExecutables[confirm("Which PowerShell executable would you like to use?", choiceStr) - 1].executablePath + else + let s:choice = a:1 + endif + + if(!executable(s:choice)) + throw "Executable not found: " . s:choice + endif + + if(g:pses_powershell_executable == s:choice) + echo "PowerShell executable already set to: " . s:choice + else + let g:pses_powershell_executable = s:choice + + echo "Restarting coc client to apply change..." + call coc#client#restart_all() + endif +endfunction From 87c0e8db8aa31a9ff30eee53aed64962cf546c09 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sat, 18 May 2019 08:50:28 -0700 Subject: [PATCH 2/3] add newline slashes --- autoload/coc/powershell/utils.vim | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/autoload/coc/powershell/utils.vim b/autoload/coc/powershell/utils.vim index 7764edf..3f7a2c0 100644 --- a/autoload/coc/powershell/utils.vim +++ b/autoload/coc/powershell/utils.vim @@ -14,19 +14,18 @@ function! s:getAvailablePowerShellExecutables () let paths = [] if(s:is_win) call add(paths, { - versionName: "Windows PowerShell", - executablePath: "C:\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" - - }) + \ versionName: "Windows PowerShell", + \ executablePath: "C:\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" + \ }) let psCoreInstallPath = "C:\\System32\\PowerShell\\" if(isdirectory(psCoreInstallPath)) let psCoreExePaths = split(glob(psCoreInstallPath . "**\\pwsh.exe"), "\n") if(!empty(psCoreExePaths)) call add(paths, { - versionName: "PowerShell Core", - executablePath: psCoreExePaths[0] - }) + \ versionName: "PowerShell Core", + \ executablePath: psCoreExePaths[0] + \ }) endif endif From f9918e9fa39e9274054a20221a18d34a03fb0d54 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Mon, 20 May 2019 09:36:51 -0700 Subject: [PATCH 3/3] handle confirm abort --- autoload/coc/powershell/utils.vim | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/autoload/coc/powershell/utils.vim b/autoload/coc/powershell/utils.vim index 3f7a2c0..b7177d8 100644 --- a/autoload/coc/powershell/utils.vim +++ b/autoload/coc/powershell/utils.vim @@ -60,7 +60,15 @@ function! coc#powershell#utils#switch_powershell_executable (...) endif let index = index + 1 endwhile - let s:choice = powershellExecutables[confirm("Which PowerShell executable would you like to use?", choiceStr) - 1].executablePath + + let selection = confirm("Which PowerShell executable would you like to use?", choiceStr) + if(selection == 0) + " confirm was aborted, do nothing. + return + endif + + " confirm is 1 indexed (0 being cancelled) so minus 1 for the 0 indexed arrays + let s:choice = powershellExecutables[selection - 1].executablePath else let s:choice = a:1 endif