@@ -1036,12 +1036,13 @@ defmodule Code do
10361036 [
10371037 unescape: false ,
10381038 literal_encoder: & { :ok , { :__block__ , & 2 , [ & 1 ] } } ,
1039+ include_comments: true ,
10391040 token_metadata: true ,
10401041 emit_warnings: false
10411042 ] ++ opts
10421043
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
10451046 doc = Code.Formatter . to_algebra ( forms , to_algebra_opts )
10461047 Inspect.Algebra . format ( doc , line_length )
10471048 end
@@ -1249,11 +1250,22 @@ defmodule Code do
12491250 file = Keyword . get ( opts , :file , "nofile" )
12501251 line = Keyword . get ( opts , :line , 1 )
12511252 column = Keyword . get ( opts , :column , 1 )
1253+ include_comments = Keyword . get ( opts , :include_comments , false )
12521254
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
12561257
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
12571269 { :error , _error_msg } = error ->
12581270 error
12591271 end
@@ -1275,7 +1287,30 @@ defmodule Code do
12751287 file = Keyword . get ( opts , :file , "nofile" )
12761288 line = Keyword . get ( opts , :line , 1 )
12771289 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
12791314 end
12801315
12811316 @ doc """
0 commit comments