11local Iter = require (' render-markdown.lib.iter' )
22
3+ --- @class render.md.debug.Range
4+ --- @field [ 1] integer
5+ --- @field [ 2]? integer
6+
37--- @class render.md.debug.Mark
48--- @field conceal boolean
59--- @field opts render.md.MarkOpts
6- --- @field row { [1] : integer , [2] : integer }
7- --- @field col { [1] : integer , [2] : integer }
10+ --- @field row render.md.debug.Range
11+ --- @field col render.md.debug.Range
812local Mark = {}
913Mark .__index = Mark
1014
@@ -13,126 +17,122 @@ Mark.__index = Mark
1317function Mark .new (mark )
1418 local self = setmetatable ({}, Mark )
1519 self .conceal , self .opts = mark .conceal , mark .opts
16- self .row = { mark .start_row , mark .opts .end_row or mark . start_row }
17- self .col = { mark .start_col , mark .opts .end_col or mark . start_col }
20+ self .row = { mark .start_row , mark .opts .end_row }
21+ self .col = { mark .start_col , mark .opts .end_col }
1822 return self
1923end
2024
25+ --- @param a render.md.debug.Mark
26+ --- @param b render.md.debug.Mark
27+ --- @return boolean
28+ function Mark .__lt (a , b )
29+ local as , bs = a :priorities (), b :priorities ()
30+ for i = 1 , math.max (# as , # bs ) do
31+ if as [i ] ~= bs [i ] then
32+ return as [i ] < bs [i ]
33+ end
34+ end
35+ return false
36+ end
37+
38+ --- @private
2139--- @return integer[]
2240function Mark :priorities ()
23- local row_offset = 0
41+ local virt_row = 0
2442 if self .opts .virt_lines ~= nil then
25- row_offset = self .opts .virt_lines_above and - 0.5 or 0.5
43+ virt_row = self .opts .virt_lines_above and - 0.5 or 0.5
2644 end
27- local col = self .opts .virt_text_win_col or 0
28- local result = { self .row [1 ] + row_offset , self .row [2 ] + row_offset }
29- return vim .list_extend (result , { math.max (self .col [1 ], col ), math.max (self .col [2 ], col ) })
45+ local win_col = self .opts .virt_text_win_col or 0
46+ return {
47+ -- rows
48+ self .row [1 ] + virt_row ,
49+ (self .row [2 ] or self .row [1 ]) + virt_row ,
50+ -- cols
51+ math.max (self .col [1 ], win_col ),
52+ math.max ((self .col [2 ] or self .col [1 ]), win_col ),
53+ }
3054end
3155
3256--- @return string
3357function Mark :__tostring ()
34- --- @param text string
35- --- @return string
36- local function serialize_text (text )
37- local chars = vim .fn .str2list (text )
38- if # chars <= 1 then
39- return string.format (' "%s"' , text )
40- end
41- local first = chars [1 ]
42- for _ , char in ipairs (chars ) do
43- if first ~= char then
44- return string.format (' "%s"' , text )
45- end
46- end
47- return string.format (' rep(%s, %d)' , vim .fn .nr2char (first ), # chars )
48- end
49-
50- --- @param highlight number | string | string[]
51- --- @return string
52- local function serialize_highlight (highlight )
53- if type (highlight ) == ' table' then
54- highlight = table.concat (highlight , ' +' )
55- end
56- local result , _ = highlight :gsub (' RenderMarkdown_?' , ' ' )
57- result , _ = result :gsub (' Inverse' , ' I' )
58- return string.format (' (%s)' , result )
59- end
60-
61- --- @param line { [1] ?: string , [2] ?: number | string | string[] } []
62- --- @return string[] ?
63- local function virt_line (line )
64- local result = {}
65- for _ , part in ipairs (line ) do
66- local serialized , text , highlight = {}, part [1 ], part [2 ]
67- if text ~= nil then
68- table.insert (serialized , serialize_text (text ))
69- end
70- if highlight ~= nil then
71- table.insert (serialized , serialize_highlight (highlight ))
72- end
73- if # serialized > 0 then
74- table.insert (result , table.concat (serialized , ' ::' ))
75- end
76- end
77- return # result > 0 and result or nil
78- end
79-
80- --- @param vals { [1] : integer , [2] : integer }
81- --- @return string | integer
82- local function collapse (vals )
83- return vals [1 ] == vals [2 ] and vals [1 ] or string.format (' %d -> %d' , vals [1 ], vals [2 ])
84- end
85-
86- local lines = {
87- string.rep (' =' , vim .o .columns - 10 ),
88- string.format (' row: %s' , collapse (self .row )),
89- string.format (' column: %s' , collapse (self .col )),
90- string.format (' hide: %s' , self .conceal ),
91- }
58+ local lines = {}
59+ lines [# lines + 1 ] = string.rep (' =' , vim .o .columns - 10 )
60+ lines [# lines + 1 ] = string.format (' row: %s' , Mark .collapse (self .row ))
61+ lines [# lines + 1 ] = string.format (' column: %s' , Mark .collapse (self .col ))
62+ lines [# lines + 1 ] = string.format (' hide: %s' , vim .inspect (self .conceal ))
9263
9364 --- @param name string
9465 --- @param value any
95- local function append (name , value )
96- if type (value ) == ' table' then
97- value = virt_line (value )
98- end
66+ --- @param f fun ( value : any ): string
67+ local function add (name , value , f )
9968 if value ~= nil then
100- if type (value ) == ' table' then
101- value = table.concat (value , ' + ' )
102- end
103- if type (value ) == ' string' and # value == 0 then
104- value = vim .inspect (value )
105- end
106- table.insert (lines , string.format (' %s: %s' , name , value ))
69+ lines [# lines + 1 ] = string.format (' %s: %s' , name , f (value ))
10770 end
10871 end
10972
110- append (' conceal' , self .opts .conceal )
111- append (' sign' , { { self .opts .sign_text , self .opts .sign_hl_group } })
112- append (' virt_text' , self .opts .virt_text )
113- append (' virt_text_pos' , self .opts .virt_text_pos )
114- append (' virt_text_win_col' , self .opts .virt_text_win_col )
115- append (' virt_text_repeat_linebreak' , self .opts .virt_text_repeat_linebreak )
116- append (' virt_line' , (self .opts .virt_lines or {})[1 ])
117- append (' virt_line_above' , self .opts .virt_lines_above )
118- append (' hl_group' , { { nil , self .opts .hl_group } })
119- append (' hl_eol' , self .opts .hl_eol )
120- append (' hl_mode' , self .opts .hl_mode )
121- append (' priority' , self .opts .priority )
73+ local opts = self .opts
74+ add (' conceal' , opts .conceal , vim .inspect )
75+ add (' conceal_lines' , opts .conceal_lines , vim .inspect )
76+ add (' sign_text' , opts .sign_text , Mark .text )
77+ add (' sign_hl_group' , opts .sign_hl_group , Mark .highlight )
78+ add (' virt_text' , opts .virt_text , Mark .line )
79+ add (' virt_text_pos' , opts .virt_text_pos , tostring )
80+ add (' virt_text_win_col' , opts .virt_text_win_col , vim .inspect )
81+ add (' virt_text_repeat_linebreak' , opts .virt_text_repeat_linebreak , vim .inspect )
82+ add (' virt_line' , (opts .virt_lines or {})[1 ], Mark .line )
83+ add (' virt_line_above' , opts .virt_lines_above , vim .inspect )
84+ add (' hl_group' , opts .hl_group , Mark .highlight )
85+ add (' hl_eol' , opts .hl_eol , vim .inspect )
86+ add (' hl_mode' , opts .hl_mode , tostring )
87+ add (' priority' , opts .priority , tostring )
12288 return table.concat (lines , ' \n ' )
12389end
12490
125- --- @param a render.md.debug.Mark
126- --- @param b render.md.debug.Mark
127- --- @return boolean
128- function Mark .__lt (a , b )
129- local as , bs = a :priorities (), b :priorities ()
130- for i = 1 , math.max (# as , # bs ) do
131- if as [i ] ~= bs [i ] then
132- return as [i ] < bs [i ]
133- end
91+ --- @private
92+ --- @param range render.md.debug.Range
93+ --- @return string
94+ function Mark .collapse (range )
95+ local s , e = range [1 ], range [2 ]
96+ return e == nil and tostring (s ) or string.format (' %d -> %d' , s , e )
97+ end
98+
99+ --- @private
100+ --- @param line render.md.MarkLine
101+ --- @return string
102+ function Mark .line (line )
103+ local result = {}
104+ for _ , text in ipairs (line ) do
105+ result [# result + 1 ] = string.format (' (%s, %s)' , Mark .text (text [1 ]), Mark .highlight (text [2 ]))
106+ end
107+ return table.concat (result , ' + ' )
108+ end
109+
110+ --- @private
111+ --- @param text string
112+ --- @return string
113+ function Mark .text (text )
114+ local chars = vim .fn .str2list (text )
115+ local first , same = chars [1 ], true
116+ for _ , char in ipairs (chars ) do
117+ same = same and (first == char )
118+ end
119+ if # chars > 1 and same then
120+ local char = vim .fn .nr2char (first )
121+ return string.format (' rep(%s, %d)' , char , # chars )
122+ else
123+ return text
134124 end
135- return false
125+ end
126+
127+ --- @private
128+ --- @param highlight string | string[]
129+ --- @return string
130+ function Mark .highlight (highlight )
131+ if type (highlight ) == ' table' then
132+ highlight = table.concat (highlight , ' +' )
133+ end
134+ local result = highlight :gsub (' RenderMarkdown' , ' Rm' )
135+ return result
136136end
137137
138138--- @class render.md.debug.Marks
@@ -141,14 +141,15 @@ local M = {}
141141--- @param row integer
142142--- @param marks render.md.Mark[]
143143function M .debug (row , marks )
144- print (string.format (' Decorations on row : %d' , row ))
144+ vim . print (string.format (' Row : %d' , row ))
145145 if # marks == 0 then
146- print (' No decorations found' )
147- end
148- local debug_marks = Iter .list .map (marks , Mark .new )
149- table.sort (debug_marks )
150- for _ , mark in ipairs (debug_marks ) do
151- print (mark )
146+ vim .print (' No decorations found' )
147+ else
148+ local debug_marks = Iter .list .map (marks , Mark .new )
149+ table.sort (debug_marks )
150+ for _ , mark in ipairs (debug_marks ) do
151+ vim .print (tostring (mark ))
152+ end
152153 end
153154end
154155
0 commit comments