aboutsummaryrefslogtreecommitdiff
path: root/plugins/search_ui.lua
blob: e030184c21f55c5cc05400c84059d9e434a1f8f7 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
-- mod-version:3
--
-- Replacement for the find/replace and project search CommandView
---interface using Widgets with some extra features.
-- @copyright Jefferson Gonzalez <jgmdev@gmail.com>
-- @license MIT
--
local core = require "core"
local config = require "core.config"
local common = require "core.common"
local command = require "core.command"
local keymap = require "core.keymap"
local search = require "core.doc.search"
local projectsearch = require "plugins.projectsearch"
local CommandView = require "core.commandview"
local DocView = require "core.docview"
local Widget = require "libraries.widget"
local Button = require "libraries.widget.button"
local CheckBox = require "libraries.widget.checkbox"
local Line = require "libraries.widget.line"
local Label = require "libraries.widget.label"
local TextBox = require "libraries.widget.textbox"
local SelectBox = require "libraries.widget.selectbox"
local FilePicker = require "libraries.widget.filepicker"

---@class config.plugins.search_ui
---@field replace_core_find boolean
---@field position "right" | "bottom"
config.plugins.search_ui = common.merge({
  replace_core_find = true,
  position = "bottom",
  config_spec = {
    name = "Search User Interface",
    {
      label = "Replace Core Find",
      description = "Replaces the core find view when using the find shortcut.",
      path = "replace_core_find",
      type = "toggle",
      default = true
    },
    {
      label = "Position",
      description = "Location of search interface.",
      path = "position",
      type = "selection",
      default = "bottom",
      values = {
        { "Top", "top" },
        { "Right", "right" },
        { "Bottom", "bottom" }
      }
    }
  }
}, config.plugins.search_ui)

---@type core.docview
local doc_view = nil

---@type widget
local widget = Widget(nil, false)
widget.name = "Search and Replace"
widget:set_border_width(0)
widget.scrollable = true
widget:hide()
widget.init_size = 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, "", "replacement...")
replacetext:set_position(10, findtext:get_bottom() + 10)
replacetext:set_tooltip("Text to replace")

---@type widget.button
local findprev = Button(widget, "")
findprev:set_icon("<")
findprev:set_position(10, replacetext:get_bottom() + 10)
findprev:set_tooltip("Find previous")

---@type widget.button
local findnext = Button(widget, "")
findnext:set_icon(">")
findnext:set_position(findprev:get_right() + 5, replacetext:get_bottom() + 10)
findnext:set_tooltip("Find next")

---@type widget.button
local findproject = Button(widget, "Find")
findproject:set_icon("L")
findproject:set_position(findprev:get_right() + 5, replacetext:get_bottom() + 10)
findproject:set_tooltip("Find in project")
findproject:hide()

---@type widget.button
local replace = Button(widget, "Replace")
replace:set_position(10, findnext:get_bottom() + 10)
replace:set_tooltip("Replace all matching results")

---@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:set_checked(true)

---@type widget.checkbox
local patterncheck = CheckBox(widget, "Pattern")
patterncheck:set_position(10, insensitive:get_bottom() + 10)
patterncheck:set_tooltip("Treat search text as a lua pattern")

---@type widget.checkbox
local regexcheck = CheckBox(widget, "Regex")
regexcheck:set_position(10, patterncheck:get_bottom() + 10)
regexcheck:set_tooltip("Treat search text as a regular expression")

---@type widget.checkbox
local replaceinselection = CheckBox(widget, "Replace in Selection")
replaceinselection:set_position(10, regexcheck:get_bottom() + 10)
replaceinselection:set_tooltip("Perform replace only on selected text")

---@type widget.selectbox
local scope = SelectBox(widget, "scope")
scope:set_position(10, regexcheck:get_bottom() + 10)
scope:add_option("current file")
scope:add_option("project files")
scope:set_selected(1)

---@type widget.filepicker
local filepicker = FilePicker(widget)
filepicker:set_mode(FilePicker.mode.DIRECTORY)
filepicker:set_position(10, scope:get_bottom() + 10)
filepicker:set_tooltip("Directory to perform the search")
filepicker:hide()

---@type widget.line
local statusline = Line(widget)
statusline:set_position(0, scope:get_bottom() + 10)

---@type widget.label
local status = Label(widget, "")
status:set_position(10, statusline:get_bottom() + 10)

--------------------------------------------------------------------------------
-- Helper class to keep track of amount of matches and display on status label
--------------------------------------------------------------------------------
---@class plugins.search_ui.result
---@field line integer
---@field col integer

---@class plugins.search_ui.results
---@field text string
---@field matches plugins.search_ui.result[]
---@field doc core.doc?
local Results = {
  text = "",
  matches = {},
  doc = nil,
  prev_search_id = 0
}

---@param text string
---@param doc core.doc
function Results:find(text, doc, force)
  if self.text == text and self.doc == doc and not force then
    self:set_status()
    return
  end

  -- disable previous search thread
  if self.prev_search_id > 0 and core.threads[self.prev_search_id] then
    core.threads[self.prev_search_id] = {
      cr = coroutine.create(function() end), wake = 0
    }
  end

  self.text = text
  self.doc = doc

  local search_func

  -- regex search
  if regexcheck:is_checked() then
    local regex_find_offsets = regex.match
    if regex.find_offsets then
      regex_find_offsets = regex.find_offsets
    end
    local pattern = regex.compile(
      findtext:get_text(),
      insensitive:is_checked() and "im" or "m"
    )
    if not pattern then return end
    search_func = function(line_text)
      ---@cast line_text string
      local results = nil
      local offsets = {regex_find_offsets(pattern, line_text)}
      if offsets[1] then
        results = {}
        for i=1, #offsets, 2 do
          table.insert(results, offsets[i])
        end
      end
      return results
    end
  -- plain or pattern search
  else
    local no_case = insensitive:is_checked()
    local is_plain = not patterncheck:is_checked()
    if is_plain and no_case then
      text = text:ulower()
    end
    search_func = function(line_text)
      ---@cast line_text string
      if is_plain and no_case then
        line_text = line_text:ulower()
      end
      local results = nil
      local col1, col2 = line_text:find(text, 1, is_plain)
      if col1 then
        results = {}
        table.insert(results, col1)
        while col1 do
          col1, col2 = line_text:find(text, col2+1, is_plain)
          if col1 then
            table.insert(results, col1)
          end
        end
      end
      return results
    end
  end

  self.prev_search_id = core.add_thread(function()
    self.matches = {}
    local lines_count = #doc.lines
    for i=1, lines_count do
      local offsets = search_func(doc.lines[i])
      if offsets then
        for _, col in ipairs(offsets) do
          table.insert(self.matches, {line = i, col = col})
        end
      end
      if i % 100 == 0 then
        coroutine.yield()
      end
    end
    self:set_status()
  end)
end

---@return integer
function Results:current()
  if not self.doc then return 0 end
  local line1, col1, line2, col2 = self.doc:get_selection()
  if line1 == line2 and col1 == col2 then return 0 end
  local line = math.min(line1, line2)
  local col = math.min(col1, col2)
  if self.matches and #self.matches > 0 then
    for i, result in ipairs(self.matches) do
      if result.line == line and result.col == col then
        return i
      end
    end
  end
  return 0
end

function Results:clear()
  self.text = ""
  self.matches = {}
  self.doc = nil
  status:set_label("")
end

function Results:set_status()
  local current = self:current()
  local total = self.matches and #self.matches or 0
  if total > 0 then
    status:set_label(
      "Result: " .. tostring(current .. " of " .. tostring(total))
    )
  else
    status:set_label("")
  end
end

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
local function view_is_open(target_view)
  if not target_view then return false end
  local found = false
  for _, view in ipairs(core.root_view.root_node:get_children()) do
    if view == target_view then
      found = true
      break
    end
  end
  return found
end

local function toggle_scope(idx, not_set)
  if not not_set then scope:set_selected(idx) end

  if idx == 1 then
    replacetext:show()
    findnext:show()
    findprev:show()
    replace:show()
    patterncheck:show()
    replaceinselection:show()
    findproject:hide()
    filepicker:hide()

    if view_is_open(doc_view) and findtext:get_text() ~= "" then
      Results:find(findtext:get_text(), doc_view.doc)
    else
      Results:clear()
    end
  else
    replacetext:hide()
    findnext:hide()
    findprev:hide()
    replace:hide()
    patterncheck:hide()
    replaceinselection:hide()
    findproject:show()
    filepicker:show()

    Results:clear()
  end
end

local function project_search()
  if findtext:get_text() == "" then return end
  if not regexcheck:is_checked() then
    projectsearch.search_plain(
      findtext:get_text(), filepicker:get_path(), insensitive:is_checked()
    )
  else
    projectsearch.search_regex(
      findtext:get_text(), filepicker:get_path(), insensitive:is_checked()
    )
  end
  command.perform "search-replace:hide"
end

local find_enabled = true
local function find(reverse)
  if
    not view_is_open(doc_view) or findtext:get_text() == "" or not find_enabled
  then
    Results:clear()
    return
  end

  if core.last_active_view and core.last_active_view:is(DocView) then
    doc_view = core.last_active_view
  end

  local doc = doc_view.doc
  local cline1, ccol1, cline2, ccol2 = doc:get_selection()
  local line, col = cline1, ccol1
  if reverse and ccol2 < ccol1 then
    col = ccol2
  end

  local opt = {
    wrap = true,
    no_case = insensitive:is_checked(),
    pattern = patterncheck:is_checked(),
    regex = regexcheck:is_checked(),
    reverse = reverse
  }

  if opt.regex and not regex.compile(findtext:get_text()) then
    return
  end

  status:set_label("")

  core.try(function()
    local line1, col1, line2, col2 = search.find(
      doc, line, col, findtext:get_text(), opt
    )

    local current_text = doc:get_text(
      table.unpack({ doc:get_selection() })
    )

    if opt.no_case and not opt.regex and not opt.pattern then
      current_text = current_text:ulower()
    end

    if line1 then
      local text = findtext:get_text()
      if opt.no_case and not opt.regex and not opt.pattern then
        text = text:ulower()
      end
      if reverse or (current_text == text or current_text == "") then
        doc:set_selection(line1, col2, line2, col1)
      else
        doc:set_selection(line1, col1, line2, col2)
      end
      doc_view:scroll_to_line(line1, true)
      Results:find(text, doc)
    end
  end)
end

local function find_replace()
  if core.last_active_view:is(DocView) then
    doc_view = core.last_active_view
  end
  local doc = doc_view.doc

  if not replaceinselection:is_checked() then
    local line1, col1, line2, col2 = doc:get_selection()
    if line1 ~= line2 or col1 ~= col2 then
      doc:set_selection(line1, col1)
    end
  end

  local old = findtext:get_text()
  local new = replacetext:get_text()

  local results = doc:replace(function(text)
    if not regexcheck:is_checked() then
      if not patterncheck:is_checked() then
        return text:gsub(old:gsub("%W", "%%%1"), new:gsub("%%", "%%%%"), nil)
      else
        return text:gsub(old, new)
      end
    end
    local result, matches = regex.gsub(regex.compile(old, "m"), text, new)
    if type(matches) == "table" then
      return result, #matches
    end
    return result, matches
  end)

  local n = 0
  for _,v in pairs(results) do
    n = n + v
  end

  status:set_label(string.format("Total Replaced: %d", n))
end

local inside_node = false
local current_node = nil
local current_position = ""

local function add_to_node()
  if not inside_node or current_position ~= config.plugins.search_ui.position then
    if
      current_position ~= ""
      and
      current_position ~= config.plugins.search_ui.position
    then
      widget:hide()
      current_node:remove_view(core.root_view.root_node, widget)
      core.root_view.root_node:update_layout()
      widget:set_size(0, 0)
      widget.init_size = true
    end
    local node = core.root_view:get_primary_node()
    if config.plugins.search_ui.position == "right" then
      current_node = node:split("right", widget, {x=true}, true)
      current_position = "right"
    elseif config.plugins.search_ui.position == "top" then
      current_node = node:split("up", widget, {y=true}, false)
      current_position = "top"
    else
      current_node = node:split("down", widget, {y=true}, false)
      current_position = "bottom"
    end
    widget:show()
    inside_node = true
  end
end

---Show or hide the search pane.
---@param av? core.docview
---@param toggle? boolean
local function show_find(av, toggle)
  if
    not view_is_open(av)
    and
    scope:get_selected() == 1
  then
    widget:swap_active_child()
    if config.plugins.search_ui.position == "right" then
      widget:hide_animated(false, true)
    else
      widget:hide_animated(true, false)
    end
    return
  end

  if inside_node and current_position == config.plugins.search_ui.position then
    if toggle then
      widget:toggle_visible(true, false, true)
    else
      if not widget:is_visible() then
        if config.plugins.search_ui.position == "right" then
          widget:show_animated(false, true)
        else
          widget:show_animated(true, false)
        end
      end
    end
  else
    add_to_node()
  end

  if widget:is_visible() then
    status:set_label("")

    widget:swap_active_child(findtext)
    doc_view = av
    if view_is_open(doc_view) and doc_view.doc then
      local doc_text = doc_view.doc:get_text(
        table.unpack({ doc_view.doc:get_selection() })
      )
      if insensitive:is_checked() then doc_text = doc_text:ulower() end
      local current_text = findtext:get_text()
      if insensitive:is_checked() then current_text = current_text:ulower() end
      if doc_text and doc_text ~= "" and current_text ~= doc_text then
        local original_text = doc_view.doc:get_text(
          table.unpack({ doc_view.doc:get_selection() })
        )
        find_enabled = false
        findtext:set_text(original_text)
        find_enabled = true
      elseif current_text ~= "" and doc_text == "" then
        if scope:get_selected() == 1 then
          find(false)
        end
      end
      if findtext:get_text() ~= "" then
        findtext.textview.doc:set_selection(1, math.huge, 1, 1)
        if scope:get_selected() == 1 then
          Results:find(findtext:get_text(), doc_view.doc)
        else
          Results:clear()
        end
      else
        Results:clear()
      end
    end
  else
    widget:swap_active_child()
    if view_is_open(doc_view) then
      core.set_active_view(doc_view)
    end
  end
end

--------------------------------------------------------------------------------
-- Widgets event overrides
--------------------------------------------------------------------------------
function findtext:on_change(text)
  if scope:get_selected() == 1 then
    find(false)
  end
end

function insensitive:on_checked(checked)
  Results:clear()
end

function patterncheck:on_checked(checked)
  if checked then
    regexcheck:set_checked(false)
  end
  Results:clear()
end

function regexcheck:on_checked(checked)
  if checked then
    patterncheck:set_checked(false)
  end
  Results:clear()
end

function scope:on_selected(idx)
  toggle_scope(idx, true)
  if not view_is_open(doc_view) and idx == 1 then
    command.perform "search-replace:hide"
  end
end

function findnext:on_click() find(false) end
function findprev:on_click() find(true) end
function findproject:on_click() project_search() end
function replace:on_click() find_replace() end

---@param self widget
local function update_size(self)
  if config.plugins.search_ui.position == "right" then
    if scope:get_selected() == 1 then
      if self.size.x < replace:get_right() + replace:get_width() / 2 then
        self.size.x = replace:get_right() + replace:get_width() / 2
      end
    else
      if self.size.x < findproject:get_right() + findproject:get_width() * 2 then
        self.size.x = findproject:get_right() + findproject:get_width() * 2
      end
    end
  else
    self:set_size(nil, self:get_real_height() + 10)
  end
end

---@param self widget
local function update_right_positioning(self)
  scope:show()
  label:show()
  status:show()
  line_options:show()
  label:set_label("Find and Replace")

  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

  if scope:get_selected() == 1 then
    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(findnext:get_right() + 5, replacetext:get_bottom() + 10)
    line_options:set_position(0, replace:get_bottom() + 10)
  else
    findproject:set_position(10, findtext:get_bottom() + 10)
    replace:set_position(findproject:get_right() + 5, replacetext:get_bottom() + 10)
    line_options:set_position(0, findproject:get_bottom() + 10)
  end

  insensitive:set_position(10, line_options:get_bottom() + 10)
  if scope:get_selected() == 1 then
    patterncheck:set_position(10, insensitive:get_bottom() + 10)
    regexcheck:set_position(10, patterncheck:get_bottom() + 10)
    replaceinselection:set_position(10, regexcheck:get_bottom() + 10)
    scope:set_position(10, replaceinselection:get_bottom() + 10)
  else
    regexcheck:set_position(10, insensitive:get_bottom() + 10)
    scope:set_position(10, regexcheck:get_bottom() + 10)
  end

  scope:set_size(self.size.x - 20)
  if scope:get_selected() == 1 then
    statusline:set_position(0, scope:get_bottom() + 30)
  else
    filepicker:set_position(10, scope:get_bottom() + 10)
    filepicker:set_size(self.size.x - 20, nil)
    statusline:set_position(0, filepicker:get_bottom() + 30)
  end

  status:set_position(10, statusline:get_bottom() + 10)
  if status.label == "" then
    statusline:hide()
  else
    statusline:show()
  end

  if self.init_size then
    update_size(self)
    self.init_size = false
    self:show_animated(false, true)
  end

  add_to_node()
end

---@param self widget
local function update_bottom_positioning(self)
  scope:hide()
  statusline:hide()

  if scope:get_selected() == 1 then
    label:hide()
    status:show()
    status:set_position(10, 10)
    replaceinselection:set_position(self.size.x - replaceinselection:get_width() - 10, 10)
    regexcheck:set_position(replaceinselection:get_position().x - 10 - regexcheck:get_width(), 10)
    patterncheck:set_position(regexcheck:get_position().x - 10  - patterncheck:get_width(), 10)
    insensitive:set_position(patterncheck:get_position().x - 10 - insensitive:get_width(), 10)
    line:set_position(0, status:get_bottom() + 10)
  else
    label:show()
    status:hide()
    label:set_label("Find in Directory")
    label:set_position(10, 10)
    regexcheck:set_position(self.size.x - regexcheck:get_width() - 10, 10)
    insensitive:set_position(regexcheck:get_position().x - 10 - insensitive:get_width(), 10)
    line:set_position(0, label:get_bottom() + 10)
  end

  if scope:get_selected() == 1 then
    findtext:set_position(10, line:get_bottom() + 10)
    findtext.size.x = self.size.x - 40 - findprev:get_width() - findnext:get_width()
    findnext:set_position(self.size.x - 10 - findnext:get_width(), line:get_bottom() + 10)
    findprev:set_position(findnext:get_position().x - 10 - findprev:get_width(), line:get_bottom() + 10)
    replacetext:set_position(10, findtext:get_bottom() + 10)
    replacetext.size.x = findtext.size.x
    replace:set_position(self.size.x - 15 - replace:get_width(), findtext:get_bottom() + 10)
    replace.size.x = findprev:get_width() + findnext:get_width() + 10
    line_options:hide()
  else
    findtext:set_position(10, line:get_bottom() + 10)
    findtext.size.x = self.size.x - 30 - findproject:get_width()
    findproject:set_position(self.size.x - 10 - findproject:get_width(), line:get_bottom() + 10)
    replace:set_position(findproject:get_right() + 5, replacetext:get_bottom() + 10)
    line_options:show()
    line_options:set_position(0, findproject:get_bottom() + 10)
    filepicker:set_position(10, line_options:get_bottom() + 10)
    filepicker:set_size(self.size.x - 20, nil)
  end

  if self.init_size then
    update_size(self)
    self.init_size = false
    self:show_animated(true, false)
  end

  add_to_node()
end

-- reposition items on scale changes
function widget:update()
  if Widget.update(self) then
    if config.plugins.search_ui.position == "right" then
      update_right_positioning(self)
    else
      update_bottom_positioning(self)
    end
  end
end

function widget:on_scale_change(...)
  Widget.on_scale_change(self, ...)
  update_size(self)
end

--------------------------------------------------------------------------------
-- Override set_active_view to keep track of currently active docview
--------------------------------------------------------------------------------
local core_set_active_view = core.set_active_view
function core.set_active_view(...)
  core_set_active_view(...)
  local view = core.next_active_view or core.active_view
  if
    view ~= doc_view
    and
    widget:is_visible()
    and
    view:extends(DocView)
    and
    view ~= findtext.textview
    and
    view ~= replacetext.textview
    and
    view.doc.filename
  then
    doc_view = view
    Results:clear()
  end
end

--------------------------------------------------------------------------------
-- Register commands
--------------------------------------------------------------------------------
command.add(
  function()
    if core.active_view:is(DocView) then
      return true, core.active_view
    elseif widget:is_visible() then
      return true, doc_view
    elseif scope:get_selected() == 2 then
      return true, nil
    end
    return false
  end,
  {
    ["search-replace:show"] = function(av)
      show_find(av, false)
    end,

    ["search-replace:toggle"] = function(av)
      show_find(av, true)
    end
  }
)

command.add(function() return widget:is_visible() and not core.active_view:is(CommandView) end, {
  ["search-replace:hide"] = function()
    widget:swap_active_child()
    if config.plugins.search_ui.position == "right" then
      widget:hide_animated(false, true)
    else
      widget:hide_animated(true, false)
    end
    if view_is_open(doc_view) then
      core.set_active_view(doc_view)
    end
  end,

  ["search-replace:file-search"] = function()
    toggle_scope(1)
    command.perform "search-replace:show"
  end,

  ["search-replace:next"] = function()
    find(false)
  end,

  ["search-replace:previous"] = function()
    find(true)
  end,

  ["search-replace:toggle-sensitivity"] = function()
    insensitive:set_checked(not insensitive:is_checked())
    Results:clear()
  end,

  ["search-replace:toggle-regex"] = function()
    regexcheck:set_checked(not regexcheck:is_checked())
    Results:clear()
  end,

  ["search-replace:toggle-in-selection"] = function()
    replaceinselection:set_checked(not replaceinselection:is_checked())
  end
})

command.add(
  function()
    return widget:is_visible()
      and
      not core.active_view:is(CommandView)
      and
      (
        widget.child_active == findtext
        or
        widget.child_active == replacetext
      )
  end,
  {
    ["search-replace:perform"] = function()
      if scope:get_selected() == 1 then
        if widget.child_active == findtext then
          ---@type core.doc
          local doc = doc_view.doc
          local line1, col1, line2, col2 = doc:get_selection()
          -- correct cursor position to properly search next result
          if line1 ~= line2 or col1 ~= col2 then
            doc:set_selection(
              line1,
              math.max(col1, col2),
              line2,
              math.min(col1, col2)
            )
          end
          find(false)
        else
          find_replace()
        end
      else
        project_search()
      end
    end
  }
)

--------------------------------------------------------------------------------
-- Override core find/replace commands
--------------------------------------------------------------------------------
local find_replace_find = command.map["find-replace:find"].perform
command.map["find-replace:find"].perform = function(...)
  if config.plugins.search_ui.replace_core_find then
    toggle_scope(1)
    command.perform "search-replace:show"
  else
    find_replace_find(...)
  end
end

local find_replace_replace = command.map["find-replace:replace"].perform
command.map["find-replace:replace"].perform = function(...)
  if config.plugins.search_ui.replace_core_find then
    toggle_scope(1)
    command.perform "search-replace:show"
  else
    find_replace_replace(...)
  end
end

local find_replace_repeat = command.map["find-replace:repeat-find"].perform
command.map["find-replace:repeat-find"].perform = function(...)
  if
    widget:is_visible()
    or
    (config.plugins.search_ui.replace_core_find and findtext:get_text() ~= "")
  then
    find(false)
    return
  end
  find_replace_repeat(...)
end

local find_replace_previous = command.map["find-replace:previous-find"].perform
command.map["find-replace:previous-find"].perform = function(...)
  if
    widget:is_visible()
    or
    (config.plugins.search_ui.replace_core_find and findtext:get_text() ~= "")
  then
    find(true)
    return
  end
  find_replace_previous(...)
end

local project_search_find = command.map["project-search:find"].perform
command.map["project-search:find"].perform = function(path)
  if config.plugins.search_ui.replace_core_find then
    toggle_scope(2)
    if path then
      filepicker:set_path(path)
    end
    local av = doc_view
    if
      core.active_view:extends(DocView)
      and
      core.active_view ~= findtext.textview
      and
      core.active_view ~= replacetext.textview
    then
      av = core.active_view
    end
    show_find(av, false)
    return
  end
  project_search_find(path)
end

--------------------------------------------------------------------------------
-- Register keymaps
--------------------------------------------------------------------------------
keymap.add {
  ["alt+h"] = "search-replace:toggle",
  ["escape"] = "search-replace:hide",
  ["f3"] = "search-replace:next",
  ["shift+f3"] = "search-replace:previous",
  ["return"] = "search-replace:perform",
  ["shift+return"] = "search-replace:previous",
  ["ctrl+i"] = "search-replace:toggle-sensitivity",
  ["ctrl+shift+i"] = "search-replace:toggle-regex",
  ["ctrl+alt+i"] = "search-replace:toggle-in-selection",
  ["ctrl+f"] = "search-replace:file-search"
}


return widget