Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Formatter/CSharpFormatter.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 5.0 KB
Line 
1//
2// CSharpFormatter.cs
3//
4// Author:
5//       Mike KrÃŒger <mkrueger@xamarin.com>
6//
7// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to deal
11// in the Software without restriction, including without limitation the rights
12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13// copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25// THE SOFTWARE.
26using System;
27using ICSharpCode.NRefactory.Editor;
28using System.Threading;
29using System.Linq;
30using ICSharpCode.NRefactory.CSharp.Refactoring;
31using ICSharpCode.NRefactory.TypeSystem;
32using System.Collections.Generic;
33
34namespace ICSharpCode.NRefactory.CSharp
35{
36  public enum FormattingMode {
37    OnTheFly,
38    Intrusive
39  }
40
41  /// <summary>
42  /// The C# Formatter generates a set of text replace actions to format a region in a C# document.
43  /// </summary>
44  public class CSharpFormatter
45  {
46    readonly CSharpFormattingOptions policy;
47    readonly TextEditorOptions options;
48
49    /// <summary>
50    /// Gets the formatting policy the formatter uses.
51    /// </summary>
52    public CSharpFormattingOptions Policy {
53      get {
54        return policy;
55      }
56    }
57
58    /// <summary>
59    /// Gets the text editor options the formatter uses.
60    /// Note: If none was specified TextEditorOptions.Default gets used.
61    /// </summary>
62    public TextEditorOptions TextEditorOptions {
63      get {
64        return options;
65      }
66    }
67
68    List<DomRegion> formattingRegions = new List<DomRegion> ();
69    internal TextLocation lastFormattingLocation = new TextLocation(int.MaxValue, int.MaxValue);
70
71    /// <summary>
72    /// Gets the formatting regions. NOTE: Will get changed to IReadOnlyList.
73    /// </summary>
74    public IList<DomRegion> FormattingRegions {
75      get {
76        return formattingRegions;
77      }
78    }
79
80    /// <summary>
81    /// Gets or sets the formatting mode. For on the fly formatting a lightweight formatting mode
82    /// gives better results.
83    /// </summary>
84    public FormattingMode FormattingMode {
85      get;
86      set;
87    }
88
89    /// <summary>
90    /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormatter"/> class.
91    /// </summary>
92    /// <param name="policy">The formatting policy to use.</param>
93    /// <param name="options">The text editor options (optional). Default is: TextEditorOptions.Default</param>
94    public CSharpFormatter(CSharpFormattingOptions policy, TextEditorOptions options = null)
95    {
96      if (policy == null)
97        throw new ArgumentNullException("policy");
98      this.policy = policy;
99      this.options = options ?? TextEditorOptions.Default;
100    }
101
102    /// <summary>
103    /// Format the specified document and gives back the formatted text as result.
104    /// </summary>
105    public string Format(IDocument document)
106    {
107      return InternalFormat (new StringBuilderDocument (document.Text));
108    }
109
110    /// <summary>
111    /// Format the specified text and gives back the formatted text as result.
112    /// </summary>
113    public string Format(string text)
114    {
115      return InternalFormat (new StringBuilderDocument (text));
116    }
117
118    string InternalFormat(IDocument document)
119    {
120      var syntaxTree = SyntaxTree.Parse (document, document.FileName);
121      var changes = AnalyzeFormatting(document, syntaxTree);
122      changes.ApplyChanges();
123      return document.Text;
124    }
125
126    /// <summary>
127    /// Analyzes the formatting of a given document and syntax tree.
128    /// </summary>
129    /// <param name="document">Document.</param>
130    /// <param name="syntaxTree">Syntax tree.</param>
131    /// <param name="token">The cancellation token.</param>
132    public FormattingChanges AnalyzeFormatting(IDocument document, SyntaxTree syntaxTree, CancellationToken token = default (CancellationToken))
133    {
134      if (document == null)
135        throw new ArgumentNullException("document");
136      if (syntaxTree == null)
137        throw new ArgumentNullException("syntaxTree");
138      var result = new FormattingChanges(document);
139      var visitor = new FormattingVisitor(this, document, result, token);
140      syntaxTree.AcceptVisitor(visitor);
141      return result;
142    }
143
144    /// <summary>
145    /// Adds a region in the document that should be formatted.
146    /// </summary>
147    public void AddFormattingRegion (DomRegion region)
148    {
149      formattingRegions.Add(region);
150      if (formattingRegions.Count == 1) {
151        lastFormattingLocation = region.End;
152      } else {
153        lastFormattingLocation = lastFormattingLocation < region.End ? region.End : lastFormattingLocation;
154      }
155    }
156
157  }
158}
159
Note: See TracBrowser for help on using the repository browser.