forked from pkulchenko/ZeroBranePackage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlightselected.lua
84 lines (71 loc) · 2.98 KB
/
highlightselected.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
-- Copyright 2015-16 Paul Kulchenko, ZeroBrane LLC; All rights reserved
local updateneeded, cfg
local indicname = "highlightselected.selected"
local function onUpdate(event, editor)
if bit.band(event:GetUpdated(), wxstc.wxSTC_UPDATE_SELECTION) > 0 then updateneeded = editor end
end
return {
name = "Highlight selected",
description = "Highlights all instances of a selected word.",
author = "Paul Kulchenko",
version = 0.19,
dependencies = "1.20",
onRegister = function(package)
ide:AddIndicator(indicname)
cfg = package:GetConfig()
ide:GetOutput():Connect(wxstc.wxEVT_STC_UPDATEUI, function(event)
onUpdate(event, ide:GetOutput())
end)
end,
onUnRegister = function()
ide:RemoveIndicator(indicname)
ide:GetOutput():Disconnect(wxstc.wxEVT_STC_UPDATEUI)
end,
onEditorUpdateUI = function(self, editor, event) onUpdate(event, editor) end,
onIdle = function(self)
if not updateneeded then return end
local editor = updateneeded
updateneeded = false
local length, curpos = editor:GetLength(), editor:GetCurrentPos()
local ssel, esel = editor:GetSelection()
local value = editor:GetTextRange(ssel, esel)
local indicator = ide:GetIndicator(indicname)
local function clearIndicator()
editor:SetIndicatorCurrent(indicator)
editor:IndicatorClearRange(0, length)
end
if #value == 0 or not value:find('%w') then return clearIndicator() end
local word = editor:GetTextRange( -- try to select a word under caret
editor:WordStartPosition(curpos, true), editor:WordEndPosition(curpos, true))
if #word == 0 then
word = editor:GetTextRange( -- try to select a non-word under caret
editor:WordStartPosition(curpos, false), editor:WordEndPosition(curpos, false))
end
if value ~= word then return clearIndicator() end
local style = bit.band(editor:GetStyleAt(editor:GetSelectionStart()),31)
local color = cfg and type(cfg.color) == "table" and #(cfg.color) == 3 and
wx.wxColour((table.unpack or unpack)(cfg.color)) or editor:StyleGetForeground(style)
editor:IndicatorSetStyle(indicator, cfg and cfg.indicator or wxstc.wxSTC_INDIC_ROUNDBOX)
editor:IndicatorSetForeground(indicator, color)
editor:SetIndicatorCurrent(indicator)
editor:IndicatorClearRange(0, length)
-- save the flags to restore after the search is done to not affect other searches
local flags = editor:GetSearchFlags()
editor:SetSearchFlags(wxstc.wxSTC_FIND_WHOLEWORD + wxstc.wxSTC_FIND_MATCHCASE)
local pos, num = 0, 0
while true do
editor:SetTargetStart(pos)
editor:SetTargetEnd(length)
pos = editor:SearchInTarget(value)
if pos == -1 then break end
editor:IndicatorFillRange(pos, #value)
pos = pos + #value
num = num + 1
end
ide:SetStatusFor(("Found %d instance(s)."):format(num), 5)
editor:SetSearchFlags(flags)
end,
}
--[[ configuration example:
highlightselected = {indicator = wxstc.wxSTC_INDIC_ROUNDBOX, color = {255, 0, 0}}
--]]