Skip to content

KyleKing/patch_it.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

patch_it.nvim

A lightweight Neovim plugin for applying LLM-generated patches with fuzzy matching.

Features

  • Apply simple diff patches from buffer to target file
  • Fuzzy matching with whitespace tolerance
  • Handles interleaved additions/removals
  • Forgiving format: accepts patches with or without space-prefixed context lines
  • Preview mode for dry-run validation

Installation

lazy.nvim

{
  'kyleking/patch_it.nvim',
  config = function()
    require('patch_it').setup()
  end,
}

mini.deps

MiniDeps.add('kyleking/patch_it.nvim')
require('patch_it').setup()

packer.nvim

use {
  'kyleking/patch_it.nvim',
  config = function()
    require('patch_it').setup()
  end,
}

vim-plug

Plug 'kyleking/patch_it.nvim'

" In your init.lua or after/plugin:
lua require('patch_it').setup()

Configuration

require('patch_it').setup({
  fuzzy_lines = 50,        -- Reserved for future hint-based search
  whitespace_trim = true,  -- Normalize whitespace when matching (default: true)
})

Usage

Command

:PatchApply path/to/target.lua

Applies the current buffer as a patch to the specified target file.

Lua API

local patch_it = require('patch_it')

-- Apply patch string to target file
patch_it.apply(patch_string, target_path)

-- Apply patch string with preview (dry-run)
patch_it.apply(patch_string, target_path, { preview = true })

-- Apply current buffer as patch to target file
patch_it.apply_buffer(target_path)

-- Preview what would be applied
patch_it.apply_buffer(target_path, { preview = true })

Typical Workflow

  1. LLM outputs a patch in chat/response
  2. Yank patch into a scratch buffer
  3. Run :PatchApply path/to/target.lua
  4. Plugin fuzzy-matches context, applies changes
  5. If wrong, hit u to undo

Patch Format

The plugin accepts patches in a simple diff format (as commonly produced by LLMs like CodeRabbit):

+import { useMemo } from 'react';
 import { dark } from '@clerk/themes';

-import { useTheme } from 'src/common/ThemeToggle';
+import { useTheme } from '#common/ThemeToggle.js';

Format Rules

  • +content - Line to add (no space after +)
  • -content - Line to remove (no space after -)
  • content - Context line (single space prefix)
  • content - Context line without prefix (forgiving mode)
  • Empty lines are treated as context
  • --- a/file and +++ b/file headers are warned and ignored
  • @@ ... @@ hunk headers are skipped

Limitations

  • v1 supports single hunk, single file
  • Files with lines starting with literal + or - (e.g., markdown lists) need space-prefixed context in the patch

API Response

The apply() function returns a result table:

-- Success
{
  success = true,
  buf = <buffer_number>,
  start_line = 10,      -- 1-indexed
  lines_removed = 3,
  lines_added = 5,
}

-- Preview success
{
  success = true,
  preview = true,
  buf = <buffer_number>,
  match_line = 10,
  original = { ... },      -- Lines that would be removed
  replacement = { ... },   -- Lines that would be added
}

-- Failure
{
  success = false,
  error = "No match found for original lines",
}

Running Tests

nvim --headless -u NONE --cmd "set rtp+=." -l tests/patch_it_spec.lua

License

MIT

About

Apply Code Rabbit diffs in nvim

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages