@@ -9,6 +9,7 @@ local Loader = require("lazy.core.loader")
9
9
--- @field noremap ? boolean
10
10
--- @field remap ? boolean
11
11
--- @field expr ? boolean
12
+ --- @field ft ? string | string[]
12
13
--- @field id string
13
14
14
15
--- @class LazyKeysHandler : LazyHandler
53
54
function M .opts (keys )
54
55
local opts = {}
55
56
for k , v in pairs (keys ) do
56
- if type (k ) ~= " number" and k ~= " mode" and k ~= " id" then
57
+ if type (k ) ~= " number" and k ~= " mode" and k ~= " id" and k ~= " ft " then
57
58
opts [k ] = v
58
59
end
59
60
end
64
65
function M :_add (keys )
65
66
local lhs = keys [1 ]
66
67
local opts = M .opts (keys )
67
- vim .keymap .set (keys .mode , lhs , function ()
68
- local plugins = self .active [keys .id ]
69
68
70
- -- always delete the mapping immediately to prevent recursive mappings
71
- self :_del (keys )
72
- self .active [keys .id ] = nil
69
+ --- @param buf ? number
70
+ local function add (buf )
71
+ vim .keymap .set (keys .mode , lhs , function ()
72
+ local plugins = self .active [keys .id ]
73
73
74
- Util . track ({ keys = lhs })
75
- Loader . load ( plugins , { keys = lhs } )
76
- Util . track ()
74
+ -- always delete the mapping immediately to prevent recursive mappings
75
+ self : _del ( keys , buf )
76
+ self . active [ keys . id ] = nil
77
77
78
- local feed = vim .api .nvim_replace_termcodes (" <Ignore>" .. lhs , true , true , true )
79
- -- insert instead of append the lhs
80
- vim .api .nvim_feedkeys (feed , " i" , false )
81
- end , {
82
- desc = opts .desc ,
83
- nowait = opts .nowait ,
84
- -- we do not return anything, but this is still needed to make operator pending mappings work
85
- expr = true ,
86
- })
78
+ if plugins then
79
+ Util .track ({ keys = lhs })
80
+ Loader .load (plugins , { keys = lhs })
81
+ Util .track ()
82
+ end
83
+
84
+ local feed = vim .api .nvim_replace_termcodes (" <Ignore>" .. lhs , true , true , true )
85
+ -- insert instead of append the lhs
86
+ vim .api .nvim_feedkeys (feed , " i" , false )
87
+ end , {
88
+ desc = opts .desc ,
89
+ nowait = opts .nowait ,
90
+ -- we do not return anything, but this is still needed to make operator pending mappings work
91
+ expr = true ,
92
+ buffer = buf ,
93
+ })
94
+ end
95
+
96
+ if keys .ft then
97
+ vim .api .nvim_create_autocmd (" FileType" , {
98
+ pattern = keys .ft ,
99
+ callback = function (event )
100
+ if self .active [keys .id ] then
101
+ add (event .buf )
102
+ else
103
+ -- Only create the mapping if its managed by lazy
104
+ -- otherwise the plugin is supposed to manage it
105
+ if keys [2 ] then
106
+ self :_del (keys , event .buf )
107
+ end
108
+ end
109
+ end ,
110
+ })
111
+ else
112
+ add ()
113
+ end
87
114
end
88
115
89
116
--- @param keys LazyKeys
90
- function M :_del (keys )
91
- pcall (vim .keymap .del , keys .mode , keys [1 ])
117
+ --- @param buf number ?
118
+ function M :_del (keys , buf )
119
+ pcall (vim .keymap .del , keys .mode , keys [1 ], { buffer = buf })
92
120
if keys [2 ] then
93
- vim .keymap .set (keys .mode , keys [1 ], keys [2 ], M .opts (keys ))
121
+ local opts = M .opts (keys )
122
+ opts .buffer = buf
123
+ vim .keymap .set (keys .mode , keys [1 ], keys [2 ], opts )
94
124
end
95
125
end
96
126
0 commit comments