|
| 1 | +------------------------------------------------------------------------------ |
| 2 | +-- Language Server Protocol -- |
| 3 | +-- -- |
| 4 | +-- Copyright (C) 2024, AdaCore -- |
| 5 | +-- -- |
| 6 | +-- This is free software; you can redistribute it and/or modify it under -- |
| 7 | +-- terms of the GNU General Public License as published by the Free Soft- -- |
| 8 | +-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 9 | +-- sion. This software is distributed in the hope that it will be useful, -- |
| 10 | +-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- -- |
| 11 | +-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public -- |
| 12 | +-- License for more details. You should have received a copy of the GNU -- |
| 13 | +-- General Public License distributed with this software; see file -- |
| 14 | +-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy -- |
| 15 | +-- of the license. -- |
| 16 | +------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +with Libadalang.Analysis; |
| 19 | +with Libadalang.Common; |
| 20 | + |
| 21 | +with VSS.Strings; |
| 22 | + |
| 23 | +with LSP.Ada_Context_Sets; |
| 24 | +with LSP.Ada_Request_Jobs; |
| 25 | +with LSP.Client_Message_Receivers; |
| 26 | +with LSP.Server_Request_Jobs; |
| 27 | +with LSP.Server_Requests.Supertypes; |
| 28 | +with LSP.Structures; |
| 29 | +with LSP.Utils; |
| 30 | + |
| 31 | +package body LSP.Ada_Type_Hierarchy_Supertypes is |
| 32 | + |
| 33 | + type Ada_Type_Hierarchy_Supertype_Job |
| 34 | + (Parent : not null access constant Ada_Type_Hierarchy_Supertype_Handler) is |
| 35 | + limited new LSP.Ada_Request_Jobs.Ada_Request_Job |
| 36 | + (Priority => LSP.Server_Jobs.Low) |
| 37 | + with null record; |
| 38 | + |
| 39 | + overriding procedure Execute_Ada_Request |
| 40 | + (Self : in out Ada_Type_Hierarchy_Supertype_Job; |
| 41 | + Client : |
| 42 | + in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class; |
| 43 | + Status : out LSP.Server_Jobs.Execution_Status); |
| 44 | + -- Execute Supertypes request |
| 45 | + |
| 46 | + ------------------------- |
| 47 | + -- Execute_Ada_Request -- |
| 48 | + ------------------------- |
| 49 | + |
| 50 | + overriding procedure Execute_Ada_Request |
| 51 | + (Self : in out Ada_Type_Hierarchy_Supertype_Job; |
| 52 | + Client : |
| 53 | + in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class; |
| 54 | + Status : out LSP.Server_Jobs.Execution_Status) |
| 55 | + is |
| 56 | + use type LSP.Structures.A_Range; |
| 57 | + use type LSP.Structures.DocumentUri; |
| 58 | + |
| 59 | + Message : LSP.Server_Requests.Supertypes.Request |
| 60 | + renames LSP.Server_Requests.Supertypes.Request (Self.Message.all); |
| 61 | + |
| 62 | + Context : constant LSP.Ada_Context_Sets.Context_Access := |
| 63 | + Self.Parent.Context.Get_Best_Context (Message.Params.item.uri); |
| 64 | + |
| 65 | + Response : LSP.Structures.TypeHierarchyItem_Vector_Or_Null; |
| 66 | + |
| 67 | + Node : constant Libadalang.Analysis.Ada_Node := |
| 68 | + Self.Parent.Context.Get_Node_At |
| 69 | + (Context.all, |
| 70 | + (LSP.Structures.TextDocumentPositionParams' |
| 71 | + ((uri => Message.Params.item.uri), |
| 72 | + Message.Params.item.selectionRange.start))); |
| 73 | + -- We expect here an identifier in the type declaration |
| 74 | + |
| 75 | + Decl : constant Libadalang.Analysis.Basic_Decl := |
| 76 | + (if not Node.Is_Null |
| 77 | + and then Node.Kind in Libadalang.Common.Ada_Name |
| 78 | + and then Node.As_Name.P_Is_Defining |
| 79 | + then Node.As_Name.P_Enclosing_Defining_Name.P_Basic_Decl |
| 80 | + else Libadalang.Analysis.No_Basic_Decl); |
| 81 | + |
| 82 | + Loc : LSP.Structures.Location; |
| 83 | + Item : LSP.Structures.TypeHierarchyItem; |
| 84 | + Name : Libadalang.Analysis.Defining_Name; |
| 85 | + begin |
| 86 | + -- Iterate over all type completion parts and find parent types for each |
| 87 | + -- part. |
| 88 | + if not Decl.Is_Null then |
| 89 | + for Part of Decl.P_All_Parts |
| 90 | + when Part.Kind in Libadalang.Common.Ada_Base_Type_Decl |
| 91 | + loop |
| 92 | + for Tipe of Part.As_Base_Type_Decl.P_Base_Types (Part) loop |
| 93 | + Name := Tipe.P_Defining_Name.P_Canonical_Part; |
| 94 | + |
| 95 | + Loc := Self.Parent.Context.To_LSP_Location (Name.P_Basic_Decl); |
| 96 | + |
| 97 | + if not |
| 98 | + (for some X of Response => |
| 99 | + X.uri = Loc.uri and X.a_range = Loc.a_range) |
| 100 | + then |
| 101 | + Item := |
| 102 | + (name => VSS.Strings.To_Virtual_String |
| 103 | + (Name.Text), |
| 104 | + kind => LSP.Utils.Get_Decl_Kind |
| 105 | + (Name.P_Basic_Decl), |
| 106 | + tags => <>, |
| 107 | + detail => LSP.Utils.Node_Location_Image |
| 108 | + (Name), |
| 109 | + uri => Loc.uri, |
| 110 | + a_range => Loc.a_range, |
| 111 | + selectionRange => Self.Parent.Context.To_LSP_Location |
| 112 | + (Name).a_range, |
| 113 | + data => <>); |
| 114 | + |
| 115 | + Response.Append (Item); |
| 116 | + end if; |
| 117 | + end loop; |
| 118 | + end loop; |
| 119 | + end if; |
| 120 | + |
| 121 | + Client.On_Supertypes_Response (Message.Id, Response); |
| 122 | + Status := LSP.Server_Jobs.Done; |
| 123 | + end Execute_Ada_Request; |
| 124 | + |
| 125 | + ---------------- |
| 126 | + -- Create_Job -- |
| 127 | + ---------------- |
| 128 | + |
| 129 | + overriding function Create_Job |
| 130 | + (Self : Ada_Type_Hierarchy_Supertype_Handler; |
| 131 | + Message : LSP.Server_Messages.Server_Message_Access) |
| 132 | + return LSP.Server_Jobs.Server_Job_Access is |
| 133 | + begin |
| 134 | + return new Ada_Type_Hierarchy_Supertype_Job' |
| 135 | + (Parent => Self'Unchecked_Access, |
| 136 | + Request => LSP.Server_Request_Jobs.Request_Access (Message)); |
| 137 | + end Create_Job; |
| 138 | + |
| 139 | +end LSP.Ada_Type_Hierarchy_Supertypes; |
0 commit comments