Skip to content

Commit 14b5a65

Browse files
committed
refactor(main): readability tweaks
1 parent 8146fe3 commit 14b5a65

File tree

2 files changed

+100
-97
lines changed

2 files changed

+100
-97
lines changed

fnl/leap/main.fnl

+88-86
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,19 @@ char separately.
150150
},
151151
}
152152
"
153+
(set targets.sublists {})
153154
; Setting a metatable to handle case insensitivity and equivalence
154155
; classes (in both cases: multiple keys -> one value).
155156
; If `ch` is not found, try to get a sublist belonging to some common
156157
; key: the equivalence class that `ch` belongs to (if there is one),
157158
; or, if case insensivity is set, the lowercased verison of `ch`.
158159
; (And in the above cases, `ch` will not be found, since we also
159160
; redirect to the common keys when inserting a new sublist.)
160-
(set targets.sublists
161-
(setmetatable {}
162-
{:__index (fn [self ch] (rawget self (->representative-char ch)))
163-
:__newindex (fn [self ch sublist]
164-
(rawset self (->representative-char ch) sublist))}))
161+
(setmetatable targets.sublists
162+
{:__newindex (fn [self ch sublist]
163+
(rawset self (->representative-char ch) sublist))
164+
:__index (fn [self ch]
165+
(rawget self (->representative-char ch)))})
165166
; Filling the sublists.
166167
(each [_ {:chars [_ ch2] &as target} (ipairs targets)]
167168
; "\n"-s after empty lines don't have a `ch2`. Still, we put these
@@ -254,92 +255,93 @@ is not shifted (if there is a match after us, we cannot be at the edge).
254255
And if we have a target with a shifted label, then the conflicting other
255256
is either labeled (C) or not (B).
256257
"
257-
; Tables to help us check potential conflicts (we'll be filling them
258-
; as we go).
259-
; { "<bufnr> <winid> <lnum> <col>" = <target> }
258+
; Tables to help us check potential conflicts (we'll be filling
259+
; them as we go):
260+
; { "<bufnr> <winid> <lnum> <col>" = <target> }
260261
(let [unlabeled-match-positions {}
261262
labeled-match-positions {}
262263
label-positions {}]
263-
; Note: A1-A2, B1-B2, C1-C2 are all necessary, as we do only one
264-
; traversal run, and we don't assume anything about the direction
265-
; and the ordering; we always resolve the conflict at the second
266-
; target - at that point, the first one has already been registered
267-
; as a potential source of conflict.
268264
(each [_ target (ipairs targets)]
269265
(when-not target.empty-line?
270-
(local {: bufnr : winid} target.wininfo)
271-
(local [lnum col-ch1] target.pos)
272-
(local col-ch2 (+ col-ch1 (string.len (. target.chars 1))))
273-
(macro ->key [col*] `(.. bufnr " " winid " " lnum " " ,col*))
274-
; `beacon` can be nil (if label-state is inactive).
275-
(if (and target.label target.beacon)
276-
(let [label-offset (. target.beacon 1)
277-
col-label (+ col-ch1 label-offset)
278-
shifted-label? (= col-label col-ch2)]
279-
280-
; (A1)
281-
; [a][b][L][-] --> nil beacon | current
282-
; [-][-][a][c] --> add match highlight | other
283-
; ^ | column to check
284-
; or
285-
; [a][a][L] --> nil beacon
286-
; [-][a][b] --> add match highlight
287-
; ^
288-
(case (. unlabeled-match-positions (->key col-label))
289-
other (do (set target.beacon nil)
290-
(set-beacon-to-match-hl other)))
291-
292-
; (B1)
293-
; [-][a][L]|
294-
; [a][a][-]| --> add match highlight
295-
; ^
296-
(when shifted-label?
297-
(case (. unlabeled-match-positions (->key col-ch1))
298-
other (set-beacon-to-match-hl other)))
299-
300-
; (C)
301-
; [-][a][L]| --> nil beacon
302-
; [a][a][L]| --> set empty label
303-
; ^
304-
; or
305-
; [a][a][L]| --> nil beacon
306-
; [-][a][L]| --> set empty label
307-
; ^
308-
(case (. label-positions (->key col-label))
309-
other (do (set target.beacon nil)
310-
(set-beacon-to-empty-label other)))
311-
312-
; NOTE: We should register the label position _after_
313-
; checking case C, as we don't want to literally chase our
314-
; own tail, i.e., our own label.
315-
(tset label-positions (->key col-label) target)
316-
(tset labeled-match-positions (->key col-ch1) target)
317-
(when-not shifted-label?
318-
(tset labeled-match-positions (->key col-ch2) target)))
319-
320-
(not target.label)
321-
(do
322-
; (A2)
323-
; [-][-][a][b] --> add match highlight
324-
; [a][c][L][-] --> nil beacon
325-
; ^
326-
; or
327-
; [-][a][b] --> add match highlight
328-
; [a][a][L] --> nil beacon
329-
; ^
330-
(each [_ key (ipairs [(->key col-ch1) (->key col-ch2)])]
331-
(tset unlabeled-match-positions key target)
332-
(case (. label-positions key)
333-
other (do (set other.beacon nil)
334-
(set-beacon-to-match-hl target))))
335-
336-
; (B2)
337-
; [a][a][-]| --> add match highlight
338-
; [-][a][L]|
339-
; ^
340-
(local col-after (+ col-ch2 (string.len (. target :chars 2))))
341-
(case (. label-positions (->key col-after))
342-
other (set-beacon-to-match-hl target))))))))
266+
(let [{: bufnr : winid} target.wininfo
267+
[lnum col-ch1] target.pos
268+
col-ch2 (+ col-ch1 (string.len (. target.chars 1)))]
269+
(macro ->key [col] `(.. bufnr " " winid " " lnum " " ,col))
270+
271+
; Note: A1-A2, B1-B2, C1-C2 are all necessary, as we do only
272+
; one traversal run, and we don't assume anything about the
273+
; direction and the ordering; we always resolve the conflict
274+
; at the second target - at that point, the first one has
275+
; already been registered as a potential source of conflict.
276+
277+
(if (and target.label target.beacon) ; beacon=nil if label is inactive
278+
(let [label-offset (. target.beacon 1)
279+
col-label (+ col-ch1 label-offset)
280+
shifted-label? (= col-label col-ch2)]
281+
282+
; (A1)
283+
; [a][b][L][-] --> nil beacon | current
284+
; [-][-][a][c] --> add match highlight | other
285+
; ^ | column to check
286+
; or
287+
; [a][a][L] --> nil beacon
288+
; [-][a][b] --> add match highlight
289+
; ^
290+
(case (. unlabeled-match-positions (->key col-label))
291+
other (do (set target.beacon nil)
292+
(set-beacon-to-match-hl other)))
293+
294+
; (B1)
295+
; [-][a][L]|
296+
; [a][a][-]| --> add match highlight
297+
; ^
298+
(when shifted-label?
299+
(case (. unlabeled-match-positions (->key col-ch1))
300+
other (set-beacon-to-match-hl other)))
301+
302+
; (C)
303+
; [-][a][L]| --> nil beacon
304+
; [a][a][L]| --> set empty label
305+
; ^
306+
; or
307+
; [a][a][L]| --> nil beacon
308+
; [-][a][L]| --> set empty label
309+
; ^
310+
(case (. label-positions (->key col-label))
311+
other (do (set target.beacon nil)
312+
(set-beacon-to-empty-label other)))
313+
314+
; NOTE: We should register the label position _after_
315+
; checking case C, as we don't want to literally chase our
316+
; own tail, i.e., our own label.
317+
(tset label-positions (->key col-label) target)
318+
(tset labeled-match-positions (->key col-ch1) target)
319+
(when-not shifted-label?
320+
(tset labeled-match-positions (->key col-ch2) target)))
321+
322+
(not target.label)
323+
(do
324+
; (A2)
325+
; [-][-][a][b] --> add match highlight
326+
; [a][c][L][-] --> nil beacon
327+
; ^
328+
; or
329+
; [-][a][b] --> add match highlight
330+
; [a][a][L] --> nil beacon
331+
; ^
332+
(each [_ key (ipairs [(->key col-ch1) (->key col-ch2)])]
333+
(tset unlabeled-match-positions key target)
334+
(case (. label-positions key)
335+
other (do (set other.beacon nil)
336+
(set-beacon-to-match-hl target))))
337+
338+
; (B2)
339+
; [a][a][-]| --> add match highlight
340+
; [-][a][L]|
341+
; ^
342+
(local col-after (+ col-ch2 (string.len (. target :chars 2))))
343+
(case (. label-positions (->key col-after))
344+
other (set-beacon-to-match-hl target)))))))))
343345

344346

345347
(fn set-beacons [targets {: no-labels? : user-given-targets? : aot?}]

lua/leap/main.lua

+12-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)