aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Gavin <danielgavin5@hotmail.com>2022-06-25 17:23:26 +0200
committerDaniel Gavin <danielgavin5@hotmail.com>2022-06-25 17:23:26 +0200
commit77e13fb4ecc4486fe2ea00b6a2f9c8b790150842 (patch)
tree948caf07a6915c9581714fbd7690f7cfb8a5ad8e /src
parentff4801dfd8ee5113d1fba64aeede0f7412a91931 (diff)
Removed the fill functionality for odinfmt
Diffstat (limited to 'src')
-rw-r--r--src/odin/printer/document.odin24
-rw-r--r--src/odin/printer/visit.odin107
2 files changed, 61 insertions, 70 deletions
diff --git a/src/odin/printer/document.odin b/src/odin/printer/document.odin
index 1f104a3..1e5ce3a 100644
--- a/src/odin/printer/document.odin
+++ b/src/odin/printer/document.odin
@@ -59,7 +59,6 @@ Document_Align :: struct {
Document_Group_Mode :: enum {
Flat,
Break,
- Fill,
Fit,
}
@@ -162,15 +161,6 @@ group :: proc(grouped_document: ^Document, allocator := context.allocator) -> ^D
return document
}
-fill_group :: proc(grouped_document: ^Document, allocator := context.allocator) -> ^Document {
- document := new(Document, allocator)
- document^ = Document_Group {
- document = grouped_document,
- mode = .Fill,
- }
- return document
-}
-
cons :: proc(lhs: ^Document, rhs: ^Document, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Cons {
@@ -251,8 +241,6 @@ fits :: proc(width: int, list: ^[dynamic]Tuple, consumed: ^int) -> bool {
if data.mode == .Break && v.newline {
consumed^ = start_width - width
return true
- } else if data.mode == .Fill && v.newline && width > 0 {
- return true
} else {
width -= len(v.value)
}
@@ -266,7 +254,8 @@ fits :: proc(width: int, list: ^[dynamic]Tuple, consumed: ^int) -> bool {
}
consumed^ = start_width - width
- return true
+
+ return width <= 0
}
format_newline :: proc(indentation: int, alignment: int, consumed: ^int, builder: ^strings.Builder, p: ^Printer) {
@@ -318,11 +307,6 @@ format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p:
case Document_Break:
if data.mode == .Break && v.newline {
format_newline(data.indentation, data.alignment, &consumed, builder, p)
- } else if data.mode == .Fill && consumed + len(v.value) < width {
- strings.write_string(builder, v.value)
- consumed += len(v.value)
- } else if data.mode == .Fill && v.newline {
- format_newline(data.indentation, data.alignment, &consumed, builder, p)
} else {
strings.write_string(builder, v.value)
consumed += len(v.value)
@@ -349,9 +333,7 @@ format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p:
else if fits(width-consumed, &l, &fits_consumed) && v.mode != .Break {
append(list, Tuple {indentation = data.indentation, mode = .Flat, document = v.document, alignment = data.alignment})
} else {
- if v.mode == .Fill {
- append(list, Tuple {indentation = data.indentation, mode = .Fill, document = v.document, alignment = data.alignment})
- } else if v.mode == .Fit {
+ if v.mode == .Fit {
append(list, Tuple {indentation = data.indentation, mode = .Fit, document = v.document, alignment = data.alignment})
} else {
append(list, Tuple {indentation = data.indentation, mode = .Break, document = v.document, alignment = data.alignment})
diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin
index 9a22c8b..4a279e2 100644
--- a/src/odin/printer/visit.odin
+++ b/src/odin/printer/visit.odin
@@ -307,11 +307,7 @@ visit_decl :: proc(p: ^Printer, decl: ^ast.Decl, called_in_stmt := false) -> ^Do
}
if len(v.values) > 0 {
- if is_values_binary(p, v.values) {
- return cons(document, fill_group(cons_with_opl(group(lhs), align(fill_group(rhs)))))
- } else {
- return cons(document, group(cons_with_nopl(group(lhs), group(rhs))))
- }
+ return cons(document, group(cons_with_nopl(group(lhs), group(rhs))))
} else {
return cons(document, group(lhs))
}
@@ -323,16 +319,6 @@ visit_decl :: proc(p: ^Printer, decl: ^ast.Decl, called_in_stmt := false) -> ^Do
}
@(private)
-is_values_binary :: proc(p: ^Printer, list: []^ast.Expr) -> bool {
- for expr in list {
- if _, bin := expr.derived.(^ast.Binary_Expr); bin {
- return true
- }
- }
- return false
-}
-
-@(private)
visit_exprs :: proc(p: ^Printer, list: []^ast.Expr, options := List_Options{}, called_from: Expr_Called_Type = .Generic) -> ^Document {
if len(list) == 0 {
return empty()
@@ -622,7 +608,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener
}
if v.cond != nil {
- if_document = cons_with_nopl(if_document, fill_group(visit_expr(p, v.cond)))
+ if_document = cons_with_nopl(if_document, group(visit_expr(p, v.cond)))
}
document = cons(document, group(hang(3, if_document)))
@@ -702,11 +688,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener
assign_document := group(cons_with_nopl(visit_exprs(p, v.lhs, {.Add_Comma, .Glue}), text(v.op.text)))
if block_stmt {
- if should_align_assignment_stmt(p, v^) {
- assign_document = fill_group(cons(assign_document, align(cons(break_with_space(), visit_exprs(p, v.rhs, {.Add_Comma}, .Assignment_Stmt)))))
- } else {
- assign_document = fill_group(cons(assign_document, cons(break_with_space(), visit_exprs(p, v.rhs, {.Add_Comma}, .Assignment_Stmt))))
- }
+ assign_document = group(cons(assign_document, nest(p.indentation_count, cons(break_with_space(), visit_exprs(p, v.rhs, {.Add_Comma}, .Assignment_Stmt)))))
} else {
assign_document = cons_with_nopl(assign_document, visit_exprs(p, v.rhs, {.Add_Comma}, .Assignment_Stmt))
}
@@ -731,7 +713,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener
if v.cond != nil {
set_source_position(p, v.cond.pos)
- for_document = cons_with_opl(for_document, fill_group(visit_expr(p, v.cond)))
+ for_document = cons_with_opl(for_document, group(visit_expr(p, v.cond)))
}
if v.post != nil {
@@ -896,20 +878,6 @@ contains_do_in_expression :: proc(p: ^Printer, expr: ^ast.Expr) -> bool {
}
@(private)
-should_align_assignment_stmt :: proc(p: ^Printer, stmt: ast.Assign_Stmt) -> bool {
- if len(stmt.rhs) == 0 {
- return false
- }
-
- for expr in stmt.rhs {
- if _, ok := stmt.rhs[0].derived.(^ast.Binary_Expr); ok {
- return true
- }
- }
- return false
-}
-
-@(private)
push_where_clauses :: proc(p: ^Printer, clauses: []^ast.Expr) -> ^Document {
if len(clauses) == 0 {
return empty()
@@ -1174,11 +1142,12 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr, called_from: Expr_Called_Type =
case ^Selector_Expr:
document = cons(visit_expr(p, v.expr), cons(text_token(p, v.op), visit_expr(p, v.field)))
case ^Paren_Expr:
- document = cons(text("("), cons(visit_expr(p, v.expr), text(")")))
+ fmt.println(p.indentation_count)
+ document = group(cons(text("("), cons(nest(p.indentation_count, visit_expr(p, v.expr)), text(")"))))
case ^Index_Expr:
document = visit_expr(p, v.expr)
document = cons(document, text("["))
- document = cons(document, fill_group(align(visit_expr(p, v.index))))
+ document = cons(document, group(align(visit_expr(p, v.index))))
document = cons(document, text("]"))
case ^Proc_Group:
document = text_token(p, v.tok)
@@ -1512,25 +1481,64 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type) -> ^Document {
}
@(private)
-visit_binary_expr :: proc(p: ^Printer, binary: ast.Binary_Expr) -> ^Document {
- lhs: ^Document
- rhs: ^Document
-
+extract_binary_expr :: proc(list: ^[dynamic]any, binary: ast.Binary_Expr) {
if v, ok := binary.left.derived.(^ast.Binary_Expr); ok {
- lhs = visit_binary_expr(p, v^)
+ extract_binary_expr(list, v^)
} else {
- lhs = visit_expr(p, binary.left)
+ append(list, binary.left)
}
+ append(list, binary.op.text)
+
if v, ok := binary.right.derived.(^ast.Binary_Expr); ok {
- rhs = visit_binary_expr(p, v^)
+ extract_binary_expr(list, v^)
} else {
- rhs = visit_expr(p, binary.right)
+ append(list, binary.right)
}
+}
+
+
+@(private)
+visit_binary_expr :: proc(p: ^Printer, binary: ast.Binary_Expr) -> ^Document {
+ list := make([dynamic]any)
- op := text(binary.op.text)
+ extract_binary_expr(&list, binary)
+
+
+ first_order := empty()
+ second_order := empty()
+
+ last_op := ""
+
+ is_first_order :: proc(s: string) -> bool {
+ return s == "+" || s == "-"
+ }
- return cons_with_nopl(lhs, cons_with_opl(op, rhs))
+ for item, i in list {
+ switch v in item {
+ case ^ast.Expr:
+ if len(list) - 1 == i {
+ if is_first_order(last_op) {
+ first_order = cons(first_order, visit_expr(p, v))
+ } else {
+ second_order = cons(second_order, visit_expr(p, v))
+ first_order = cons(first_order, group(nest(p.indentation_count, second_order)))
+ }
+ } else {
+ last_op := list[i+1].(string)
+
+ if is_first_order(last_op) {
+ first_order = cons(first_order, group(nest(p.indentation_count, second_order)))
+ second_order = empty()
+ first_order = cons(first_order, cons_with_nopl(visit_expr(p, v), cons(text(last_op), break_with_space())))
+ } else {
+ second_order = cons(second_order, cons_with_nopl(visit_expr(p, v), cons(text(last_op), break_with_space())))
+ }
+ }
+ }
+ }
+
+ return group(nest(p.indentation_count, first_order))
}
@(private)
@@ -1543,8 +1551,9 @@ visit_call_exprs :: proc(p: ^Printer, call_expr: ^ast.Call_Expr) -> ^Document {
if i == len(call_expr.args) - 1 && ellipsis {
document = cons(document, text(".."))
}
+
document = cons(document, group(visit_expr(p, expr)))
-
+
if i != len(call_expr.args) - 1 {
document = cons(document, text(","))