@@ -6,6 +6,9 @@ use crate::{
66 Accept , FromEmmy , Layout , Settings , Visitor ,
77} ;
88
9+ /// Text Width
10+ const TW : usize = 80 ;
11+
912#[ derive( Debug ) ]
1013pub struct VimDoc ( String ) ;
1114
@@ -21,15 +24,15 @@ impl Visitor for VimDoc {
2124 doc. push_str ( & format ! (
2225 "{:>w$}" ,
2326 format!( "*{}*" , n. name) ,
24- w = 80 - desc. len( )
27+ w = TW - desc. len( )
2528 ) ) ;
2629 doc. push ( '\n' ) ;
2730 doc
2831 }
2932
3033 fn divider ( & self , n : & crate :: parser:: Divider , _: & Self :: S ) -> Self :: R {
31- let mut doc = String :: with_capacity ( 81 ) ;
32- for _ in 0 ..80 {
34+ let mut doc = String :: with_capacity ( TW - 1 ) ;
35+ for _ in 0 ..TW - 2 {
3336 doc. push ( n. 0 ) ;
3437 }
3538 doc. push ( '\n' ) ;
@@ -43,7 +46,7 @@ impl Visitor for VimDoc {
4346 }
4447
4548 fn tag ( & self , n : & crate :: parser:: Tag , _: & Self :: S ) -> Self :: R {
46- format ! ( "{:>80 }" , format!( "*{}*" , n. 0 ) )
49+ format ! ( "{:>w$ }" , format!( "*{}*" , n. 0 ) , w = TW )
4750 }
4851
4952 fn func ( & self , n : & crate :: parser:: Func , s : & Self :: S ) -> Self :: R {
@@ -68,16 +71,16 @@ impl Visitor for VimDoc {
6871 & format ! ( "{}{}" , n. prefix. right. as_deref( ) . unwrap_or_default( ) , n. op) ,
6972 ) ) ;
7073 if !n. desc . is_empty ( ) {
71- doc. push_str ( & description ( & n. desc . join ( "\n " ) ) )
74+ doc. push_str ( & description ( & n. desc . join ( "\n " ) , s . indent_width ) )
7275 }
7376 doc. push ( '\n' ) ;
7477 if !n. params . is_empty ( ) {
75- doc. push_str ( & description ( "Parameters: ~" ) ) ;
78+ doc. push_str ( & description ( "Parameters: ~" , s . indent_width ) ) ;
7679 doc. push_str ( & self . params ( & n. params , s) ) ;
7780 doc. push ( '\n' ) ;
7881 }
7982 if !n. returns . is_empty ( ) {
80- doc. push_str ( & description ( "Returns: ~" ) ) ;
83+ doc. push_str ( & description ( "Returns: ~" , s . indent_width ) ) ;
8184 doc. push_str ( & self . returns ( & n. returns , s) ) ;
8285 doc. push ( '\n' ) ;
8386 }
@@ -91,7 +94,7 @@ impl Visitor for VimDoc {
9194 }
9295
9396 fn params ( & self , n : & [ crate :: parser:: Param ] , s : & Self :: S ) -> Self :: R {
94- let mut table = Table :: new ( ) ;
97+ let mut table = Table :: new ( s . indent_width ) ;
9598 for param in n {
9699 let ( name, ty) = match ( s. expand_opt , & param. name ) {
97100 ( true , Name :: Opt ( n) ) => ( format ! ( "{{{n}}}" ) , format ! ( "(nil|{})" , param. ty) ) ,
@@ -122,7 +125,7 @@ impl Visitor for VimDoc {
122125 }
123126
124127 fn r#returns ( & self , n : & [ crate :: parser:: Return ] , s : & Self :: S ) -> Self :: R {
125- let mut table = Table :: new ( ) ;
128+ let mut table = Table :: new ( s . indent_width ) ;
126129 for entry in n {
127130 if let Layout :: Mini ( n) = s. layout {
128131 table. add_row ( [ format ! (
@@ -163,11 +166,11 @@ impl Visitor for VimDoc {
163166 doc. push_str ( & header ( & name, & n. name ) ) ;
164167 }
165168 if !n. desc . is_empty ( ) {
166- doc. push_str ( & description ( & n. desc . join ( "\n " ) ) ) ;
169+ doc. push_str ( & description ( & n. desc . join ( "\n " ) , s . indent_width ) ) ;
167170 }
168171 doc. push ( '\n' ) ;
169172 if !n. fields . is_empty ( ) {
170- doc. push_str ( & description ( "Fields: ~" ) ) ;
173+ doc. push_str ( & description ( "Fields: ~" , s . indent_width ) ) ;
171174 doc. push_str ( & self . fields ( & n. fields , s) ) ;
172175 doc. push ( '\n' ) ;
173176 }
@@ -178,7 +181,7 @@ impl Visitor for VimDoc {
178181 }
179182
180183 fn fields ( & self , n : & [ crate :: parser:: Field ] , s : & Self :: S ) -> Self :: R {
181- let mut table = Table :: new ( ) ;
184+ let mut table = Table :: new ( s . indent_width ) ;
182185 for field in n {
183186 let ( name, ty) = match ( s. expand_opt , & field. name ) {
184187 ( true , Name :: Opt ( n) ) => ( format ! ( "{{{n}}}" ) , format ! ( "(nil|{})" , field. ty) ) ,
@@ -210,27 +213,27 @@ impl Visitor for VimDoc {
210213 table. to_string ( )
211214 }
212215
213- fn alias ( & self , n : & crate :: parser:: Alias , _ : & Self :: S ) -> Self :: R {
216+ fn alias ( & self , n : & crate :: parser:: Alias , s : & Self :: S ) -> Self :: R {
214217 let mut doc = String :: new ( ) ;
215218 if let Some ( prefix) = & n. prefix . right {
216219 doc. push_str ( & header ( & n. name , & format ! ( "{prefix}.{}" , n. name) ) ) ;
217220 } else {
218221 doc. push_str ( & header ( & n. name , & n. name ) ) ;
219222 }
220223 if !n. desc . is_empty ( ) {
221- doc. push_str ( & description ( & n. desc . join ( "\n " ) ) ) ;
224+ doc. push_str ( & description ( & n. desc . join ( "\n " ) , s . indent_width ) ) ;
222225 }
223226 doc. push ( '\n' ) ;
224227 match & n. kind {
225228 AliasKind :: Type ( ty) => {
226- doc. push_str ( & description ( "Type: ~" ) ) ;
227- let ty = ty . to_string ( ) ;
228- doc. push_str ( & format ! ( "{:>w$}" , ty , w = 8 + ty . len ( ) ) ) ;
229+ doc. push_str ( & description ( "Type: ~" , s . indent_width ) ) ;
230+ doc . push_str ( & ( " " ) . repeat ( s . indent_width * 2 ) ) ;
231+ doc. push_str ( & ty . to_string ( ) ) ;
229232 doc. push ( '\n' ) ;
230233 }
231234 AliasKind :: Enum ( variants) => {
232- doc. push_str ( & description ( "Variants: ~" ) ) ;
233- let mut table = Table :: new ( ) ;
235+ doc. push_str ( & description ( "Variants: ~" , s . indent_width ) ) ;
236+ let mut table = Table :: new ( s . indent_width ) ;
234237 for ( ty, desc) in variants {
235238 table. add_row ( [ & format ! ( "({})" , ty) , desc. as_deref ( ) . unwrap_or_default ( ) ] ) ;
236239 }
@@ -249,11 +252,11 @@ impl Visitor for VimDoc {
249252 ) ) ;
250253 let ( extract, desc) = & n. desc ;
251254 if !extract. is_empty ( ) {
252- doc. push_str ( & description ( & extract. join ( "\n " ) ) ) ;
255+ doc. push_str ( & description ( & extract. join ( "\n " ) , s . indent_width ) ) ;
253256 }
254257 doc. push ( '\n' ) ;
255- doc. push_str ( & description ( "Type: ~" ) ) ;
256- let mut table = Table :: new ( ) ;
258+ doc. push_str ( & description ( "Type: ~" , s . indent_width ) ) ;
259+ let mut table = Table :: new ( s . indent_width ) ;
257260 table. add_row ( [ & format ! ( "({})" , n. ty) , desc. as_deref ( ) . unwrap_or_default ( ) ] ) ;
258261 doc. push_str ( & table. to_string ( ) ) ;
259262 doc. push ( '\n' ) ;
@@ -266,23 +269,27 @@ impl Visitor for VimDoc {
266269 doc
267270 }
268271
269- fn see ( & self , n : & crate :: parser:: See , _ : & Self :: S ) -> Self :: R {
272+ fn see ( & self , n : & crate :: parser:: See , s : & Self :: S ) -> Self :: R {
270273 let mut doc = String :: new ( ) ;
271- doc. push_str ( & description ( "See: ~" ) ) ;
272- for s in & n. refs {
273- doc. push_str ( & format ! ( " |{s}|\n " ) ) ;
274+ doc. push_str ( & description ( "See: ~" , s. indent_width ) ) ;
275+ for reff in & n. refs {
276+ doc. push_str ( & ( " " ) . repeat ( s. indent_width * 2 ) ) ;
277+ doc. push_str ( & format ! ( "|{reff}|\n " ) ) ;
274278 }
275279 doc. push ( '\n' ) ;
276280 doc
277281 }
278282
279- fn usage ( & self , n : & crate :: parser:: Usage , _ : & Self :: S ) -> Self :: R {
283+ fn usage ( & self , n : & crate :: parser:: Usage , s : & Self :: S ) -> Self :: R {
280284 let mut doc = String :: new ( ) ;
281- doc. push_str ( & description ( "Usage: ~" ) ) ;
285+ doc. push_str ( & description ( "Usage: ~" , s . indent_width ) ) ;
282286 doc. push ( '>' ) ;
283287 doc. push_str ( n. lang . as_deref ( ) . unwrap_or ( "lua" ) ) ;
284288 doc. push ( '\n' ) ;
285- doc. push_str ( & textwrap:: indent ( & n. code , " " ) ) ;
289+ doc. push_str ( & textwrap:: indent (
290+ & n. code ,
291+ & ( " " ) . repeat ( s. indent_width * 2 ) ,
292+ ) ) ;
286293 doc. push_str ( "\n <\n \n " ) ;
287294 doc
288295 }
@@ -337,15 +344,14 @@ impl Display for VimDoc {
337344
338345// #################
339346
340- struct Table ( comfy_table:: Table ) ;
347+ struct Table ( comfy_table:: Table , usize ) ;
341348
342349impl Table {
343- pub fn new ( ) -> Self {
350+ pub fn new ( ident : usize ) -> Self {
344351 let mut tbl = comfy_table:: Table :: new ( ) ;
345352 tbl. load_preset ( comfy_table:: presets:: NOTHING ) ;
346353 // tbl.column_iter_mut().map(|c| c.set_padding((0, 0)));
347-
348- Self ( tbl)
354+ Self ( tbl, ident)
349355 }
350356
351357 pub fn add_row < T : Into < comfy_table:: Row > > ( & mut self , row : T ) -> & Self {
@@ -356,14 +362,17 @@ impl Table {
356362
357363impl std:: fmt:: Display for Table {
358364 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
359- f. write_str ( & textwrap:: indent ( & self . 0 . trim_fmt ( ) , " " ) ) ?;
365+ f. write_str ( & textwrap:: indent (
366+ & self . 0 . trim_fmt ( ) ,
367+ & ( " " ) . repeat ( ( self . 1 * 2 ) - 1 ) ,
368+ ) ) ?;
360369 f. write_str ( "\n " )
361370 }
362371}
363372
364373#[ inline]
365- fn description ( desc : & str ) -> String {
366- let mut d = textwrap:: indent ( desc, " " ) ;
374+ fn description ( desc : & str , indent : usize ) -> String {
375+ let mut d = textwrap:: indent ( desc, & ( " " ) . repeat ( indent ) ) ;
367376 d. push ( '\n' ) ;
368377 d
369378}
@@ -372,7 +381,7 @@ fn description(desc: &str) -> String {
372381fn header ( name : & str , tag : & str ) -> String {
373382 let len = name. len ( ) ;
374383 if len > 40 || tag. len ( ) > 40 {
375- return format ! ( "{:>80 }\n {}\n " , format!( "*{}*" , tag) , name) ;
384+ return format ! ( "{:>w$ }\n {}\n " , format!( "*{}*" , tag) , name, w = TW ) ;
376385 }
377- format ! ( "{}{:>w$}\n " , name, format!( "*{}*" , tag) , w = 80 - len)
386+ format ! ( "{}{:>w$}\n " , name, format!( "*{}*" , tag) , w = TW - len)
378387}
0 commit comments