@@ -25,8 +25,8 @@ local logger = require("gp.logger")
25
25
--- @field description string
26
26
--- @field default ? string
27
27
--- @field max_occurrences ? number
28
- --- @field triggered fun ( params : gp.Macro_cmd_params , state : table ): boolean
29
- --- @field completion fun ( params : gp.Macro_cmd_params , state : table ): string[]
28
+ --- @field triggered fun ( params : gp.Macro_cmd_params ): boolean
29
+ --- @field completion fun ( params : gp.Macro_cmd_params ): string[]
30
30
--- @field parser fun ( params : gp.Macro_parser_result ): gp.Macro_parser_result
31
31
32
32
--- @param value string # string to hash
@@ -81,21 +81,20 @@ M.build_parser = function(macros)
81
81
end
82
82
83
83
--- @param macros gp.Macro[]
84
- --- @param state table
85
- --- @return fun ( arg_lead : string , cmd_line : string , cursor_pos : number ): string[]
86
- M .build_completion = function (macros , state )
84
+ --- @param raw boolean | nil # which function to return (completion or raw_completion)
85
+ --- @return fun ( arg_lead : string , cmd_line : string , cursor_pos : number ): string[] , boolean | nil
86
+ M .build_completion = function (macros , raw )
87
87
--- @type table<string , gp.Macro>
88
88
local map = {}
89
89
for _ , macro in pairs (macros ) do
90
90
map [macro .name ] = macro
91
- state [macro .name .. " _default" ] = macro .default
92
91
end
93
92
94
93
--- @param arg_lead string
95
94
--- @param cmd_line string
96
95
--- @param cursor_pos number
97
- --- @return string[]
98
- local function completion (arg_lead , cmd_line , cursor_pos )
96
+ --- @return string[] , boolean # returns suggestions and whether some macro was triggered
97
+ local function raw_completion (arg_lead , cmd_line , cursor_pos )
99
98
local cropped_line = cmd_line :sub (1 , cursor_pos )
100
99
101
100
--- @type gp.Macro_cmd_params
@@ -106,11 +105,13 @@ M.build_completion = function(macros, state)
106
105
cropped_line = cropped_line ,
107
106
}
108
107
108
+ cropped_line = " " .. cropped_line
109
+
109
110
local suggestions = {}
111
+ local triggered = false
110
112
111
113
logger .debug (" macro completion input: " .. vim .inspect ({
112
114
params = params ,
113
- state = state ,
114
115
}))
115
116
116
117
--- @type table<string , number>
@@ -122,8 +123,9 @@ M.build_completion = function(macros, state)
122
123
end
123
124
logger .debug (" macro completion candidates: " .. vim .inspect (candidates ))
124
125
125
- if cand and map [cand ] and map [cand ].triggered (params , state ) then
126
- suggestions = map [cand ].completion (params , state )
126
+ if cand and map [cand ] and map [cand ].triggered (params ) then
127
+ suggestions = map [cand ].completion (params )
128
+ triggered = true
127
129
elseif cropped_line :match (" %s$" ) or cropped_line :match (" %s@%S*$" ) then
128
130
for _ , c in pairs (macros ) do
129
131
if not candidates [c .name ] or candidates [c .name ] < c .max_occurrences then
@@ -133,7 +135,16 @@ M.build_completion = function(macros, state)
133
135
end
134
136
135
137
logger .debug (" macro completion suggestions: " .. vim .inspect (suggestions ))
136
- return vim .deepcopy (suggestions )
138
+ return vim .deepcopy (suggestions ), triggered
139
+ end
140
+
141
+ local completion = function (arg_lead , cmd_line , cursor_pos )
142
+ local suggestions , _ = raw_completion (arg_lead , cmd_line , cursor_pos )
143
+ return suggestions
144
+ end
145
+
146
+ if raw then
147
+ return raw_completion
137
148
end
138
149
139
150
return completion
0 commit comments