|
| 1 | +# Getting Started |
| 2 | +The PowerShell Editor Services project provides a Language Server Protocol (LSP) |
| 3 | +HTTP server that runs outside the editor. The server supplies rich editor |
| 4 | +functionality like code completion, syntax highlighting, and code annotation. |
| 5 | +This document will guide you through getting a minimal setup working with |
| 6 | +several editors. |
| 7 | + |
| 8 | +## Editors |
| 9 | +1. [Neovim](#neovim) |
| 10 | + |
| 11 | +## Neovim |
| 12 | + |
| 13 | +### Install the Server |
| 14 | +Download and extract the PowerShell Editor Services server from the |
| 15 | +[releases page](https://github.com/PowerShell/PowerShellEditorServices/releases) |
| 16 | +into a directory of your choice. Remember the path that you extract the |
| 17 | +project into. |
| 18 | +```powershell |
| 19 | +$DownloadUrl = 'https://github.com/PowerShell/PowerShellEditorServices/releases/latest/download/PowerShellEditorServices.zip'; |
| 20 | +$ZipPath = "$HOME/Desktop/PowerShellEditorServices.zip"; |
| 21 | +$InstallPath = "$HOME/Desktop/PowerShellEditorServices"; |
| 22 | +Invoke-WebRequest -Method 'GET' -Uri $DownloadUrl -OutFile $ZipPath; |
| 23 | +Expand-Archive -Path $ZipPath -DestinationPath $InstallPath; |
| 24 | +``` |
| 25 | + |
| 26 | +### Install Neovim's Quickstart LSP Configurations |
| 27 | +Neovim has a repository of quickstart LSP configurations for a number of |
| 28 | +languages, including PowerShell. Install the quickstart LSP configuration into |
| 29 | +one of the package directories inside `$XDG_CONFIG_HOME`. The path |
| 30 | +`$XDG_CONFIG_HOME` will vary depending on which operating system you are on: |
| 31 | + |
| 32 | +| OS | Path | |
| 33 | +| ---------- | -------------------------- | |
| 34 | +| Windows | `$HOME/AppData/local/nvim` | |
| 35 | +| *nix/macOS | `$HOME/.config/nvim` | |
| 36 | + |
| 37 | +The easiest way is to install the quickstart configuration is to clone the |
| 38 | +repository using git: |
| 39 | +```powershell |
| 40 | +git clone https://github.com/neovim/nvim-lspconfig.git "$HOME/AppData/local/nvim/pack/complete/start/nvim-lspconfig" |
| 41 | +``` |
| 42 | + |
| 43 | +Alternatively, you can extract the zip file into the same place: |
| 44 | +```powershell |
| 45 | +$DownloadUrl = 'https://github.com/neovim/nvim-lspconfig/archive/refs/heads/master.zip'; |
| 46 | +$ZipPath = "$HOME/AppData/local/nvim/nvim-lspconfig.zip"; |
| 47 | +$InstallPath = "$HOME/AppData/local/nvim/pack/complete/start/nvim-lspconfig"; |
| 48 | +Invoke-WebRequest -Method 'GET' Uri $DownloadUrl -OutFile $ZipPath; |
| 49 | +Expand-Archive -Path $ZipPath -DestinationPath $InstallPath; |
| 50 | +``` |
| 51 | + |
| 52 | +> NOTE: If the corresponding neovim configuration and package directories have |
| 53 | +> not been created yet, create them before installing the quickstart LSP |
| 54 | +> configuration repository. |
| 55 | +
|
| 56 | +### Configure the Server |
| 57 | + |
| 58 | +#### Setup Keybindings and Path Information |
| 59 | +Once the basic language configurations have been installed, add this to your |
| 60 | +`init.lua` located in `$XDG_CONFIG_HOME`: |
| 61 | +```lua |
| 62 | +local on_attach = function(client, bufnr) |
| 63 | + -- Enable completion triggered by <c-x><c-o> |
| 64 | + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') |
| 65 | + |
| 66 | + local bufopts = { noremap = true, silent = true, buffer = bufnr } |
| 67 | + vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts) |
| 68 | + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) |
| 69 | + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) |
| 70 | + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) |
| 71 | + vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) |
| 72 | + vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) |
| 73 | + vim.keymap.set('n', '<Leader>ca', vim.lsp.buf.code_action, bufopts) |
| 74 | + vim.keymap.set('n', '<Leader>f', function() vim.lsp.buf.format { async = true } end, bufopts) |
| 75 | + vim.keymap.set('n', '<Leader>rn', vim.lsp.buf.rename, bufopts) |
| 76 | + vim.keymap.set('n', '<Leader>td', vim.lsp.buf.type_definition, bufopts) |
| 77 | +end |
| 78 | + |
| 79 | +local home_directory = os.getenv('HOME') |
| 80 | +if home_directory == nil then |
| 81 | + home_directory = os.getenv('USERPROFILE') |
| 82 | +end |
| 83 | + |
| 84 | +-- The bundle_path is where PowerShell Editor Services was installed |
| 85 | +local bundle_path = home_directory .. '/Desktop/PowerShellEditorServices' |
| 86 | + |
| 87 | +require('lspconfig')['powershell_es'].setup { |
| 88 | + bundle_path = bundle_path, |
| 89 | + on_attach = on_attach |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +> NOTE: Be sure to set the bundle_path variable to the correct location, |
| 94 | +> otherwise the server will not know the path to start the server. |
| 95 | +
|
| 96 | +If you use an `init.vim` file, you may put the keybinding and path configuration |
| 97 | +in your `init.vim` with the `lua` heredoc syntax instead. |
| 98 | +```vim |
| 99 | +lua << EOF |
| 100 | +-- lua keybindings and path configuration here |
| 101 | +EOF |
| 102 | +``` |
| 103 | + |
| 104 | +#### Configure Additional Settings |
| 105 | +To further configure the server, you can supply settings to the setup table. |
| 106 | +For example, you can set the code formatting preset to one true brace style |
| 107 | +(OTBS). |
| 108 | +```lua |
| 109 | +require('lspconfig')['powershell_es'].setup { |
| 110 | + bundle_path = bundle_path, |
| 111 | + on_attach = on_attach, |
| 112 | + settings = { powershell = { codeFormatting = { Preset = 'OTBS' } } } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +You can also set the bundled PSScriptAnalyzer's custom rule path like so: |
| 117 | +```lua |
| 118 | +local custom_settings_path = home_directory .. '/PSScriptAnalyzerSettings.psd1' |
| 119 | +require('lspconfig')['powershell_es'].setup { |
| 120 | + bundle_path = bundle_path, |
| 121 | + on_attach = on_attach, |
| 122 | + settings = { powershell = { scriptAnalysis = { settingsPath = custom_settings_path } } } |
| 123 | +} |
| 124 | +``` |
0 commit comments