jcberquist / commandbox-cfformat

A CommandBox module for formatting CFML component files.
MIT License
21 stars 10 forks source link

cfquery formatting #105

Closed Daemach closed 2 years ago

Daemach commented 3 years ago

In the formatted code below, what setting do I have to change to remove the CR between the parentheses and the double quotes? I want it to look like the second example, not the first:

    queryExecute(
      "
      INSERT INTO synaptrix_account_history
      SELECT a.account_id, a.init_holdings, a.market_val, a.available_cash, r.init_track_err, r.opt_track_err, r.init_total_port_risk, r.init_total_bench_risk, r.init_r_squared, r.account_beta, a.as_of_date
      FROM  mars_out_accounts as a 
        JOIN mars_out_add_risk_decom as r 
          ON a.account_id = r.account_id
      WHERE a.user_id = 'mars_engine' AND r.user_id = 'mars_engine'
    "
    )
    queryExecute("
      INSERT INTO synaptrix_account_history
      SELECT a.account_id, a.init_holdings, a.market_val, a.available_cash, r.init_track_err, r.opt_track_err, r.init_total_port_risk, r.init_total_bench_risk, r.init_r_squared, r.account_beta, a.as_of_date
      FROM  mars_out_accounts as a 
        JOIN mars_out_add_risk_decom as r 
          ON a.account_id = r.account_id
      WHERE a.user_id = 'mars_engine' AND r.user_id = 'mars_engine'
    ")

This would be acceptable too:

    queryExecute( "
      INSERT INTO synaptrix_account_history
      SELECT a.account_id, a.init_holdings, a.market_val, a.available_cash, r.init_track_err, r.opt_track_err, r.init_total_port_risk, r.init_total_bench_risk, r.init_r_squared, r.account_beta, a.as_of_date
      FROM  mars_out_accounts as a 
        JOIN mars_out_add_risk_decom as r 
          ON a.account_id = r.account_id
      WHERE a.user_id = 'mars_engine' AND r.user_id = 'mars_engine'
    " )
jcberquist commented 3 years ago

I will have to look into this. I think it should be possible to special case single string arguments to allow keeping the quotes on the same line as the parentheses.

Daemach commented 3 years ago

That would be great. Thanks!

Daemach commented 2 years ago

Just checking up on this again. I will be doing code reviews soon and have to manually fix these after running cfformat:

queryExecute(
  "
      declare @total float = (select sum(market_val) from veritiadmin.idx.constituents where benchmarkID = '#benchmark.id#')

      update veritiadmin.idx.constituents set
      pct_wt = (market_val/@total) * 100
      where benchmarkID = '#benchmark.id#'
    "
)

would look better as

queryExecute( "
  declare @total float = (select sum(market_val) from veritiadmin.idx.constituents where benchmarkID = '#benchmark.id#')

  update veritiadmin.idx.constituents set
  pct_wt = (market_val/@total) * 100
  where benchmarkID = '#benchmark.id#'
" )
jcberquist commented 2 years ago

Thanks for the nudge, I have added some special case single string argument formatting in v0.17.4 - I would appreciate you trying it out. I tried to copy how prettier does this with JavaScript and you can see the new behavior illustrated with the current test files here: tests/data/functionCallSingleString/source.cfc tests/data/functionCallSingleString/formatted.txt

Daemach commented 2 years ago

I like the first case the best.

On a side note, what would it take to work this into vscode as a formatter for cfml files? I might be willing to sponsor the effort.

jcberquist commented 2 years ago

I could have been clearer, the test cases match the behavior of Prettier with JavaScript, if you look through the source and then compare with the output, you will see that if you have a string argument (a string containing newlines) and the source starts the string on the same line as the opening parentheses, then the output will preserve that, and if the string starts on the next line, the output will preserve that. So it is source dependent what happens.

With regard to VSCode, I don't use it day to day, so I am not aware of how formatters work in it. I think it supports terminals running within it, so maybe one could get CommandBox running and then watch files to format on save, or even send a format command to the terminal immediately when a save happens? I am not sure what the options are.