antosha417/nvim-lsp-file-operations

github github
lsp
stars 434
issues 2
subscribers 5
forks 25
CREATED

UPDATED


nvim-lsp-file-operations

nvim-lsp-file-operations is a Neovim plugin that adds support for file operations using built-in LSP support.

This plugin works by subscribing to events emitted by either of these plugins (other integrations may be added if needed):

https://user-images.githubusercontent.com/14187674/211327507-39f21a74-0a43-43f0-ba3e-91109125286c.mp4


Features

Full implementation of all workspace.fileOperations for the current LSP spec:

If you have use cases for any other operations please open an issue.


Table of Contents


Installation

[!IMPORTANT] The order in which the plugins are loaded matters!

For example, neo-tree.nvim must load before nvim-lsp-file-operations for this to work, so nvim-lsp-file-operations depends on neo-tree.nvim, not the other way around!

require("pckr").add({
  "antosha417/nvim-lsp-file-operations",
  -- Uncomment whichever supported plugin(s) you use
  -- requires = {
  --   "nvim-tree/nvim-tree.lua",
  --   "nvim-neo-tree/neo-tree.nvim",
  --   "simonmclean/triptych.nvim"
  -- },
  config = function()
    require("lsp-file-operations").setup()
  end,
})
return {
  {
    "antosha417/nvim-lsp-file-operations",
    cmd = 'LFO',
    -- Uncomment whichever supported plugin(s) you use
    -- dependencies = {
    --   "nvim-tree/nvim-tree.lua",
    --   "nvim-neo-tree/neo-tree.nvim",
    --   "simonmclean/triptych.nvim"
    -- },
    config = function()
      require("lsp-file-operations").setup()
    end,
  },
}
vim.pack.add({
  { src = "https://github.com/antosha417/nvim-lsp-file-operations" },

  -- Uncomment whichever supported plugin(s) you use
  -- { src = "https://github.com/nvim-tree/nvim-tree.lua" },
  -- { src = "https://github.com/nvim-neo-tree/neo-tree.nvim" },
  -- { src = "https://github.com/simonmclean/triptych.nvim" },
})

require("lsp-file-operations").setup()

Setup

Simply call:

require("lsp-file-operations").setup()

The default options are:

{
  -- If `true`, allows the plugin to handle auto-saving after making LSP operations
  auto_save = false,

  -- Used to see debug logs, located at `vim.fn.stdpath("cache") .. "/lsp-file-operations.log"`
  debug = false,

  -- Select which file operations to enable
  operations = {
    didCreateFiles = true,
    didDeleteFiles = true,
    didRenameFiles = true,
    willCreateFiles = true,
    willDeleteFiles = true,
    willRenameFiles = true,
  },

  -- How long to wait (in milliseconds) for file rename information before cancelling
  timeout_ms = 10000,
}

LSP Capabilities

Some LSP servers also expect to be informed about the extended client capabilities. Follow any of the instructions below based on your Neovim version:

You can use vim.lsp.config() to set the global capabilities for every server:

-- Set global defaults for all servers
vim.lsp.config("*", {
  capabilities = vim.tbl_deep_extend(
    "force",
    vim.lsp.protocol.make_client_capabilities(),
    -- ANY OTHER CAPABILITIES. e.g. for `blink.cmp`, etc.
    require("lsp-file-operations").default_capabilities()
  )
})

If you use nvim-lspconfig you can configure the default client capabilities for all servers:

local lspconfig = require("lspconfig")

-- Set global defaults for all servers
lspconfig.util.default_config = vim.tbl_deep_extend(
  "force",
  lspconfig.util.default_config,
  {
    capabilities = vim.tbl_deep_extend(
      "force",
      vim.lsp.protocol.make_client_capabilities(),
      -- ANY OTHER CAPABILITIES. e.g. for `blink.cmp`, etc.
      require("lsp-file-operations").default_capabilities()
    )
  }
)

User Command

[!NOTE] The command comes with <Tab> completion aswell.

This plugin comes with the :LFO user command:

:LFO create <PATH/TO/FILE>                         " Create a file in the current workspace
:LFO delete <PATH/TO/FILE>                         " Delete a file in the current workspace
:LFO rename <PATH/TO/NEW_FILE>                     " Rename the current buffer's file to the new path
:LFO rename <PATH/TO/OLD_FILE> <PATH/TO/NEW_FILE>  " Rename an existing file path to a new path

API

You can also trigger any of these events manually from the main module. Credits to @Crysthamus as the implementation was based on theirs.

create()

You can now trigger a workspace event for file creation (willCreate and didCreate):

require("lsp-file-operations").create("<PATH/TO/FILE>")

delete()

You can now trigger a workspace event for file deletion (willDelete and didDelete):

require("lsp-file-operations").delete("<PATH/TO/FILE>")

rename()

You can now trigger a workspace event for file renaming (willRename and didRename):

-- Renames the current file
require("lsp-file-operations").rename(
  "<PATH/TO/NEW_FILE>" -- `new_name`
)

-- Renames the first file into the second one
require("lsp-file-operations").rename(
  "<PATH/TO/OLD_FILE>", -- `old_name`
  "<PATH/TO/NEW_FILE>" -- `new_name`
)

Contributing

PRs are always welcome.

This project uses StyLua. Please run stylua . before committing.


Credits


License

Apache-2.0