Trivadis / plsql-formatter-settings

PL/SQL & SQL formatter settings based on the Trivadis PL/SQL & SQL Coding Guidelines
Apache License 2.0
134 stars 46 forks source link

noformat not working #246

Closed jcarey9149 closed 1 year ago

jcarey9149 commented 1 year ago

I've been trying to handle a couple of cases where I don't want the formatter to do anything. First one is this:

DECLARE
/* @formatter:off */
   full_dnames CLOB := 
   '[{"NAME":"ORDERNUMBER","TYPE":"NUMBER","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"ORDERNUMBER","TYPE":"VARCHAR","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"FIRSTNAME","TYPE":"VARCHAR","DESIGNATION":"ATTRIBUTE","GRADE":1}';
/* @formatter:on */
BEGIN
   NULL;
END;

Formatter result:

DECLARE
/* @formatter:off */
   full_dnames CLOB :=  '[{"NAME":"ORDERNUMBER","TYPE":"NUMBER","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"ORDERNUMBER","TYPE":"VARCHAR","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"FIRSTNAME","TYPE":"VARCHAR","DESIGNATION":"ATTRIBUTE","GRADE":1}';
/* @formatter:on */
BEGIN
   NULL;
END;

My intent is to override the formatter's desire to put the first line of the assignment on the same line as the declaration to keep the data lined up. I've tried both the @formatter:off and noformat start versions in both single line and multiline comments.

I'm using the files in settings/sql_developer with sqldeveloper v22.2.1.

PhilippSalvisberg commented 1 year ago

Hi,

I cannot reproduce the problem. When I format this:

DECLARE
/* @formatter:off */
   full_dnames CLOB := 
   '[{"NAME":"ORDERNUMBER","TYPE":"NUMBER","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"ORDERNUMBER","TYPE":"VARCHAR","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"FIRSTNAME","TYPE":"VARCHAR","DESIGNATION":"ATTRIBUTE","GRADE":1}';
/* @formatter:on */
BEGIN
   NULL;
END; 

Then I get this formatted result:

declare
   /* @formatter:off */
   full_dnames CLOB := 
   '[{"NAME":"ORDERNUMBER","TYPE":"NUMBER","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"ORDERNUMBER","TYPE":"VARCHAR","DESIGNATION":"DIMENSION","GRADE":1},
   {"NAME":"FIRSTNAME","TYPE":"VARCHAR","DESIGNATION":"ATTRIBUTE","GRADE":1}';
/* @formatter:on */
begin
   null;
end;

I suspect that you do not use the settings provided in this repository. Another Arbori program. Maybe the default provided by SQLDev?

Could you please provide screenshots of your settings? They should look similar to this:

image image
jcarey9149 commented 1 year ago

I should mention that I can work around this with a strategically placed comment. However in the full case the assignment had to be broken up into a couple of strings, and I don't want the default '||' operator alignment (with the ':=') for the same reason as the first line break.

jcarey9149 commented 1 year ago

I tried reloading the settings from the repository, and all my formatting went away. I'll look into this.

jcarey9149 commented 1 year ago

I think you were right, but having reloaded the files there is no formatting. Looks like when I load the advanced xml file it resets the custom code. How can I debug this?

jcarey9149 commented 1 year ago

Looks like the files as downloaded a couple of days ago just don't work with sqldeveloper 22.2.1. I reinstalled v 21.4.3 and it seems to be working.

PhilippSalvisberg commented 1 year ago

I think you were right

Thanks for confirming it.

having reloaded the files there is no formatting. Looks like when I load the advanced xml file it resets the custom code.

Correct. Just follow the installation instructions. ;-)

How can I debug this?

Debugging in SQLDev is a bit tricky. The simplest approach is to enable logging. You have to change the logging.conf in SQLDev. This comment explains how to do it.

However, you can also check the version of the Arbori program in the Setting under "Code Editor -> Format -> Advanced Format -> Custom Format". As shown in the screenshot above. Here are some links based on released versions:

Looks like the files as downloaded a couple of days ago just don't work with sqldeveloper 22.2.1.

The current version in the main branch is 5 months old and works with a manually patched version of SQLDev 22.2.1 and SQLcl 22.3.0 and SQLcl 22.4.0.

The latest release is from 2022-10-09.

So, which version did you download? Actually, one that does not match the product version you are using.

The XML configuration file trivadis_advanced_format.xmlis more or less version independent. However, the Arbor file trivadis_custom_format.arbori is very much dependent on the version of SQLDev and SQLcl. Unfortunately, the release cycles of SQLDev and SQLDev are independent now. This leads to the situation where sometimes no configuration exists that works in the latest versions of both products. The dirty workaround is to manually patch SQLDev with the libraries from SQLcl.

The reason for the version-dependency is that the Arbori program queries the parse-tree and uses specific symbol names expected in the parse tree. When new SQL features are supported in the product or parser bugs are fixed then the parse-tree structure and the used symbol names change as well. This leads to runtime errors in the Arbori program. This is why you really need to check that you install the configuration for the correct product version.

If you are interested in the details you can look at the differences between version sqldev-21.4.3 and sqlcl-22.3.0. There you see that some symbol names in trivadis_custom_format.arbori have changed, e.g.

In other words, a lot changed between these versions making them completely incompatible from a SQL grammar point of view.

In https://github.com/Trivadis/plsql-formatter-settings/releases you find all released versions for the formatter settings. There is also a section that defines the compatibility with SQLDev and SQLcl. In some cases, you have to manually patch SQLDev to make it work (copy a JAR file from the SQLcl installation). If you do not want to do that, you have to use a different version.