Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ada Source Formatter #901

Open
Joebeazelman opened this issue Jan 31, 2022 · 5 comments
Open

Ada Source Formatter #901

Joebeazelman opened this issue Jan 31, 2022 · 5 comments
Assignees

Comments

@Joebeazelman
Copy link

I had given up on trying to format my Ada code in VSCode. There's no mention of it in Ada's documentation. Trying to execute the command resulted in VSCode asking if you'd like to download a formatter for Ada. After looking for alternatives, I was just about to write one until I tested my initial extension code. My extension formatting wasn't loading, so after much investigation I discovered Ada Language server was performing the formatting. It suddenly worked. Not sure what happened. Looking at the VSCode base, I don't see where Ada Language Server registers itself as a code formatter.

The remaining issue is that there's little documentation of this feature even existing and there are no options for setting its formatting options such as formatting comments, line length, number of tabs, etc.

@reznikmm
Copy link
Member

See this line for formatting registration code.

ALS uses gnatpp code for formatting. So it supports all gnatpp option. Just set them in your project file, like this:

package Pretty_Printer is
  for Default_Switches ("ada") use
    ("--no-align-modes",
     "--no-separate-is",
     "--call-threshold=1",
     "--par-threshold=2",
     "--vertical-named-aggregates",
     "--wide-character-encoding=8");
end Pretty_Printer;

@Joebeazelman
Copy link
Author

Joebeazelman commented Feb 3, 2022

Thanks. Three issues. First, I may be intentional, but the formatter doesn't abide by VSCode's tab spacing. I think it should use VSCode's default unless it's explicit set in the Default_Switches of the Pretty_Printer package definition. Second, the original error I received about it not finding the formatter has reoccurred when I tried to format the GPR file. I think I was applying it to the GPR file. Third, after adding the switches I'm getting an error message from the Language Server:

[Trace - 10:39:22 PM] Received response 'textDocument/formatting - (784)' in 2ms. Request failed: Internal error (-32603).
[Error - 10:39:22 PM] Request textDocument/formatting failed.
Message: Internal error
Code: -32603

Here's my project generated by ALR:

with "config/adb_controller_config.gpr";
project Adb_Controller is

for Source_Dirs use ("src");
for Object_Dir use "obj";
for Create_Missing_Dirs use "True";
for Exec_Dir use "bin";
for Main use ("adb_controller.adb");

type Enabled_Kind is ("enabled", "disabled");
Compile_Checks : Enabled_Kind := External ("ADB_CONTROLLER_COMPILE_CHECKS", "disabled");
Runtime_Checks : Enabled_Kind := External ("ADB_CONTROLLER_RUNTIME_CHECKS", "disabled");
Style_Checks : Enabled_Kind := External ("ADB_CONTROLLER_STYLE_CHECKS", "disabled");
Contracts_Checks : Enabled_Kind := External ("ADB_CONTROLLER_CONTRACTS", "disabled");

type Build_Kind is ("debug", "optimize");
Build_Mode : Build_Kind := External ("ADB_CONTROLLER_BUILD_MODE", "optimize");

Compile_Checks_Switches := ();
case Compile_Checks is
when "enabled" =>
Compile_Checks_Switches :=
("-gnatwa", -- All warnings
"-gnatVa", -- All validity checks
"-gnatwe"); -- Warnings as errors
when others => null;
end case;

Runtime_Checks_Switches := ();
case Runtime_Checks is
when "enabled" => null;
when others =>
Runtime_Checks_Switches :=
("-gnatp"); -- Suppress checks
end case;

Style_Checks_Switches := ();
case Style_Checks is
when "enabled" =>
Style_Checks_Switches :=
("-gnatyg", -- GNAT Style checks
"-gnaty-d", -- Disable no DOS line terminators
"-gnatyM80", -- Maximum line length
"-gnatyO"); -- Overriding subprograms explicitly marked as such
when others => null;
end case;

Contracts_Switches := ();
case Contracts_Checks is
when "enabled" =>
Contracts_Switches :=
("-gnata"); -- Enable assertions and contracts
when others => null;
end case;

Build_Switches := ();
case Build_Mode is
when "optimize" =>
Build_Switches := ("-O3", -- Optimization
"-gnatn"); -- Enable inlining
when "debug" =>
Build_Switches := ("-g", -- Debug info
"-Og"); -- No optimization
end case;

package Compiler is
for Default_Switches ("Ada") use
Compile_Checks_Switches &
Build_Switches &
Runtime_Checks_Switches &
Style_Checks_Switches &
Contracts_Switches &
("-gnatw.X", -- Disable warnings for No_Exception_Propagation
"-gnatQ"); -- Don't quit. Generate ALI and tree files even if illegalities
end Compiler;

package Binder is
for Switches ("Ada") use ("-Es"); -- Symbolic traceback
end Binder;

package Pretty_Printer is
for Default_Switches ("ada") use
("--no-separate-is",
"--name-mixed-case",
"--call-threshold=1",
"--par-threshold=2",
"--vertical-named-aggregates",
"--wide-character-encoding=8");
end Pretty_Printer;

end Adb_Controller;

@Joebeazelman
Copy link
Author

Any updates on why this error is happening?

@reznikmm
Copy link
Member

Currently Ada Language Server supports only Ada files (no support for editing GPR files yet). Could you provide a complete self-contained reproducer?

@joaopsazevedo joaopsazevedo self-assigned this Dec 14, 2022
@AnthonyLeonardoGracio
Copy link
Collaborator

Hello @Joebeazelman,

Note that tabSize is correctly taken into account by default. But for tabSize to be effective in VS Code, you need disable the Detect Indentation VS Code setting first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants