vfmt: keep UnsafeExpr linebreacks as set by developer (#7558)

pull/7561/head
Lukas Neubert 2020-12-25 15:52:16 +01:00 committed by GitHub
parent 1605c3b5f8
commit 8cd74df2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 12 deletions

View File

@ -39,8 +39,10 @@ pub fn parse_rfc2822(s string) ?Time {
unsafe { unsafe {
tmstr = malloc(s.len * 2) tmstr = malloc(s.len * 2)
} }
count := unsafe { C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str, count := unsafe {
mm, fields[1].str, fields[4].str) } C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str, mm, fields[1].str,
fields[4].str)
}
return parse(tos(tmstr, count)) return parse(tos(tmstr, count))
} }
@ -66,12 +68,16 @@ fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) {
plus_min_z := `a` plus_min_z := `a`
offset_hour := 0 offset_hour := 0
offset_minute := 0 offset_minute := 0
mut count := unsafe { C.sscanf(charptr(s.str), '%2d:%2d:%2d.%6d%c%2d:%2d', &hour, mut count := unsafe {
&minute, &second, &microsecond, charptr(&plus_min_z), &offset_hour, &offset_minute) } C.sscanf(charptr(s.str), '%2d:%2d:%2d.%6d%c%2d:%2d', &hour, &minute, &second,
&microsecond, charptr(&plus_min_z), &offset_hour, &offset_minute)
}
// Missread microsecond ([Sec Hour Minute].len == 3 < 4) // Missread microsecond ([Sec Hour Minute].len == 3 < 4)
if count < 4 { if count < 4 {
count = unsafe { C.sscanf(charptr(s.str), '%2d:%2d:%2d%c%2d:%2d', &hour, &minute, count = unsafe {
&second, charptr(&plus_min_z), &offset_hour, &offset_minute) } C.sscanf(charptr(s.str), '%2d:%2d:%2d%c%2d:%2d', &hour, &minute, &second,
charptr(&plus_min_z), &offset_hour, &offset_minute)
}
count++ // Increment count because skipped microsecond count++ // Increment count because skipped microsecond
} }
if count < 4 { if count < 4 {

View File

@ -1111,9 +1111,23 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.write(')') f.write(')')
} }
ast.UnsafeExpr { ast.UnsafeExpr {
f.write('unsafe { ') single_line := node.pos.line_nr >= node.pos.last_line
f.write('unsafe {')
if single_line {
f.write(' ')
} else {
f.writeln('')
f.indent++
f.empty_line = true
}
f.expr(node.expr) f.expr(node.expr)
f.write(' }') if single_line {
f.write(' ')
} else {
f.writeln('')
f.indent--
}
f.write('}')
} }
} }
} }

View File

@ -0,0 +1,17 @@
fn main() {
unsafe {
println('hi')
println('hi2')
}
unsafe {
println('qwer')
}
unsafe { 6 }
x := unsafe {
5
}
y := unsafe { 7 }
unsafe {}
unsafe {
}
}

View File

@ -2275,6 +2275,7 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt {
// `unsafe {expr}` // `unsafe {expr}`
if stmt.expr.is_expr() { if stmt.expr.is_expr() {
p.next() p.next()
pos.last_line = p.prev_tok.line_nr - 1
ue := ast.UnsafeExpr{ ue := ast.UnsafeExpr{
expr: stmt.expr expr: stmt.expr
pos: pos pos: pos
@ -2293,7 +2294,6 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt {
for p.tok.kind != .rcbr { for p.tok.kind != .rcbr {
stmts << p.stmt(false) stmts << p.stmt(false)
} }
pos.last_line = p.tok.line_nr
p.next() p.next()
return ast.Block{ return ast.Block{
stmts: stmts stmts: stmts

View File

@ -103,7 +103,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
} }
.key_unsafe { .key_unsafe {
// unsafe { // unsafe {
pos := p.tok.position() mut pos := p.tok.position()
p.next() p.next()
if p.inside_unsafe { if p.inside_unsafe {
p.error_with_pos('already inside `unsafe` block', pos) p.error_with_pos('already inside `unsafe` block', pos)
@ -111,11 +111,13 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
} }
p.inside_unsafe = true p.inside_unsafe = true
p.check(.lcbr) p.check(.lcbr)
e := p.expr(0)
p.check(.rcbr)
pos.last_line = p.prev_tok.line_nr - 1
node = ast.UnsafeExpr{ node = ast.UnsafeExpr{
expr: p.expr(0) expr: e
pos: pos pos: pos
} }
p.check(.rcbr)
p.inside_unsafe = false p.inside_unsafe = false
} }
.key_lock, .key_rlock { .key_lock, .key_rlock {