1 # This file contains a few helper functions for Hints mode, that didn’t really
4 activateMatch = (vim, storage, match, matchedMarkers, callback) ->
5 {markerContainer} = storage
7 marker.markMatched(true) for marker in matchedMarkers
9 [largestMatchedMarker] = matchedMarkers
10 .sort((a, b) -> b.wrapper.shape.area - a.wrapper.shape.area)
12 # Prevent `onLeave` cleanup if the callback enters another mode.
13 storage.skipOnLeaveCleanup = true
14 again = callback(largestMatchedMarker, storage.count, match.keyStr)
15 storage.skipOnLeaveCleanup = false
17 switchedMode = (vim.mode != 'hints')
19 if again and not switchedMode
21 vim.window.setTimeout((->
22 marker.markMatched(false) for marker in matchedMarkers
23 updateVisualFeedback(vim, markerContainer, [])
25 ), vim.options['hints.matched_timeout'])
26 markerContainer.reset()
29 vim.window.setTimeout((->
30 # Don’t clean up if Hints mode has been re-entered before the
32 unless vim.mode == 'hints'
33 # Don’t blur frames (in `utils.clearSelectionDeep`) in case the callback
34 # has focused something in a frame.
35 cleanup(vim, storage, {blur: false})
36 ), vim.options['hints.matched_timeout'])
39 storage.skipOnLeaveCleanup = true
40 vim._enterMode('normal')
41 storage.skipOnLeaveCleanup = false
43 cleanup = (vim, storage, options = {}) ->
44 {markerContainer, matchText} = storage
45 markerContainer?.remove()
46 vim._run('clear_selection', options) if matchText and vim.mode != 'caret'
47 if vim.options.notify_entered_keys and
48 markerContainer.enteredText == vim._state.lastNotification
49 vim.hideNotification()
50 storage.clearInterval?()
55 getChar = (match, {markerContainer, matchText}) ->
56 {unmodifiedKey} = match
61 when unmodifiedKey.length == 1
62 markerContainer.isHintChar(unmodifiedKey)
66 char = if isHintChar then unmodifiedKey else match.rawKey
68 return {char, isHintChar}
70 return {char: null, isHintChar: false}
72 updateVisualFeedback = (vim, markerContainer, visibleMarkers) ->
73 hasEnteredText = (markerContainer.enteredText != '')
75 if vim.options.notify_entered_keys
77 vim._notifyPersistent(markerContainer.enteredText)
79 vim.hideNotification()
81 elements = visibleMarkers.map((marker) ->
83 elementIndex: marker.wrapper.elementIndex
84 selectAll: marker.highlighted and hasEnteredText
87 strings = markerContainer.splitEnteredText()
88 vim._send('highlightMarkableElements', {elements, strings})
90 isMatched = (visibleMarkers, {enteredHint}) ->
91 isUnique = (new Set(visibleMarkers.map((marker) -> marker.hint)).size == 1)
93 return {byText: true, byHint: (enteredHint == visibleMarkers[0].hint)}
95 return {byText: false, byHint: false}