diff --git a/source/ada/lsp-ada_driver.adb b/source/ada/lsp-ada_driver.adb index ec4d16e5f..213c35c95 100644 --- a/source/ada/lsp-ada_driver.adb +++ b/source/ada/lsp-ada_driver.adb @@ -44,7 +44,7 @@ with LSP.Ada_Handlers; with LSP.Ada_Handlers.Named_Parameters_Commands; with LSP.Ada_Handlers.Other_File_Commands; with LSP.Ada_Handlers.Project_Reload_Commands; -with LSP.Ada_Handlers.Refactor.Auto_Import; +with LSP.Ada_Handlers.Refactor.Imports_Commands; with LSP.Ada_Handlers.Refactor.Add_Parameter; with LSP.Ada_Handlers.Refactor.Remove_Parameter; with LSP.Ada_Handlers.Refactor.Move_Parameter; @@ -148,7 +148,7 @@ procedure LSP.Ada_Driver is LSP.Commands.Register (LSP.Ada_Handlers.Named_Parameters_Commands.Command'Tag); LSP.Commands.Register - (LSP.Ada_Handlers.Refactor.Auto_Import.Command'Tag); + (LSP.Ada_Handlers.Refactor.Imports_Commands.Command'Tag); LSP.Commands.Register (LSP.Ada_Handlers.Refactor.Suppress_Seperate.Command'Tag); LSP.Commands.Register diff --git a/source/ada/lsp-ada_handlers-refactor-auto_import.adb b/source/ada/lsp-ada_handlers-refactor-imports_commands.adb similarity index 50% rename from source/ada/lsp-ada_handlers-refactor-auto_import.adb rename to source/ada/lsp-ada_handlers-refactor-imports_commands.adb index 160e7b567..a81aedd3b 100644 --- a/source/ada/lsp-ada_handlers-refactor-auto_import.adb +++ b/source/ada/lsp-ada_handlers-refactor-imports_commands.adb @@ -1,7 +1,7 @@ ------------------------------------------------------------------------------ -- Language Server Protocol -- -- -- --- Copyright (C) 2020-2023, AdaCore -- +-- Copyright (C) 2020-2022, AdaCore -- -- -- -- This is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -16,18 +16,22 @@ ------------------------------------------------------------------------------ with Ada.Strings.UTF_Encoding; +with Ada.Strings.Unbounded; with Ada.Strings.Wide_Wide_Unbounded; with Langkit_Support.Text; with Libadalang.Analysis; +with Libadalang.Common; + +with Laltools.Common; -with LSP.Commands; with LSP.Messages; with VSS.Strings.Conversions; +with LSP.Commands; -package body LSP.Ada_Handlers.Refactor.Auto_Import is +package body LSP.Ada_Handlers.Refactor.Imports_Commands is ---------------- -- Initialize -- @@ -42,11 +46,8 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is begin Self.Context := Context.Id; Self.Where := Where; - Self.Suggestion := - (Import => - VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String (With_Clause), - Qualifier => - VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String (Prefix)); + Self.With_Clause := With_Clause; + Self.Prefix := Prefix; end Initialize; ------------ @@ -73,25 +74,10 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is LSP.Types.Read_String (JS, V.Context); elsif Key = "where" then LSP.Messages.TextDocumentPositionParams'Read (JS, V.Where); - elsif Key = "import" then - declare - Import : VSS.Strings.Virtual_String; - begin - LSP.Types.Read_String (JS, Import); - V.Suggestion.Import := - VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String - (Import); - end; - - elsif Key = "qualifier" then - declare - Qualififer : VSS.Strings.Virtual_String; - begin - LSP.Types.Read_String (JS, Qualififer); - V.Suggestion.Qualifier := - VSS.Strings.Conversions.To_Unbounded_Wide_Wide_String - (Qualififer); - end; + elsif Key = "with_clause" then + LSP.Types.Read_String (JS, V.With_Clause); + elsif Key = "prefix" then + LSP.Types.Read_String (JS, V.Prefix); else JS.Skip_Value; end if; @@ -110,13 +96,13 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is Context : Context_Access; Where : LSP.Messages.Location; Commands_Vector : in out LSP.Messages.CodeAction_Vector; - Suggestion : LAL_Refactor.Auto_Import.Import_Type) + Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion) is Pointer : LSP.Commands.Command_Pointer; Item : LSP.Messages.CodeAction; function Create_Suggestion_Title - (Suggestion : LAL_Refactor.Auto_Import.Import_Type) + (Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion) return VSS.Strings.Virtual_String; -- Creates the suggestion text that will be shown by the client to -- to the developer. The text is costumized based on the need of @@ -126,17 +112,42 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is -- Create_Suggestions_Title -- ------------------------------ function Create_Suggestion_Title - (Suggestion : LAL_Refactor.Auto_Import.Import_Type) + (Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion) return VSS.Strings.Virtual_String is - use Ada.Strings.Wide_Wide_Unbounded; + Title : Ada.Strings.Wide_Wide_Unbounded. + Unbounded_Wide_Wide_String + := Ada.Strings.Wide_Wide_Unbounded. + Null_Unbounded_Wide_Wide_String; + use type Ada.Strings.Wide_Wide_Unbounded. + Unbounded_Wide_Wide_String; - Title : constant Langkit_Support.Text.Unbounded_Text_Type := - "Qualify with " & Suggestion.Qualifier; begin - return - VSS.Strings.To_Virtual_String - (Langkit_Support.Text.To_Text (Title)); + if Suggestion.With_Clause_Text /= "" then + if Suggestion.Prefix_Text /= "" then + -- Add with clause and prefix + Title := + Title + & "Add 'with' clause for " + & Suggestion.With_Clause_Text + & " and prefix the object with " + & Suggestion.Prefix_Text; + + else + -- Add with clause and leave the prefix as it is + Title := + Title + & "Add 'with' clause for " + & Suggestion.With_Clause_Text; + end if; + else + -- Only add prefix + + Title := Title & "Prefix the object with " + & Suggestion.Prefix_Text; + end if; + return VSS.Strings.To_Virtual_String + (Langkit_Support.Text.To_Text (Title)); end Create_Suggestion_Title; begin @@ -146,15 +157,15 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is Where.span.first), With_Clause => VSS.Strings.Conversions.To_Virtual_String - (Suggestion.Import), + (Suggestion.With_Clause_Text), Prefix => VSS.Strings.Conversions.To_Virtual_String - (Suggestion.Qualifier)); + (Suggestion.Prefix_Text)); Pointer.Set (Self); Item := (title => Create_Suggestion_Title (Suggestion), kind => (Is_Set => True, - Value => LSP.Messages.QuickFix), + Value => LSP.Messages.RefactorRewrite), diagnostics => (Is_Set => False), disabled => (Is_Set => False), edit => (Is_Set => False), @@ -177,20 +188,119 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is Document : LSP.Ada_Documents.Document_Access) return LAL_Refactor.Refactoring_Edits is + use Langkit_Support.Text; use Libadalang.Analysis; - use LAL_Refactor.Auto_Import; + use Libadalang.Common; + use Libadalang.Slocs; + use LAL_Refactor; + use VSS.Strings; + use VSS.Strings.Conversions; - Name : constant Libadalang.Analysis.Name := - Document.Get_Node_At (Context, Self.Where.position).As_Name; + Node : Ada_Node := + Document.Get_Node_At (Context, Self.Where.position); - function Units return Analysis_Unit_Array is ([]); + Edits : LAL_Refactor.Refactoring_Edits; begin - return - Create_Auto_Importer - (Name, - Self.Suggestion) - .Refactor (Units'Access); + -- Add prefix + + if not Self.Prefix.Is_Empty + and then Node.Kind in Ada_Identifier + then + -- If this is a DottedName them remove the current prefix and replace + -- it by the suggested one. Otherwise, just add the prepend the + -- prefix + + while Node.Parent.Kind in Ada_Dotted_Name_Range loop + Node := Node.Parent; + end loop; + + if Node.Kind in Ada_Dotted_Name_Range then + Node := Node.As_Dotted_Name.F_Suffix.As_Ada_Node; + end if; + + if Node.Parent.Kind = Ada_Dotted_Name then + -- Node.Parent is the full Dotted Name: this includes the + -- current prefixes and the identifier. Using this SLOC instead + -- of only the current prefixes SLOC is better since this covers + -- cases when the Dotted Name is splitted in multiple lines. + + Safe_Insert + (Edits => Edits.Text_Edits, + File_Name => Node.Unit.Get_Filename, + Edit => + Text_Edit' + (Location => + Make_Range + (Start_Sloc + (Node.Parent.As_Dotted_Name.F_Prefix.Sloc_Range), + Start_Sloc (Node.Sloc_Range)), + Text => + Ada.Strings.Unbounded.To_Unbounded_String + (To_UTF8 (To_Wide_Wide_String (Self.Prefix))))); + + else + Safe_Insert + (Edits => Edits.Text_Edits, + File_Name => Node.Unit.Get_Filename, + Edit => + Text_Edit' + (Location => + Make_Range + (Start_Sloc (Node.Sloc_Range), + Start_Sloc (Node.Sloc_Range)), + Text => + Ada.Strings.Unbounded.To_Unbounded_String + (To_UTF8 (To_Wide_Wide_String (Self.Prefix))))); + end if; + end if; + + -- Add with clause + + if not Self.With_Clause.Is_Empty then + declare + Last : Boolean; + S : constant Libadalang.Slocs.Source_Location := + Laltools.Common.Get_Insert_With_Location + (Node => Laltools.Common.Get_Compilation_Unit (Node), + Pack_Name => + VSS.Strings.Conversions.To_Wide_Wide_String + (Self.With_Clause), + Last => Last); + begin + if S /= Libadalang.Slocs.No_Source_Location then + if Last then + Safe_Insert + (Edits => Edits.Text_Edits, + File_Name => Node.Unit.Get_Filename, + Edit => + Text_Edit' + (Location => Make_Range (S, S), + Text => + Ada.Strings.Unbounded.To_Unbounded_String + (To_UTF8 (To_Wide_Wide_String + (Document.Line_Terminator + & "with " & Self.With_Clause & ";"))))); + + else + Safe_Insert + (Edits => Edits.Text_Edits, + File_Name => Node.Unit.Get_Filename, + Edit => + Text_Edit' + (Location => Make_Range (S, S), + Text => + Ada.Strings.Unbounded.To_Unbounded_String + (To_UTF8 (To_Wide_Wide_String + ("with " & Self.With_Clause & ";" + & Document.Line_Terminator))))); + end if; + + end if; + end; + end if; + + return Edits; end Command_To_Refactoring_Edits; -------------- @@ -235,15 +345,11 @@ package body LSP.Ada_Handlers.Refactor.Auto_Import is LSP.Types.Write_String (S, V.Context); JS.Key ("where"); LSP.Messages.TextDocumentPositionParams'Write (S, V.Where); - JS.Key ("import"); - LSP.Types.Write_String - (S, - VSS.Strings.Conversions.To_Virtual_String (V.Suggestion.Import)); - JS.Key ("qualifier"); - LSP.Types.Write_String - (S, - VSS.Strings.Conversions.To_Virtual_String (V.Suggestion.Qualifier)); + JS.Key ("with_clause"); + LSP.Types.Write_String (S, V.With_Clause); + JS.Key ("prefix"); + LSP.Types.Write_String (S, V.Prefix); JS.End_Object; end Write_Command; -end LSP.Ada_Handlers.Refactor.Auto_Import; +end LSP.Ada_Handlers.Refactor.Imports_Commands; diff --git a/source/ada/lsp-ada_handlers-refactor-auto_import.ads b/source/ada/lsp-ada_handlers-refactor-imports_commands.ads similarity index 84% rename from source/ada/lsp-ada_handlers-refactor-auto_import.ads rename to source/ada/lsp-ada_handlers-refactor-imports_commands.ads index 01e43baab..c9b2c61de 100644 --- a/source/ada/lsp-ada_handlers-refactor-auto_import.ads +++ b/source/ada/lsp-ada_handlers-refactor-imports_commands.ads @@ -1,7 +1,7 @@ ------------------------------------------------------------------------------ -- Language Server Protocol -- -- -- --- Copyright (C) 2020-2023, AdaCore -- +-- Copyright (C) 2020-2020, AdaCore -- -- -- -- This is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -22,18 +22,18 @@ with Ada.Streams; with VSS.Strings; with LAL_Refactor; -with LAL_Refactor.Auto_Import; +with LAL_Refactor.Refactor_Imports; with LSP.Client_Message_Receivers; with LSP.JSON_Streams; -package LSP.Ada_Handlers.Refactor.Auto_Import is +package LSP.Ada_Handlers.Refactor.Imports_Commands is type Command is new LSP.Ada_Handlers.Refactor.Command with private; overriding function Name (Self : Command) return String is - ("Auto Import"); + ("Imports Command"); procedure Initialize (Self : in out Command'Class; @@ -48,16 +48,17 @@ package LSP.Ada_Handlers.Refactor.Auto_Import is Context : Context_Access; Where : LSP.Messages.Location; Commands_Vector : in out LSP.Messages.CodeAction_Vector; - Suggestion : LAL_Refactor.Auto_Import.Import_Type); + Suggestion : LAL_Refactor.Refactor_Imports.Import_Suggestion); -- Initializes Command based on Suggestion and appends it to -- Commands_Vector. private type Command is new LSP.Ada_Handlers.Refactor.Command with record - Context : VSS.Strings.Virtual_String; - Where : LSP.Messages.TextDocumentPositionParams; - Suggestion : LAL_Refactor.Auto_Import.Import_Type; + Context : VSS.Strings.Virtual_String; + Where : LSP.Messages.TextDocumentPositionParams; + With_Clause : VSS.Strings.Virtual_String; + Prefix : VSS.Strings.Virtual_String; end record; overriding function Create @@ -86,6 +87,6 @@ private -- converted in a WorkspaceEdit. for Command'Write use Write_Command; - for Command'External_Tag use "als-auto-import"; + for Command'External_Tag use "als-refactor-imports"; -end LSP.Ada_Handlers.Refactor.Auto_Import; +end LSP.Ada_Handlers.Refactor.Imports_Commands; diff --git a/source/ada/lsp-ada_handlers.adb b/source/ada/lsp-ada_handlers.adb index cd8b8d6d6..8269c6ca7 100644 --- a/source/ada/lsp-ada_handlers.adb +++ b/source/ada/lsp-ada_handlers.adb @@ -63,7 +63,7 @@ with LSP.Ada_Handlers.Refactor.Change_Parameters_Default_Value; with LSP.Ada_Handlers.Refactor.Add_Parameter; with LSP.Ada_Handlers.Refactor.Introduce_Parameter; with LSP.Ada_Handlers.Refactor.Extract_Subprogram; -with LSP.Ada_Handlers.Refactor.Auto_Import; +with LSP.Ada_Handlers.Refactor.Imports_Commands; with LSP.Ada_Handlers.Refactor.Move_Parameter; with LSP.Ada_Handlers.Refactor.Remove_Parameter; with LSP.Ada_Handlers.Refactor.Suppress_Seperate; @@ -90,7 +90,7 @@ with Langkit_Support.Text; with Laltools.Common; with Laltools.Partial_GNATPP; -with LAL_Refactor.Auto_Import; +with LAL_Refactor.Refactor_Imports; with LAL_Refactor.Subprogram_Signature; with LAL_Refactor.Safe_Rename; with LAL_Refactor.Suppress_Separate; @@ -1366,7 +1366,6 @@ package body LSP.Ada_Handlers is procedure Analyse_Node (Context : Context_Access; - Document : LSP.Ada_Documents.Document_Access; Node : Libadalang.Analysis.Ada_Node; Result : out LSP.Messages.CodeAction_Vector; Found : in out Boolean); @@ -1512,11 +1511,10 @@ package body LSP.Ada_Handlers is ------------------ procedure Analyse_Node - (Context : Context_Access; - Document : LSP.Ada_Documents.Document_Access; - Node : Libadalang.Analysis.Ada_Node; - Result : out LSP.Messages.CodeAction_Vector; - Found : in out Boolean) + (Context : Context_Access; + Node : Libadalang.Analysis.Ada_Node; + Result : out LSP.Messages.CodeAction_Vector; + Found : in out Boolean) is procedure Change_Parameters_Type_Code_Action; -- Checks if the Change Parameters Type refactoring tool is avaiable, @@ -1726,7 +1724,7 @@ package body LSP.Ada_Handlers is procedure Import_Package_Code_Action is use Libadalang.Analysis; - use LAL_Refactor.Auto_Import; + use LAL_Refactor.Refactor_Imports; use LSP.Messages; Single_Location : constant Boolean := @@ -1736,55 +1734,133 @@ package body LSP.Ada_Handlers is Units_Array : constant Analysis_Unit_Array := Context.Analysis_Units; - Name : Libadalang.Analysis.Name := No_Name; - Import_Suggestions : Import_Type_Ordered_Set; + Import_Suggestions : Import_Suggestions_Vector.Vector; - begin - if not Single_Location then - return; - end if; + function Is_Import_Suggestions_Available + (This_Node : Ada_Node'Class) + return Boolean; + -- Checks if This_Node is a suitable node to get import + -- suggestions. A suitable node must be an identifier, non + -- defining and if it resolves, it must be to a declaration not + -- declared in the standard package. + -- This function also prepares Units_Vector with the right units + -- where suggestions should be searched for. - for Unit of Units_Array loop - Units_Vector.Append (Unit); - end loop; + ------------------------------------- + -- Is_Import_Suggestions_Available -- + ------------------------------------- - for F in Self.Project_Predefined_Sources.Iterate loop - declare - VF : GNATCOLL.VFS.Virtual_File renames - LSP.Ada_File_Sets.File_Sets.Element (F); - begin - Units_Vector.Append - (Context.LAL_Context.Get_From_File - (VF.Display_Full_Name, - -- ??? What is the charset for predefined - -- files? - "")); - end; - end loop; + function Is_Import_Suggestions_Available + (This_Node : Ada_Node'Class) + return Boolean + is + Aux_Node : Ada_Node := + (if This_Node.Is_Null then No_Ada_Node + else This_Node.As_Ada_Node); + Referenced_Definition : Defining_Name := No_Defining_Name; + + begin + -- Only get suggestions for Identifiers or Dotted_Names + if Aux_Node.Is_Null + or else Aux_Node.Kind not in + Ada_Identifier_Range | Ada_Dotted_Name_Range + then + return False; + end if; + + -- Get the full Dotted_Name if applicable + while not Aux_Node.Is_Null + and then not Aux_Node.Parent.Is_Null + and then Aux_Node.Parent.Kind in Ada_Dotted_Name_Range + loop + Aux_Node := Aux_Node.Parent; + end loop; - if not Is_Auto_Import_Available - (Node.Unit, - Document.Get_Source_Location (Params.span.first), - Units_Vector, - Name, - Import_Suggestions) + -- Defining names do not need prefixes + if Aux_Node.Is_Null or else Aux_Node.As_Name.P_Is_Defining then + return False; + end if; + + Referenced_Definition := + Aux_Node.As_Name.P_Referenced_Defining_Name; + + -- Declarations in the standard package do not need prefixes + if not Referenced_Definition.Is_Null then + if Referenced_Definition.Unit = Node.P_Standard_Unit then + return False; + end if; + end if; + + if Referenced_Definition.Is_Null then + -- The name could not be resolved so a full search needs to + -- be done. + + for U of Units_Array loop + Units_Vector.Append (U); + end loop; + + -- Add runtime analysis units for this context + -- ??? If possible, this should be cached. + + for F in Self.Project_Predefined_Sources.Iterate loop + declare + VF : GNATCOLL.VFS.Virtual_File renames + LSP.Ada_File_Sets.File_Sets.Element (F); + begin + Units_Vector.Append + (Context.LAL_Context.Get_From_File + (VF.Display_Full_Name, + -- ??? What is the charset for predefined + -- files? + "")); + end; + end loop; + + else + -- Libadalang sometimes can resolve names that are not + -- withed. + -- For instance, with Ada.Text_IO, resolve + -- Ada.Text_IO.Put_Line, remove the Ada.Text_IO and then + -- resolve again Ada.Text_IO.Put_Line. Even though + -- Ada.Text_IO is no longer withed, Libadalang is still + -- able to resolve Put_Line. + -- For such cases, include only Referenced_Definition's + -- Analysis_Units and the tool will suggest the prefixes + -- (there can be more than one, for instance, when there + -- are nested packages. + Units_Vector.Append (Referenced_Definition.Unit); + end if; + + return True; + exception + when others => return False; + end Is_Import_Suggestions_Available; + + begin + if not Single_Location + or else not Is_Import_Suggestions_Available (Node) then return; end if; + -- Get suggestions for all reachable declarations. + -- Each suggestion contains a with clause and a + -- prefix. + + Import_Suggestions := + Get_Import_Suggestions (Node, Units_Vector); + -- Create a new codeAction command for each suggestion for Suggestion of Import_Suggestions loop declare - Name_Location : constant LSP.Messages.Location := - LSP.Lal_Utils.Get_Node_Location (Name); - Command : - LSP.Ada_Handlers.Refactor.Auto_Import.Command; - + Command : LSP.Ada_Handlers. + Refactor.Imports_Commands.Command; begin Command.Append_Suggestion (Context => Context, - Where => Name_Location, + Where => + LSP.Lal_Utils.Get_Node_Location (Node), Commands_Vector => Result, Suggestion => Suggestion); end; @@ -2206,7 +2282,7 @@ package body LSP.Ada_Handlers is return; end if; - Analyse_Node (Context, Document, Node, Result, Found); + Analyse_Node (Context, Node, Result, Found); end Analyse_In_Context; ---------------------------------------- diff --git a/testsuite/ada_lsp/refactoring_imports_commands/U511-009.refactoring.import_with_use/test.json b/testsuite/ada_lsp/refactoring_imports_commands/U511-009.refactoring.import_with_use/test.json index df21589ac..e96afe68b 100644 --- a/testsuite/ada_lsp/refactoring_imports_commands/U511-009.refactoring.import_with_use/test.json +++ b/testsuite/ada_lsp/refactoring_imports_commands/U511-009.refactoring.import_with_use/test.json @@ -1,12 +1,16 @@ [ { - "comment": ["Test codeaction import refactor"] - }, + "comment": [ + "Test codeaction import refactor" + ] + }, { "start": { - "cmd": ["${ALS}"] + "cmd": [ + "${ALS}" + ] } - }, + }, { "send": { "request": { @@ -23,8 +27,7 @@ "id": 1, "method": "initialize" }, - "wait": [ - { + "wait": [{ "id": 1, "result": { "capabilities": { @@ -32,12 +35,22 @@ "referencesProvider": true, "textDocumentSync": 2, "completionProvider": { - "triggerCharacters": [".", ",", "'", "("], + "triggerCharacters": [ + ".", + ",", + "'", + "(" + ], "resolveProvider": true }, "signatureHelpProvider": { - "triggerCharacters": [",", "("], - "retriggerCharacters": ["\b"] + "triggerCharacters": [ + ",", + "(" + ], + "retriggerCharacters": [ + "\b" + ] }, "definitionProvider": true } @@ -49,27 +62,27 @@ { "send": { "request": { - "jsonrpc": "2.0", + "jsonrpc": "2.0", "method": "initialized" - }, + }, "wait": [] } - }, + }, { "send": { "request": { "params": { "settings": { "ada": { - "projectFile": "$URI{default.gpr}", + "projectFile": "$URI{default.gpr}", "scenarioVariables": {}, "defaultCharset": "ISO-8859-1" } } - }, - "jsonrpc": "2.0", + }, + "jsonrpc": "2.0", "method": "workspace/didChangeConfiguration" - }, + }, "wait": [ { "jsonrpc": "2.0", @@ -81,7 +94,7 @@ } ] } - }, + }, { "send": { "request": { @@ -202,11 +215,11 @@ "id": "ada-9", "result": [ { - "title": "Qualify with Bar", - "kind": "quickfix", + "title": "Add 'with' clause for Bar and prefix the object with Bar.", + "kind": "refactor.rewrite", "command": { "title": "", - "command": "als-auto-import", + "command": "als-refactor-imports", "arguments": [ { "context": "Default", @@ -219,8 +232,8 @@ "character": 28 } }, - "import": "Bar", - "qualifier": "Bar" + "with_clause": "Bar", + "prefix": "Bar." } ] } @@ -253,7 +266,7 @@ "prefix": "Bar." } ], - "command": "als-auto-import" + "command": "als-refactor-imports" } }, "wait": [ @@ -268,14 +281,14 @@ "range": { "start": { "line": 1, - "character": 0 + "character": 34 }, "end": { "line": 1, - "character": 0 + "character": 34 } }, - "newText": "with ;\n" + "newText": "\nwith Bar;" }, { "range": { @@ -288,7 +301,7 @@ "character": 28 } }, - "newText": "" + "newText": "Bar." } ] } @@ -355,7 +368,8 @@ ] } }, - "wait": [] + "wait": [ + ] } }, { @@ -416,11 +430,11 @@ "id": "ada-17", "result": [ { - "title": "Qualify with Foo.Nested", - "kind": "quickfix", + "title": "Prefix the object with Nested.", + "kind": "refactor.rewrite", "command": { "title": "", - "command": "als-auto-import", + "command": "als-refactor-imports", "arguments": [ { "context": "Default", @@ -433,8 +447,8 @@ "character": 20 } }, - "import": "Foo", - "qualifier": "Foo.Nested" + "with_clause": "", + "prefix": "Nested." } ] } @@ -467,7 +481,7 @@ "prefix": "Nested." } ], - "command": "als-auto-import" + "command": "als-refactor-imports" } }, "wait": [ @@ -478,19 +492,6 @@ "edit": { "changes": { "$URI{foo.adb}": [ - { - "range": { - "start": { - "line": 1, - "character": 0 - }, - "end": { - "line": 1, - "character": 0 - } - }, - "newText": "with ;\n" - }, { "range": { "start": { @@ -502,7 +503,7 @@ "character": 20 } }, - "newText": "" + "newText": "Nested." } ] } diff --git a/testsuite/ada_lsp/refactoring_imports_commands/suggestions_with_nested_packages/refactor_imports_commands.json b/testsuite/ada_lsp/refactoring_imports_commands/suggestions_with_nested_packages/refactor_imports_commands.json index da734fd14..a5996e948 100644 --- a/testsuite/ada_lsp/refactoring_imports_commands/suggestions_with_nested_packages/refactor_imports_commands.json +++ b/testsuite/ada_lsp/refactoring_imports_commands/suggestions_with_nested_packages/refactor_imports_commands.json @@ -1,346 +1,357 @@ [ - { - "comment": [ - "This test checks if imports (with clause + prefix) are being suggested to unresolved names." - ] - }, - { - "start": { - "cmd": ["${ALS}"] - } - }, - { - "send": { - "request": { - "params": { - "processId": 13950, - "capabilities": { - "workspace": { - "workspaceEdit": { - "documentChanges": true - }, - "applyEdit": false - } - }, - "rootUri": "$URI{.}" + { + "comment": [ + "This test checks if imports (with clause + prefix) are being suggested to unresolved names." + ] + }, + { + "start": { + "cmd": [ + "${ALS}" + ] + } + }, + { + "send": { + "request": { + "params": { + "processId": 13950, + "capabilities": { + "workspace": { + "workspaceEdit": { + "documentChanges": true + }, + "applyEdit": false + } + }, + "rootUri": "$URI{.}" + }, + "jsonrpc": "2.0", + "id": 1, + "method": "initialize" }, - "jsonrpc": "2.0", - "id": 1, - "method": "initialize" - }, - "wait": [ - { - "id": 1, - "result": { - "capabilities": { - "textDocumentSync": 2, - "completionProvider": { - "triggerCharacters": [".", ",", "'", "("], - "resolveProvider": true - }, - "hoverProvider": true, - "declarationProvider": true, - "definitionProvider": true, - "typeDefinitionProvider": true, - "implementationProvider": true, - "referencesProvider": true, - "codeActionProvider": {}, - "documentFormattingProvider": true, - "renameProvider": {}, - "foldingRangeProvider": true, - "executeCommandProvider": { - "commands": ["", "als-auto-import"] - }, - "workspaceSymbolProvider": true, - "alsShowDepsProvider": true, - "alsReferenceKinds": [ - "reference", - "access", - "write", - "call", - "dispatching call", - "parent", - "child", - "overriding" - ] - } - } - } - ] - } - }, - { - "send": { - "request": { - "jsonrpc": "2.0", - "method": "initialized" - }, - "wait": [] - } - }, - { - "send": { - "request": { - "params": { - "settings": { - "ada": { - "projectFile": "default.gpr", - "scenarioVariables": {}, - "defaultCharset": "ISO-8859-1" - } - } + "wait": [ + { + "id": 1, + "result": { + "capabilities": { + "textDocumentSync": 2, + "completionProvider": { + "triggerCharacters": [ + ".", + ",", + "'", + "(" + ], + "resolveProvider": true + }, + "hoverProvider": true, + "declarationProvider": true, + "definitionProvider": true, + "typeDefinitionProvider": true, + "implementationProvider": true, + "referencesProvider": true, + "codeActionProvider": {}, + "documentFormattingProvider": true, + "renameProvider": {}, + "foldingRangeProvider": true, + "executeCommandProvider": { + "commands": [ + "", + "als-refactor-imports" + ] + }, + "workspaceSymbolProvider": true, + "alsShowDepsProvider": true, + "alsReferenceKinds": [ + "reference", + "access", + "write", + "call", + "dispatching call", + "parent", + "child", + "overriding" + ] + } + } + } + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "method": "initialized" }, - "jsonrpc": "2.0", - "method": "workspace/didChangeConfiguration" - }, - "wait": [ - { - "jsonrpc": "2.0", - "id": 2, - "method": "window/workDoneProgress/create", - "params": { - "token": "" - } + "wait": [] + } + }, + { + "send": { + "request": { + "params": { + "settings": { + "ada": { + "projectFile": "default.gpr", + "scenarioVariables": {}, + "defaultCharset": "ISO-8859-1" + } + } + }, + "jsonrpc": "2.0", + "method": "workspace/didChangeConfiguration" }, - { - "jsonrpc": "2.0", - "method": "$/progress", - "params": { - "token": "", - "value": { - "kind": "end" - } - } - } - ] - } - }, - { - "send": { - "request": { - "params": { - "textDocument": { - "text": "procedure Main is\nbegin\n Subprogram;\nend Main;", - "version": 0, - "uri": "$URI{main.adb}", - "languageId": "Ada" - } + "wait": [ + { + "jsonrpc": "2.0", + "id": 2, + "method": "window/workDoneProgress/create", + "params": { + "token": "" + } + }, + { + "jsonrpc": "2.0", + "method": "$/progress", + "params": { + "token": "", + "value": { + "kind": "end" + } + } + } + ] + } + }, + { + "send": { + "request": { + "params": { + "textDocument": { + "text": "procedure Main is\nbegin\n Subprogram;\nend Main;", + "version": 0, + "uri": "$URI{main.adb}", + "languageId": "Ada" + } + }, + "jsonrpc": "2.0", + "method": "textDocument/didOpen" }, - "jsonrpc": "2.0", - "method": "textDocument/didOpen" - }, - "wait": [] - } - }, - { - "send": { - "request": { - "jsonrpc": "2.0", - "id": 2, - "method": "textDocument/codeAction", - "params": { - "textDocument": { - "uri": "$URI{main.adb}" - }, - "range": { - "start": { - "line": 2, - "character": 3 - }, - "end": { - "line": 2, - "character": 3 - } - }, - "context": { - "diagnostics": [] - } - } - }, - "wait": [ - { - "jsonrpc": "2.0", - "id": 2, - "result": [ - { - "title": "Qualify with A", - "kind": "quickfix", - "command": { - "title": "", - "command": "als-auto-import", - "arguments": [ - { - "context": "Default", - "where": { - "textDocument": { - "uri": "$URI{main.adb}" - }, - "position": { - "line": 2, - "character": 3 - } - }, - "import": "A", - "qualifier": "A" - } - ] - } - }, - { - "title": "Qualify with A.B", - "kind": "quickfix", - "command": { - "title": "", - "command": "als-auto-import", - "arguments": [ - { - "context": "Default", - "where": { - "textDocument": { - "uri": "$URI{main.adb}" - }, - "position": { - "line": 2, - "character": 3 - } - }, - "import": "A", - "qualifier": "A.B" - } - ] - } - }, - { - "title": "Qualify with E", - "kind": "quickfix", - "command": { - "title": "", - "command": "als-auto-import", - "arguments": [ - { - "context": "Default", - "where": { - "textDocument": { - "uri": "$URI{main.adb}" - }, - "position": { - "line": 2, - "character": 3 - } - }, - "import": "E", - "qualifier": "E" - } - ] - } - }, - { - "title": "Qualify with E.B", - "kind": "quickfix", - "command": { - "title": "", - "command": "als-auto-import", - "arguments": [ - { - "context": "Default", - "where": { - "textDocument": { - "uri": "$URI{main.adb}" - }, - "position": { - "line": 2, - "character": 3 - } - }, - "import": "E", - "qualifier": "E.B" - } - ] - } - } - ] - } - ] - } - }, - { - "send": { - "request": { - "jsonrpc": "2.0", - "id": 3, - "method": "workspace/executeCommand", - "params": { - "command": "als-auto-import", - "arguments": [ - { - "context": "Default", - "where": { - "textDocument": { - "uri": "$URI{main.adb}" + "wait": [ + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "id": 2, + "method": "textDocument/codeAction", + "params": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "range": { + "start": { + "line": 2, + "character": 3 }, - "position": { - "line": 2, - "character": 3 + "end": { + "line": 2, + "character": 3 } - }, - "with_clause": "A", - "prefix": "A.B." - } - ] - } - }, - "wait": [ - { - "jsonrpc": "2.0", - "id": 3, - "method": "workspace/applyEdit", - "params": { - "edit": { - "documentChanges": [ + }, + "context": { + "diagnostics": [] + } + } + }, + "wait": [ + { + "jsonrpc": "2.0", + "id": 2, + "result": [ { - "textDocument": { - "uri": "$URI{main.adb}", - "version": 1 - }, - "edits": [ - { - "range": { - "start": { - "line": 0, - "character": 0 - }, - "end": { - "line": 0, - "character": 0 + "title": "Add 'with' clause for A and prefix the object with A.", + "kind": "refactor.rewrite", + "command": { + "title": "", + "command": "als-refactor-imports", + "arguments": [ + { + "context": "Default", + "where": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "position": { + "line": 2, + "character": 3 + } + }, + "with_clause": "A", + "prefix": "A." } - }, - "newText": "with ;\n" - }, - { - "range": { - "start": { - "line": 2, - "character": 3 - }, - "end": { - "line": 2, - "character": 3 + ] + } + }, + { + "title": "Add 'with' clause for A and prefix the object with A.B.", + "kind": "refactor.rewrite", + "command": { + "title": "", + "command": "als-refactor-imports", + "arguments": [ + { + "context": "Default", + "where": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "position": { + "line": 2, + "character": 3 + } + }, + "with_clause": "A", + "prefix": "A.B." + } + ] + } + }, + { + "title": "Add 'with' clause for E and prefix the object with E.", + "kind": "refactor.rewrite", + "command": { + "title": "", + "command": "als-refactor-imports", + "arguments": [ + { + "context": "Default", + "where": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "position": { + "line": 2, + "character": 3 + } + }, + "with_clause": "E", + "prefix": "E." + } + ] + } + }, + { + "title": "Add 'with' clause for E and prefix the object with E.B.", + "kind": "refactor.rewrite", + "command": { + "title": "", + "command": "als-refactor-imports", + "arguments": [ + { + "context": "Default", + "where": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "position": { + "line": 2, + "character": 3 + } + }, + "with_clause": "E", + "prefix": "E.B." } - }, - "newText": "" - } - ] + ] + } + } + ] + } + ] + } + }, + { + "send": { + "request": { + "jsonrpc": "2.0", + "id": 3, + "method": "workspace/executeCommand", + "params": { + "command": "als-refactor-imports", + "arguments": [ + { + "context": "Default", + "where": { + "textDocument": { + "uri": "$URI{main.adb}" + }, + "position": { + "line": 2, + "character": 3 + } + }, + "with_clause": "A", + "prefix": "A.B." + } + ] + } + }, + "wait": [ + { + "jsonrpc": "2.0", + "id": 3, + "method": "workspace/applyEdit", + "params": { + "edit": { + "documentChanges": [ + { + "textDocument": { + "uri": "$URI{main.adb}", + "version": 1 + }, + "edits": [ + { + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 0, + "character": 0 + } + }, + "newText": "with A;\n" + }, + { + "range": { + "start": { + "line": 2, + "character": 3 + }, + "end": { + "line": 2, + "character": 3 + } + }, + "newText": "A.B." + } + ] + } + ] } - ] - } - } - } - ] - } - }, - { - "stop": { - "exit_code": 0 - } - } + } + } + ] + } + }, + { + "stop": { + "exit_code": 0 + } + } ]