@@ -8,7 +8,7 @@ module SyntaxErrorSearch
88 class DisplayInvalidBlocks
99 attr_reader :filename
1010
11- def initialize ( code_lines : , blocks :, io : $stderr, filename : nil , terminal : false , invalid_type : :unmatched_end )
11+ def initialize ( code_lines : , blocks :, io : $stderr, filename : nil , terminal : false , invalid_obj : WhoDisSyntaxError :: Null . new )
1212 @terminal = terminal
1313 @filename = filename
1414 @io = io
@@ -18,7 +18,7 @@ def initialize(code_lines: ,blocks:, io: $stderr, filename: nil, terminal: false
1818 @invalid_lines = @blocks . map ( &:lines ) . flatten
1919 @code_lines = code_lines
2020
21- @invalid_type = invalid_type
21+ @invalid_obj = invalid_obj
2222 end
2323
2424 def call
@@ -36,34 +36,56 @@ def call
3636 end
3737
3838 private def found_invalid_blocks
39- case @invalid_type
40- when :missing_end
41- @io . puts <<~EOM
39+ @io . puts
40+ @io . puts banner
41+ @io . puts
42+ @io . puts ( "file: #{ filename } " ) if filename
43+ @io . puts <<~EOM
44+ simplified:
45+
46+ #{ indent ( code_block ) }
47+ EOM
48+ end
4249
50+ def banner
51+ case @invalid_obj . error_symbol
52+ when :missing_end
53+ <<~EOM
4354 SyntaxSearch: Missing `end` detected
4455
4556 This code has a missing `end`. Ensure that all
4657 syntax keywords (`def`, `do`, etc.) have a matching `end`.
47-
48- EOM
49- when :unmatched_end
50- @io . puts <<~EOM
51-
52- SyntaxSearch: Unmatched `end` detected
53-
54- This code has an unmatched `end`. Ensure that all `end` lines
55- in your code have a matching syntax keyword (`def`, `do`, etc.)
56- and that you don't have any extra `end` lines.
57-
5858 EOM
59+ when :unmatched_syntax
60+ case @invalid_obj . unmatched_symbol
61+ when :end
62+ <<~EOM
63+ SyntaxSearch: Unmatched `end` detected
64+
65+ This code has an unmatched `end`. Ensure that all `end` lines
66+ in your code have a matching syntax keyword (`def`, `do`, etc.)
67+ and that you don't have any extra `end` lines.
68+ EOM
69+ when :|
70+ <<~EOM
71+ SyntaxSearch: Unmatched `|` character detected
72+
73+ Example:
74+
75+ `do |x` should be `do |x|`
76+ EOM
77+ when :"}"
78+ <<~EOM
79+ SyntaxSearch: Unmatched `}` character detected
80+
81+ This code has an unmatched `}`. Ensure that opening curl braces are
82+ closed: `{ }`.
83+ EOM
84+ else
85+ "SyntaxSearch: Unmatched #{ @invalid_obj . unmatched_symbol } ` detected"
86+ end
5987 end
6088
61- @io . puts ( "file: #{ filename } " ) if filename
62- @io . puts <<~EOM
63- simplified:
64-
65- #{ indent ( code_block ) }
66- EOM
6789 end
6890
6991 def indent ( string , with : " " )
0 commit comments