fmt: reorder and sort methods into section (#9363)
							parent
							
								
									04095f4088
								
							
						
					
					
						commit
						f8fcf3ff66
					
				| 
						 | 
				
			
			@ -0,0 +1,60 @@
 | 
			
		|||
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
 | 
			
		||||
// Use of this source code is governed by an MIT license
 | 
			
		||||
// that can be found in the LICENSE file.
 | 
			
		||||
module fmt
 | 
			
		||||
 | 
			
		||||
import v.table
 | 
			
		||||
 | 
			
		||||
pub fn (mut f Fmt) attrs(attrs []table.Attr) {
 | 
			
		||||
	mut sorted_attrs := attrs.clone()
 | 
			
		||||
	// Sort the attributes. The ones with arguments come first.
 | 
			
		||||
	sorted_attrs.sort(a.arg.len > b.arg.len)
 | 
			
		||||
	for i, attr in sorted_attrs {
 | 
			
		||||
		if attr.arg.len == 0 {
 | 
			
		||||
			f.single_line_attrs(sorted_attrs[i..], {})
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		f.writeln('[$attr]')
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct AttrsOptions {
 | 
			
		||||
	inline bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn (mut f Fmt) single_line_attrs(attrs []table.Attr, options AttrsOptions) {
 | 
			
		||||
	if attrs.len == 0 {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	mut sorted_attrs := attrs.clone()
 | 
			
		||||
	sorted_attrs.sort(a.name < b.name)
 | 
			
		||||
	if options.inline {
 | 
			
		||||
		f.write(' ')
 | 
			
		||||
	}
 | 
			
		||||
	f.write('[')
 | 
			
		||||
	for i, attr in sorted_attrs {
 | 
			
		||||
		if i > 0 {
 | 
			
		||||
			f.write('; ')
 | 
			
		||||
		}
 | 
			
		||||
		f.write('$attr')
 | 
			
		||||
	}
 | 
			
		||||
	f.write(']')
 | 
			
		||||
	if !options.inline {
 | 
			
		||||
		f.writeln('')
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn inline_attrs_len(attrs []table.Attr) int {
 | 
			
		||||
	if attrs.len == 0 {
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
	mut n := 2 // ' ['.len
 | 
			
		||||
	for i, attr in attrs {
 | 
			
		||||
		if i > 0 {
 | 
			
		||||
			n += 2 // '; '.len
 | 
			
		||||
		}
 | 
			
		||||
		n += '$attr'.len
 | 
			
		||||
	}
 | 
			
		||||
	n++ // ']'.len
 | 
			
		||||
	return n
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2618
									
								
								vlib/v/fmt/fmt.v
								
								
								
								
							
							
						
						
									
										2618
									
								
								vlib/v/fmt/fmt.v
								
								
								
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
		Reference in New Issue