|
| 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; |
| 19 | +with Libadalang.Analysis; |
| 20 | +with Libadalang.Common; |
| 21 | + |
| 22 | +with VSS.Strings; |
| 23 | + |
| 24 | +with LSP.Ada_Context_Sets; |
| 25 | +with LSP.Ada_Request_Jobs; |
| 26 | +with LSP.Client_Message_Receivers; |
| 27 | +with LSP.Server_Request_Jobs; |
| 28 | +with LSP.Server_Requests.PrepareTypeHierarchy; |
| 29 | +with LSP.Structures; |
| 30 | +with LSP.Utils; |
| 31 | + |
| 32 | +package body LSP.Ada_Prepare_Type_Hierarchy is |
| 33 | + |
| 34 | + type Ada_Prepare_Type_Hierarchy_Job |
| 35 | + (Parent : not null access constant Ada_Prepare_Type_Hierarchy_Handler) is |
| 36 | + limited new LSP.Ada_Request_Jobs.Ada_Request_Job |
| 37 | + (Priority => LSP.Server_Jobs.Low) |
| 38 | + with null record; |
| 39 | + |
| 40 | + overriding procedure Execute_Ada_Request |
| 41 | + (Self : in out Ada_Prepare_Type_Hierarchy_Job; |
| 42 | + Client : |
| 43 | + in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class; |
| 44 | + Status : out LSP.Server_Jobs.Execution_Status); |
| 45 | + -- Execute PrepareTypeHierarchy request |
| 46 | + |
| 47 | + function Skip_Subtypes |
| 48 | + (Tipe : Libadalang.Analysis.Base_Type_Decl) |
| 49 | + return Libadalang.Analysis.Base_Type_Decl; |
| 50 | + |
| 51 | + ------------------------- |
| 52 | + -- Execute_Ada_Request -- |
| 53 | + ------------------------- |
| 54 | + |
| 55 | + overriding procedure Execute_Ada_Request |
| 56 | + (Self : in out Ada_Prepare_Type_Hierarchy_Job; |
| 57 | + Client : |
| 58 | + in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class; |
| 59 | + Status : out LSP.Server_Jobs.Execution_Status) |
| 60 | + is |
| 61 | + Message : LSP.Server_Requests.PrepareTypeHierarchy.Request |
| 62 | + renames LSP.Server_Requests.PrepareTypeHierarchy.Request |
| 63 | + (Self.Message.all); |
| 64 | + |
| 65 | + Context : constant LSP.Ada_Context_Sets.Context_Access := |
| 66 | + Self.Parent.Context.Get_Best_Context (Message.Params.textDocument.uri); |
| 67 | + |
| 68 | + Response : LSP.Structures.TypeHierarchyItem_Vector_Or_Null; |
| 69 | + |
| 70 | + Name : constant Libadalang.Analysis.Defining_Name := |
| 71 | + Self.Parent.Context.Imprecise_Resolve_Name |
| 72 | + (Context.all, Message.Params); |
| 73 | + -- Canonical defining name |
| 74 | + |
| 75 | + Type_Decl : constant Libadalang.Analysis.Base_Type_Decl := |
| 76 | + (if not Name.Is_Null |
| 77 | + and then not Name.P_Basic_Decl.Is_Null |
| 78 | + and then Name.P_Basic_Decl.Kind in |
| 79 | + Libadalang.Common.Ada_Base_Type_Decl |
| 80 | + then Skip_Subtypes (Name.P_Basic_Decl.As_Base_Type_Decl) |
| 81 | + else Libadalang.Analysis.No_Base_Type_Decl); |
| 82 | + |
| 83 | + Loc : LSP.Structures.Location; |
| 84 | + Decl : Libadalang.Analysis.Basic_Decl; |
| 85 | + Item : LSP.Structures.TypeHierarchyItem; |
| 86 | + begin |
| 87 | + if not Type_Decl.Is_Null then |
| 88 | + -- We have got a type, return its the very first declaration "part" |
| 89 | + Decl := Type_Decl.P_Canonical_Part; |
| 90 | + |
| 91 | + Loc := Self.Parent.Context.To_LSP_Location (Decl); |
| 92 | + |
| 93 | + Item := |
| 94 | + (name => VSS.Strings.To_Virtual_String |
| 95 | + (Decl.P_Defining_Name.Text), |
| 96 | + kind => LSP.Utils.Get_Decl_Kind (Decl), |
| 97 | + tags => <>, |
| 98 | + detail => LSP.Utils.Node_Location_Image |
| 99 | + (Decl.P_Defining_Name), |
| 100 | + uri => Loc.uri, |
| 101 | + a_range => Loc.a_range, |
| 102 | + selectionRange => Self.Parent.Context.To_LSP_Location |
| 103 | + (Decl.P_Defining_Name).a_range, |
| 104 | + data => <>); |
| 105 | + |
| 106 | + Response.Append (Item); |
| 107 | + end if; |
| 108 | + |
| 109 | + Client.On_PrepareTypeHierarchy_Response (Message.Id, Response); |
| 110 | + Status := LSP.Server_Jobs.Done; |
| 111 | + end Execute_Ada_Request; |
| 112 | + |
| 113 | + ---------------- |
| 114 | + -- Create_Job -- |
| 115 | + ---------------- |
| 116 | + |
| 117 | + overriding function Create_Job |
| 118 | + (Self : Ada_Prepare_Type_Hierarchy_Handler; |
| 119 | + Message : LSP.Server_Messages.Server_Message_Access) |
| 120 | + return LSP.Server_Jobs.Server_Job_Access is |
| 121 | + begin |
| 122 | + return new Ada_Prepare_Type_Hierarchy_Job' |
| 123 | + (Parent => Self'Unchecked_Access, |
| 124 | + Request => LSP.Server_Request_Jobs.Request_Access (Message)); |
| 125 | + end Create_Job; |
| 126 | + |
| 127 | + ------------------- |
| 128 | + -- Skip_Subtypes -- |
| 129 | + ------------------- |
| 130 | + |
| 131 | + function Skip_Subtypes |
| 132 | + (Tipe : Libadalang.Analysis.Base_Type_Decl) |
| 133 | + return Libadalang.Analysis.Base_Type_Decl |
| 134 | + is |
| 135 | + use type Libadalang.Analysis.Base_Type_Decl; |
| 136 | + |
| 137 | + Result : constant Libadalang.Analysis.Base_Type_Decl := |
| 138 | + Tipe.P_Base_Subtype; |
| 139 | + begin |
| 140 | + return (if Tipe = Result then Tipe else Skip_Subtypes (Result)); |
| 141 | + exception |
| 142 | + when others => |
| 143 | + return Tipe; |
| 144 | + end Skip_Subtypes; |
| 145 | + |
| 146 | +end LSP.Ada_Prepare_Type_Hierarchy; |
0 commit comments