prettier_ada_25.0.0_a67a3337/testsuite/test_programs/src/document_formatter-main.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
--
--  Copyright (C) 2023, AdaCore
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Ada.Text_IO;
with Ada.Text_IO.Unbounded_IO;

with GNAT.Strings;

with GNATCOLL.VFS;
with GNATCOLL.Traces;

with Document_Formatter.Command_Line;

with Prettier_Ada.Documents;
with Prettier_Ada.Documents.Builders;
with Prettier_Ada.Documents.Json;

procedure Document_Formatter.Main is

   function Build_Format_Options
     return Prettier_Ada.Documents.Format_Options_Type;
   --  TODO: Description

   function Detect_End_Of_Line return Prettier_Ada.Documents.End_Of_Line_Kind;
   --  TODO: Description

   --------------------------
   -- Build_Format_Options --
   --------------------------

   function Build_Format_Options
     return Prettier_Ada.Documents.Format_Options_Type
   is
   begin
      return Options : Prettier_Ada.Documents.Format_Options_Type do
         Options.Width := Document_Formatter.Command_Line.Print_Width.Get;
         if Document_Formatter.Command_Line.Use_Tabs.Get then
            Options.Indentation.Kind := Prettier_Ada.Documents.Tabs;
         else
            Options.Indentation.Kind := Prettier_Ada.Documents.Spaces;
         end if;
         Options.Indentation.Width :=
           Document_Formatter.Command_Line.Indent_Width.Get;
         Options.Indentation.Offset.Spaces :=
           Document_Formatter.Command_Line.Indentation_Offset_Spaces.Get;
         Options.Indentation.Offset.Tabs :=
           Document_Formatter.Command_Line.Indentation_Offset_Tabs.Get;
         Options.End_Of_Line :=
           (case Document_Formatter.Command_Line.End_Of_Line.Get is
              when Document_Formatter.Command_Line.LF   =>
                Prettier_Ada.Documents.LF,
              when Document_Formatter.Command_Line.CR   =>
                Prettier_Ada.Documents.CR,
              when Document_Formatter.Command_Line.CRLF =>
                Prettier_Ada.Documents.CRLF,
              when Document_Formatter.Command_Line.AUTO =>
                Detect_End_Of_Line);
      end return;
   end Build_Format_Options;

   ------------------------
   -- Detect_End_Of_Line --
   ------------------------

   function Detect_End_Of_Line return Prettier_Ada.Documents.End_Of_Line_Kind
   is
   begin
      --  TODO: Implement this function
      return Prettier_Ada.Documents.LF;
   end Detect_End_Of_Line;

   use type GNAT.Strings.String_Access;
   use type GNATCOLL.VFS.Virtual_File;
   use type Prettier_Ada.Documents.Document_Type;

begin
   GNATCOLL.Traces.Parse_Config_File;

   if Document_Formatter.Command_Line.Parser.Parse then
      if Document_Formatter.Command_Line.Verbose.Get then
         Document_Formatter_Trace.Set_Active (True);
      end if;

      if Document_Formatter.Command_Line.Document.Get
           /= GNATCOLL.VFS.No_File
      then
         declare
            Document_Text : GNAT.Strings.String_Access :=
              GNATCOLL.VFS.Read_File
                (Document_Formatter.Command_Line.Document.Get);

         begin
            if Document_Text = null then
               --  TODO: Print error message to the user and exit with code
               --  /= 0.
               raise Program_Error;
            end if;

            declare
               Document : constant Prettier_Ada.Documents.Document_Type :=
                  Prettier_Ada.Documents.Json.Deserialize
                    (Document_Text.all);

            begin

               GNAT.Strings.Free (Document_Text);
               if Document
                  = Prettier_Ada.Documents.Builders.No_Document
               then
                  --  TODO: Print error message to the user and exit with
                  --  code /= 0.
                  raise Program_Error;
               end if;
               Ada.Text_IO.Unbounded_IO.Put_Line
                 (Prettier_Ada.Documents.Format
                    (Document, Build_Format_Options));
            end;
         end;
      end if;
   end if;

end Document_Formatter.Main;