Skip to content

Commit

Permalink
Remember to go into helper type in semantic tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGavin committed Aug 10, 2024
1 parent 62a7d1d commit 8726748
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 1,019 deletions.
148 changes: 25 additions & 123 deletions src/odin/printer/document.odin
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,15 @@ newline :: proc(amount: int, allocator := context.allocator) -> ^Document {
return document
}

nest :: proc(
nested_document: ^Document,
allocator := context.allocator,
) -> ^Document {
nest :: proc(nested_document: ^Document, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Nest {
document = nested_document,
}
return document
}

escape_nest :: proc(
nested_document: ^Document,
allocator := context.allocator,
) -> ^Document {
escape_nest :: proc(nested_document: ^Document, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Nest {
document = nested_document,
Expand All @@ -120,24 +114,11 @@ escape_nest :: proc(
return document
}

nest_if_break :: proc(
nested_document: ^Document,
group_id := "",
allocator := context.allocator,
) -> ^Document {
return if_break_or_document(
nest(nested_document, allocator),
nested_document,
group_id,
allocator,
)
nest_if_break :: proc(nested_document: ^Document, group_id := "", allocator := context.allocator) -> ^Document {
return if_break_or_document(nest(nested_document, allocator), nested_document, group_id, allocator)
}

hang :: proc(
align: int,
hanged_document: ^Document,
allocator := context.allocator,
) -> ^Document {
hang :: proc(align: int, hanged_document: ^Document, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Nest {
alignment = align,
Expand All @@ -146,10 +127,7 @@ hang :: proc(
return document
}

enforce_fit :: proc(
fitted_document: ^Document,
allocator := context.allocator,
) -> ^Document {
enforce_fit :: proc(fitted_document: ^Document, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Group {
document = fitted_document,
Expand All @@ -158,10 +136,7 @@ enforce_fit :: proc(
return document
}

fill :: proc(
filled_document: ^Document,
allocator := context.allocator,
) -> ^Document {
fill :: proc(filled_document: ^Document, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Group {
document = filled_document,
Expand All @@ -184,10 +159,7 @@ enforce_break :: proc(
return document
}

align :: proc(
aligned_document: ^Document,
allocator := context.allocator,
) -> ^Document {
align :: proc(aligned_document: ^Document, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Align {
document = aligned_document,
Expand All @@ -210,12 +182,7 @@ if_break_or_string :: proc(
group_id := "",
allocator := context.allocator,
) -> ^Document {
return if_break_or_document(
text(break_value, allocator),
text(fit_value, allocator),
group_id,
allocator,
)
return if_break_or_document(text(break_value, allocator), text(fit_value, allocator), group_id, allocator)
}

if_break_or_document :: proc(
Expand All @@ -233,11 +200,7 @@ if_break_or_document :: proc(
return document
}

break_with :: proc(
value: string,
newline := true,
allocator := context.allocator,
) -> ^Document {
break_with :: proc(value: string, newline := true, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Break {
value = value,
Expand All @@ -252,10 +215,7 @@ break_parent :: proc(allocator := context.allocator) -> ^Document {
return document
}

line_suffix :: proc(
value: string,
allocator := context.allocator,
) -> ^Document {
line_suffix :: proc(value: string, allocator := context.allocator) -> ^Document {
document := new(Document, allocator)
document^ = Document_Line_Suffix {
value = value,
Expand Down Expand Up @@ -299,11 +259,7 @@ cons :: proc(elems: ..^Document, allocator := context.allocator) -> ^Document {
return document
}

cons_with_opl :: proc(
lhs: ^Document,
rhs: ^Document,
allocator := context.allocator,
) -> ^Document {
cons_with_opl :: proc(lhs: ^Document, rhs: ^Document, allocator := context.allocator) -> ^Document {
if _, ok := lhs.(Document_Nil); ok {
return rhs
}
Expand All @@ -312,17 +268,10 @@ cons_with_opl :: proc(
return lhs
}

return cons(
elems = {lhs, break_with_space(allocator), rhs},
allocator = allocator,
)
return cons(elems = {lhs, break_with_space(allocator), rhs}, allocator = allocator)
}

cons_with_nopl :: proc(
lhs: ^Document,
rhs: ^Document,
allocator := context.allocator,
) -> ^Document {
cons_with_nopl :: proc(lhs: ^Document, rhs: ^Document, allocator := context.allocator) -> ^Document {
if _, ok := lhs.(Document_Nil); ok {
return rhs
}
Expand All @@ -331,10 +280,7 @@ cons_with_nopl :: proc(
return lhs
}

return cons(
elems = {lhs, break_with_no_newline(allocator), rhs},
allocator = allocator,
)
return cons(elems = {lhs, break_with_no_newline(allocator), rhs}, allocator = allocator)
}

Tuple :: struct {
Expand Down Expand Up @@ -389,12 +335,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
case Document_Align:
append(
list,
Tuple {
indentation = 0,
mode = data.mode,
document = v.document,
alignment = start_width - width,
},
Tuple{indentation = 0, mode = data.mode, document = v.document, alignment = start_width - width},
)

case Document_Nest:
Expand Down Expand Up @@ -466,13 +407,7 @@ fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool {
return width > 0
}

format_newline :: proc(
indentation: int,
alignment: int,
consumed: ^int,
builder: ^strings.Builder,
p: ^Printer,
) {
format_newline :: proc(indentation: int, alignment: int, consumed: ^int, builder: ^strings.Builder, p: ^Printer) {
strings.write_string(builder, p.newline)
for i := 0; i < indentation; i += 1 {
strings.write_string(builder, p.indentation)
Expand All @@ -484,20 +419,12 @@ format_newline :: proc(
consumed^ = indentation * p.indentation_width + alignment
}

flush_line_suffix :: proc(
builder: ^strings.Builder,
suffix_builder: ^strings.Builder,
) {
flush_line_suffix :: proc(builder: ^strings.Builder, suffix_builder: ^strings.Builder) {
strings.write_string(builder, strings.to_string(suffix_builder^))
strings.builder_reset(suffix_builder)
}

format :: proc(
width: int,
list: ^[dynamic]Tuple,
builder: ^strings.Builder,
p: ^Printer,
) {
format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p: ^Printer) {
assert(list != nil)
assert(builder != nil)

Expand Down Expand Up @@ -528,8 +455,7 @@ format :: proc(
for i := 0; i < data.alignment; i += 1 {
strings.write_string(builder, " ")
}
consumed =
data.indentation * p.indentation_width + data.alignment
consumed = data.indentation * p.indentation_width + data.alignment

if data.mode == .Flat {
recalculate = true
Expand Down Expand Up @@ -574,38 +500,21 @@ format :: proc(
align := consumed - data.indentation * p.indentation_width
append(
list,
Tuple {
indentation = data.indentation,
mode = data.mode,
document = v.document,
alignment = align,
},
Tuple{indentation = data.indentation, mode = data.mode, document = v.document, alignment = align},
)
case Document_Text:
strings.write_string(builder, v.value)
consumed += len(v.value)
case Document_Break:
if data.mode == .Break && v.newline {
flush_line_suffix(builder, &suffix_builder)
format_newline(
data.indentation,
data.alignment,
&consumed,
builder,
p,
)
format_newline(data.indentation, data.alignment, &consumed, builder, p)
} else if data.mode == .Fill && consumed < width {
strings.write_string(builder, v.value)
consumed += len(v.value)
} else if data.mode == .Fill && v.newline {
flush_line_suffix(builder, &suffix_builder)
format_newline(
data.indentation,
data.alignment,
&consumed,
builder,
p,
)
format_newline(data.indentation, data.alignment, &consumed, builder, p)
} else {
strings.write_string(builder, v.value)
consumed += len(v.value)
Expand Down Expand Up @@ -655,12 +564,7 @@ format :: proc(

append(
&list_fits,
Tuple {
indentation = data.indentation,
mode = .Flat,
document = v.document,
alignment = data.alignment,
},
Tuple{indentation = data.indentation, mode = .Flat, document = v.document, alignment = data.alignment},
)

recalculate = false
Expand All @@ -675,9 +579,7 @@ format :: proc(
alignment = data.alignment,
},
)
} else if fits(width - consumed, &list_fits) &&
v.mode != .Break &&
v.mode != .Fit {
} else if fits(width - consumed, &list_fits) && v.mode != .Break && v.mode != .Fit {
append(
list,
Tuple {
Expand Down
38 changes: 8 additions & 30 deletions src/odin/printer/printer.odin
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ when ODIN_OS == .Windows {
}
}

make_printer :: proc(
config: Config,
allocator := context.allocator,
) -> Printer {
make_printer :: proc(config: Config, allocator := context.allocator) -> Printer {
return {config = config, allocator = allocator}
}

Expand All @@ -131,19 +128,13 @@ build_disabled_lines_info :: proc(p: ^Printer) {

for group in p.comments {
for comment in group.list {
comment_text, _ := strings.replace_all(
comment.text[:],
" ",
"",
context.temp_allocator,
)
comment_text, _ := strings.replace_all(comment.text[:], " ", "", context.temp_allocator)

if strings.contains(comment_text, "//odinfmt:disable") {
found_disable = true
empty = true
disable_position = comment.pos
} else if strings.contains(comment_text, "//odinfmt:enable") &&
found_disable {
} else if strings.contains(comment_text, "//odinfmt:enable") && found_disable {
begin := disable_position.offset - (comment.pos.column - 1)
end := comment.pos.offset + len(comment.text)

Expand All @@ -154,9 +145,7 @@ build_disabled_lines_info :: proc(p: ^Printer) {
empty = empty,
}

for line := disable_position.line;
line <= comment.pos.line;
line += 1 {
for line := disable_position.line; line <= comment.pos.line; line += 1 {
p.disabled_lines[line] = disabled_info
}

Expand All @@ -168,11 +157,7 @@ build_disabled_lines_info :: proc(p: ^Printer) {
}

@(private)
set_comment_option :: proc(
p: ^Printer,
line: int,
option: Line_Suffix_Option,
) {
set_comment_option :: proc(p: ^Printer, line: int, option: Line_Suffix_Option) {
p.comments_option[line] = option
}

Expand Down Expand Up @@ -222,19 +207,15 @@ print_file :: proc(p: ^Printer, file: ^ast.File) -> string {
p.source_position.column = 1

p.document = move_line(p, file.pkg_token.pos)
p.document = cons(
p.document,
cons_with_nopl(text(file.pkg_token.text), text(file.pkg_name)),
)
p.document = cons(p.document, cons_with_nopl(text(file.pkg_token.text), text(file.pkg_name)))

// Keep track of the first import in a row, to sort them later.
import_group_start: Maybe(int)

for decl, i in file.decls {
decl := cast(^ast.Decl)decl

if imp, is_import := decl.derived.(^ast.Import_Decl);
p.config.sort_imports && is_import {
if imp, is_import := decl.derived.(^ast.Import_Decl); p.config.sort_imports && is_import {
// First import in this group.
if import_group_start == nil {
import_group_start = i
Expand Down Expand Up @@ -284,10 +265,7 @@ print_sorted_imports :: proc(p: ^Printer, decls: []^ast.Stmt) {
start_line := decls[0].pos.line

slice.stable_sort_by(decls, proc(imp1, imp2: ^ast.Stmt) -> bool {
return(
imp1.derived.(^ast.Import_Decl).fullpath <
imp2.derived.(^ast.Import_Decl).fullpath \
)
return imp1.derived.(^ast.Import_Decl).fullpath < imp2.derived.(^ast.Import_Decl).fullpath
})

for decl, i in decls {
Expand Down
Loading

0 comments on commit 8726748

Please sign in to comment.