Last active
September 6, 2023 12:35
-
-
Save linguini1/ee91b6d8c196cbd731d10a61447af6a3 to your computer and use it in GitHub Desktop.
Automatic f-string transformation for Neovim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Treesitter automatic Python format strings | |
vim.api.nvim_create_augroup("py-fstring", { clear = true }) | |
vim.api.nvim_create_autocmd("InsertCharPre", { | |
pattern = { "*.py" }, | |
group = "py-fstring", | |
--- @param opts AutoCmdCallbackOpts | |
--- @return nil | |
callback = function(opts) | |
-- Only run if f-string escape character is typed | |
if vim.v.char ~= "{" then return end | |
-- Get node and return early if not in a string | |
local node = vim.treesitter.get_node() | |
if not node then return end | |
if node:type() ~= "string" then node = node:parent() end | |
if not node or node:type() ~= "string" then return end | |
vim.print(node:type()) | |
local row, col, _, _ = vim.treesitter.get_node_range(node) | |
-- Return early if string is already a format string | |
local first_char = vim.api.nvim_buf_get_text(opts.buf, row, col, row, col + 1, {})[1] | |
vim.print("row " .. row .. " col " .. col) | |
vim.print("char: '" .. first_char .. "'") | |
if first_char == "f" then return end | |
-- Otherwise, make the string a format string | |
vim.api.nvim_input("<Esc>m'" .. row + 1 .. "gg" .. col + 1 .. "|if<Esc>`'la") | |
end, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment