@@ -1036,12 +1036,13 @@ defmodule Code do
1036
1036
[
1037
1037
unescape: false ,
1038
1038
literal_encoder: & { :ok , { :__block__ , & 2 , [ & 1 ] } } ,
1039
+ include_comments: true ,
1039
1040
token_metadata: true ,
1040
1041
emit_warnings: false
1041
1042
] ++ opts
1042
1043
1043
- { forms , comments } = string_to_quoted_with_comments !( string , to_quoted_opts )
1044
- to_algebra_opts = [ comments: comments ] ++ opts
1044
+ forms = string_to_quoted !( string , to_quoted_opts )
1045
+ to_algebra_opts = opts
1045
1046
doc = Code.Formatter . to_algebra ( forms , to_algebra_opts )
1046
1047
Inspect.Algebra . format ( doc , line_length )
1047
1048
end
@@ -1249,11 +1250,22 @@ defmodule Code do
1249
1250
file = Keyword . get ( opts , :file , "nofile" )
1250
1251
line = Keyword . get ( opts , :line , 1 )
1251
1252
column = Keyword . get ( opts , :column , 1 )
1253
+ include_comments = Keyword . get ( opts , :include_comments , false )
1252
1254
1253
- case :elixir . string_to_tokens ( to_charlist ( string ) , line , column , file , opts ) do
1254
- { :ok , tokens } ->
1255
- :elixir . tokens_to_quoted ( tokens , file , opts )
1255
+ Process . put ( :code_formatter_comments , [ ] )
1256
+ opts = [ preserve_comments: & preserve_comments / 5 ] ++ opts
1256
1257
1258
+ with { :ok , tokens } <- :elixir . string_to_tokens ( to_charlist ( string ) , line , column , file , opts ) ,
1259
+ { :ok , quoted } <- :elixir . tokens_to_quoted ( tokens , file , opts ) do
1260
+ if include_comments do
1261
+ quoted = Code.Normalizer . normalize ( quoted )
1262
+ quoted = Code.Comments . merge_comments ( quoted , Process . get ( :code_formatter_comments ) )
1263
+
1264
+ { :ok , quoted }
1265
+ else
1266
+ { :ok , quoted }
1267
+ end
1268
+ else
1257
1269
{ :error , _error_msg } = error ->
1258
1270
error
1259
1271
end
@@ -1275,7 +1287,30 @@ defmodule Code do
1275
1287
file = Keyword . get ( opts , :file , "nofile" )
1276
1288
line = Keyword . get ( opts , :line , 1 )
1277
1289
column = Keyword . get ( opts , :column , 1 )
1278
- :elixir . string_to_quoted! ( to_charlist ( string ) , line , column , file , opts )
1290
+ include_comments = Keyword . get ( opts , :include_comments , false )
1291
+
1292
+ Process . put ( :code_formatter_comments , [ ] )
1293
+
1294
+ opts =
1295
+ if include_comments do
1296
+ [ preserve_comments: & preserve_comments / 5 ,
1297
+ literal_encoder: & { :ok , { :__block__ , & 2 , [ & 1 ] } } ,
1298
+ token_metadata: true ,
1299
+ unescape: false ,
1300
+ columns: true ,
1301
+ ] ++ opts
1302
+ else
1303
+ opts
1304
+ end
1305
+
1306
+ quoted = :elixir . string_to_quoted! ( to_charlist ( string ) , line , column , file , opts )
1307
+
1308
+ if include_comments do
1309
+ # quoted = Code.Normalizer.normalize(quoted)
1310
+ Code.Comments . merge_comments ( quoted , Process . get ( :code_formatter_comments ) )
1311
+ else
1312
+ quoted
1313
+ end
1279
1314
end
1280
1315
1281
1316
@ doc """
0 commit comments