Coder Social home page Coder Social logo

copilotchat.nvim's Introduction

Copilot Chat for Neovim

All Contributors

Note

A new command, CopilotChatInPlace has been introduced. It functions like the ChatGPT plugin. Please run ":UpdateRemotePlugins" command and restart Neovim before starting a chat with Copilot. To stay updated on our roadmap, please join our Discord community.

Authentication

It will prompt you with instructions on your first start. If you already have Copilot.vim or Copilot.lua, it will work automatically.

Installation

Lazy.nvim

  1. pip install python-dotenv requests pynvim==0.5.0 prompt-toolkit
  2. pip install tiktoken (optional for displaying prompt token counts)
  3. Put it in your lazy setup
return {
  {
    "jellydn/CopilotChat.nvim",
    dependencies = { "zbirenbaum/copilot.lua" }, -- Or { "github/copilot.vim" }
    opts = {
      show_help = "yes", -- Show help text for CopilotChatInPlace, default: yes
      debug = false, -- Enable or disable debug mode, the log file will be in ~/.local/state/nvim/CopilotChat.nvim.log
    },
    build = function()
      vim.notify("Please update the remote plugins by running ':UpdateRemotePlugins', then restart Neovim.")
    end,
    event = "VeryLazy",
    keys = {
      { "<leader>cce", "<cmd>CopilotChatExplain<cr>", desc = "CopilotChat - Explain code" },
      { "<leader>cct", "<cmd>CopilotChatTests<cr>", desc = "CopilotChat - Generate tests" },
      {
        "<leader>ccv",
        ":CopilotChatVisual",
        mode = "x",
        desc = "CopilotChat - Open in vertical split",
      },
      {
        "<leader>ccx",
        ":CopilotChatInPlace<cr>",
        mode = "x",
        desc = "CopilotChat - Run in-place code",
      },
    },
  },
}
  1. Run command :UpdateRemotePlugins, then inspect the file ~/.local/share/nvim/rplugin.vim for additional details. You will notice that the commands have been registered.

For example:

" python3 plugins
call remote#host#RegisterPlugin('python3', '/Users/huynhdung/.local/share/nvim/lazy/CopilotChat.nvim/rplugin/python3/copilot-plugin.py', [
      \ {'sync': v:false, 'name': 'CopilotChat', 'type': 'command', 'opts': {'nargs': '1'}},
      \ {'sync': v:false, 'name': 'CopilotChatVisual', 'type': 'command', 'opts': {'nargs': '1', 'range': ''}},
      \ {'sync': v:false, 'name': 'CopilotChatInPlace', 'type': 'command', 'opts': {'nargs': '*', 'range': ''}},
      \ {'sync': v:false, 'name': 'CopilotChatAutocmd', 'type': 'command', 'opts': {'nargs': '*'}},
      \ {'sync': v:false, 'name': 'CopilotChatMapping', 'type': 'command', 'opts': {'nargs': '*'}},
     \ ])
  1. Restart neovim

Manual

  1. Put the files in the right place
$ git clone https://github.com/jellydn/CopilotChat.nvim
$ cd CopilotChat.nvim
$ cp -r --backup=nil rplugin ~/.config/nvim/
  1. Install dependencies
$ pip install -r requirements.txt
  1. Open up Neovim and run :UpdateRemotePlugins
  2. Restart Neovim

Usage

Configuration

You have the ability to tailor this plugin to your specific needs using the configuration options outlined below:

{
  debug = false, -- Enable or disable debug mode
  show_help = 'yes', -- Show help text for CopilotChatInPlace
  prompts = { -- Set dynamic prompts for CopilotChat commands
    Explain = 'Explain how it works.',
    Tests = 'Briefly explain how the selected code works, then generate unit tests.',
  }
}

You have the capability to expand the prompts to create more versatile commands:

return {
    "jellydn/CopilotChat.nvim",
    opts = {
      debug = true,
      show_help = "yes",
      prompts = {
        Explain = "Explain how it works.",
        Review = "Review the following code and provide concise suggestions.",
        Tests = "Briefly explain how the selected code works, then generate unit tests.",
        Refactor = "Refactor the code to improve clarity and readability.",
      },
    },
    build = function()
      vim.notify("Please update the remote plugins by running ':UpdateRemotePlugins', then restart Neovim.")
    end,
    event = "VeryLazy",
    keys = {
      { "<leader>cce", "<cmd>CopilotChatExplain<cr>", desc = "CopilotChat - Explain code" },
      { "<leader>cct", "<cmd>CopilotChatTests<cr>", desc = "CopilotChat - Generate tests" },
      { "<leader>ccr", "<cmd>CopilotChatReview<cr>", desc = "CopilotChat - Review code" },
      { "<leader>ccR", "<cmd>CopilotChatRefactor<cr>", desc = "CopilotChat - Refactor code" },
    }
}

For further reference, you can view @jellydn's configuration.

Chat with Github Copilot

  1. Copy some code into the unnamed register using the y command.
  2. Run the command :CopilotChat followed by your question. For example, :CopilotChat What does this code do?

Chat Demo

Code Explanation

  1. Copy some code into the unnamed register using the y command.
  2. Run the command :CopilotChatExplain.

Explain Code Demo

Generate Tests

  1. Copy some code into the unnamed register using the y command.
  2. Run the command :CopilotChatTests.

Generate tests

Token count & Fold with visual mode

  1. Select some code using visual mode.
  2. Run the command :CopilotChatVisual with your question.

Fold Demo

In-place Chat Popup

  1. Select some code using visual mode.
  2. Run the command :CopilotChatInPlace and type your prompt. For example, What does this code do?
  3. Press Enter to send your question to Github Copilot.
  4. Press q to quit. There is help text at the bottom of the screen. You can also press ? to toggle the help text.

In-place Demo

Tips

Debugging with :messages and :CopilotChatDebugInfo

If you encounter any issues, you can run the command :messages to inspect the log. You can also run the command :CopilotChatDebugInfo to inspect the debug information.

Debug Info

How to setup with which-key.nvim

A special thanks to @ecosse3 for the configuration of which-key.

  {
    "jellydn/CopilotChat.nvim",
    event = "VeryLazy",
    opts = {
      prompts = {
        Explain = "Explain how it works.",
        Review = "Review the following code and provide concise suggestions.",
        Tests = "Briefly explain how the selected code works, then generate unit tests.",
        Refactor = "Refactor the code to improve clarity and readability.",
      },
    },
    build = function()
      vim.notify("Please update the remote plugins by running ':UpdateRemotePlugins', then restart Neovim.")
    end,
    config = function()
      local present, wk = pcall(require, "which-key")
      if not present then
        return
      end

      wk.register({
        c = {
          c = {
            name = "Copilot Chat",
          }
        }
      }, {
        mode = "n",
        prefix = "<leader>",
        silent = true,
        noremap = true,
        nowait = false,
      })
    end,
    keys = {
      { "<leader>ccc", ":CopilotChat ",                desc = "CopilotChat - Prompt" },
      { "<leader>cce", ":CopilotChatExplain ",         desc = "CopilotChat - Explain code" },
      { "<leader>cct", "<cmd>CopilotChatTests<cr>",    desc = "CopilotChat - Generate tests" },
      { "<leader>ccr", "<cmd>CopilotChatReview<cr>",   desc = "CopilotChat - Review code" },
      { "<leader>ccR", "<cmd>CopilotChatRefactor<cr>", desc = "CopilotChat - Refactor code" },
    }
  },

Create a simple input for CopilotChat

Follow the example below to create a simple input for CopilotChat.

-- Create input for CopilotChat
  {
    "<leader>cci",
    function()
      local input = vim.fn.input("Ask Copilot: ")
      if input ~= "" then
        vim.cmd("CopilotChat " .. input)
      end
    end,
    desc = "CopilotChat - Ask input",
  },

Roadmap

  • Translation to pure Lua
  • Tokenizer
  • Use vector encodings to automatically select code
  • Sub commands - See issue #5

Development

Installing Pre-commit Tool

For development, you can use the provided Makefile command to install the pre-commit tool:

make install-pre-commit

This will install the pre-commit tool and the pre-commit hooks.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

gptlang
gptlang

💻 📖
Dung Duc Huynh (Kaka)
Dung Duc Huynh (Kaka)

💻 📖
Ahmed Haracic
Ahmed Haracic

💻
Trí Thiện Nguyễn
Trí Thiện Nguyễn

💻
He Zhizhou
He Zhizhou

💻
Guruprakash Rajakkannu
Guruprakash Rajakkannu

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

copilotchat.nvim's People

Contributors

jellydn avatar github-actions[bot] avatar gptlang avatar allcontributors[bot] avatar example123 avatar renovate[bot] avatar qoobes avatar cassius0924 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.