@@ -27,16 +27,16 @@ M.get_bounded_query = function(query, lang, startR, stopR)
2727 table.insert (out , node )
2828 end )
2929 end
30- return out ;
30+ return out
3131end
3232
3333local refactor_constants = {
3434 lua = {
3535 scope = {
3636 [" function" ] = true ,
37- [" function_definition" ] = true
38- }
39- }
37+ [" function_definition" ] = true ,
38+ },
39+ },
4040}
4141
4242-- determines if a contains node b.
@@ -48,8 +48,8 @@ function M.node_contains(a, b)
4848 end
4949
5050 local start_row , start_col , end_row , end_col = b :range ()
51- return ts_utils .is_in_node_range (a , start_row , start_col ) and
52- ts_utils .is_in_node_range (a , end_row , end_col )
51+ return ts_utils .is_in_node_range (a , start_row , start_col )
52+ and ts_utils .is_in_node_range (a , end_row , end_col )
5353end
5454
5555-- determines if a node exists within a range. Imagine a range selection
5959-- @param a the containing node
6060-- @param b the node to be contained
6161M .range_contains_node = function (node , start_row , start_col , end_row , end_col )
62- local node_start_row , node_start_col , node_end_row , node_end_col = node :range ()
62+ local node_start_row , node_start_col , node_end_row , node_end_col =
63+ node :range ()
6364
6465 -- There are five possible conditions
6566 -- 1. node start/end row are contained exclusively within the range.
@@ -77,11 +78,10 @@ M.range_contains_node = function(node, start_row, start_col, end_row, end_col)
7778 if start_row < node_start_row and end_row > node_end_row then
7879 return true
7980 elseif start_row == end_row then
80- return start_row == node_start_row and
81- end_row == node_end_row and
82- start_col <= node_start_col and
83- end_col >= node_end_col
84-
81+ return start_row == node_start_row
82+ and end_row == node_end_row
83+ and start_col <= node_start_col
84+ and end_col >= node_end_col
8585 elseif start_row == node_start_row and start_row == node_end_row then
8686 return start_col <= node_start_col
8787 elseif end_row == node_start_row and end_row == node_end_row then
@@ -93,23 +93,27 @@ M.range_contains_node = function(node, start_row, start_col, end_row, end_col)
9393 return false
9494end
9595
96- M .get_scope_over_selection = function (root , start_line , start_col , end_line , end_col , lang )
97- local start_scope = M .get_scope (root , start_line , start_col , lang )
98- local end_scope = M .get_scope (root , end_line , end_col , lang )
96+ M .get_scope_over_selection =
97+ function (root , start_line , start_col , end_line , end_col , lang )
98+ local start_scope = M .get_scope (root , start_line , start_col , lang )
99+ local end_scope = M .get_scope (root , end_line , end_col , lang )
99100
100- if start_scope ~= end_scope then
101- error (" Selection spans over two scopes, cannot determine scope" )
102- end
101+ if start_scope ~= end_scope then
102+ error (" Selection spans over two scopes, cannot determine scope" )
103+ end
103104
104- return start_scope
105- end
105+ return start_scope
106+ end
106107
107108M .get_scope = function (root , line , col , lang )
108109 local function_scopes = {}
109110 local query = vim .treesitter .get_query (lang , " locals" )
110111
111112 for id , n , _ in query :iter_captures (root , 0 , 0 , - 1 ) do
112- if query .captures [id ] == " scope" and refactor_constants [lang ].scope [n :type ()] then
113+ if
114+ query .captures [id ] == " scope"
115+ and refactor_constants [lang ].scope [n :type ()]
116+ then
113117 table.insert (function_scopes , n )
114118 end
115119 end
@@ -120,9 +124,10 @@ M.get_scope = function(root, line, col, lang)
120124 -- should a scope that contains another scope but terminates at the
121125 -- same point be the outer or inner? Should potentially be considered
122126 -- a list of scopes...
123- if ts_utils .is_in_node_range (scope , line , col ) and
124- (out == nil or M .node_contains (out , scope )) then
125-
127+ if
128+ ts_utils .is_in_node_range (scope , line , col )
129+ and (out == nil or M .node_contains (out , scope ))
130+ then
126131 out = scope
127132 end
128133 end
133138local function get_refactoring_query (lang )
134139 local query = vim .treesitter .get_query (lang , " refactoring" )
135140 if not query then
136- error (" refactoring not supported in this language. Please provide a queries/<lang>/refactoring.scm" )
141+ error (
142+ " refactoring not supported in this language. Please provide a queries/<lang>/refactoring.scm"
143+ )
137144 end
138145 return query
139146end
@@ -142,7 +149,10 @@ local function pluck_by_capture(scope, lang, query, capture_name)
142149 local local_defs = {}
143150 local root = M .get_root (lang )
144151 for id , node , _ in query :iter_captures (root , 0 , 0 , - 1 ) do
145- if query .captures [id ] == capture_name and M .node_contains (scope , node ) then
152+ if
153+ query .captures [id ] == capture_name
154+ and M .node_contains (scope , node )
155+ then
146156 table.insert (local_defs , node )
147157 end
148158 end
@@ -151,15 +161,30 @@ local function pluck_by_capture(scope, lang, query, capture_name)
151161end
152162
153163M .get_function_args = function (scope , lang )
154- return pluck_by_capture (scope , lang , get_refactoring_query (lang ), " definition.function_argument" )
164+ return pluck_by_capture (
165+ scope ,
166+ lang ,
167+ get_refactoring_query (lang ),
168+ " definition.function_argument"
169+ )
155170end
156171
157172M .get_locals_defs = function (scope , lang )
158- return pluck_by_capture (scope , lang , get_refactoring_query (lang ), " definition.local_var" )
173+ return pluck_by_capture (
174+ scope ,
175+ lang ,
176+ get_refactoring_query (lang ),
177+ " definition.local_var"
178+ )
159179end
160180
161- M .get_all_identifiers = function (scope , lang )
162- return pluck_by_capture (scope , lang , vim .treesitter .get_query (lang , " locals" ), " reference" )
181+ M .get_all_identifiers = function (scope , lang )
182+ return pluck_by_capture (
183+ scope ,
184+ lang ,
185+ vim .treesitter .get_query (lang , " locals" ),
186+ " reference"
187+ )
163188end
164189
165190-- is there a better way?
@@ -168,7 +193,7 @@ M.range_to_table = function(node)
168193 return " range nil"
169194 end
170195 local a , b , c , d = node :range ()
171- return {a , b , c , d }
196+ return { a , b , c , d }
172197end
173198
174199return M
0 commit comments