generated from chrisgrieser/nvim-pseudometa-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsmart-insert-location.lua
52 lines (45 loc) · 1.86 KB
/
smart-insert-location.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
local M = {}
--------------------------------------------------------------------------------
---The config should map a filetype to a function that takes the node under
---the cursor and returns a number representing the shift in line number where
---the log statement should be inserted instead. For example, `-1` means to
---insert above the current line, and `0` means below the current line. If not
---return value is provided, will return below the current line.
---@type table<string, fun(node: TSNode): integer?>
M.ftConfig = {
lua = function(node)
local parent = node:parent()
local grandparent = parent and parent:parent()
if not parent or not grandparent then return end
-- return statement
local exprNode
if parent:type() == "expression_list" then exprNode = parent end
if grandparent:type() == "expression_list" then exprNode = grandparent end
if exprNode and exprNode:parent() and exprNode:parent():type() == "return_statement" then
return -1
end
-- multiline assignment
if grandparent:type() == "assignment_statement" then
local assignmentExtraLines = grandparent:end_() - grandparent:start()
return assignmentExtraLines
end
end,
javascript = function(node)
local parent = node:parent()
if not parent then return end
-- return statement
local inReturnStatement = parent:type() == "return_statement"
or (parent:parent() and parent:parent():type() == "return_statement")
if inReturnStatement then return -1 end
-- multiline assignment
local isAssignment = parent:type() == "variable_declarator"
or parent:type() == "assignment_expression"
if isAssignment then
local assignmentExtraLines = parent:end_() - parent:start()
return assignmentExtraLines
end
end,
}
require("chainsaw.config.config").supersetInheritance(M.ftConfig)
--------------------------------------------------------------------------------
return M