Skip to content

Commit

Permalink
Merge branch 'python-f-string'
Browse files Browse the repository at this point in the history
  • Loading branch information
zztrieuzz committed Sep 5, 2023
2 parents a52fc6e + 022a2b8 commit 3549355
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
1 change: 0 additions & 1 deletion lua/nvim-autopairs/conds.lua
Expand Up @@ -115,7 +115,6 @@ cond.not_after_text = function(text)
end

cond.not_before_regex = function(regex, length)
log.debug(length)
length = length or 1
if length < 0 then length = nil end
---@param opts CondOpts
Expand Down
11 changes: 10 additions & 1 deletion lua/nvim-autopairs/rules/basic.lua
@@ -1,5 +1,6 @@
local Rule = require("nvim-autopairs.rule")
local cond = require("nvim-autopairs.conds")
local utils = require('nvim-autopairs.utils')

local function quote_creator(opt)
local quote = function(...)
Expand Down Expand Up @@ -40,7 +41,15 @@ local function setup(opt)
Rule("```.*$", "```", { "markdown", "vimwiki", "rmarkdown", "rmd", "pandoc" }):only_cr():use_regex(true),
Rule('"""', '"""', { "python", "elixir", "julia", "kotlin" }):with_pair(cond.not_before_char('"', 3)),
Rule("'''", "'''", { "python" }):with_pair(cond.not_before_char('"', 3)),
quote("'", "'", "-rust"):with_pair(cond.not_before_regex("%w")),
quote("'", "'", "-rust")
:with_pair(function(opts)
-- python literals string
local str = utils.text_sub_char(opts.line, opts.col - 1, 1)
if vim.bo.filetype == 'python' and str:match("[frbuFRBU]") then
return true
end
end)
:with_pair(cond.not_before_regex("%w")),
quote("'", "'", "rust"):with_pair(cond.not_before_regex("[%w<&]")):with_pair(cond.not_after_text(">")),
quote("`", "`"),
quote('"', '"', "-vim"),
Expand Down
6 changes: 4 additions & 2 deletions lua/nvim-autopairs/utils.lua
Expand Up @@ -68,8 +68,10 @@ M.is_in_quotes = function (line, pos, quote_type)
result == false and
M.is_quote(char) and
(not quote_type or char == quote_type) and
--a single quote with a word before is not count
not (char == "'" and prev_char:match("%w"))
--a single quote with a word before is not count unless it is a
-- prefixed string in python (e.g. f'string {with_brackets}')
not (char == "'" and prev_char:match(vim.bo.filetype == "python"
and "[^frbuFRBU]" or "%w"))
then
last_char = quote_type or char
result = true
Expand Down
35 changes: 35 additions & 0 deletions tests/nvim-autopairs_spec.lua
Expand Up @@ -766,6 +766,41 @@ local data = {
before = [[aa| 'aa]],
after = [[aa"|" 'aa]]
},
{
name = "81 add closing single quote for python prefixed string",
filetype = "python",
key = [[']],
before = [[print(f|)]],
after = [[print(f'|')]]
},
{
name = "82 add closing single quote for capital python prefixed string",
filetype = "python",
key = [[']],
before = [[print(B|)]],
after = [[print(B'|')]]
},
{
name = "83 don't add closing single quote for random prefix string",
filetype = "python",
key = [[']],
before = [[print(s|)]],
after = [[print(s'|)]]
},
{
name = "84 don't add closing single quote for other filetype prefixed string",
filetype = "lua",
key = [[']],
before = [[print(f|)]],
after = [[print(f'|)]]
},
{
name = "85 allow brackets in prefixed python single quote string",
filetype = "python",
key = [[{]],
before = [[print(f'|')]],
after = [[print(f'{|}')]]
},
}

local run_data = _G.Test_filter(data)
Expand Down

0 comments on commit 3549355

Please sign in to comment.