mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: line comments fixes
This commit is contained in:
parent
439bb0c5de
commit
1ccd1979a4
@ -42,4 +42,4 @@ pub fn new_primary() &Clipboard {
|
||||
} $else {
|
||||
panic("Primary clipboard is not supported on non-Linux systems.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,4 +63,4 @@ fn (cb &Clipboard) get_text() string {
|
||||
|
||||
#utf8_clip = [ns_clip UTF8String];
|
||||
return string(utf8_clip)
|
||||
}
|
||||
}
|
||||
|
@ -20,4 +20,4 @@ fn test_primary(){
|
||||
|
||||
fn test_clipboard(){
|
||||
run_test(false)
|
||||
}
|
||||
}
|
||||
|
@ -144,4 +144,4 @@ fn (cb &Clipboard) get_text() string {
|
||||
str := string_from_wide(&u16(GlobalLock(h_data)))
|
||||
GlobalUnlock(h_data)
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ fn (p mut Parser) enum_decl(no_name bool) {
|
||||
p.warn('enum values cannot contain uppercase letters, use snake_case instead')
|
||||
}
|
||||
fields << field
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
name := '${mod_gen_name(p.mod)}__${enum_name}_$field'
|
||||
if p.tok == .assign {
|
||||
mut enum_assign_tidx := p.cur_tok_index()
|
||||
|
@ -223,7 +223,9 @@ fn (p mut Parser) fn_decl() {
|
||||
is_amp := p.tok == .amp
|
||||
if is_mut || is_amp {
|
||||
p.check(p.tok)
|
||||
p.fspace()
|
||||
if !is_amp {
|
||||
p.fspace()
|
||||
}
|
||||
}
|
||||
receiver_typ = p.get_type()
|
||||
t := p.table.find_type(receiver_typ)
|
||||
@ -364,7 +366,7 @@ fn (p mut Parser) fn_decl() {
|
||||
if !is_c && !p.is_vh && !is_fn_header {
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
//p.fgenln('')
|
||||
//p.fgen_nl()
|
||||
}
|
||||
// Register ?option type
|
||||
if typ.starts_with('Option_') {
|
||||
@ -425,7 +427,7 @@ fn (p mut Parser) fn_decl() {
|
||||
|
||||
if is_fn_header {
|
||||
p.genln('$typ $fn_name_cgen($str_args);')
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
if is_c {
|
||||
p.fgenln('\n')
|
||||
|
@ -41,6 +41,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||
|
||||
// unwrap match if there is only else
|
||||
if i == 0 {
|
||||
p.fspace()
|
||||
if is_expr {
|
||||
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
||||
|
||||
@ -80,6 +81,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||
// allow braces is else
|
||||
got_brace := p.tok == .lcbr
|
||||
if got_brace {
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
}
|
||||
|
||||
@ -97,6 +99,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||
p.returns = false
|
||||
p.genln('else // default:')
|
||||
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
|
||||
p.genln('{ ')
|
||||
@ -190,6 +193,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||
}
|
||||
else {
|
||||
p.returns = false
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
|
||||
p.genln('{ ')
|
||||
@ -199,7 +203,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
||||
// p.gen(')')
|
||||
}
|
||||
i++
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
|
||||
if is_expr {
|
||||
|
@ -407,10 +407,10 @@ fn (p mut Parser) parse(pass Pass) {
|
||||
if !p.cgen.nogen {
|
||||
p.cgen.consts << g
|
||||
}
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
if p.tok != .key_global {
|
||||
// An extra empty line to separate a block of globals
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
}
|
||||
.eof {
|
||||
@ -472,10 +472,10 @@ fn (p mut Parser) imports() {
|
||||
p.fspace()
|
||||
p.check(.lpar)
|
||||
p.fmt_inc()
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
for p.tok != .rpar && p.tok != .eof {
|
||||
p.import_statement()
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
p.fmt_dec()
|
||||
p.check(.rpar)
|
||||
@ -484,9 +484,9 @@ fn (p mut Parser) imports() {
|
||||
}
|
||||
// `import foo`
|
||||
p.import_statement()
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
if p.tok != .key_import {
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
}
|
||||
|
||||
@ -539,7 +539,7 @@ fn (p mut Parser) const_decl() {
|
||||
p.check(.key_const)
|
||||
p.fspace()
|
||||
p.check(.lpar)
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
p.fmt_inc()
|
||||
for p.tok == .name {
|
||||
if p.lit == '_' && p.peek() == .assign && !p.cgen.nogen {
|
||||
@ -611,7 +611,7 @@ fn (p mut Parser) const_decl() {
|
||||
if p.pref.build_mode != .build_module && is_compile_time_const(p.cgen.cur_line) {
|
||||
p.cgen.consts << '#define $name $p.cgen.cur_line'
|
||||
p.cgen.resetln('')
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
continue
|
||||
}
|
||||
if typ.starts_with('[') {
|
||||
@ -625,7 +625,7 @@ fn (p mut Parser) const_decl() {
|
||||
}
|
||||
p.cgen.resetln('')
|
||||
}
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
p.fmt_dec()
|
||||
p.check(.rpar)
|
||||
@ -679,7 +679,7 @@ fn (p mut Parser) interface_method(field_name, receiver string) &Fn {
|
||||
} else {
|
||||
method.typ = p.get_type()// method return type
|
||||
//p.fspace()
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
return method
|
||||
}
|
||||
@ -764,7 +764,7 @@ fn (p mut Parser) check(expected TokenKind) {
|
||||
p.fgen(p.strtok())
|
||||
// vfmt: increase indentation on `{` unless it's `{}`
|
||||
if expected == .lcbr { //&& p.scanner.pos + 1 < p.scanner.text.len && p.scanner.text[p.scanner.pos + 1] != `}` {
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
p.fmt_inc()
|
||||
}
|
||||
*/
|
||||
@ -1015,7 +1015,7 @@ fn (p mut Parser) statements_no_rcbr() string {
|
||||
// println('last st typ=$last_st_typ')
|
||||
if !p.inside_if_expr {
|
||||
//p.genln('')// // end st tok= ${p.strtok()}')
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
i++
|
||||
if i > 50000 {
|
||||
@ -2096,7 +2096,7 @@ fn (p mut Parser) assoc() string {
|
||||
if p.tok != .rcbr {
|
||||
p.check(.comma)
|
||||
}
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
// Copy the rest of the fields
|
||||
T := p.table.find_type(var.typ)
|
||||
@ -2302,14 +2302,14 @@ fn (p mut Parser) map_init() string {
|
||||
}
|
||||
vals_gen += '$val_expr, '
|
||||
if p.tok == .rcbr {
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
p.check(.rcbr)
|
||||
break
|
||||
}
|
||||
if p.tok == .comma {
|
||||
p.check(.comma)
|
||||
}
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
p.gen('new_map_init($i, sizeof($val_type), ' +
|
||||
'(string[$i]){ $keys_gen }, ($val_type [$i]){ $vals_gen } )')
|
||||
@ -2556,7 +2556,7 @@ fn (p mut Parser) if_st(is_expr bool, elif_depth int) string {
|
||||
p.returns = false
|
||||
if p.tok == .key_else {
|
||||
if !p.inside_if_expr {
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
p.check(.key_else)
|
||||
p.fspace()
|
||||
@ -2889,7 +2889,7 @@ fn (p mut Parser) attribute() {
|
||||
p.attr = p.attr + ':' + p.check_name()
|
||||
}
|
||||
p.check(.rsbr)
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
if p.tok == .key_fn || (p.tok == .key_pub && p.peek() == .key_fn) {
|
||||
p.fn_decl()
|
||||
p.attr = ''
|
||||
|
@ -589,6 +589,8 @@ fn (s mut Scanner) scan() ScanRes {
|
||||
s.line_comment = s.text[start + 1..s.pos]
|
||||
s.line_comment = s.line_comment.trim_space()
|
||||
if s.is_fmt {
|
||||
s.pos-- // fix line_nr, \n was read, and the comment is marked on the next line
|
||||
s.line_nr--
|
||||
return scan_res(.line_comment, s.line_comment)
|
||||
}
|
||||
//s.fgenln('// ${s.prev_tok.str()} "$s.line_comment"')
|
||||
|
@ -143,7 +143,7 @@ fn (p mut Parser) struct_decl() {
|
||||
p.check(.colon)
|
||||
}
|
||||
p.fmt_inc()
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
if p.tok == .key_mut {
|
||||
if is_mut {
|
||||
@ -156,7 +156,7 @@ fn (p mut Parser) struct_decl() {
|
||||
p.check(.colon)
|
||||
}
|
||||
p.fmt_inc()
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
}
|
||||
// if is_pub {
|
||||
// }
|
||||
@ -229,7 +229,7 @@ fn (p mut Parser) struct_decl() {
|
||||
if p.first_pass() {
|
||||
p.table.add_field(typ.name, field_name, field_type, is_mut, attr, access_mod)
|
||||
}
|
||||
p.fgenln('') // newline between struct fields
|
||||
p.fgen_nl() // newline between struct fields
|
||||
}
|
||||
p.check(.rcbr)
|
||||
if !is_c && !did_gen_something && p.first_pass() {
|
||||
@ -280,7 +280,7 @@ fn (p mut Parser) struct_init(typ string) string {
|
||||
}
|
||||
p.fspace()
|
||||
did_gen_something = true
|
||||
p.fgenln('') // newline between struct fields
|
||||
p.fgen_nl() // newline between struct fields
|
||||
}
|
||||
// If we already set some fields, need to prepend a comma
|
||||
if t.fields.len != inited_fields.len && inited_fields.len > 0 {
|
||||
|
@ -31,6 +31,12 @@ fn (scanner mut Scanner) fgenln(s_ string) {
|
||||
scanner.fmt_line_empty = true
|
||||
}
|
||||
|
||||
[if vfmt]
|
||||
fn (scanner mut Scanner) fgen_nl() {
|
||||
scanner.fmt_out.writeln('')
|
||||
scanner.fmt_line_empty = true
|
||||
}
|
||||
|
||||
|
||||
[if vfmt]
|
||||
fn (p mut Parser) fgen(s string) {
|
||||
@ -57,6 +63,18 @@ fn (p mut Parser) fgenln(s string) {
|
||||
p.scanner.fgenln(s)
|
||||
}
|
||||
|
||||
[if vfmt]
|
||||
fn (p mut Parser) fgen_nl() {
|
||||
if p.pass != .main {
|
||||
return
|
||||
}
|
||||
println(p.tok)
|
||||
if p.prev_tok == .line_comment {
|
||||
return
|
||||
}
|
||||
p.scanner.fgen_nl()
|
||||
}
|
||||
|
||||
/*
|
||||
fn (p mut Parser) peek() TokenKind {
|
||||
for {
|
||||
@ -108,8 +126,9 @@ fn (p mut Parser) fnext() {
|
||||
p.fgen(s)
|
||||
}
|
||||
// vfmt: increase indentation on `{` unless it's `{}`
|
||||
mut inc_indent := false
|
||||
if p.tok == .lcbr && !p.inside_if_expr && p.peek() != .rcbr {
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
p.fmt_inc()
|
||||
}
|
||||
|
||||
@ -117,33 +136,51 @@ fn (p mut Parser) fnext() {
|
||||
if p.tokens[p.token_idx].tok in [.line_comment, .mline_comment] {
|
||||
// Newline before the comment and after consts and closing }
|
||||
if p.inside_const {
|
||||
p.fgenln('\n')
|
||||
}
|
||||
if p.tok == .rcbr {
|
||||
p.fgenln('')
|
||||
p.fgen_nl()
|
||||
p.fgen_nl()
|
||||
}
|
||||
is_rcbr := p.tok == .rcbr
|
||||
for p.token_idx < p.tokens.len - 1 {
|
||||
i := p.token_idx
|
||||
tok := p.tokens[p.token_idx].tok
|
||||
if tok != .line_comment && tok != .mline_comment {
|
||||
break
|
||||
}
|
||||
comment_token := p.tokens[p.token_idx]
|
||||
next := p.tokens[p.token_idx+1]
|
||||
comment_on_new_line := p.token_idx == 0 ||
|
||||
comment_token.line_nr > p.tokens[p.token_idx - 1].line_nr
|
||||
//prev_token := p.tokens[p.token_idx - 1]
|
||||
comment := comment_token.lit
|
||||
if p.token_idx > 0 && comment_token.line_nr > p.tokens[p.token_idx-1].line_nr {
|
||||
//p.fgenln('')
|
||||
if i > 0 && p.tokens[i-1].tok != .line_comment &&
|
||||
comment_token.line_nr > p.tokens[i-1].line_nr {
|
||||
p.fgen_nl()
|
||||
}
|
||||
if tok == .line_comment {
|
||||
if !comment_on_new_line { //prev_token.line_nr < comment_token.line_nr {
|
||||
p.fgen(' ')
|
||||
}
|
||||
p.fgen('// ' + comment)
|
||||
/*
|
||||
if false && i > 0 {
|
||||
p.fgen(
|
||||
'pln=${p.tokens[i-1].line_nr} ${comment_token.str()} ' +
|
||||
'line_nr=$comment_token.line_nr next=${next.str()} next_line_nr=$next.line_nr')
|
||||
}
|
||||
*/
|
||||
|
||||
} else {
|
||||
p.fgen(comment)
|
||||
}
|
||||
if p.token_idx > 0 &&
|
||||
comment_token.line_nr < p.tokens[p.token_idx+1].line_nr
|
||||
{
|
||||
p.fgenln('')
|
||||
if next.tok == .line_comment && comment_token.line_nr < next.line_nr {
|
||||
p.fgen_nl()
|
||||
}
|
||||
p.token_idx++
|
||||
}
|
||||
|
||||
if inc_indent {
|
||||
p.fgen_nl()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -162,7 +199,7 @@ fn (p mut Parser) gen_fmt() {
|
||||
if s == '' {
|
||||
return
|
||||
}
|
||||
println('GENERATING ${p.file_name}.V')
|
||||
println('generating ${p.file_name}.v')
|
||||
out := os.create('/var/tmp/fmt/' + p.file_name) or {
|
||||
verror('failed to create fmt.v')
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user