-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.lua
129 lines (110 loc) · 3.93 KB
/
search.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
128
129
--
-- A basic search layout example.
--
local core = require "core"
local command = require "core.command"
local Widget = require "widget"
local Button = require "widget.button"
local CheckBox = require "widget.checkbox"
local Line = require "widget.line"
local Label = require "widget.label"
local TextBox = require "widget.textbox"
local MessageBox = require "widget.messagebox"
local SelectBox = require "widget.selectbox"
local function on_button_click(self)
MessageBox.info("Clicked:", self.label)
end
---@type widget
local widget = Widget()
widget.name = "Search and Replace"
widget.size.x = 300
widget.size.y = 300
widget.position.x = 100
widget.draggable = true
widget.scrollable = true
---@type widget.label
local label = Label(widget, "Find and Replace")
label:set_position(10, 10)
---@type widget.line
local line = Line(widget)
line:set_position(0, label:get_bottom() + 10)
---@type widget.textbox
local findtext = TextBox(widget, "", "search...")
findtext:set_position(10, line:get_bottom() + 10)
findtext:set_tooltip("Text to search")
---@type widget.textbox
local replacetext = TextBox(widget, "", "replace...")
replacetext:set_position(10, findtext:get_bottom() + 10)
replacetext:set_tooltip("Text to replace")
---@type widget.button
local findprev = Button(widget, "Find Prev")
findprev:set_position(10, replacetext:get_bottom() + 10)
findprev:set_tooltip("Find backwards")
findprev.on_click = on_button_click
---@type widget.button
local findnext = Button(widget, "Find Next")
findnext:set_position(findprev:get_right() + 5, replacetext:get_bottom() + 10)
findnext:set_tooltip("Find forward")
findnext.on_click = on_button_click
---@type widget.button
local replace = Button(widget, "Replace All")
replace:set_position(10, findnext:get_bottom() + 10)
replace:set_tooltip("Replace all matching results")
replace.on_click = on_button_click
---@type widget.line
local line_options = Line(widget)
line_options:set_position(0, replace:get_bottom() + 10)
---@type widget.checkbox
local insensitive = CheckBox(widget, "Insensitive")
insensitive:set_position(10, line_options:get_bottom() + 10)
insensitive:set_tooltip("Case insensitive search")
insensitive.on_checked = function(_, checked)
core.log_quiet(tostring(checked))
end
---@type widget.checkbox
local regex = CheckBox(widget, "Regex")
regex:set_position(10, insensitive:get_bottom() + 10)
regex:set_tooltip("Treat search text as a regular expression")
regex.on_checked = function(_, checked)
core.log_quiet(tostring(checked))
end
---@type widget.selectbox
local scope = SelectBox(widget, "scope")
scope:set_position(10, regex:get_bottom() + 10)
scope:add_option("current file")
scope:add_option("project files")
scope:add_option("some really long option to see")
scope:add_option("other item")
scope:add_option("other option")
-- reposition items on scale changes
widget.update = function(self)
if Widget.update(self) then
label:set_position(10, 10)
line:set_position(0, label:get_bottom() + 10)
findtext:set_position(10, line:get_bottom() + 10)
findtext.size.x = self.size.x - 20
replacetext:set_position(10, findtext:get_bottom() + 10)
replacetext.size.x = self.size.x - 20
findprev:set_position(10, replacetext:get_bottom() + 10)
findnext:set_position(findprev:get_right() + 5, replacetext:get_bottom() + 10)
replace:set_position(10, findnext:get_bottom() + 10)
line_options:set_position(0, replace:get_bottom() + 10)
insensitive:set_position(10, line_options:get_bottom() + 10)
regex:set_position(10, insensitive:get_bottom() + 10)
scope:set_position(10, regex:get_bottom() + 10)
scope.size.x = self.size.x - 20
end
end
widget:show()
-- You can add the widget as a pragtical node
widget.border.width = 0
widget.draggable = false
widget.defer_draw = false
widget.target_size = 250
local node = core.root_view:get_primary_node()
node:split("right", widget, {x=true}, true)
command.add(nil,{
["find-widget:toggle"] = function()
widget:toggle_visible()
end
})