From 69f88696b623487944eb4fc5fefd6144f8dadbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Azevedo?= Date: Wed, 6 Sep 2023 09:27:22 +0000 Subject: [PATCH] Revert "Merge branch 'topic/lalrefactor_issue8' into 'master'" This reverts merge request !138 --- src/laltools-common.adb | 51 ++++++++++++++++++++++++++++++++++++++++- src/laltools-common.ads | 11 ++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/laltools-common.adb b/src/laltools-common.adb index ad345b68..32d8fc43 100644 --- a/src/laltools-common.adb +++ b/src/laltools-common.adb @@ -2,7 +2,7 @@ -- -- -- Libadalang Tools -- -- -- --- Copyright (C) 2021-2023, AdaCore -- +-- Copyright (C) 2021-2022, AdaCore -- -- -- -- Libadalang Tools is free software; you can redistribute it and/or modi- -- -- fy it under terms of the GNU General Public License as published by -- @@ -1520,6 +1520,55 @@ package body Laltools.Common is end return; end Get_Compilation_Units; + ------------------------------ + -- Get_Insert_With_Location -- + ------------------------------ + + function Get_Insert_With_Location + (Node : Compilation_Unit'Class; + Pack_Name : Text_Type; + Last : out Boolean) + return Source_Location + is + -- Cover the no with clause case + Res : Source_Location := Start_Sloc (Node.Sloc_Range); + Searching_Insert_Loc : Boolean := True; + begin + Last := False; + for N of Node.F_Prelude loop + if N.Kind in Ada_With_Clause_Range then + -- Handle list of packages: "with A, B, C;" + for P of N.As_With_Clause.F_Packages loop + if Pack_Name = P.Text then + -- We are already withed + return No_Source_Location; + elsif Searching_Insert_Loc + and then Pack_Name < P.Text + then + -- Assuming the with clauses are sorted alphabetically, + -- the insert location is before the first clause higher + -- than us. (Attention we must insert before N and not P) + Last := False; + Res := Start_Sloc (N.Sloc_Range); + Searching_Insert_Loc := False; + end if; + end loop; + end if; + + if Searching_Insert_Loc + and then N.Kind in Ada_With_Clause_Range | Ada_Use_Package_Clause + then + -- If the highest alphabetically, insert it after the last + -- with clause. To not split a pair also keep track of the last + -- use clause. + Last := True; + Res := End_Sloc (N.Sloc_Range); + end if; + end loop; + + return Res; + end Get_Insert_With_Location; + --------------------------- -- Find_Matching_Parents -- --------------------------- diff --git a/src/laltools-common.ads b/src/laltools-common.ads index c29cea10..68735f3f 100644 --- a/src/laltools-common.ads +++ b/src/laltools-common.ads @@ -2,7 +2,7 @@ -- -- -- Libadalang Tools -- -- -- --- Copyright (C) 2021-2023, AdaCore -- +-- Copyright (C) 2021-2022, AdaCore -- -- -- -- Libadalang Tools is free software; you can redistribute it and/or modi- -- -- fy it under terms of the GNU General Public License as published by -- @@ -494,6 +494,15 @@ package Laltools.Common is return Compilation_Unit_Vector; -- Returns a vector with all Compilation_Unit nodes of Analysis_Unit + function Get_Insert_With_Location + (Node : Compilation_Unit'Class; + Pack_Name : Text_Type; + Last : out Boolean) + return Source_Location; + -- Returns the Source_Location where to insert a with clause for Pack_Name + -- or No_Source_Location if Pack_Name is already withed. + -- Last indicates if the location is after the last with/use clause. + function Get_Decl_Block_Declarative_Part (Decl_B : Decl_Block) return Declarative_Part; -- Gets the Declarative_Part of a Decl_Block