1 // =================================
  2 // Copyright (c) 2024 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.IO;
  8 
  9 namespace System.Text
 10 {
 11     public class CodeFormatter : IOBase
 12     {
 13         public CodeFormatter(TextWriter& writer_) : 
 14             base()writer(writer_)indent(0)indentSize(4)atBeginningOfLine(true)line(1)start(false)startText()preserveSpace(false)pos(0)
 15         {
 16             if (writer.Error())
 17             {
 18                 SetErrorId(writer.GetErrorId());
 19             }
 20         }
 21         [nodiscard]
 22         public Result<bool> Write(const string& text)
 23         {
 24             if (Error())
 25             {
 26                 return Result<bool>(ErrorId(GetErrorId()));
 27             }
 28             if (atBeginningOfLine)
 29             {
 30                 if (indent != 0)
 31                 {
 32                     string s(' 'indentSize * indent);
 33                     auto result = writer.Write(s);
 34                     if (result.Error())
 35                     {
 36                         SetErrorId(result.GetErrorId());
 37                         return result;
 38                     }
 39                     pos = pos + indentSize * indent;
 40                     atBeginningOfLine = false;
 41                 }
 42             }
 43             auto result = writer.Write(text);
 44             if (result.Error())
 45             {
 46                 SetErrorId(result.GetErrorId());
 47                 return result;
 48             }
 49             pos = pos + text.Length();
 50             return Result<bool>(true);
 51         }
 52         [nodiscard]
 53         public Result<bool> Write(const char* text)
 54         {
 55             if (Error())
 56             {
 57                 return Result<bool>(ErrorId(GetErrorId()));
 58             }
 59             string s(text);
 60             return Write(s);
 61         }
 62         [nodiscard]
 63         public Result<bool> WriteLine(const char* text)
 64         {
 65             if (Error())
 66             {
 67                 return Result<bool>(ErrorId(GetErrorId()));
 68             }
 69             auto result = Write(text);
 70             return result.AndThen(WriteLine());
 71         }
 72         [nodiscard]
 73         public Result<bool> WriteLine(const string& text)
 74         {
 75             if (Error())
 76             {
 77                 return Result<bool>(ErrorId(GetErrorId()));
 78             }
 79             auto result = Write(text);
 80             return result.AndThen(WriteLine());
 81         }
 82         [nodiscard]
 83         public Result<bool> WriteLine()
 84         {
 85             if (Error())
 86             {
 87                 return Result<bool>(ErrorId(GetErrorId()));
 88             }
 89             auto result = writer.WriteLine();
 90             if (result.Error())
 91             {
 92                 SetErrorId(result.GetErrorId());
 93                 return result;
 94             }
 95             atBeginningOfLine = true;
 96             ++line;
 97             pos = 0;
 98             return Result<bool>(true);
 99         }
100         public inline int Indent() const
101         {
102             return indent;
103         }
104         public inline void IncIndent()
105         {
106             ++indent;
107         }
108         public inline void DecIndent()
109         {
110             --indent;
111         }
112         public inline int IndentSize() const
113         {
114             return indentSize;
115         }
116         public inline void SetIndentSize(int indentSize_)
117         {
118             indentSize = indentSize_;
119         }
120         public inline int CurrentIndent() const
121         {
122             return indent * indentSize;
123         }
124         public inline bool AtBeginningOfLine() const
125         {
126             return atBeginningOfLine;
127         }
128         public inline int Line() const
129         {
130             return line;
131         }
132         public inline void SetLine(int line_)
133         {
134             line = line_;
135         }
136         public inline bool Start() const
137         {
138             return start;
139         }
140         public inline void SetStart(bool start_)
141         {
142             start = start_;
143         }
144         public inline const string& StartText() const
145         {
146             return startText;
147         }
148         public void SetStartText(const string& startText_)
149         {
150             startText = startText_;
151         }
152         public inline TextWriter& Writer()
153         {
154             return writer;
155         }
156         public inline bool PreserveSpace() const
157         {
158             return preserveSpace;
159         }
160         public inline void SetPreserveSpace(bool preserveSpace_)
161         {
162             preserveSpace = preserveSpace_;
163         }
164         public inline long Pos() const
165         {
166             return pos;
167         }
168         private TextWriter& writer;
169         private int indent;
170         private int indentSize;
171         private bool atBeginningOfLine;
172         private int line;
173         private bool start;
174         private string startText;
175         private bool preserveSpace;
176         private long pos;
177     }
178 
179     public CodeFormatter& operator<<(CodeFormatter& formatterconst char* s)
180     {
181         if (formatter.Error()) return formatter;
182         auto result = formatter.Write(s);
183         return formatter;
184     }
185 
186     public CodeFormatter& operator<<(CodeFormatter& formatterconst string& s)
187     {
188         if (formatter.Error()) return formatter;
189         auto result = formatter.Write(s);
190         return formatter;
191     }
192 
193     public CodeFormatter& operator<<(CodeFormatter& formatterbool x)
194     {
195         if (formatter.Error()) return formatter;
196         string s = ToString(x);
197         auto result = formatter.Write(s);
198         return formatter;
199     }
200 
201     public CodeFormatter& operator<<(CodeFormatter& formattersbyte x)
202     {
203         if (formatter.Error()) return formatter;
204         string s = ToString(x);
205         auto result = formatter.Write(s);
206         return formatter;
207     }
208 
209     public CodeFormatter& operator<<(CodeFormatter& formatterbyte x)
210     {
211         if (formatter.Error()) return formatter;
212         string s = ToString(x);
213         auto result = formatter.Write(s);
214         return formatter;
215     }
216 
217     public CodeFormatter& operator<<(CodeFormatter& formattershort x)
218     {
219         if (formatter.Error()) return formatter;
220         string s = ToString(x);
221         auto result = formatter.Write(s);
222         return formatter;
223     }
224 
225     public CodeFormatter& operator<<(CodeFormatter& formatterushort x)
226     {
227         if (formatter.Error()) return formatter;
228         string s = ToString(x);
229         auto result = formatter.Write(s);
230         return formatter;
231     }
232 
233     public CodeFormatter& operator<<(CodeFormatter& formatterint x)
234     {
235         if (formatter.Error()) return formatter;
236         string s = ToString(x);
237         auto result = formatter.Write(s);
238         return formatter;
239     }
240 
241     public CodeFormatter& operator<<(CodeFormatter& formatteruint x)
242     {
243         if (formatter.Error()) return formatter;
244         string s = ToString(x);
245         auto result = formatter.Write(s);
246         return formatter;
247     }
248 
249     public CodeFormatter& operator<<(CodeFormatter& formatterlong x)
250     {
251         if (formatter.Error()) return formatter;
252         string s = ToString(x);
253         auto result = formatter.Write(s);
254         return formatter;
255     }
256 
257     public CodeFormatter& operator<<(CodeFormatter& formatterulong x)
258     {
259         if (formatter.Error()) return formatter;
260         string s = ToString(x);
261         auto result = formatter.Write(s);
262         return formatter;
263     }
264 
265     public CodeFormatter& operator<<(CodeFormatter& formatterfloat x)
266     {
267         if (formatter.Error()) return formatter;
268         string s = ToString(x);
269         auto result = formatter.Write(s);
270         return formatter;
271     }
272 
273     public CodeFormatter& operator<<(CodeFormatter& formatterdouble x)
274     {
275         if (formatter.Error()) return formatter;
276         string s = ToString(x);
277         auto result = formatter.Write(s);
278         return formatter;
279     }
280 
281     public CodeFormatter& operator<<(CodeFormatter& formatterchar c)
282     {
283         if (formatter.Error()) return formatter;
284         string s = ToString(c);
285         auto result = formatter.Write(s);
286         return formatter;
287     }
288 
289     public CodeFormatter& operator<<(CodeFormatter& formatterwchar c)
290     {
291         if (formatter.Error()) return formatter;
292         auto s = ToString(c);
293         if (s.Error())
294         {
295             formatter.SetErrorId(s.GetErrorId());
296             return formatter;
297         }
298         auto result = formatter.Write(s.Value());
299         return formatter;
300     }
301 
302     public CodeFormatter& operator<<(CodeFormatter& formatteruchar c)
303     {
304         if (formatter.Error()) return formatter;
305         auto s = ToString(c);
306         if (s.Error())
307         {
308             formatter.SetErrorId(s.GetErrorId());
309             return formatter;
310         }
311         auto result = formatter.Write(s.Value());
312         return formatter;
313     }
314 
315     public CodeFormatter& operator<<(CodeFormatter& formatterconst Endl&)
316     {
317         if (formatter.Error()) return formatter;
318         auto result = formatter.WriteLine();
319         return formatter;
320     }