|
| 1 | +------------------------------------------------------------------------------ |
| 2 | +-- Language Server Protocol -- |
| 3 | +-- -- |
| 4 | +-- Copyright (C) 2023, 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 VSS.Characters; |
| 19 | +with VSS.Strings.Character_Iterators; |
| 20 | +with VSS.String_Vectors; |
| 21 | + |
| 22 | +with Langkit_Support.Text; |
| 23 | +with Libadalang.Common; |
| 24 | + |
| 25 | +with GNATdoc.Comments.Helpers; |
| 26 | + |
| 27 | +with LSP.Lal_Utils; |
| 28 | + |
| 29 | +package body LSP.Ada_Documentation is |
| 30 | + |
| 31 | + use all type Libadalang.Common.Ada_Node_Kind_Type; |
| 32 | + use Libadalang.Analysis; |
| 33 | + use Libadalang.Common; |
| 34 | + |
| 35 | + function Get_Hover_Text_For_Node |
| 36 | + (Node : Libadalang.Analysis.Ada_Node'Class) |
| 37 | + return VSS.String_Vectors.Virtual_String_Vector; |
| 38 | + -- Return a pretty printed version of the node's text to be |
| 39 | + -- displayed on hover requests, removing unnecessary indentation |
| 40 | + -- whitespaces if needed and attaching extra information in some cases. |
| 41 | + |
| 42 | + procedure Append_To_Last_Line |
| 43 | + (Lines : in out VSS.String_Vectors.Virtual_String_Vector; |
| 44 | + Item : VSS.Characters.Virtual_Character); |
| 45 | + -- Append given Item to the last line of the vector. Append new line when |
| 46 | + -- vector is empty. |
| 47 | + |
| 48 | + Document_LSP_New_Line_Function : constant VSS.Strings.Line_Terminator := |
| 49 | + VSS.Strings.LF; |
| 50 | + -- Line terminator to be used to generate replies. It is fixed to LF now. |
| 51 | + |
| 52 | + ------------------------- |
| 53 | + -- Append_To_Last_Line -- |
| 54 | + ------------------------- |
| 55 | + |
| 56 | + procedure Append_To_Last_Line |
| 57 | + (Lines : in out VSS.String_Vectors.Virtual_String_Vector; |
| 58 | + Item : VSS.Characters.Virtual_Character) |
| 59 | + is |
| 60 | + Line : VSS.Strings.Virtual_String := |
| 61 | + (if Lines.Is_Empty |
| 62 | + then VSS.Strings.Empty_Virtual_String |
| 63 | + else Lines.Element (Lines.Length)); |
| 64 | + |
| 65 | + begin |
| 66 | + Line.Append (Item); |
| 67 | + |
| 68 | + if Lines.Is_Empty then |
| 69 | + Lines.Append (Line); |
| 70 | + |
| 71 | + else |
| 72 | + Lines.Replace (Lines.Length, Line); |
| 73 | + end if; |
| 74 | + end Append_To_Last_Line; |
| 75 | + |
| 76 | + ----------------------------- |
| 77 | + -- Get_Hover_Text_For_Node -- |
| 78 | + ----------------------------- |
| 79 | + |
| 80 | + function Get_Hover_Text_For_Node |
| 81 | + (Node : Libadalang.Analysis.Ada_Node'Class) |
| 82 | + return VSS.String_Vectors.Virtual_String_Vector |
| 83 | + is |
| 84 | + Result : VSS.String_Vectors.Virtual_String_Vector; |
| 85 | + |
| 86 | + function Is_Space |
| 87 | + (Char : VSS.Characters.Virtual_Character) return Boolean; |
| 88 | + -- Check if given character is a whitespace |
| 89 | + |
| 90 | + function Get_Indent |
| 91 | + (Line : VSS.Strings.Virtual_String) return Natural; |
| 92 | + -- Count number of starting spaces |
| 93 | + |
| 94 | + function Tail_From |
| 95 | + (Line : VSS.Strings.Virtual_String; |
| 96 | + Skip : Natural) return VSS.Strings.Virtual_String; |
| 97 | + -- Return slice of Line from given index to the end of Line |
| 98 | + |
| 99 | + procedure Get_Loop_Var_Hover_Text; |
| 100 | + -- Create the hover text for loop variable declarations |
| 101 | + |
| 102 | + procedure Get_Aspect_Hover_Text; |
| 103 | + -- Create the hover text for aspect statement |
| 104 | + |
| 105 | + ---------------- |
| 106 | + -- Get_Indent -- |
| 107 | + ---------------- |
| 108 | + |
| 109 | + function Get_Indent |
| 110 | + (Line : VSS.Strings.Virtual_String) return Natural |
| 111 | + is |
| 112 | + Result : Natural := 0; |
| 113 | + J : VSS.Strings.Character_Iterators.Character_Iterator := |
| 114 | + Line.Before_First_Character; |
| 115 | + |
| 116 | + begin |
| 117 | + while J.Forward and then Is_Space (J.Element) loop |
| 118 | + Result := Result + 1; |
| 119 | + end loop; |
| 120 | + |
| 121 | + return Result; |
| 122 | + end Get_Indent; |
| 123 | + |
| 124 | + ----------------------------- |
| 125 | + -- Get_Loop_Var_Hover_Text -- |
| 126 | + ----------------------------- |
| 127 | + |
| 128 | + procedure Get_Loop_Var_Hover_Text is |
| 129 | + Parent_Text : constant Langkit_Support.Text.Text_Type := |
| 130 | + Node.Parent.Text; |
| 131 | + |
| 132 | + begin |
| 133 | + Result.Append (LSP.Lal_Utils.To_Virtual_String (Parent_Text)); |
| 134 | + end Get_Loop_Var_Hover_Text; |
| 135 | + |
| 136 | + --------------------------- |
| 137 | + -- Get_Aspect_Hover_Text -- |
| 138 | + --------------------------- |
| 139 | + |
| 140 | + procedure Get_Aspect_Hover_Text is |
| 141 | + Text : constant VSS.Strings.Virtual_String := |
| 142 | + LSP.Lal_Utils.To_Virtual_String (Node.Text); |
| 143 | + |
| 144 | + Lines : constant VSS.String_Vectors.Virtual_String_Vector := |
| 145 | + Text.Split_Lines; |
| 146 | + |
| 147 | + -- Get the indentation for the first line |
| 148 | + Indentation : Integer := Get_Indent (Lines (1)); |
| 149 | + Idx : Integer; |
| 150 | + Line : VSS.Strings.Virtual_String; |
| 151 | + begin |
| 152 | + Line := Tail_From (Lines (1), Indentation); |
| 153 | + -- Force an indentation of 2 for the first line |
| 154 | + Line.Prepend (" "); |
| 155 | + Result.Append (Line); |
| 156 | + |
| 157 | + -- The next line should have one more indentation level |
| 158 | + Indentation := Indentation + 3; |
| 159 | + |
| 160 | + for J in 2 .. Lines.Length loop |
| 161 | + Line := Lines (J); |
| 162 | + Idx := Get_Indent (Line); |
| 163 | + |
| 164 | + if Indentation > Idx then |
| 165 | + -- Uncommon indentation: just print the line |
| 166 | + Result.Append (Line); |
| 167 | + else |
| 168 | + -- Remove the uneeded indentation |
| 169 | + Result.Append (Tail_From (Line, Indentation)); |
| 170 | + end if; |
| 171 | + end loop; |
| 172 | + end Get_Aspect_Hover_Text; |
| 173 | + |
| 174 | + -------------- |
| 175 | + -- Is_Space -- |
| 176 | + -------------- |
| 177 | + |
| 178 | + function Is_Space |
| 179 | + (Char : VSS.Characters.Virtual_Character) return Boolean is |
| 180 | + begin |
| 181 | + return VSS.Characters.Get_General_Category (Char) in |
| 182 | + VSS.Characters.Space_Separator; |
| 183 | + end Is_Space; |
| 184 | + |
| 185 | + --------------- |
| 186 | + -- Tail_From -- |
| 187 | + --------------- |
| 188 | + |
| 189 | + function Tail_From |
| 190 | + (Line : VSS.Strings.Virtual_String; |
| 191 | + Skip : Natural) return VSS.Strings.Virtual_String |
| 192 | + is |
| 193 | + From : VSS.Strings.Character_Iterators. |
| 194 | + Character_Iterator := Line.At_First_Character; |
| 195 | + |
| 196 | + To : constant VSS.Strings.Character_Iterators. |
| 197 | + Character_Iterator := Line.At_Last_Character; |
| 198 | + |
| 199 | + begin |
| 200 | + for J in 1 .. Skip loop |
| 201 | + exit when not From.Forward; |
| 202 | + end loop; |
| 203 | + |
| 204 | + return Line.Slice (From, To); |
| 205 | + end Tail_From; |
| 206 | + |
| 207 | + begin |
| 208 | + case Node.Kind is |
| 209 | + when Ada_For_Loop_Var_Decl => |
| 210 | + Get_Loop_Var_Hover_Text; |
| 211 | + |
| 212 | + when Ada_Aspect_Assoc => |
| 213 | + Get_Aspect_Hover_Text; |
| 214 | + |
| 215 | + when others => |
| 216 | + null; |
| 217 | + end case; |
| 218 | + |
| 219 | + return Result; |
| 220 | + end Get_Hover_Text_For_Node; |
| 221 | + |
| 222 | + ---------------------- |
| 223 | + -- Get_Tooltip_Text -- |
| 224 | + ---------------------- |
| 225 | + |
| 226 | + procedure Get_Tooltip_Text |
| 227 | + (BD : Libadalang.Analysis.Basic_Decl; |
| 228 | + Style : GNATdoc.Comments.Options.Documentation_Style; |
| 229 | + Declaration_Text : out VSS.Strings.Virtual_String; |
| 230 | + Qualifier_Text : out VSS.Strings.Virtual_String; |
| 231 | + Location_Text : out VSS.Strings.Virtual_String; |
| 232 | + Documentation_Text : out VSS.Strings.Virtual_String; |
| 233 | + Aspects_Text : out VSS.Strings.Virtual_String) |
| 234 | + is |
| 235 | + Options : constant |
| 236 | + GNATdoc.Comments.Options.Extractor_Options := |
| 237 | + (Style => Style, |
| 238 | + Pattern => <>, |
| 239 | + Fallback => True); |
| 240 | + Decl_Lines : VSS.String_Vectors.Virtual_String_Vector; |
| 241 | + Doc_Lines : VSS.String_Vectors.Virtual_String_Vector; |
| 242 | + Aspects_Lines : VSS.String_Vectors.Virtual_String_Vector; |
| 243 | + |
| 244 | + begin |
| 245 | + Qualifier_Text.Clear; |
| 246 | + |
| 247 | + -- Extract documentation with GNATdoc when supported. |
| 248 | + |
| 249 | + GNATdoc.Comments.Helpers.Get_Plain_Text_Documentation |
| 250 | + (Name => BD.P_Defining_Name, |
| 251 | + Options => Options, |
| 252 | + Code_Snippet => Decl_Lines, |
| 253 | + Documentation => Doc_Lines); |
| 254 | + |
| 255 | + Declaration_Text := Decl_Lines.Join_Lines (VSS.Strings.LF, False); |
| 256 | + Documentation_Text := Doc_Lines.Join_Lines (VSS.Strings.LF, False); |
| 257 | + |
| 258 | + -- If GNATdoc failed to compute the declaration text, use the old engine |
| 259 | + -- Only known case now is loop variable of the 'for' loop. |
| 260 | + |
| 261 | + if Declaration_Text.Is_Empty then |
| 262 | + Declaration_Text := |
| 263 | + Get_Hover_Text_For_Node (BD).Join_Lines |
| 264 | + (Document_LSP_New_Line_Function, False); |
| 265 | + end if; |
| 266 | + |
| 267 | + Location_Text := LSP.Lal_Utils.Node_Location_Image (BD); |
| 268 | + |
| 269 | + -- For subprograms, do additional analysis and construct qualifier. |
| 270 | + |
| 271 | + case BD.Kind is |
| 272 | + when Ada_Abstract_Subp_Decl => |
| 273 | + Qualifier_Text.Append ("abstract"); |
| 274 | + |
| 275 | + when Ada_Null_Subp_Decl => |
| 276 | + Qualifier_Text.Append ("null"); |
| 277 | + |
| 278 | + when others => |
| 279 | + null; |
| 280 | + end case; |
| 281 | + |
| 282 | + -- Extract aspects when declaration has them. |
| 283 | + |
| 284 | + declare |
| 285 | + Aspects : constant Libadalang.Analysis.Aspect_Spec := |
| 286 | + BD.F_Aspects; |
| 287 | + |
| 288 | + begin |
| 289 | + if not Aspects.Is_Null then |
| 290 | + for Aspect of Aspects.F_Aspect_Assocs loop |
| 291 | + if not Aspects_Lines.Is_Empty then |
| 292 | + -- need to add "," for the highlighting |
| 293 | + |
| 294 | + Append_To_Last_Line (Aspects_Lines, ','); |
| 295 | + end if; |
| 296 | + |
| 297 | + Aspects_Lines.Append (Get_Hover_Text_For_Node (Aspect)); |
| 298 | + end loop; |
| 299 | + end if; |
| 300 | + end; |
| 301 | + |
| 302 | + Aspects_Text := Aspects_Lines.Join_Lines (VSS.Strings.LF, False); |
| 303 | + end Get_Tooltip_Text; |
| 304 | + |
| 305 | +end LSP.Ada_Documentation; |
0 commit comments