David-Kunz / gen.nvim

Neovim plugin to generate text using LLMs with customizable prompts
The Unlicense
977 stars 62 forks source link

substitute \"" with \\\" for cmd #56

Closed DanielEliasib closed 5 months ago

DanielEliasib commented 6 months ago

This change fixes #4 for double quotes on windows. On windows vim.fn.shellescape() by default assumes the resulting command is gonna be called with cmd, so it substitutes double quotes with a pair of double quotes (" -> ""), and vim.fn.jobstart() uses vim.o.shell to run commands which by default is 'cmd.exe'. The problem comes from vim.fn.json_encode() that already tries to espace double quotes as \" so after vim.fn.shellescape() there would be a \"" for every " in the command body. This fix basically substitutes \"" for \\\" so that the final command is valid and contains any double quotes in the prompt correctly escaped.


Original code:

vim.opt.signcolumn = "yes"

Windows before fix:

curl --silent --no-buffer -X POST http://localhost:11434/api/generate -d "{""model"": ""zephyr"", ""stream"": true, ""prompt"": ""Summarize the following text:\nvim.opt.signcolumn = \""yes\""""}"

Windows after fix:

curl --silent --no-buffer -X POST http://localhost:11434/api/generate -d "{""model"": ""zephyr"", ""stream"": true, ""prompt"": ""Summarize the following text:\nvim.opt.signcolumn = \\\"yes\\\"""}"

Linux:

curl --silent --no-buffer -X POST http://localhost:11434/api/generate -d '{"model": "zephyr", "stream": true, "prompt": "Summarize the following text:\nvim.opt.signcolumn = \"yes\""}'
David-Kunz commented 5 months ago

Thank you, @DanielEliasib , also for the nice explanation and outputs on various systems!