nomnivore / ollama.nvim

A plugin for managing and integrating your ollama workflows in neovim.
MIT License
311 stars 22 forks source link

Replace `nvim_buf_set_text` #31

Open yamsergey opened 2 months ago

yamsergey commented 2 months ago

As described in this comment https://github.com/nomnivore/ollama.nvim/issues/29#issuecomment-2072461964, I've changed behavior of replace function by using nvim_buf_set_lines instead of nvim_buf_set_text.

As the first step I delete all selected lines:

vim.api.nvim_buf_set_lines(bufnr, start_line, end_line, false, {})

And then insert all lines into the document starting from the first selected line:

vim.api.nvim_buf_set_lines(bufnr, start_line, start_line, false, lines)

P.S. It also gives an opportunity for enhancement, after deletion we can insert the lines whenever user cursor is. But I will involve some calculations if the cursor below the deleted lines, not hard though.

P.S. P.S.

README changes related to LazyVim configuration example, I changed the GitHub path, because I'm lazy.

nomnivore commented 1 month ago

I'm playing with this PR on my local setup, and when running any sort of 'replace' action, it doesn't properly remove the last line. Does this happen for you as well?

I've run in to situations already where some of these neovim API's seem to behave differently for different people - possibly related to neovim versions, selection method, or something else. I'm on the latest nightly build.

Example - I start with this code:

Bun.serve({
  fetch(req: Request) {
    return new Response("Hello World!");
  },
}); // this line will persist

Select all text, run the modify code action with a query to use express.js, and I'm left with the resulting snippet:

import express from 'express';
import { Request, Response } from 'express';

const app = express(); // Initialize the Express application

app.get('/', (req: Request, res: Response) => {
  res.send('Hello World!');
});

// Start the server on port 3000
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

}); // this line will persist

It might be as simple as adjusting the index to be replaced, but for reasons stated above, I'd like to hear someone else's experience with it as well.