From 7b3d5d8abbc602e49b0916029391a93d79b670d5 Mon Sep 17 00:00:00 2001 From: Joao Azevedo Date: Wed, 15 May 2024 10:32:05 +0000 Subject: [PATCH 1/2] Add GNATformat as a secondary formatter Activated through the ALS.GNATFORMAT trace. --- gnat/lsp_server.gpr | 1 + source/ada/lsp-ada_contexts.adb | 2 + source/ada/lsp-ada_contexts.ads | 13 ++ source/ada/lsp-ada_documents.adb | 57 ++++++ source/ada/lsp-ada_documents.ads | 14 ++ source/ada/lsp-ada_driver.adb | 3 + source/ada/lsp-ada_handlers-formatting.adb | 164 ++++++++++++++---- testsuite/.als/gpr_ls_traces.cfg | 4 + .../gnatformat/range_format/.gnatdebug | 1 + .../ada_lsp/gnatformat/range_format/main.adb | 13 ++ .../ada_lsp/gnatformat/range_format/test.gpr | 4 + .../ada_lsp/gnatformat/range_format/test.json | 140 +++++++++++++++ .../ada_lsp/gnatformat/range_format/test.yaml | 1 + .../gnatformat/whole_document/.gnatdebug | 1 + .../no_format_package/.gnatdebug | 1 + .../whole_document/no_format_package/main.adb | 1 + .../whole_document/no_format_package/test.gpr | 5 + .../no_format_package/test.json | 150 ++++++++++++++++ .../no_format_package/test.yaml | 1 + .../with_format_package/.gnatdebug | 1 + .../with_format_package/main.adb | 1 + .../with_format_package/test.gpr | 8 + .../with_format_package/test.json | 150 ++++++++++++++++ .../with_format_package/test.yaml | 1 + .../completion_package_resolve/test.json | 6 + .../gpr_lsp/completion_references/test.json | 24 +++ 26 files changed, 730 insertions(+), 37 deletions(-) create mode 100644 testsuite/.als/gpr_ls_traces.cfg create mode 100644 testsuite/ada_lsp/gnatformat/range_format/.gnatdebug create mode 100644 testsuite/ada_lsp/gnatformat/range_format/main.adb create mode 100644 testsuite/ada_lsp/gnatformat/range_format/test.gpr create mode 100644 testsuite/ada_lsp/gnatformat/range_format/test.json create mode 100644 testsuite/ada_lsp/gnatformat/range_format/test.yaml create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/.gnatdebug create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/no_format_package/.gnatdebug create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/no_format_package/main.adb create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.gpr create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.json create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.yaml create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/with_format_package/.gnatdebug create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/with_format_package/main.adb create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.gpr create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.json create mode 100644 testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.yaml diff --git a/gnat/lsp_server.gpr b/gnat/lsp_server.gpr index 71f2357fc..3cd1ed480 100644 --- a/gnat/lsp_server.gpr +++ b/gnat/lsp_server.gpr @@ -11,6 +11,7 @@ with "lal_refactor.gpr"; with "ada_libfswatch.gpr"; with "libgnatdoc.gpr"; with "spawn.gpr"; +with "gnatformat.gpr"; with "lsp_3_17"; with "lsp_common"; diff --git a/source/ada/lsp-ada_contexts.adb b/source/ada/lsp-ada_contexts.adb index 58ba8b1c5..d50293ee6 100644 --- a/source/ada/lsp-ada_contexts.adb +++ b/source/ada/lsp-ada_contexts.adb @@ -760,7 +760,9 @@ package body LSP.Ada_Contexts is Self.Reload; Update_Source_Files; + Pretty_Printer_Setup; + Self.Format_Options := Gnatformat.Configuration.From_Project (Root); end Load_Project; ------------ diff --git a/source/ada/lsp-ada_contexts.ads b/source/ada/lsp-ada_contexts.ads index a0c336c48..7a6ebd6d8 100644 --- a/source/ada/lsp-ada_contexts.ads +++ b/source/ada/lsp-ada_contexts.ads @@ -23,6 +23,8 @@ with GNATCOLL.VFS; with GNATdoc.Comments.Options; +with Gnatformat.Configuration; + with GPR2.Project.Tree; with GPR2.Project.View; @@ -177,6 +179,10 @@ package LSP.Ada_Contexts is Utils.Command_Lines.Command_Line; -- Return the command line for the Pretty Printer + function Get_Format_Options + (Self : Context) return Gnatformat.Configuration.Format_Options_Type; + -- Return the formatting options for Gnatformat + function Get_Documentation_Style (Self : Context) return GNATdoc.Comments.Options.Documentation_Style; -- Get the documentation style used for this context. @@ -314,6 +320,8 @@ private (Pp.Command_Lines.Descriptor'Access); -- Object to keep gnatpp options + Format_Options : Gnatformat.Configuration.Format_Options_Type; + Style : GNATdoc.Comments.Options.Documentation_Style := GNATdoc.Comments.Options.GNAT; -- The context's documentation style. @@ -339,6 +347,11 @@ private function Get_PP_Options (Self : Context) return Utils.Command_Lines.Command_Line is (Self.PP_Options); + function Get_Format_Options + (Self : Context) + return Gnatformat.Configuration.Format_Options_Type + is (Self.Format_Options); + function Get_Documentation_Style (Self : Context) return GNATdoc.Comments.Options.Documentation_Style is (Self.Style); diff --git a/source/ada/lsp-ada_documents.adb b/source/ada/lsp-ada_documents.adb index 7e2f736ef..a994918bf 100644 --- a/source/ada/lsp-ada_documents.adb +++ b/source/ada/lsp-ada_documents.adb @@ -21,6 +21,8 @@ with GNAT.Strings; with GNATCOLL.Traces; with GNATCOLL.VFS; +with Gnatformat.Formatting; + with Langkit_Support.Symbols; with Langkit_Support.Text; @@ -613,6 +615,28 @@ package body LSP.Ada_Documents is return False; end Formatting; + ------------ + -- Format -- + ------------ + + function Format + (Self : Document; + Context : LSP.Ada_Contexts.Context) + return LSP.Structures.TextEdit_Vector + is + Result : LSP.Structures.TextEdit_Vector; + + Formatted_Document : constant VSS.Strings.Virtual_String := + VSS.Strings.Conversions.To_Virtual_String + (Gnatformat.Formatting.Format (Self.Unit (Context), + Context.Get_Format_Options)); + + begin + Self.Diff (New_Text => Formatted_Document, Edit => Result); + + return Result; + end Format; + -------------------- -- Get_Any_Symbol -- -------------------- @@ -1211,6 +1235,39 @@ package body LSP.Ada_Documents is return False; end Range_Formatting; + ------------------ + -- Range_Format -- + ------------------ + + function Range_Format + (Self : Document; + Context : LSP.Ada_Contexts.Context; + Span : LSP.Structures.A_Range) + return LSP.Structures.TextEdit + is + use type LSP.Structures.A_Range; + + begin + if Span = LSP.Text_Documents.Empty_Range then + return (LSP.Constants.Empty, VSS.Strings.Empty_Virtual_String); + end if; + + declare + Range_Formatted_Document : + constant Gnatformat.Formatting.Formatted_Edits := + Gnatformat.Formatting.Range_Format + (Self.Unit (Context), + Self.To_Source_Location_Range (Span), + Context.Get_Format_Options); + + begin + return + (Self.To_A_Range (Range_Formatted_Document.Edit.Location), + VSS.Strings.Conversions.To_Virtual_String + (Range_Formatted_Document.Edit.Text)); + end; + end Range_Format; + ------------------------ -- Reset_Symbol_Cache -- ------------------------ diff --git a/source/ada/lsp-ada_documents.ads b/source/ada/lsp-ada_documents.ads index f06db53b6..9155eaf6e 100644 --- a/source/ada/lsp-ada_documents.ads +++ b/source/ada/lsp-ada_documents.ads @@ -177,6 +177,12 @@ package LSP.Ada_Documents is Messages : out VSS.String_Vectors.Virtual_String_Vector) return Boolean; -- Format document or its part defined in Span + function Format + (Self : Document; + Context : LSP.Ada_Contexts.Context) + return LSP.Structures.TextEdit_Vector; + -- Format Self with formatting options based on Context + function Range_Formatting (Self : Document; Context : LSP.Ada_Contexts.Context; @@ -187,6 +193,14 @@ package LSP.Ada_Documents is return Boolean; -- Format document or its part defined in Span + function Range_Format + (Self : Document; + Context : LSP.Ada_Contexts.Context; + Span : LSP.Structures.A_Range) + return LSP.Structures.TextEdit; + -- Format part of Self defined by Span with formatting options based on + -- Context. + procedure Find_All_References (Self : Document; Context : LSP.Ada_Contexts.Context; diff --git a/source/ada/lsp-ada_driver.adb b/source/ada/lsp-ada_driver.adb index 3a916062d..0475f0d50 100644 --- a/source/ada/lsp-ada_driver.adb +++ b/source/ada/lsp-ada_driver.adb @@ -24,6 +24,7 @@ with GNAT.OS_Lib; with GNAT.Strings; pragma Warnings (Off, "is an internal GNAT unit"); +with Gnatformat.Configuration; with System.Soft_Links; with System.Secondary_Stack; @@ -428,6 +429,8 @@ begin Ada.Text_IO.Set_Output (Ada.Text_IO.Standard_Error); -- Protect stdout from pollution by accidental Put_Line calls + Gnatformat.Configuration.Elaborate_GPR2; + declare Allow_Incremental_Text_Changes : constant GNATCOLL.Traces.Trace_Handle := GNATCOLL.Traces.Create ("ALS.ALLOW_INCREMENTAL_TEXT_CHANGES", diff --git a/source/ada/lsp-ada_handlers-formatting.adb b/source/ada/lsp-ada_handlers-formatting.adb index d9d8b414b..9decce3c9 100644 --- a/source/ada/lsp-ada_handlers-formatting.adb +++ b/source/ada/lsp-ada_handlers-formatting.adb @@ -27,6 +27,9 @@ package body LSP.Ada_Handlers.Formatting is Formatting_Trace : constant GNATCOLL.Traces.Trace_Handle := GNATCOLL.Traces.Create ("ALS.FORMATTING", GNATCOLL.Traces.On); + Gnatformat_Trace : constant GNATCOLL.Traces.Trace_Handle := + GNATCOLL.Traces.Create ("ALS.GNATFORMAT", GNATCOLL.Traces.Off); + procedure Update_Pp_Formatting_Options (Pp_Options : in out Utils.Command_Lines.Command_Line; LSP_Options : LSP.Structures.FormattingOptions); @@ -48,36 +51,75 @@ package body LSP.Ada_Handlers.Formatting is Messages : out VSS.String_Vectors.Virtual_String_Vector; Error : out LSP.Errors.ResponseError) is - PP_Options : Utils.Command_Lines.Command_Line := Context.Get_PP_Options; + procedure Gnatpp_Format; + + procedure Gnatformat_Format; + + ------------------- + -- Gnatpp_Format -- + ------------------- + + procedure Gnatpp_Format + is + PP_Options : Utils.Command_Lines.Command_Line := + Context.Get_PP_Options; + + begin + -- Take into account the options set by the request only if the + -- corresponding GPR switches are not explicitly set. + + Update_Pp_Formatting_Options + (Pp_Options => PP_Options, LSP_Options => Options); + + Success := Document.Formatting + (Context => Context, + Span => Span, + Cmd => PP_Options, + Edit => Response, + Messages => Messages); + + if not Success then + Error := + (code => LSP.Enumerations.InternalError, + message => Messages.Join (' ')); + Messages.Clear; + end if; + end Gnatpp_Format; + + ----------------------- + -- Gnatformat_Format -- + ----------------------- + + procedure Gnatformat_Format + is + begin + Success := True; + Response := Document.Format (Context); + + exception + when E : others => + Context.Tracer.Trace_Exception (E, "in GNATformat Format"); + Success := False; + Error := + (code => LSP.Enumerations.InternalError, + message => "GNATformat failed to format source"); + end Gnatformat_Format; begin if Document.Has_Diagnostics (Context) then Success := False; Error := - (code => LSP.Enumerations.InternalError, + (code => LSP.Enumerations.InternalError, message => "Incorrect code can't be formatted"); return; end if; - -- Take into account the options set by the request only if the - -- corresponding GPR switches are not explicitly set. + if Gnatformat_Trace.Is_Active then + Gnatformat_Format; - Update_Pp_Formatting_Options - (Pp_Options => PP_Options, LSP_Options => Options); - - Success := Document.Formatting - (Context => Context, - Span => Span, - Cmd => PP_Options, - Edit => Response, - Messages => Messages); - - if not Success then - Error := - (code => LSP.Enumerations.InternalError, - message => Messages.Join (' ')); - Messages.Clear; + else + Gnatpp_Format; end if; end Format; @@ -94,8 +136,68 @@ package body LSP.Ada_Handlers.Formatting is Response : out LSP.Structures.TextEdit_Vector; Error : out LSP.Errors.ResponseError) is - PP_Options : Utils.Command_Lines.Command_Line := Context.Get_PP_Options; - Messages : VSS.String_Vectors.Virtual_String_Vector; + procedure Gnatpp_Range_Format; + + procedure Gnatformat_Range_Format; + + ------------------------- + -- Gnatpp_Range_Format -- + ------------------------- + + procedure Gnatpp_Range_Format + is + PP_Options : Utils.Command_Lines.Command_Line := + Context.Get_PP_Options; + Messages : VSS.String_Vectors.Virtual_String_Vector; + + begin + -- Take into account the options set by the request only if the + -- corresponding GPR switches are not explicitly set. + + Update_Pp_Formatting_Options + (Pp_Options => PP_Options, LSP_Options => Options); + + Success := Document.Range_Formatting + (Context => Context, + Span => Span, + PP_Options => PP_Options, + Edit => Response, + Messages => Messages); + + if not Success then + Error := + (code => LSP.Enumerations.InternalError, + message => Messages.Join (' ')); + end if; + if Document.Has_Diagnostics (Context) then + Success := False; + Error := + (code => LSP.Enumerations.InternalError, + message => "Incorrect code can't be formatted"); + + return; + end if; + end Gnatpp_Range_Format; + + ---------------------------- + -- Gnatformat_Range_Format -- + ----------------------------- + + procedure Gnatformat_Range_Format + is + begin + Success := True; + Response.Clear; + Response.Append (Document.Range_Format (Context, Span)); + + exception + when E : others => + Context.Tracer.Trace_Exception (E, "in GNATformat Range_Format"); + Success := False; + Error := + (code => LSP.Enumerations.InternalError, + message => "GNATformat failed to format source"); + end Gnatformat_Range_Format; begin if Document.Has_Diagnostics (Context) then @@ -107,23 +209,11 @@ package body LSP.Ada_Handlers.Formatting is return; end if; - -- Take into account the options set by the request only if the - -- corresponding GPR switches are not explicitly set. + if Gnatformat_Trace.Is_Active then + Gnatformat_Range_Format; - Update_Pp_Formatting_Options - (Pp_Options => PP_Options, LSP_Options => Options); - - Success := Document.Range_Formatting - (Context => Context, - Span => Span, - PP_Options => PP_Options, - Edit => Response, - Messages => Messages); - - if not Success then - Error := - (code => LSP.Enumerations.InternalError, - message => Messages.Join (' ')); + else + Gnatpp_Range_Format; end if; end Range_Format; diff --git a/testsuite/.als/gpr_ls_traces.cfg b/testsuite/.als/gpr_ls_traces.cfg new file mode 100644 index 000000000..0c46eb1e4 --- /dev/null +++ b/testsuite/.als/gpr_ls_traces.cfg @@ -0,0 +1,4 @@ +>gpr_ls_log.$T.log:buffer_size=0 +ALS.MAIN=yes +ALS.IN=no +ALS.OUT=no diff --git a/testsuite/ada_lsp/gnatformat/range_format/.gnatdebug b/testsuite/ada_lsp/gnatformat/range_format/.gnatdebug new file mode 100644 index 000000000..e1bc4b740 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/range_format/.gnatdebug @@ -0,0 +1 @@ +ALS.GNATFORMAT=yes diff --git a/testsuite/ada_lsp/gnatformat/range_format/main.adb b/testsuite/ada_lsp/gnatformat/range_format/main.adb new file mode 100644 index 000000000..5253e532a --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/range_format/main.adb @@ -0,0 +1,13 @@ +with Ada.Text_IO; + +procedure Main is + procedure You_Say_Hello; + + procedure You_Say_Hello is begin + Ada.Text_Io.Put_Line ("Hello"); + end; + +begin + Ada.Text_IO.Put_Line ("Hello!"); + You_Say_Hello; +end Main; diff --git a/testsuite/ada_lsp/gnatformat/range_format/test.gpr b/testsuite/ada_lsp/gnatformat/range_format/test.gpr new file mode 100644 index 000000000..fafff950c --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/range_format/test.gpr @@ -0,0 +1,4 @@ +project Test is + for Object_Dir use "obj"; + for Main use ("main.adb"); +end Test; diff --git a/testsuite/ada_lsp/gnatformat/range_format/test.json b/testsuite/ada_lsp/gnatformat/range_format/test.json new file mode 100644 index 000000000..b1d289ee4 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/range_format/test.json @@ -0,0 +1,140 @@ +[ + { + "start": { + "cmd": [ + "${ALS}" + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "id": 1, + "method": "initialize", + "params": { + "rootUri": "$URI{.}", + "capabilities": { + "textDocument": { + "rangeFormatting": { + "dynamicRegistration": true + } + } + } + } + }, + "wait": [ + { + "jsonrpc": "2.0", + "id": 1, + "result": { + "capabilities": { + "documentFormattingProvider": true + } + } + } + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "method": "initialized", + "params": {} + }, + "wait": [] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "method": "workspace/didChangeConfiguration", + "params": { + "settings": { + "ada": { + "trace": { + "server": "off" + }, + "projectFile": "test.gpr", + "scenarioVariables": {}, + "defaultCharset": "iso-8859-1", + "displayMethodAncestryOnNavigation": "usage_and_abstract_only", + "enableDiagnostics": true, + "useCompletionSnippets": false, + "renameInComments": false + } + } + } + }, + "wait": [] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "method": "textDocument/didOpen", + "params": { + "textDocument": { + "uri": "$URI{main.adb}", + "languageId": "ada", + "version": 1, + "text": "with Ada.Text_IO;\n\nprocedure Main is\n procedure You_Say_Hello;\n procedure You_Say_Hello is begin\n Ada.Text_Io.Put_Line (\"Hello\");\n end;\n\nbegin\n Ada.Text_IO.Put_Line (\"Hello!\");\n You_Say_Hello;\nend Main;" + } + } + }, + "wait": [] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "id": 2, + "method": "textDocument/rangeFormatting", + "params": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "range": { + "start": { + "line": 5, + "character": 8 + }, + "end": { + "line": 7, + "character": 1 + } + } + } + }, + "wait": [ + { + "id": 2, + "result": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 6, + "character": 7 + } + }, + "newText": " procedure You_Say_Hello\n is\n begin\n Ada.Text_Io.Put_Line (\"Hello\");\n end;" + } + ] + } + ] + } + }, + { + "stop": { + "exit_code": 0 + } + } +] diff --git a/testsuite/ada_lsp/gnatformat/range_format/test.yaml b/testsuite/ada_lsp/gnatformat/range_format/test.yaml new file mode 100644 index 000000000..a63fce29a --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/range_format/test.yaml @@ -0,0 +1 @@ +title: 'GNATformat - Range format document' diff --git a/testsuite/ada_lsp/gnatformat/whole_document/.gnatdebug b/testsuite/ada_lsp/gnatformat/whole_document/.gnatdebug new file mode 100644 index 000000000..e1bc4b740 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/.gnatdebug @@ -0,0 +1 @@ +ALS.GNATFORMAT=yes diff --git a/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/.gnatdebug b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/.gnatdebug new file mode 100644 index 000000000..e1bc4b740 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/.gnatdebug @@ -0,0 +1 @@ +ALS.GNATFORMAT=yes diff --git a/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/main.adb b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/main.adb new file mode 100644 index 000000000..129594cb7 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/main.adb @@ -0,0 +1 @@ +procedure Main is begin null; end Main; diff --git a/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.gpr b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.gpr new file mode 100644 index 000000000..cb4d50de6 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.gpr @@ -0,0 +1,5 @@ +project Test is + for Source_Dirs use ("."); + for Object_Dir use "obj"; + for Main use ("main.adb"); +end Test; diff --git a/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.json b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.json new file mode 100644 index 000000000..a303b6c09 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.json @@ -0,0 +1,150 @@ +[ + { + "comment": [ + "Test formatting request with GNATformat" + ] + }, + { + "start": { + "cmd": [ + "${ALS}" + ] + } + }, + { + "send": { + "request": { + "params": { + "processId": 20634, + "capabilities": { + "textDocument": { + "formatting": { + "dynamicRegistration": true + } + } + }, + "rootUri": "$URI{.}" + }, + "jsonrpc": "2.0", + "id": 1, + "method": "initialize" + }, + "wait": [ + { + "id": 1, + "result": { + "capabilities": { + "documentFormattingProvider": true + } + } + } + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "method": "initialized" + }, + "wait": [] + } + }, + { + "send": { + "request": { + "params": { + "settings": { + "ada": { + "projectFile": "$URI{test.gpr}", + "scenarioVariables": {}, + "enableDiagnostics": false, + "defaultCharset": "ISO-8859-1" + } + } + }, + "jsonrpc": "2.0", + "method": "workspace/didChangeConfiguration" + }, + "wait": [] + } + }, + { + "send": { + "request": { + "params": { + "textDocument": { + "text": "procedure Main is begin null; end Main;\n", + "version": 0, + "uri": "$URI{main.adb}", + "languageId": "Ada" + } + }, + "jsonrpc": "2.0", + "method": "textDocument/didOpen" + }, + "wait": [] + } + }, + { + "send": { + "request": { + "params": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "options": { + "trimTrailingWhitespace": true, + "insertSpaces": true, + "tabSize": 3, + "trimFinalNewlines": true, + "insertFinalNewline": false + } + }, + "jsonrpc": "2.0", + "id": 3, + "method": "textDocument/formatting" + }, + "wait": [ + { + "id": 3, + "result": [ + { + "newText": "procedure Main\nis\nbegin\n null;\nend Main;", + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 1, + "character": 0 + } + } + } + ] + } + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "id": 5, + "method": "shutdown" + }, + "wait": [ + { + "id": 5, + "result": null + } + ] + } + }, + { + "stop": { + "exit_code": 0 + } + } +] diff --git a/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.yaml b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.yaml new file mode 100644 index 000000000..e398ac430 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/no_format_package/test.yaml @@ -0,0 +1 @@ +title: 'GNATformat - Format whole document' diff --git a/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/.gnatdebug b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/.gnatdebug new file mode 100644 index 000000000..e1bc4b740 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/.gnatdebug @@ -0,0 +1 @@ +ALS.GNATFORMAT=yes diff --git a/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/main.adb b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/main.adb new file mode 100644 index 000000000..129594cb7 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/main.adb @@ -0,0 +1 @@ +procedure Main is begin null; end Main; diff --git a/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.gpr b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.gpr new file mode 100644 index 000000000..81e679241 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.gpr @@ -0,0 +1,8 @@ +project Test is + for Source_Dirs use ("."); + for Object_Dir use "obj"; + for Main use ("main.adb"); + package Format is + for Indentation ("Ada") use "4"; + end Format; +end Test; diff --git a/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.json b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.json new file mode 100644 index 000000000..626b3e760 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.json @@ -0,0 +1,150 @@ +[ + { + "comment": [ + "Test formatting request with GNATformat" + ] + }, + { + "start": { + "cmd": [ + "${ALS}" + ] + } + }, + { + "send": { + "request": { + "params": { + "processId": 20634, + "capabilities": { + "textDocument": { + "formatting": { + "dynamicRegistration": true + } + } + }, + "rootUri": "$URI{.}" + }, + "jsonrpc": "2.0", + "id": 1, + "method": "initialize" + }, + "wait": [ + { + "id": 1, + "result": { + "capabilities": { + "documentFormattingProvider": true + } + } + } + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "method": "initialized" + }, + "wait": [] + } + }, + { + "send": { + "request": { + "params": { + "settings": { + "ada": { + "projectFile": "$URI{test.gpr}", + "scenarioVariables": {}, + "enableDiagnostics": false, + "defaultCharset": "ISO-8859-1" + } + } + }, + "jsonrpc": "2.0", + "method": "workspace/didChangeConfiguration" + }, + "wait": [] + } + }, + { + "send": { + "request": { + "params": { + "textDocument": { + "text": "procedure Main is begin null; end Main;\n", + "version": 0, + "uri": "$URI{main.adb}", + "languageId": "Ada" + } + }, + "jsonrpc": "2.0", + "method": "textDocument/didOpen" + }, + "wait": [] + } + }, + { + "send": { + "request": { + "params": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "options": { + "trimTrailingWhitespace": true, + "insertSpaces": true, + "tabSize": 3, + "trimFinalNewlines": true, + "insertFinalNewline": false + } + }, + "jsonrpc": "2.0", + "id": 3, + "method": "textDocument/formatting" + }, + "wait": [ + { + "id": 3, + "result": [ + { + "newText": "procedure Main\nis\nbegin\n null;\nend Main;", + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 1, + "character": 0 + } + } + } + ] + } + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "id": 5, + "method": "shutdown" + }, + "wait": [ + { + "id": 5, + "result": null + } + ] + } + }, + { + "stop": { + "exit_code": 0 + } + } +] diff --git a/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.yaml b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.yaml new file mode 100644 index 000000000..cfaad0147 --- /dev/null +++ b/testsuite/ada_lsp/gnatformat/whole_document/with_format_package/test.yaml @@ -0,0 +1 @@ +title: 'GNATformat - Format whole document with settings from package Format' diff --git a/testsuite/gpr_lsp/completion_package_resolve/test.json b/testsuite/gpr_lsp/completion_package_resolve/test.json index 30d3afe08..ccaa124c3 100644 --- a/testsuite/gpr_lsp/completion_package_resolve/test.json +++ b/testsuite/gpr_lsp/completion_package_resolve/test.json @@ -477,6 +477,12 @@ "Remote" ] }, + { + "label": "Format", + "data": [ + "Format" + ] + }, { "label": "Pretty_Printer", "data": [ diff --git a/testsuite/gpr_lsp/completion_references/test.json b/testsuite/gpr_lsp/completion_references/test.json index 51bcf2161..35a75183d 100644 --- a/testsuite/gpr_lsp/completion_references/test.json +++ b/testsuite/gpr_lsp/completion_references/test.json @@ -209,6 +209,10 @@ "label": "Remote", "data": ["Remote"] }, + { + "label": "Format", + "data": ["Format"] + }, { "label": "Pretty_Printer", "data": ["Pretty_Printer"] @@ -356,6 +360,10 @@ "label": "Remote", "data": ["Remote"] }, + { + "label": "Format", + "data": ["Format"] + }, { "label": "Pretty_Printer", "data": ["Pretty_Printer"] @@ -863,6 +871,10 @@ "label": "Remote", "data": ["Remote"] }, + { + "label": "Format", + "data": ["Format"] + }, { "label": "Pretty_Printer", "data": ["Pretty_Printer"] @@ -1203,6 +1215,10 @@ "label": "Remote", "data": ["Remote"] }, + { + "label": "Format", + "data": ["Format"] + }, { "label": "Pretty_Printer", "data": ["Pretty_Printer"] @@ -1359,6 +1375,10 @@ "label": "Remote", "data": ["Remote"] }, + { + "label": "Format", + "data": ["Format"] + }, { "label": "Pretty_Printer", "data": ["Pretty_Printer"] @@ -1515,6 +1535,10 @@ "label": "Remote", "data": ["Remote"] }, + { + "label": "Format", + "data": ["Format"] + }, { "label": "Pretty_Printer", "data": ["Pretty_Printer"] From eefa6da02ff789a72a234b1a9651051de8a6cb44 Mon Sep 17 00:00:00 2001 From: Joao Azevedo Date: Thu, 25 Jul 2024 13:11:47 +0000 Subject: [PATCH 2/2] Add ALS.GNATFORMAT trace documentation --- doc/traces.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/traces.md b/doc/traces.md index f03ffa92c..41f0edf26 100644 --- a/doc/traces.md +++ b/doc/traces.md @@ -61,3 +61,9 @@ in `initialize` response. Log lalpp output if `yes`. ALS.LAL_PP_OUTPUT_ON_FORMATTING=yes + +## `ALS.GNATFORMAT` (default no) + +Use GNATformat as format provider. + + ALS.GNATFORMAT=yes