altermo / ultimate-autopair.nvim

A treesitter supported autopairing plugin with extensions, and much more
MIT License
503 stars 5 forks source link

how can I autopair < and > only for C header files? #96

Open 4rtemis-4rrow opened 3 months ago

4rtemis-4rrow commented 3 months ago

I searched high and low, went over all the docs, and though I don't know lua (I know, I have to learn it, my config is 1225 lines of lua, it's getting hard to maintain), I tried my best

internal_pairs={-- *ultimate-autopair-pairs-default-pairs*
    {'[',']',fly=true,dosuround=true,newline=true,space=true},
    {'(',')',fly=true,dosuround=true,newline=true,space=true},
    {'{','}',fly=true,dosuround=true,newline=true,space=true},
    {'"','"',suround=true,multiline=false},
    {"'","'",suround=true,cond=function(fn) return not fn.in_lisp() or fn.in_string() end,alpha=true,nft={'tex'},multiline=false},
    {'`','`',cond=function(fn) return not fn.in_lisp() or fn.in_string() end,nft={'tex'},multiline=false},
    {'``',"''",ft={'tex'}},
    {'```','```',newline=true,ft={'markdown'}},
    {'<!--','-->',ft={'markdown','html'},space=true},
    {'"""','"""',newline=true,ft={'python'}},
    {"'''","'''",newline=true,ft={'python'}},
    {'<','>',-- Condition for #include
    cond=function()
        local line = vim.api.nvim_get_current_line()
        return line:match("#include%s*$") ~= nil
    end,
    ft={'c','cpp'}
    },
},

I failed, if anyone can help me, that'd be greatly appreciated, I submit this here, for I don't know where else to ask

thank you for taking the time to read this, and have a great day, whoever you may be, wherever you may be

altermo commented 3 months ago

Wouldn't checking if the buffer name ends with .h work? (e.g. vim.api.nvim_buf_get_name(0):match('%.h$')

4rtemis-4rrow commented 3 months ago

no, a buffer ending with .h would be when you are editing the header file itself, I'm talking about when you are including a header file in a C project, eg #include <stdlib.h> and I can't do it to all C files in general, because using < and > for anything other than include statements would be annoying, so it has to be only when the line begins with #include

altermo commented 3 months ago

Then wouldn't current_line:match('^%s*#%s*include[^%w]') solve the problem? (notice that the pattern doesn't use $ at the end)