Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace nvim_buf_set_text #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

yamsergey
Copy link

@yamsergey yamsergey commented Apr 23, 2024

As described in this comment #29 (comment), 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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants