trees-lang 0.2.5

Official impl of Trees (programming-language), supporting only parsing code now
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
/// Module containing error types for the compiler.
pub mod errors;

use std::cmp::Ordering;

use errors::CompileError;
use unicode_width::UnicodeWidthStr;

/// Stores settings used during code compilation.
///
/// This struct is used to configure how character widths are interpreted during the
/// compilation process for now.
///
/// # Example
///
/// ```rust
/// use trees_lang::compile::{CompileConfig, CharWidthMode};
///
/// let config = CompileConfig {
///     char_width: CharWidthMode::Full,
/// };
/// ```
///
/// This configuration can then be passed to compilation functions such as `split_code`
/// or `find_blocks` to control how character positions and widths are calculated.
#[derive(Debug, Clone)]
pub struct CompileConfig {
  /// Character width mode used during compilation.
  pub char_width: CharWidthMode,
}

impl CompileConfig {
  /// Default setup for compile
  pub const DEFAULT: CompileConfig = CompileConfig {
    char_width: CharWidthMode::Mono,
  };
}

/// Determines how character widths are calculated during code parsing.
///
/// This enum controls how ambiguous-width characters (such as those in East Asian scripts)
/// are interpreted during layout calculations. It affects how each character contributes to
/// the horizontal spacing of visual elements.
#[derive(Debug, Clone)]
pub enum CharWidthMode {
  /// Treat all characters as width 1.
  Mono,
  /// Treat ambiguous-width characters as half-width.
  Half,
  /// Treat ambiguous-width characters as full-width.
  Full,
}

/// A single character in the source code with layout metadata.
///
/// Used internally to track the character, its x-position, and width in a parsed line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeCharacter {
  /// The character itself.
  pub char: String,
  /// The x position (column) of the character.
  pub x: usize,
  /// The display width of the character.
  pub len: usize,
}

/// A code that has been split into lines and characters for processing
///
/// # Example
/// ```
/// use trees_lang::compile::{SplitedCode, split_code, CharWidthMode, CompileConfig, CodeCharacter};
///
/// // Split each character
/// let mut splited_code: SplitedCode = split_code(
///   &vec![" ┌─".to_owned()],
///   &CompileConfig {
///     char_width: CharWidthMode::Mono
///   }
/// );
///
/// // Get each characters' position
/// // (It is useful if char_width is not Mono)
/// assert_eq!(splited_code.enumurate_x(0).collect::<Vec<_>>(), vec![0, 1, 2]);
///
/// // Get char of target position
/// assert_eq!(splited_code.get(1, 0), Some(CodeCharacter {
///   char: "┌".to_owned(), x: 1, len: 1
/// }));
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SplitedCode {
  body: Vec<Vec<CodeCharacter>>,
}

impl SplitedCode {
  /// Retrieves a specific character at position `(x, y)` from the split code.
  ///
  /// If the position is out of bounds or does not contain a character, returns `None`.
  pub fn get(&self, x: usize, y: usize) -> Option<CodeCharacter> {
    self.body.get(y)?.iter().find(|cc| cc.x == x).cloned()
  }

  /// Retrieves a slice of characters from the specified line between `x_min_exclusive` and `x_max_exclusive`.
  ///
  /// If the range is invalid or out of bounds, returns `None`.
  pub fn get_slice_of_line(&self, x_min_exclusive: usize, x_max_exclusive: usize, y: usize) -> Option<String> {
    let (mut index, _) = self.body.get(y)?.iter().enumerate().find(|(_index, cc)| cc.x == x_min_exclusive)?;
    let mut return_str = "".to_string();

    index += 1;

    while let Some(cc) = self.body.get(y)?.get(index) {
      match cc.x.cmp(&x_max_exclusive) {
        Ordering::Equal => break,
        Ordering::Greater => return None,
        Ordering::Less => {}
      }
      return_str += &cc.char;

      index += 1;
    }

    Some(return_str.to_string())
  }

  /// Retrieves the position of the character to the left of the given `(x, y)` position.
  ///
  /// If there is no character to the left, returns `None`.
  pub fn left_x(&self, x: usize, y: usize) -> Option<usize> {
    let index =
      self.body.get(y)?.iter().enumerate().find_map(|(index, cc)| if cc.x == x { Some(index) } else { None })?;
    self.body.get(y)?.get(index - 1).map(|cc| cc.x)
  }

  /// Retrieves the position of the character to the right of the given `(x, y)` position.
  ///
  /// If there is no character to the right, returns `None`.
  pub fn right_x(&self, x: usize, y: usize) -> Option<usize> {
    let index =
      self.body.get(y)?.iter().enumerate().find_map(|(index, cc)| if cc.x == x { Some(index) } else { None })?;
    self.body.get(y)?.get(index + 1).map(|cc| cc.x)
  }

  fn new() -> Self {
    SplitedCode { body: vec![vec![]] }
  }

  fn append(&mut self, char: &str, char_width: &CharWidthMode) {
    let now_line = self.body.last_mut().unwrap();

    let x = if now_line.is_empty() {
      0
    } else {
      now_line.last().unwrap().x + now_line.last().unwrap().len
    };

    let width = char.width();
    let width_cjk = char.width_cjk();

    now_line.push(CodeCharacter {
      char: char.to_string(),
      x,
      len: match char_width {
        CharWidthMode::Mono => 1,
        CharWidthMode::Half => width,
        CharWidthMode::Full => width_cjk,
      },
    });
  }
  fn new_line(&mut self) {
    self.body.push(vec![]);
  }

  /// Returns the number of lines in the `SplitedCode`.
  pub fn len_y(&self) -> usize {
    self.body.len()
  }

  /// Enumerates the x-positions of all characters in the given line `y`.\
  pub fn enumurate_x(&self, y: usize) -> Box<dyn std::iter::Iterator<Item = usize> + '_> {
    Box::new(self.body[y].iter().map(|cc| cc.x))
  }
}

/// A parsed visual block in the code, including its position, size, and connections.
///
/// This is an intermediate representation used during compilation before converting to a `Block`.
///
/// # Example
/// ```rust
/// use trees_lang::compile::{CompilingBlock, ArgPlug, BlockPlug, Orientation,
///                             CompileConfig, CharWidthMode, split_code, find_blocks, connect_blocks};
///
/// let code = vec![
///     "    ".to_owned(),
///     "    ┌───────┐".to_owned(),
///     "    │ abc   │    ".to_owned(),
///     "    └───┬───┘   ".to_owned(),
///     "    ┌───┴──┐".to_owned(),
///     "    │ def  │    ".to_owned(),
///     "    └──────┘   ".to_owned(),
/// ];
///
/// let config = CompileConfig {
///   char_width: CharWidthMode::Mono
/// };
/// let splited_code = split_code(&code, &config);
/// let mut blocks = find_blocks(&splited_code, &config);
/// let head_block: CompilingBlock = connect_blocks(&splited_code, &mut blocks, &config).unwrap();
///
/// assert_eq!(head_block.proc_name, "abc");
/// assert_eq!(head_block.arg_plugs.len(), 1);
/// assert_eq!(head_block.args.len(), 1);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompilingBlock {
  /// Procedure name in the block.
  pub proc_name: String,
  /// X position (column) of the top-left of the block.
  pub x: usize,
  /// Y position (row) of the top-left of the block.
  pub y: usize,
  /// Width of the block.
  pub width: usize,
  /// Height of the block.
  pub height: usize,
  /// Optional block plug for connecting this block to others.
  pub block_plug: Option<BlockPlug>,
  /// Edge connecting block-plug of this block to another block.
  ///
  /// This is setted by `connect_blocks` function. Before that, it is empty.
  pub connect_from: Option<Edge>,
  /// Argument plugs for this block.
  pub arg_plugs: Vec<ArgPlug>,
  /// Edges (connections) representing the arguments passed to this block.
  ///
  /// This is setted by `connect_blocks` function. Before that, it is empty.
  pub args: Vec<Edge>,
}

/// An argument plug in a `CompilingBlock`, indicating where an argument can be connected.
///
/// Used for tracking the connection points for arguments in a visual block, including the position and orientation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArgPlug {
  /// X position of the plug.
  pub x: usize,
  /// Y position of the plug.
  pub y: usize,
  /// Whether the argument plug is expand or not. (Typically "@").
  pub expand: bool,
  /// Orientation of the plug (direction from which argument connects).
  pub ori: Orientation,
}

/// A fragment of an edge in the code's flow, with position and direction.
///
/// Tracks a specific piece of an edge connection, indicating the direction and location of a flow path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EdgeFragment {
  /// X coordinate of the edge fragment.
  pub x: usize,
  /// Y coordinate of the edge fragment.
  pub y: usize,
  /// Orientation of this edge fragment.
  pub ori: Orientation,
}

/// An edge connecting two blocks, including fragments and plug information.
///
/// Used to describe the connections between blocks and their arguments in a visual code flow.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Edge {
  /// Index of the block that owns the argument plug.
  pub block_index_of_arg_plug: usize,
  /// Information about the argument plug.
  pub arg_plug_info: ArgPlug,
  /// Sequence of fragments composing this edge.
  pub fragments: Vec<EdgeFragment>,
  /// Index of the block that the argument plug is connected to.
  pub block_index_of_block_plug: usize,
}

/// A plug point for a block, where it can be connected to other blocks.
///
/// Tracks the position of a plug and its associated quote style for connection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockPlug {
  /// X position of the plug.
  pub x: usize,
  /// Y position of the plug.
  pub y: usize,
  /// The quoting style used at this plug.
  pub quote: QuoteStyle,
}

/// Direction/orientation used for argument and edge routing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Orientation {
  /// The upward direction.
  Up,
  /// The leftward direction.
  Left,
  /// The rightward direction.
  Right,
  /// The downward direction.
  Down,
}

/// Quote style of block.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuoteStyle {
  /// Quote
  Quote,
  /// Closure
  Closure,
  /// No quote style applied
  None,
}

fn find_a_block(code: &SplitedCode, x: usize, y: usize, _config: &CompileConfig) -> Option<CompilingBlock> {
  let cc = |dx: usize, dy: usize| -> Option<CodeCharacter> { code.get(x + dx, y + dy) };
  let char = |dx: usize, dy: usize| -> Option<String> { code.get(x + dx, y + dy).map(|x| x.char.clone()) };

  let char_is_in = |dx: usize, dy: usize, targets: &[&str]| -> Option<bool> {
    let c = char(dx, dy)?;

    let matched = targets.iter().any(|t| *t == c);

    Some(matched)
  };

  let mut up_plug = None;
  let mut arg_plugs: Vec<_> = vec![];

  if char(0, 0)? != "" {
    return None;
  };
  // 右回り
  // 1から始める
  let mut width1 = code.right_x(x, y)? - x;
  while char_is_in(width1, 0, &["", "", "", "/"])? {
    if char_is_in(width1, 0, &["", "", "/"])? {
      if up_plug.is_some() {
        return None;
      }
      match char(width1, 0)?.as_str() {
        "" => {
          up_plug = Some(BlockPlug {
            x: x + width1,
            y,
            quote: QuoteStyle::None,
          });
        }
        "" => {
          up_plug = Some(BlockPlug {
            x: x + width1,
            y,
            quote: QuoteStyle::Quote,
          });
        }
        "/" => {
          up_plug = Some(BlockPlug {
            x: x + width1,
            y,
            quote: QuoteStyle::Closure,
          });
        }
        _ => {}
      }
    }

    width1 += cc(width1, 0)?.len;
  }
  if char(width1, 0)? != "" {
    return None;
  };

  let mut height1 = 1;
  while char_is_in(width1, height1, &["", "", "@"])? {
    match char(width1, height1)?.as_str() {
      "" => {
        arg_plugs.push(ArgPlug {
          x: x + width1,
          y: y + height1,
          expand: false,
          ori: Orientation::Right,
        });
      }
      "@" => {
        arg_plugs.push(ArgPlug {
          x: x + width1,
          y: y + height1,
          expand: true,
          ori: Orientation::Right,
        });
      }
      _ => {}
    }
    height1 += 1;
  }
  if char(width1, height1)? != "" {
    return None;
  };

  let mut under_width1 = code.right_x(x, y + height1)? - x;
  while char_is_in(under_width1, height1, &["", "", "@"])? {
    match char(under_width1, height1)?.as_str() {
      "" => {
        arg_plugs.push(ArgPlug {
          x: x + under_width1,
          y: y + height1,
          expand: false,
          ori: Orientation::Down,
        });
      }
      "@" => {
        arg_plugs.push(ArgPlug {
          x: x + under_width1,
          y: y + height1,
          expand: true,
          ori: Orientation::Down,
        });
      }
      _ => {}
    }
    under_width1 += cc(under_width1, height1)?.len;
  }
  if char(0, height1)? != "" || under_width1 != width1 {
    return None;
  };

  let mut under_height1 = 1;
  while char_is_in(0, under_height1, &["", "", "@"])? {
    if char(0, under_height1)? == "" {
      arg_plugs.push(ArgPlug {
        x,
        y: y + under_height1,
        expand: false,
        ori: Orientation::Left,
      });
    } else if char(0, under_height1)? == "@" {
      arg_plugs.push(ArgPlug {
        x,
        y: y + under_height1,
        expand: true,
        ori: Orientation::Left,
      });
    }
    under_height1 += 1;
  }
  if under_height1 != height1 {
    return None;
  };

  let mut proc_name = "".to_owned();

  for inside_y in 1..height1 {
    proc_name += code.get_slice_of_line(x, x + width1, y + inside_y)?.trim();
    proc_name += "\n";
  }

  arg_plugs.sort_by(|a, b| {
    if a.x != b.x {
      a.x.cmp(&b.x)
    } else if a.x == x {
      a.y.cmp(&b.y)
    } else if a.x == x + width1 {
      b.y.cmp(&a.y)
    } else {
      Ordering::Equal
    }
  });

  Some(CompilingBlock {
    proc_name: proc_name.trim().to_owned(),
    args: vec![],
    x,
    y,
    width: width1 + cc(width1, 0)?.len,
    height: height1 + 1,
    block_plug: up_plug,
    connect_from: None,
    arg_plugs,
  })
}

/// Finds all the blocks in a given split code according to configuration.
///
/// Returns a vector of detected `CompilingBlock`s.
pub fn find_blocks(splited_code: &SplitedCode, config: &CompileConfig) -> Vec<CompilingBlock> {
  let mut blocks: Vec<CompilingBlock> = vec![];

  for y in 0..splited_code.len_y() {
    for x in splited_code.enumurate_x(y) {
      if let Some(b) = find_a_block(splited_code, x, y, config) {
        blocks.push(b);
      }
    }
  }

  blocks
}

fn find_next_edge(code: &SplitedCode, x: &usize, y: &usize, ori: &Orientation) -> Result<EdgeFragment, EdgeFragment> {
  let update_and_check =
    |new_x: usize, new_y: usize, up: &str, left: &str, right: &str, down: &str| -> Result<EdgeFragment, EdgeFragment> {
      let cc = code.get(new_x, new_y).ok_or(EdgeFragment {
        x: new_x,
        y: new_y,
        ori: *ori,
      })?;

      let t = cc.char;
      if t == up {
        Ok(EdgeFragment {
          x: new_x,
          y: new_y,
          ori: Orientation::Up,
        })
      } else if t == left {
        Ok(EdgeFragment {
          x: new_x,
          y: new_y,
          ori: Orientation::Left,
        })
      } else if t == right {
        Ok(EdgeFragment {
          x: new_x,
          y: new_y,
          ori: Orientation::Right,
        })
      } else if t == down {
        Ok(EdgeFragment {
          x: new_x,
          y: new_y,
          ori: Orientation::Down,
        })
      } else {
        Err(EdgeFragment {
          x: new_x,
          y: new_y,
          ori: *ori,
        })
      }
    };

  match ori {
    Orientation::Up => update_and_check(*x, y - 1, "", "", "", ""),
    Orientation::Left => update_and_check(code.left_x(*x, *y).unwrap_or(*x - 1), *y, "", "", "", ""),
    Orientation::Right => update_and_check(
      code.right_x(*x, *y).unwrap_or(*x + code.get(*x, *y).unwrap().len),
      *y,
      "",
      "",
      "",
      "",
    ),
    Orientation::Down => update_and_check(*x, y + 1, "", "", "", ""),
  }
}

/// Connects the blocks by resolving edges between argument plugs and block plugs.
///
/// Returns the root `CompilingBlock` if successful.
pub fn connect_blocks(
  code: &SplitedCode,
  blocks: &mut [CompilingBlock],
  config: &CompileConfig,
) -> Result<CompilingBlock, CompileError> {
  let blocks_cloned = blocks.to_owned();

  let head_candinates: Vec<usize> = blocks
    .iter()
    .enumerate()
    .filter_map(|(i, block)| if block.block_plug.is_some() { None } else { Some(i) })
    .collect();

  if head_candinates.len() != 1 {
    return Err(CompileError::NonUniqueStartBlock(Box::new(
      errors::NonUniqueStartBlockError {
        candinates: head_candinates.iter().map(|i| blocks[*i].clone()).collect(),
      },
    )));
  }
  let head = head_candinates[0];

  // 借用権をかわすため、connect_fromは後から入れる。
  let mut deferred_connections = Vec::new();

  for (block_index, block) in blocks.iter_mut().enumerate() {
    for arg_plug in block.arg_plugs.iter() {
      let ArgPlug { x, y, ori, .. } = arg_plug;

      let mut mut_x = *x;
      let mut mut_y = *y;
      let mut mut_ori = *ori;

      // Edge構成用
      let mut fragments = Vec::new();

      loop {
        match find_next_edge(code, &mut_x, &mut_y, &mut_ori) {
          Ok(edge) => {
            mut_x = edge.x;
            mut_y = edge.y;
            mut_ori = edge.ori;
            fragments.push(edge);
          }
          Err(edge) => {
            mut_x = edge.x;
            mut_y = edge.y;
            break;
          }
        }
      }

      let (arg_block_index, _) = blocks_cloned
        .iter()
        .enumerate()
        .find(|(_, b)| {
          if let Some(p) = &b.block_plug {
            p.x == mut_x && p.y == mut_y
          } else {
            false
          }
        })
        .ok_or(CompileError::DanglingArgEdge(Box::new(errors::DanglingArgEdgeError {
          block_of_arg_plug: block.clone(),
          arg_plug: arg_plug.clone(),
          edge_fragments: fragments.clone(),
          dangling_position: (mut_x, mut_y),
        })))?;

      let connect_edge = Edge {
        block_index_of_arg_plug: block_index,
        arg_plug_info: arg_plug.clone(),
        fragments,
        block_index_of_block_plug: arg_block_index,
      };

      block.args.push(connect_edge.clone());

      // connect_fromをセット
      deferred_connections.push((arg_block_index, connect_edge.clone()));
    }
  }

  for (arg_block_index, connect_edge) in deferred_connections {
    let block = &mut blocks[arg_block_index];
    block.connect_from = Some(connect_edge);
  }

  Ok(blocks[head].clone())
}

/// Splits a list of code lines into a `SplitedCode` representation.
///
/// Each character is measured based on `CharWidthMode`.
pub fn split_code(code: &Vec<String>, config: &CompileConfig) -> SplitedCode {
  let mut splited_code = SplitedCode::new();

  for line in code {
    for char in line.split("") {
      if !char.is_empty() {
        splited_code.append(char, &config.char_width);
      }
    }

    splited_code.new_line();
  }

  splited_code
}

#[cfg(test)]
mod tests {
  use crate::compile::{
    ArgPlug, BlockPlug, CodeCharacter, CompileConfig, CompilingBlock, Edge, EdgeFragment, Orientation, QuoteStyle,
    SplitedCode,
    errors::{self, CompileError},
    find_a_block, find_blocks,
  };

  use super::{connect_blocks, split_code};

  #[test]
  fn test_split_code() {
    let code = vec![" ┌┐".to_owned()];
    let splited = split_code(&code, &CompileConfig::DEFAULT);
    let target = SplitedCode {
      body: vec![
        vec![
          CodeCharacter {
            char: " ".to_owned(),
            x: 0,
            len: 1,
          },
          CodeCharacter {
            char: "".to_owned(),
            x: 1,
            len: 1,
          },
          CodeCharacter {
            char: "".to_owned(),
            x: 2,
            len: 1,
          },
        ],
        vec![],
      ],
    };
    assert_eq!(splited, target);
  }
  #[test]
  fn test_split_code_cjk() {
    let mut config = CompileConfig::DEFAULT.clone();
    config.char_width = crate::compile::CharWidthMode::Full;

    let code = vec![" ┌┐".to_owned()];
    let splited = split_code(&code, &config);
    let target = SplitedCode {
      body: vec![
        vec![
          CodeCharacter {
            char: " ".to_owned(),
            x: 0,
            len: 1,
          },
          CodeCharacter {
            char: "".to_owned(),
            x: 1,
            len: 2,
          },
          CodeCharacter {
            char: "".to_owned(),
            x: 3,
            len: 2,
          },
        ],
        vec![],
      ],
    };
    assert_eq!(splited, target);
  }

  #[test]
  fn test_find_a_block() {
    let config = CompileConfig::DEFAULT;

    let block = find_a_block(
      &split_code(
        &vec![
          "               ".to_owned(),
          "    ┌─────┐    ".to_owned(),
          "    │ abc │    ".to_owned(),
          "    └─────┘    ".to_owned(),
          "               ".to_owned(),
        ],
        &config,
      ),
      4,
      1,
      &config,
    );

    assert_eq!(
      Some(CompilingBlock {
        proc_name: "abc".to_string(),
        x: 4,
        y: 1,
        width: 7,
        height: 3,
        block_plug: None,
        connect_from: None,
        arg_plugs: vec![],
        args: vec![]
      }),
      block
    );
  }

  #[test]
  fn test_find_a_block_cjk() {
    let mut config = CompileConfig::DEFAULT.clone();
    config.char_width = crate::compile::CharWidthMode::Full;

    let block = find_a_block(
      &split_code(
        &vec![
          "               ".to_owned(),
          "    ┌───┐    ".to_owned(),
          "    │ abc  │    ".to_owned(),
          "    └───┘    ".to_owned(),
          "               ".to_owned(),
        ],
        &config,
      ),
      4,
      1,
      &config,
    );

    assert_eq!(
      Some(CompilingBlock {
        proc_name: "abc".to_string(),
        x: 4,
        y: 1,
        width: 10,
        height: 3,
        block_plug: None,
        connect_from: None,
        arg_plugs: vec![],
        args: vec![]
      }),
      block
    );
  }

  #[test]
  fn check_find_blocks() {
    let config = CompileConfig::DEFAULT;

    let blocks = find_blocks(
      &split_code(
        &vec![
          "    ".to_owned(),
          "    ┌───────┐".to_owned(),
          "    │ abc   │    ".to_owned(),
          "    └───┬───┘   ".to_owned(),
          "    ┌───┴──┐".to_owned(),
          "    │ def  │    ".to_owned(),
          "    └──────┘   ".to_owned(),
        ],
        &config,
      ),
      &config,
    );

    assert_eq!(
      vec![
        CompilingBlock {
          proc_name: "abc".to_owned(),
          x: 4,
          y: 1,
          width: 9,
          height: 3,
          block_plug: None,
          connect_from: None,
          arg_plugs: vec![ArgPlug {
            x: 8,
            y: 3,
            expand: false,
            ori: Orientation::Down
          }],
          args: vec![]
        },
        CompilingBlock {
          proc_name: "def".to_owned(),
          x: 4,
          y: 4,
          width: 8,
          height: 3,
          block_plug: Some(BlockPlug {
            x: 8,
            y: 4,
            quote: QuoteStyle::None
          }),
          connect_from: None,
          arg_plugs: vec![],
          args: vec![]
        }
      ],
      blocks
    );
  }

  #[test]
  fn check_find_blocks_half() {
    let mut config = CompileConfig::DEFAULT.clone();
    config.char_width = crate::compile::CharWidthMode::Half;

    let blocks = find_blocks(
      &split_code(
        &vec![
          "    ".to_owned(),
          "    ┌───────┐".to_owned(),
          "    │ あc   │    ".to_owned(),
          "    └───┬───┘   ".to_owned(),
          "    ┌───┴──┐".to_owned(),
          "    │ いf  │    ".to_owned(),
          "    └──────┘   ".to_owned(),
        ],
        &config,
      ),
      &config,
    );

    assert_eq!(
      vec![
        CompilingBlock {
          proc_name: "あc".to_owned(),
          x: 4,
          y: 1,
          width: 9,
          height: 3,
          block_plug: None,
          connect_from: None,
          arg_plugs: vec![ArgPlug {
            x: 8,
            y: 3,
            expand: false,
            ori: Orientation::Down
          }],
          args: vec![]
        },
        CompilingBlock {
          proc_name: "いf".to_owned(),
          x: 4,
          y: 4,
          width: 8,
          height: 3,
          block_plug: Some(BlockPlug {
            x: 8,
            y: 4,
            quote: QuoteStyle::None
          }),
          connect_from: None,
          arg_plugs: vec![],
          args: vec![]
        }
      ],
      blocks
    );
  }

  #[test]
  fn check_find_blocks_cjk() {
    let mut config = CompileConfig::DEFAULT.clone();
    config.char_width = crate::compile::CharWidthMode::Full;

    let blocks = find_blocks(
      &split_code(
        &vec![
          "    ".to_owned(),
          "    ┌────┐".to_owned(),
          "    │ abc    │    ".to_owned(),
          "    └─┬──┘   ".to_owned(),
          "    ┌─┴─┐".to_owned(),
          "    │ def  │    ".to_owned(),
          "    └───┘   ".to_owned(),
        ],
        &config,
      ),
      &config,
    );

    assert_eq!(
      vec![
        CompilingBlock {
          proc_name: "abc".to_owned(),
          x: 4,
          y: 1,
          width: 12,
          height: 3,
          block_plug: None,
          connect_from: None,
          arg_plugs: vec![ArgPlug {
            x: 8,
            y: 3,
            expand: false,
            ori: Orientation::Down
          }],
          args: vec![]
        },
        CompilingBlock {
          proc_name: "def".to_owned(),
          x: 4,
          y: 4,
          width: 10,
          height: 3,
          block_plug: Some(BlockPlug {
            x: 8,
            y: 4,
            quote: QuoteStyle::None
          }),
          connect_from: None,
          arg_plugs: vec![],
          args: vec![]
        }
      ],
      blocks
    );
  }

  #[test]
  fn two_connect() {
    let splited_code = split_code(
      &vec![
        "    ".to_owned(),
        "    ┌───────┐".to_owned(),
        "    │ abc   │    ".to_owned(),
        "    └───┬───┘   ".to_owned(),
        "".to_owned(),
        "    ┌───┴──┐".to_owned(),
        "    │ def  │    ".to_owned(),
        "    └──────┘   ".to_owned(),
      ],
      &CompileConfig::DEFAULT,
    );

    let mut blocks = find_blocks(&splited_code, &CompileConfig::DEFAULT);
    let head = connect_blocks(&splited_code, &mut blocks, &CompileConfig::DEFAULT).unwrap();

    let arg_edge = Edge {
      block_index_of_arg_plug: 0,
      arg_plug_info: ArgPlug {
        x: 8,
        y: 3,
        expand: false,
        ori: Orientation::Down,
      },
      fragments: vec![EdgeFragment {
        x: 8,
        y: 4,
        ori: Orientation::Down,
      }],
      block_index_of_block_plug: 1,
    };

    assert_eq!(
      head,
      CompilingBlock {
        proc_name: "abc".to_owned(),
        x: 4,
        y: 1,
        width: 9,
        height: 3,
        block_plug: None,
        connect_from: None,
        arg_plugs: vec![ArgPlug {
          x: 8,
          y: 3,
          expand: false,
          ori: Orientation::Down
        }],
        args: vec![arg_edge.clone()]
      }
    );

    assert_eq!(
      blocks[1],
      CompilingBlock {
        proc_name: "def".to_owned(),
        x: 4,
        y: 5,
        width: 8,
        height: 3,
        block_plug: Some(BlockPlug {
          x: 8,
          y: 5,
          quote: QuoteStyle::None
        }),
        connect_from: Some(arg_edge),
        arg_plugs: vec![],
        args: vec![]
      }
    );
  }

  #[test]
  fn error_non_unique_start_block() {
    let code = vec![
      "    ".to_owned(),
      "    ┌───────┐".to_owned(),
      "    │ abc   │    ".to_owned(),
      "    └───────┘   ".to_owned(),
      "    ┌──────┐".to_owned(),
      "    │ def  │    ".to_owned(),
      "    └──────┘   ".to_owned(),
    ];

    let splited_code = split_code(&code, &CompileConfig::DEFAULT);
    let mut blocks = find_blocks(&splited_code, &CompileConfig::DEFAULT);

    let result = connect_blocks(&splited_code, &mut blocks, &CompileConfig::DEFAULT);

    assert_eq!(
      result,
      Err(CompileError::NonUniqueStartBlock(Box::new(
        errors::NonUniqueStartBlockError {
          candinates: vec![blocks[0].clone(), blocks[1].clone()],
        }
      )))
    );
  }

  #[test]
  fn error_dangling_arg_edge() {
    let code = vec![
      "    ".to_owned(),
      "    ┌───────┐".to_owned(),
      "    │ abc   │    ".to_owned(),
      "    └───┬───┘   ".to_owned(),
      "".to_owned(),
      "               ".to_owned(),
      "    ┌───┴──┐".to_owned(),
      "    │ def  │    ".to_owned(),
      "    └──────┘   ".to_owned(),
    ];

    let splited_code = split_code(&code, &CompileConfig::DEFAULT);
    let mut blocks = find_blocks(&splited_code, &CompileConfig::DEFAULT);

    let result = connect_blocks(&splited_code, &mut blocks, &CompileConfig::DEFAULT);

    assert_eq!(
      result,
      Err(CompileError::DanglingArgEdge(Box::new(errors::DanglingArgEdgeError {
        block_of_arg_plug: blocks[0].clone(),
        arg_plug: blocks[0].arg_plugs[0].clone(),
        edge_fragments: vec![EdgeFragment {
          x: 8,
          y: 4,
          ori: Orientation::Down
        }],
        dangling_position: (8, 5)
      })))
    );
  }

  #[test]
  fn ignore_two_block_plug() {
    let code = vec![
      "    ".to_owned(),
      "    ┌───────┐".to_owned(),
      "    │ abc   │    ".to_owned(),
      "    └───────┘   ".to_owned(),
      "           ".to_owned(),
      "    ┌──┴┴──┐".to_owned(),
      "    │ def  │    ".to_owned(),
      "    └──────┘   ".to_owned(),
    ];

    let splited_code = split_code(&code, &CompileConfig::DEFAULT);
    let blocks = find_blocks(&splited_code, &CompileConfig::DEFAULT);

    assert_eq!(blocks.len(), 1);
  }
}