printer.odin 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. package odin_printer
  2. import "core:odin/ast"
  3. import "core:odin/tokenizer"
  4. import "core:strings"
  5. import "core:fmt"
  6. import "core:mem"
  7. Type_Enum :: enum {Line_Comment, Value_Decl, Switch_Stmt, Struct, Assign, Call, Enum, If, For, Proc_Lit}
  8. Line_Type :: bit_set[Type_Enum]
  9. /*
  10. Represents an unwrapped line
  11. */
  12. Line :: struct {
  13. format_tokens: [dynamic]Format_Token,
  14. finalized: bool,
  15. used: bool,
  16. depth: int,
  17. types: Line_Type, //for performance, so you don't have to verify what types are in it by going through the tokens - might give problems when adding linebreaking
  18. }
  19. /*
  20. Represents a singular token in a unwrapped line
  21. */
  22. Format_Token :: struct {
  23. kind: tokenizer.Token_Kind,
  24. text: string,
  25. type: Type_Enum,
  26. spaces_before: int,
  27. parameter_count: int,
  28. }
  29. Printer :: struct {
  30. string_builder: strings.Builder,
  31. config: Config,
  32. depth: int, //the identation depth
  33. comments: [dynamic]^ast.Comment_Group,
  34. latest_comment_index: int,
  35. allocator: mem.Allocator,
  36. file: ^ast.File,
  37. source_position: tokenizer.Pos,
  38. last_source_position: tokenizer.Pos,
  39. lines: [dynamic]Line, //need to look into a better data structure, one that can handle inserting lines rather than appending
  40. skip_semicolon: bool,
  41. current_line: ^Line,
  42. current_line_index: int,
  43. last_line_index: int,
  44. last_token: ^Format_Token,
  45. merge_next_token: bool,
  46. space_next_token: bool,
  47. debug: bool,
  48. }
  49. Config :: struct {
  50. spaces: int, //Spaces per indentation
  51. newline_limit: int, //The limit of newlines between statements and declarations.
  52. tabs: bool, //Enable or disable tabs
  53. convert_do: bool, //Convert all do statements to brace blocks
  54. semicolons: bool, //Enable semicolons
  55. split_multiple_stmts: bool,
  56. align_switch: bool,
  57. brace_style: Brace_Style,
  58. align_assignments: bool,
  59. align_structs: bool,
  60. align_style: Alignment_Style,
  61. align_enums: bool,
  62. align_length_break: int,
  63. indent_cases: bool,
  64. newline_style: Newline_Style,
  65. }
  66. Brace_Style :: enum {
  67. _1TBS,
  68. Allman,
  69. Stroustrup,
  70. K_And_R,
  71. }
  72. Block_Type :: enum {
  73. None,
  74. If_Stmt,
  75. Proc,
  76. Generic,
  77. Comp_Lit,
  78. Switch_Stmt,
  79. }
  80. Alignment_Style :: enum {
  81. Align_On_Type_And_Equals,
  82. Align_On_Colon_And_Equals,
  83. }
  84. Newline_Style :: enum {
  85. CRLF,
  86. LF,
  87. }
  88. default_style := Config {
  89. spaces = 4,
  90. newline_limit = 2,
  91. convert_do = false,
  92. semicolons = false,
  93. tabs = true,
  94. brace_style = ._1TBS,
  95. split_multiple_stmts = true,
  96. align_assignments = true,
  97. align_style = .Align_On_Type_And_Equals,
  98. indent_cases = false,
  99. align_switch = true,
  100. align_structs = true,
  101. align_enums = true,
  102. newline_style = .CRLF,
  103. align_length_break = 9,
  104. }
  105. make_printer :: proc(config: Config, allocator := context.allocator) -> Printer {
  106. return {
  107. config = config,
  108. allocator = allocator,
  109. debug = false,
  110. }
  111. }
  112. print :: proc(p: ^Printer, file: ^ast.File) -> string {
  113. p.comments = file.comments
  114. if len(file.decls) > 0 {
  115. p.lines = make([dynamic]Line, 0, (file.decls[len(file.decls) - 1].end.line - file.decls[0].pos.line) * 2, context.temp_allocator)
  116. }
  117. set_source_position(p, file.pkg_token.pos)
  118. p.last_source_position.line = 1
  119. set_line(p, 0)
  120. push_generic_token(p, .Package, 0)
  121. push_ident_token(p, file.pkg_name, 1)
  122. for decl in file.decls {
  123. visit_decl(p, cast(^ast.Decl)decl)
  124. }
  125. if len(p.comments) > 0 {
  126. infinite := p.comments[len(p.comments) - 1].end
  127. infinite.offset = 9999999
  128. push_comments(p, infinite)
  129. }
  130. fix_lines(p)
  131. builder := strings.builder_make(0, 5 * mem.Megabyte, p.allocator)
  132. last_line := 0
  133. newline: string
  134. if p.config.newline_style == .LF {
  135. newline = "\n"
  136. } else {
  137. newline = "\r\n"
  138. }
  139. for line, line_index in p.lines {
  140. diff_line := line_index - last_line
  141. for i := 0; i < diff_line; i += 1 {
  142. strings.write_string(&builder, newline)
  143. }
  144. if p.config.tabs {
  145. for i := 0; i < line.depth; i += 1 {
  146. strings.write_byte(&builder, '\t')
  147. }
  148. } else {
  149. for i := 0; i < line.depth * p.config.spaces; i += 1 {
  150. strings.write_byte(&builder, ' ')
  151. }
  152. }
  153. if p.debug {
  154. strings.write_string(&builder, fmt.tprintf("line %v: ", line_index))
  155. }
  156. for format_token in line.format_tokens {
  157. for i := 0; i < format_token.spaces_before; i += 1 {
  158. strings.write_byte(&builder, ' ')
  159. }
  160. strings.write_string(&builder, format_token.text)
  161. }
  162. last_line = line_index
  163. }
  164. strings.write_string(&builder, newline)
  165. return strings.to_string(builder)
  166. }
  167. fix_lines :: proc(p: ^Printer) {
  168. align_var_decls(p)
  169. format_generic(p)
  170. align_comments(p) //align them last since they rely on the other alignments
  171. }
  172. format_value_decl :: proc(p: ^Printer, index: int) {
  173. eq_found := false
  174. eq_token: Format_Token
  175. eq_line: int
  176. largest := 0
  177. found_eq: for line, line_index in p.lines[index:] {
  178. for format_token in line.format_tokens {
  179. largest += len(format_token.text) + format_token.spaces_before
  180. if format_token.kind == .Eq {
  181. eq_token = format_token
  182. eq_line = line_index + index
  183. eq_found = true
  184. break found_eq
  185. }
  186. }
  187. }
  188. if !eq_found {
  189. return
  190. }
  191. align_next := false
  192. //check to see if there is a binary operator in the last token(this is guaranteed by the ast visit), otherwise it's not multilined
  193. for line in p.lines[eq_line:] {
  194. if len(line.format_tokens) == 0 {
  195. break
  196. }
  197. if align_next {
  198. line.format_tokens[0].spaces_before = largest + 1
  199. align_next = false
  200. }
  201. kind := find_last_token(line.format_tokens).kind
  202. if tokenizer.Token_Kind.B_Operator_Begin < kind && kind <= tokenizer.Token_Kind.Cmp_Or {
  203. align_next = true
  204. }
  205. if !align_next {
  206. break
  207. }
  208. }
  209. }
  210. find_last_token :: proc(format_tokens: [dynamic]Format_Token) -> Format_Token {
  211. for i := len(format_tokens) - 1; i >= 0; i -= 1 {
  212. if format_tokens[i].kind != .Comment {
  213. return format_tokens[i]
  214. }
  215. }
  216. panic("not possible")
  217. }
  218. format_assignment :: proc(p: ^Printer, index: int) {
  219. }
  220. format_call :: proc(p: ^Printer, line_index: int, format_index: int) {
  221. paren_found := false
  222. paren_token: Format_Token
  223. paren_line: int
  224. paren_token_index: int
  225. largest := 0
  226. found_paren: for line, i in p.lines[line_index:] {
  227. for format_token, j in line.format_tokens {
  228. largest += len(format_token.text) + format_token.spaces_before
  229. if i == 0 && j < format_index {
  230. continue
  231. }
  232. if format_token.kind == .Open_Paren && format_token.type == .Call {
  233. paren_token = format_token
  234. paren_line = line_index + i
  235. paren_found = true
  236. paren_token_index = j
  237. break found_paren
  238. }
  239. }
  240. }
  241. if !paren_found {
  242. panic("Should not be possible")
  243. }
  244. paren_count := 1
  245. done := false
  246. for line in p.lines[paren_line:] {
  247. if len(line.format_tokens) == 0 {
  248. continue
  249. }
  250. for format_token, i in line.format_tokens {
  251. if format_token.kind == .Comment {
  252. continue
  253. }
  254. if line_index == 0 && i <= paren_token_index {
  255. continue
  256. }
  257. if format_token.kind == .Open_Paren {
  258. paren_count += 1
  259. } else if format_token.kind == .Close_Paren {
  260. paren_count -= 1
  261. }
  262. if paren_count == 0 {
  263. done = true
  264. }
  265. }
  266. if line_index != 0 {
  267. line.format_tokens[0].spaces_before = largest
  268. }
  269. if done {
  270. return
  271. }
  272. }
  273. }
  274. format_keyword_to_brace :: proc(p: ^Printer, line_index: int, format_index: int, keyword: tokenizer.Token_Kind) {
  275. keyword_found := false
  276. keyword_token: Format_Token
  277. keyword_line: int
  278. largest := 0
  279. brace_count := 0
  280. done := false
  281. found_keyword: for line, i in p.lines[line_index:] {
  282. for format_token in line.format_tokens {
  283. largest += len(format_token.text) + format_token.spaces_before
  284. if format_token.kind == keyword {
  285. keyword_token = format_token
  286. keyword_line = line_index + i
  287. keyword_found = true
  288. break found_keyword
  289. }
  290. }
  291. }
  292. if !keyword_found {
  293. panic("Should not be possible")
  294. }
  295. for line, line_idx in p.lines[keyword_line:] {
  296. if len(line.format_tokens) == 0 {
  297. continue
  298. }
  299. for format_token, i in line.format_tokens {
  300. if format_token.kind == .Comment {
  301. break
  302. } else if format_token.kind == .Undef {
  303. return
  304. }
  305. if line_idx == 0 && i <= format_index {
  306. continue
  307. }
  308. if format_token.kind == .Open_Brace {
  309. brace_count += 1
  310. } else if format_token.kind == .Close_Brace {
  311. brace_count -= 1
  312. }
  313. if brace_count == 1 {
  314. done = true
  315. }
  316. }
  317. if line_idx != 0 {
  318. line.format_tokens[0].spaces_before = largest + 1
  319. }
  320. if done {
  321. return
  322. }
  323. }
  324. }
  325. format_generic :: proc(p: ^Printer) {
  326. next_struct_line := 0
  327. for line, line_index in p.lines {
  328. if len(line.format_tokens) <= 0 {
  329. continue
  330. }
  331. for format_token, token_index in line.format_tokens {
  332. #partial switch format_token.kind {
  333. case .For, .If, .When, .Switch:
  334. format_keyword_to_brace(p, line_index, token_index, format_token.kind)
  335. case .Proc:
  336. if format_token.type == .Proc_Lit {
  337. format_keyword_to_brace(p, line_index, token_index, format_token.kind)
  338. }
  339. case:
  340. if format_token.type == .Call {
  341. format_call(p, line_index, token_index)
  342. }
  343. }
  344. }
  345. if .Switch_Stmt in line.types && p.config.align_switch {
  346. align_switch_stmt(p, line_index)
  347. }
  348. if .Enum in line.types && p.config.align_enums {
  349. align_enum(p, line_index)
  350. }
  351. if .Struct in line.types && p.config.align_structs && next_struct_line <= 0 {
  352. next_struct_line = align_struct(p, line_index)
  353. }
  354. if .Value_Decl in line.types {
  355. format_value_decl(p, line_index)
  356. }
  357. if .Assign in line.types {
  358. format_assignment(p, line_index)
  359. }
  360. next_struct_line -= 1
  361. }
  362. }
  363. align_var_decls :: proc(p: ^Printer) {
  364. current_line: int
  365. current_typed: bool
  366. current_not_mutable: bool
  367. largest_lhs := 0
  368. largest_rhs := 0
  369. TokenAndLength :: struct {
  370. format_token: ^Format_Token,
  371. length: int,
  372. }
  373. colon_tokens := make([dynamic]TokenAndLength, 0, 10, context.temp_allocator)
  374. type_tokens := make([dynamic]TokenAndLength, 0, 10, context.temp_allocator)
  375. equal_tokens := make([dynamic]TokenAndLength, 0, 10, context.temp_allocator)
  376. for line, line_index in p.lines {
  377. //It is only possible to align value decls that are one one line, otherwise just ignore them
  378. if .Value_Decl not_in line.types {
  379. continue
  380. }
  381. typed := true
  382. not_mutable := false
  383. continue_flag := false
  384. for i := 0; i < len(line.format_tokens); i += 1 {
  385. if line.format_tokens[i].kind == .Colon && line.format_tokens[min(i + 1, len(line.format_tokens) - 1)].kind == .Eq {
  386. typed = false
  387. }
  388. if line.format_tokens[i].kind == .Colon && line.format_tokens[min(i + 1, len(line.format_tokens) - 1)].kind == .Colon {
  389. not_mutable = true
  390. }
  391. if line.format_tokens[i].kind == .Union ||
  392. line.format_tokens[i].kind == .Enum ||
  393. line.format_tokens[i].kind == .Struct ||
  394. line.format_tokens[i].kind == .For ||
  395. line.format_tokens[i].kind == .If ||
  396. line.format_tokens[i].kind == .Comment {
  397. continue_flag = true
  398. }
  399. //enforced undef is always on the last line, if it exists
  400. if line.format_tokens[i].kind == .Proc && line.format_tokens[len(line.format_tokens)-1].kind != .Undef {
  401. continue_flag = true
  402. }
  403. }
  404. if continue_flag {
  405. continue
  406. }
  407. if line_index != current_line + 1 || typed != current_typed || not_mutable != current_not_mutable {
  408. if p.config.align_style == .Align_On_Colon_And_Equals || !current_typed || current_not_mutable {
  409. for colon_token in colon_tokens {
  410. colon_token.format_token.spaces_before = largest_lhs - colon_token.length + 1
  411. }
  412. } else if p.config.align_style == .Align_On_Type_And_Equals {
  413. for type_token in type_tokens {
  414. type_token.format_token.spaces_before = largest_lhs - type_token.length + 1
  415. }
  416. }
  417. if current_typed {
  418. for equal_token in equal_tokens {
  419. equal_token.format_token.spaces_before = largest_rhs - equal_token.length + 1
  420. }
  421. } else {
  422. for equal_token in equal_tokens {
  423. equal_token.format_token.spaces_before = 0
  424. }
  425. }
  426. clear(&colon_tokens)
  427. clear(&type_tokens)
  428. clear(&equal_tokens)
  429. largest_rhs = 0
  430. largest_lhs = 0
  431. current_typed = typed
  432. current_not_mutable = not_mutable
  433. }
  434. current_line = line_index
  435. current_token_index := 0
  436. lhs_length := 0
  437. rhs_length := 0
  438. //calcuate the length of lhs of a value decl i.e. `a, b:`
  439. for; current_token_index < len(line.format_tokens); current_token_index += 1 {
  440. lhs_length += len(line.format_tokens[current_token_index].text) + line.format_tokens[current_token_index].spaces_before
  441. if line.format_tokens[current_token_index].kind == .Colon {
  442. append(&colon_tokens, TokenAndLength {format_token = &line.format_tokens[current_token_index], length = lhs_length})
  443. if len(line.format_tokens) > current_token_index && line.format_tokens[current_token_index + 1].kind != .Eq {
  444. append(&type_tokens, TokenAndLength {format_token = &line.format_tokens[current_token_index + 1], length = lhs_length})
  445. }
  446. current_token_index += 1
  447. largest_lhs = max(largest_lhs, lhs_length)
  448. break
  449. }
  450. }
  451. //calcuate the length of the rhs i.e. `[dynamic]int = 123123`
  452. for; current_token_index < len(line.format_tokens); current_token_index += 1 {
  453. rhs_length += len(line.format_tokens[current_token_index].text) + line.format_tokens[current_token_index].spaces_before
  454. if line.format_tokens[current_token_index].kind == .Eq {
  455. append(&equal_tokens, TokenAndLength {format_token = &line.format_tokens[current_token_index], length = rhs_length})
  456. largest_rhs = max(largest_rhs, rhs_length)
  457. break
  458. }
  459. }
  460. }
  461. //repeating myself, move to sub procedure
  462. if p.config.align_style == .Align_On_Colon_And_Equals || !current_typed || current_not_mutable {
  463. for colon_token in colon_tokens {
  464. colon_token.format_token.spaces_before = largest_lhs - colon_token.length + 1
  465. }
  466. } else if p.config.align_style == .Align_On_Type_And_Equals {
  467. for type_token in type_tokens {
  468. type_token.format_token.spaces_before = largest_lhs - type_token.length + 1
  469. }
  470. }
  471. if current_typed {
  472. for equal_token in equal_tokens {
  473. equal_token.format_token.spaces_before = largest_rhs - equal_token.length + 1
  474. }
  475. } else {
  476. for equal_token in equal_tokens {
  477. equal_token.format_token.spaces_before = 0
  478. }
  479. }
  480. }
  481. align_switch_stmt :: proc(p: ^Printer, index: int) {
  482. switch_found := false
  483. brace_token: Format_Token
  484. brace_line: int
  485. found_switch_brace: for line, line_index in p.lines[index:] {
  486. for format_token in line.format_tokens {
  487. if format_token.kind == .Open_Brace && switch_found {
  488. brace_token = format_token
  489. brace_line = line_index + index
  490. break found_switch_brace
  491. } else if format_token.kind == .Open_Brace {
  492. break
  493. } else if format_token.kind == .Switch {
  494. switch_found = true
  495. }
  496. }
  497. }
  498. if !switch_found {
  499. return
  500. }
  501. largest := 0
  502. case_count := 0
  503. TokenAndLength :: struct {
  504. format_token: ^Format_Token,
  505. length: int,
  506. }
  507. format_tokens := make([dynamic]TokenAndLength, 0, brace_token.parameter_count, context.temp_allocator)
  508. //find all the switch cases that are one lined
  509. for line, line_index in p.lines[brace_line + 1:] {
  510. case_found := false
  511. colon_found := false
  512. length := 0
  513. for format_token, i in line.format_tokens {
  514. if format_token.kind == .Comment {
  515. break
  516. }
  517. //this will only happen if the case is one lined
  518. if case_found && colon_found {
  519. append(&format_tokens, TokenAndLength {format_token = &line.format_tokens[i], length = length})
  520. largest = max(length, largest)
  521. break
  522. }
  523. if format_token.kind == .Case {
  524. case_found = true
  525. case_count += 1
  526. } else if format_token.kind == .Colon {
  527. colon_found = true
  528. }
  529. length += len(format_token.text) + format_token.spaces_before
  530. }
  531. if case_count >= brace_token.parameter_count {
  532. break
  533. }
  534. }
  535. for token in format_tokens {
  536. token.format_token.spaces_before = largest - token.length + 1
  537. }
  538. }
  539. align_enum :: proc(p: ^Printer, index: int) {
  540. enum_found := false
  541. brace_token: Format_Token
  542. brace_line: int
  543. found_enum_brace: for line, line_index in p.lines[index:] {
  544. for format_token in line.format_tokens {
  545. if format_token.kind == .Open_Brace && enum_found {
  546. brace_token = format_token
  547. brace_line = line_index + index
  548. break found_enum_brace
  549. } else if format_token.kind == .Open_Brace {
  550. break
  551. } else if format_token.kind == .Enum {
  552. enum_found = true
  553. }
  554. }
  555. }
  556. if !enum_found {
  557. return
  558. }
  559. largest := 0
  560. comma_count := 0
  561. TokenAndLength :: struct {
  562. format_token: ^Format_Token,
  563. length: int,
  564. }
  565. format_tokens := make([dynamic]TokenAndLength, 0, brace_token.parameter_count, context.temp_allocator)
  566. for line, line_index in p.lines[brace_line + 1:] {
  567. length := 0
  568. for format_token, i in line.format_tokens {
  569. if format_token.kind == .Comment {
  570. break
  571. }
  572. if format_token.kind == .Eq {
  573. append(&format_tokens, TokenAndLength {format_token = &line.format_tokens[i], length = length})
  574. largest = max(length, largest)
  575. break
  576. } else if format_token.kind == .Comma {
  577. comma_count += 1
  578. }
  579. length += len(format_token.text) + format_token.spaces_before
  580. }
  581. if comma_count >= brace_token.parameter_count {
  582. break
  583. }
  584. }
  585. for token in format_tokens {
  586. token.format_token.spaces_before = largest - token.length + 1
  587. }
  588. }
  589. align_struct :: proc(p: ^Printer, index: int) -> int {
  590. struct_found := false
  591. brace_token: Format_Token
  592. brace_line: int
  593. found_struct_brace: for line, line_index in p.lines[index:] {
  594. for format_token in line.format_tokens {
  595. if format_token.kind == .Open_Brace && struct_found {
  596. brace_token = format_token
  597. brace_line = line_index + index
  598. break found_struct_brace
  599. } else if format_token.kind == .Open_Brace {
  600. break
  601. } else if format_token.kind == .Struct {
  602. struct_found = true
  603. }
  604. }
  605. }
  606. if !struct_found {
  607. return 0
  608. }
  609. largest := 0
  610. colon_count := 0
  611. nested := false
  612. seen_brace := false
  613. TokenAndLength :: struct {
  614. format_token: ^Format_Token,
  615. length: int,
  616. }
  617. format_tokens := make([]TokenAndLength, brace_token.parameter_count, context.temp_allocator)
  618. if brace_token.parameter_count == 0 {
  619. return 0
  620. }
  621. end_line_index := 0
  622. for line, line_index in p.lines[brace_line + 1:] {
  623. length := 0
  624. for format_token, i in line.format_tokens {
  625. //give up on nested structs
  626. if format_token.kind == .Comment {
  627. break
  628. } else if format_token.kind == .Open_Paren {
  629. break
  630. } else if format_token.kind == .Open_Brace {
  631. seen_brace = true
  632. } else if format_token.kind == .Close_Brace {
  633. seen_brace = false
  634. } else if seen_brace {
  635. continue
  636. }
  637. if format_token.kind == .Colon {
  638. format_tokens[colon_count] = {format_token = &line.format_tokens[i + 1], length = length}
  639. if format_tokens[colon_count].format_token.kind == .Struct {
  640. nested = true
  641. }
  642. colon_count += 1
  643. largest = max(length, largest)
  644. }
  645. length += len(format_token.text) + format_token.spaces_before
  646. }
  647. if nested {
  648. end_line_index = line_index + brace_line + 1
  649. }
  650. if colon_count >= brace_token.parameter_count {
  651. break
  652. }
  653. }
  654. //give up aligning nested, it never looks good
  655. if nested {
  656. for line, line_index in p.lines[end_line_index:] {
  657. for format_token in line.format_tokens {
  658. if format_token.kind == .Close_Brace {
  659. return end_line_index + line_index - index
  660. }
  661. }
  662. }
  663. }
  664. for token in format_tokens {
  665. token.format_token.spaces_before = largest - token.length + 1
  666. }
  667. return 0
  668. }
  669. align_comments :: proc(p: ^Printer) {
  670. Comment_Align_Info :: struct {
  671. length: int,
  672. begin: int,
  673. end: int,
  674. depth: int,
  675. }
  676. comment_infos := make([dynamic]Comment_Align_Info, 0, context.temp_allocator)
  677. current_info: Comment_Align_Info
  678. for line, line_index in p.lines {
  679. if len(line.format_tokens) <= 0 {
  680. continue
  681. }
  682. if .Line_Comment in line.types {
  683. if current_info.end + 1 != line_index || current_info.depth != line.depth ||
  684. (current_info.begin == current_info.end && current_info.length == 0) {
  685. if (current_info.begin != 0 && current_info.end != 0) || current_info.length > 0 {
  686. append(&comment_infos, current_info)
  687. }
  688. current_info.begin = line_index
  689. current_info.end = line_index
  690. current_info.depth = line.depth
  691. current_info.length = 0
  692. }
  693. length := 0
  694. for format_token, i in line.format_tokens {
  695. if format_token.kind == .Comment {
  696. current_info.length = max(current_info.length, length)
  697. current_info.end = line_index
  698. }
  699. length += format_token.spaces_before + len(format_token.text)
  700. }
  701. }
  702. }
  703. if (current_info.begin != 0 && current_info.end != 0) || current_info.length > 0 {
  704. append(&comment_infos, current_info)
  705. }
  706. for info in comment_infos {
  707. if info.begin == info.end || info.length == 0 {
  708. continue
  709. }
  710. for i := info.begin; i <= info.end; i += 1 {
  711. l := p.lines[i]
  712. length := 0
  713. for format_token in l.format_tokens {
  714. if format_token.kind == .Comment {
  715. if len(l.format_tokens) == 1 {
  716. l.format_tokens[i].spaces_before = info.length + 1
  717. } else {
  718. l.format_tokens[i].spaces_before = info.length - length + 1
  719. }
  720. }
  721. length += format_token.spaces_before + len(format_token.text)
  722. }
  723. }
  724. }
  725. }