-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
128 lines (113 loc) · 3.64 KB
/
init.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- mod-version:3
local config = require "core.config"
local command = require "core.command"
local keymap = require "core.keymap"
local common = require "core.common"
local DocView = require "core.docview"
config.plugins.code_plus = common.merge({
enabled = true, --- enabled by default
config_spec = { --- config specification used by the settings gui
name = "Code+",
{
label = "Enable",
description = "Toggle to enable this plugin.",
path = "enabled",
type = "toggle",
default = true
},
{
label = "Todo Color",
description = "Define the color that highlights the todo comments.",
path = "todo",
type = "color",
default = "#5592CF"
},
{
label = "Fixme Color",
description = "Defines the color that highlights the fixme comments.",
path = "fixme",
type = "color",
default = "#EF6385"
},
{
label = "Enable Autocomplete",
description = "Standard autocomplete for an EU keyboard layout.",
path = "autocomplete_enabled",
type = "toggle",
default = true
},
}
}, config.plugins.code_plus)
--- draw comments highlights
local white = { common.color "#ffffff" }
local function draw_highlight(self, str, line, x, y, s, e, color)
local x1 = x + self:get_col_x_offset(line, s)
local x2 = x + self:get_col_x_offset(line, e + 1)
local oy = self:get_line_text_y_offset()
renderer.draw_rect(x1, y, x2 - x1, self:get_line_height(), color)
renderer.draw_text(self:get_font(), str, x1, y + oy, white)
end
local function highlight_comment(self, line, x, y, comment, color)
local text = self.doc.lines[line]
local s, e = 0, 0
while true do
s, e = text:lower():find(comment .. "%((.-)%)", e + 1)
if s then
local str = text:sub(s, e)
draw_highlight(self, str, line, x, y, s, e, color)
end
if not s then
break
end
end
end
local draw_line_text = DocView.draw_line_text
function DocView:draw_line_text(line, x, y)
local lh = draw_line_text(self, line, x, y)
if config.plugins.code_plus.enabled then
highlight_comment(self, line, x, y, "@todo", config.plugins.code_plus.todo)
highlight_comment(self, line, x, y, "@fixme", config.plugins.code_plus.fixme)
end
return lh
end
--- auto complete brackets, parantheses, etc...
local function complete(dv, s, e)
if not config.plugins.code_plus.enabled or not config.plugins.code_plus.autocomplete_enabled then
return
end
if dv.doc:has_selection() then
local text = dv.doc:get_text(dv.doc:get_selection())
dv.doc:text_input(s .. text .. e)
else
local doc = dv.doc
local idx = dv.doc.last_selection
local line1, col1 = doc:get_selection_idx(idx)
doc:insert(line1, col1, s .. e)
doc:move_to_cursor(idx, idx)
end
end
command.add("core.docview!", {
["code_plus:complete_brackets"] = function(dv)
complete(dv, "[", "]")
end,
["code_plus:complete_curly_brackets"] = function(dv)
complete(dv, "{", "}")
end,
["code_plus:complete_parantheses"] = function(dv)
complete(dv, "(", ")")
end,
["code_plus:complete_quotation_marks"] = function(dv)
complete(dv, '"', '"')
end,
["code_plus:complete_single_quotation_marks"] = function(dv)
complete(dv, "'", "'")
end,
})
keymap.add {
["altgr+8"] = "code_plus:complete_brackets",
["ctrl+alt+8"] = "code_plus:complete_brackets",
["altgr+7"] = "code_plus:complete_curly_brackets",
["ctrl+alt+7"] = "code_plus:complete_curly_brackets",
["shift+8"] = "code_plus:complete_parantheses",
["shift+2"] = "code_plus:complete_quotation_marks",
}