@@ -123,7 +123,9 @@ defmodule Sourceror do
123
123
"""
124
124
@ spec parse_string ( String . t ( ) , Keyword . t ( ) ) :: { :ok , Macro . t ( ) } | { :error , term ( ) }
125
125
def parse_string ( source , opts \\ [ ] ) do
126
- opts = Keyword . take ( opts , [ :line , :column ] )
126
+ opts = Keyword . take ( opts , [ :line , :column , :indentation ] )
127
+
128
+ { source , opts } = maybe_apply_columns_fix ( source , opts )
127
129
128
130
with { :ok , quoted , comments } <- string_to_quoted ( source , opts ++ to_quoted_opts ( ) ) do
129
131
{ :ok , Sourceror.Comments . merge_comments ( quoted , comments ) }
@@ -135,12 +137,55 @@ defmodule Sourceror do
135
137
"""
136
138
@ spec parse_string! ( String . t ( ) ) :: Macro . t ( )
137
139
def parse_string! ( source , opts \\ [ ] ) do
138
- opts = Keyword . take ( opts , [ :line , :column ] )
140
+ opts = Keyword . take ( opts , [ :line , :column , :indentation ] )
141
+
142
+ { source , opts } = maybe_apply_columns_fix ( source , opts )
139
143
140
144
{ quoted , comments } = string_to_quoted! ( source , opts ++ to_quoted_opts ( ) )
141
145
Sourceror.Comments . merge_comments ( quoted , comments )
142
146
end
143
147
148
+ cond do
149
+ Version . match? ( System . version ( ) , ">= 1.16.0 and < 1.19.0" ) ->
150
+ defp maybe_apply_columns_fix ( source , opts ) do
151
+ { source , opts } =
152
+ case Keyword . get ( opts , :column ) do
153
+ start_column when is_integer ( start_column ) ->
154
+ source = String . duplicate ( " " , start_column - 1 ) <> source
155
+ opts = Keyword . delete ( opts , :column )
156
+ { source , opts }
157
+
158
+ _ ->
159
+ { source , opts }
160
+ end
161
+
162
+ apply_indentation_fix ( source , opts )
163
+ end
164
+
165
+ Version . match? ( System . version ( ) , "< 1.19.0" ) ->
166
+ defp maybe_apply_columns_fix ( source , opts ) do
167
+ apply_indentation_fix ( source , opts )
168
+ end
169
+
170
+ true ->
171
+ defp maybe_apply_columns_fix ( source , opts ) do
172
+ { source , opts }
173
+ end
174
+ end
175
+
176
+ defp apply_indentation_fix ( source , opts ) do
177
+ case Keyword . get ( opts , :indentation ) do
178
+ indentation when is_integer ( indentation ) ->
179
+ opts = Keyword . delete ( opts , :indentation )
180
+ source = indent ( source , " " , indentation - 1 )
181
+
182
+ { source , opts }
183
+
184
+ _ ->
185
+ { source , opts }
186
+ end
187
+ end
188
+
144
189
defp to_quoted_opts do
145
190
[
146
191
literal_encoder: & { :ok , { :__block__ , & 2 , [ & 1 ] } } ,
0 commit comments