@@ -30,14 +30,41 @@ forms.get_form_boundaries = function()
30
30
31
31
return {
32
32
from = from ,
33
- to = to
33
+ to = to ,
34
+ bufnr = vim .api .nvim_get_current_buf ()
34
35
}
35
36
end
36
37
37
- forms .extract = function (bufnr )
38
- local coordinates = forms .get_form_boundaries ()
38
+ forms .motion = function (bufnr , mode )
39
+ local b_line , b_col , e_line , e_col , _
40
+
41
+ if mode == ' visual' then
42
+ _ , b_line , b_col = unpack (vim .api .nvim_call_function (" getpos" , {" v" }))
43
+ _ , e_line , e_col = unpack (vim .api .nvim_call_function (" getpos" , {" ." }))
44
+
45
+ b_col = b_col - 1
46
+ e_col = e_col - 1
47
+ elseif mode == ' block' then
48
+ b_line , b_col = unpack (vim .api .nvim_buf_get_mark (bufnr , ' <' ))
49
+ e_line , e_col = unpack (vim .api .nvim_buf_get_mark (bufnr , ' >' ))
50
+ else
51
+ b_line , b_col = unpack (vim .api .nvim_buf_get_mark (bufnr , ' [' ))
52
+ e_line , e_col = unpack (vim .api .nvim_buf_get_mark (bufnr , ' ]' ))
53
+ end
54
+
55
+ b_col = b_col + 1
56
+ e_col = e_col + 2
57
+
58
+ return {
59
+ from = {b_line , b_col },
60
+ to = {e_line , e_col },
61
+ bufnr = bufnr
62
+ }
63
+ end
39
64
40
- local lines = vim .api .nvim_buf_get_lines (bufnr , coordinates .from [1 ] - 1 , coordinates .to [1 ], 0 )
65
+
66
+ forms .extract = function (coordinates )
67
+ local lines = vim .api .nvim_buf_get_lines (coordinates .bufnr , coordinates .from [1 ] - 1 , coordinates .to [1 ], 0 )
41
68
42
69
if coordinates .from [2 ] ~= 0 then
43
70
lines [1 ] = string.sub (lines [1 ], coordinates .from [2 ])
@@ -54,26 +81,40 @@ forms.extract = function(bufnr)
54
81
return lines , coordinates
55
82
end
56
83
84
+ --- Extracts the form according to given motion (or visual mode)
85
+ -- @tparam string mode Motion mode or 'visual'
86
+ -- @tparam [opt] int bufnr Buffer number in neovim. Will take current if none given
87
+ -- @treturn string symbol under cursor
88
+ -- @treturn table coordinates {from = {row,col}, to = {row,col}, bufnr = 1}
89
+ forms .form_from_motion = function (mode , bufnr )
90
+ bufnr = bufnr or vim .api .nvim_get_current_buf ()
91
+ local coordinates = forms .motion (bufnr , mode )
92
+
93
+ return forms .extract (coordinates )
94
+
95
+ end
96
+
57
97
--- Extracts the innermost form under the cursor
58
98
-- @treturn string symbol under cursor
59
- -- @treturn table coordinates {from = {row,col}, to = {row,col}}
99
+ -- @treturn table coordinates {from = {row,col}, to = {row,col}, bufnr = 1 }
60
100
forms .form_under_cursor = function ()
61
- local cb = vim . api . nvim_get_current_buf ()
101
+ local coordinates = forms . get_form_boundaries ()
62
102
63
- return forms .extract (cb )
103
+ return forms .extract (coordinates )
64
104
end
65
105
66
106
--- Extracts the symbol under the cursor
67
107
-- @treturn string symbol under cursor
68
- -- @treturn table coordinates {from = {row,col}, to = {row,col}}
108
+ -- @treturn table coordinates {from = {row,col}, to = {row,col}, bofnr = 1 }
69
109
forms .symbol_under_cursor = function ()
70
110
local cw = vim .api .nvim_call_function (" expand" , {" <cword>" })
71
111
local from = vim .api .nvim_call_function (" searchpos" , {cw , " nc" })
72
112
local to = vim .api .nvim_call_function (" searchpos" , {cw , " nce" })
73
113
74
114
return cw , {
75
115
from = from ,
76
- to = to
116
+ to = to ,
117
+ bufnr = vim .api .nvim_get_current_buf ()
77
118
}
78
119
end
79
120
0 commit comments