]> git.gir.st - VimFx.git/blob - extension/lib/hints-mode.coffee
Activate the hint marker for the largest element
[VimFx.git] / extension / lib / hints-mode.coffee
1 ###
2 # Copyright Simon Lydell 2016.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This file contains a few helper functions for Hints mode, that didn’t really
21 # fit in modes.coffee.
22
23 activateMatch = (vim, storage, match, matchedMarkers, callback) ->
24 {markerContainer} = storage
25
26 marker.markMatched(true) for marker in matchedMarkers
27
28 [largestMatchedMarker] = matchedMarkers
29 .sort((a, b) -> b.wrapper.shape.area - a.wrapper.shape.area)
30
31 # Prevent `onLeave` cleanup if the callback enters another mode.
32 storage.skipOnLeaveCleanup = true
33 again = callback(largestMatchedMarker, storage.count, match.keyStr)
34 storage.skipOnLeaveCleanup = false
35
36 switchedMode = (vim.mode != 'hints')
37
38 if again and not switchedMode
39 storage.count -= 1
40 vim.window.setTimeout((->
41 marker.markMatched(false) for marker in matchedMarkers
42 updateVisualFeedback(vim, markerContainer, [])
43 return
44 ), vim.options['hints.matched_timeout'])
45 markerContainer.reset()
46
47 else
48 vim.window.setTimeout((->
49 # Don’t clean up if Hints mode has been re-entered before the
50 # timeout has passed.
51 cleanup(vim, storage) unless vim.mode == 'hints'
52 ), vim.options['hints.matched_timeout'])
53
54 unless switchedMode
55 storage.skipOnLeaveCleanup = true
56 vim._enterMode('normal')
57 storage.skipOnLeaveCleanup = false
58
59 cleanup = (vim, storage) ->
60 {markerContainer, matchText} = storage
61 markerContainer?.remove()
62 vim._run('clear_selection') if matchText and vim.mode != 'caret'
63 if vim.options.notify_entered_keys and
64 markerContainer.enteredText == vim._state.lastNotification
65 vim.hideNotification()
66 storage.clearInterval?()
67 for key of storage
68 storage[key] = null
69 return
70
71 getChar = (match, {markerContainer, matchText}) ->
72 {unmodifiedKey} = match
73
74 isHintChar = switch
75 when not matchText
76 true
77 when unmodifiedKey.length == 1
78 markerContainer.isHintChar(unmodifiedKey)
79 else
80 false
81
82 char = if isHintChar then unmodifiedKey else match.rawKey
83 if char.length == 1
84 return {char, isHintChar}
85 else
86 return {char: null, isHintChar: false}
87
88 updateVisualFeedback = (vim, markerContainer, visibleMarkers) ->
89 hasEnteredText = (markerContainer.enteredText != '')
90
91 if vim.options.notify_entered_keys
92 if hasEnteredText
93 vim._notifyPersistent(markerContainer.enteredText)
94 else
95 vim.hideNotification()
96
97 elements = visibleMarkers.map((marker) ->
98 return {
99 elementIndex: marker.wrapper.elementIndex
100 selectAll: marker.highlighted and hasEnteredText
101 }
102 )
103 strings = markerContainer.splitEnteredText()
104 vim._send('highlightMarkableElements', {elements, strings})
105
106 isMatched = (visibleMarkers, {enteredHint}) ->
107 isUnique = (new Set(visibleMarkers.map((marker) -> marker.hint)).size == 1)
108 if isUnique
109 return {byText: true, byHint: (enteredHint == visibleMarkers[0].hint)}
110 else
111 return {byText: false, byHint: false}
112
113 module.exports = {
114 activateMatch
115 cleanup
116 getChar
117 updateVisualFeedback
118 isMatched
119 }
Imprint / Impressum