Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Powershell.nvim

GitHub Workflow Status Lua

This extension provides rich PowerShell language support for Neovim. Now you can write and debug PowerShell scripts using the excellent IDE-like interface that Neovim provides.

Features

Requirements

Installation

use {
    "TheLeoP/powershell.nvim"
}
{
    "TheLeoP/powershell.nvim",
    ---@type powershell.user_config
    opts = {
      bundle_path = 'path/to/your/bundle_path/'
    }
}
Plug 'TheLeoP/powershell.nvim'

Configuration

The only required field for the plugin to work is bundle_path, this has to be the path of where Powershell Editor Services is installed.

For example, if you are using mason with default settings, you would have to do something like the following:

require('powershell').setup({
  bundle_path = vim.fn.stdpath "data" .. "/mason/packages/powershell-editor-services",
})

Default configuration

-- This is the default configuration
require('powershell').setup({
  shell = "pwsh",
  bundle_path = "",
  feature_flags = {},
  lsp_log_level = "Warning",
  capabilities = vim.lsp.protocol.make_client_capabilities(),
  init_options = vim.empty_dict() --[[@as table]],
  settings = vim.empty_dict() --[[@as table]],
  handlers = base_handlers, -- see lua/powershell/handlers.lua
  commands = base_commands, -- see lua/powershell/commands.lua
  root_dir = function(buf)
    local current_file_dir = fs.dirname(api.nvim_buf_get_name(buf))
    return fs.dirname(fs.find({ ".git" }, { upward = true, path = current_file_dir })[1]) or current_file_dir
  end,
})

Lua API

Toggle Powershell Extension Terminal

require('powershell').toggle_term()

To create a keymap only for powershell files, put the following in your config.

-- this should go in `~/.config/nvim/ftplugin/ps1.lua` or inside a `FileType` autocmd
vim.keymap.set("n", "<leader>lt", function() require("powershell").toggle_term() end)

If you also want to create the toggle keymap inside the Powershell Extension Terminal buffer, put the following in your config.

-- this should go anywhere in your config. For example in your `init.lua` or next to the `require('powershell').setup()` call
local augroup = vim.api.nvim_create_augroup("personal-powershell", { clear = true })
vim.api.nvim_create_autocmd("User", {
  group = augroup,
  pattern = "powershell.nvim-term",
  callback = function(opts)
    vim.keymap.set("n", "<leader>lt", function()
      require("powershell").toggle_term()
    end, { buffer = opts.data.buf })
  end,
})

Eval expression on Powershell Extension Terminal

require('powershell').eval_operator() is a Neovim operator (checkout :h operator for more information) just like the builtin d or c. This means that it can be used in both normal (waits for a motion or textobject) or visual mode (operates on current visual selection)

To create a keymap only for powershell files, put the following in your config.

-- this should go in `~/.config/nvim/ftplugin/ps1.lua` or inside a `FileType` autocmd
-- IMPORTANT: don't forget to create the keymap with `expr = true` and return the result of `require('powershell').eval_operator()`
vim.keymap.set({ "n", "x" }, "g=", function() return require("powershell").eval_operator() end, { expr = true })

-- to make `g==` operate in the current like just like builtin `dd` or `cc`
vim.keymap.set({ "n" }, "g==", function() return require("powershell").eval_operator() .. "_" end, { expr = true })

Toggle Powershell Debug Terminal

require('powershell').toggle_debug_term()

To create a keymap only for powershell files, put the following in your config.

-- this should go in `~/.config/nvim/ftplugin/ps1.lua` or inside a `FileType` autocmd
vim.keymap.set("n", "<leader>ld", function() require("powershell").toggle_debug_term() end)

If you also want to create the toggle keymap inside the Powershell Debug Terminal buffer, put the following in your config.

-- this should go anywhere in your config. For example in your `init.lua` or next to the `require('powershell').setup()` call
local augroup = vim.api.nvim_create_augroup("personal-powershell", { clear = true })
vim.api.nvim_create_autocmd("User", {
  group = augroup,
  pattern = "powershell.nvim-debug_term",
  callback = function(opts)
    vim.keymap.set("n", "<leader>ld", function()
      require("powershell").toggle_term()
    end, { buffer = opts.data.buf })
  end,
})

DAP

By default, the plugin includes the following nvim-dap configurations:

dap.configurations.ps1 = {
  {
    name = "PowerShell: Launch Current File",
    type = "ps1",
    request = "launch",
    script = "${file}",
  },
  {
    name = "PowerShell: Launch Script",
    type = "ps1",
    request = "launch",
    script = function()
      return coroutine.create(function(co)
        vim.ui.input({
          prompt = 'Enter path or command to execute, for example: "${workspaceFolder}/src/foo.ps1" or "Invoke-Pester"',
          completion = "file",
        }, function(selected) coroutine.resume(co, selected) end)
      end)
    end,
  },
  {
    name = "PowerShell: Attach to PowerShell Host Process",
    type = "ps1",
    request = "attach",
    processId = "${command:pickProcess}",
  },
}

To use them, simply call require('dap').continue() inside of a ps1 file.

TODO

  • Tests

About

First class powershell editor integration into Neovim

Topics

Resources

Stars

82 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages