5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using ICSharpCode.NRefactory.Editor;
28
using System.Threading;
30
using ICSharpCode.NRefactory.CSharp.Refactoring;
31
using ICSharpCode.NRefactory.TypeSystem;
32
using System.Collections.Generic;
34
namespace ICSharpCode.NRefactory.CSharp
36
public enum FormattingMode {
42
/// The C# Formatter generates a set of text replace actions to format a region in a C# document.
44
public class CSharpFormatter
46
readonly CSharpFormattingOptions policy;
47
readonly TextEditorOptions options;
50
/// Gets the formatting policy the formatter uses.
52
public CSharpFormattingOptions Policy {
59
/// Gets the text editor options the formatter uses.
60
/// Note: If none was specified TextEditorOptions.Default gets used.
62
public TextEditorOptions TextEditorOptions {
68
List<DomRegion> formattingRegions = new List<DomRegion> ();
69
internal TextLocation lastFormattingLocation = new TextLocation(int.MaxValue, int.MaxValue);
72
/// Gets the formatting regions. NOTE: Will get changed to IReadOnlyList.
74
public IList<DomRegion> FormattingRegions {
76
return formattingRegions;
81
/// Gets or sets the formatting mode. For on the fly formatting a lightweight formatting mode
82
/// gives better results.
84
public FormattingMode FormattingMode {
90
/// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormatter"/> class.
92
/// <param name="policy">The formatting policy to use.</param>
93
/// <param name="document">The text document to work upon.</param>
94
/// <param name="options">The text editor options (optional). Default is: TextEditorOptions.Default</param>
95
public CSharpFormatter(CSharpFormattingOptions policy, TextEditorOptions options = null)
98
throw new ArgumentNullException("policy");
100
this.options = options ?? TextEditorOptions.Default;
104
/// Format the specified document and gives back the formatted text as result.
106
public string Format(IDocument document)
108
return InternalFormat (new StringBuilderDocument (document.Text));
112
/// Format the specified text and gives back the formatted text as result.
114
public string Format(string text)
116
return InternalFormat (new StringBuilderDocument (text));
119
string InternalFormat(IDocument document)
121
var syntaxTree = SyntaxTree.Parse (document, document.FileName);
122
var changes = AnalyzeFormatting(document, syntaxTree);
123
changes.ApplyChanges();
124
return document.Text;
128
/// Analyzes the formatting of a given document and syntax tree.
130
/// <param name="document">Document.</param>
131
/// <param name="syntaxTree">Syntax tree.</param>
132
/// <param name="token">The cancellation token.</param>
133
public FormattingChanges AnalyzeFormatting(IDocument document, SyntaxTree syntaxTree, CancellationToken token = default (CancellationToken))
135
if (document == null)
136
throw new ArgumentNullException("document");
137
if (syntaxTree == null)
138
throw new ArgumentNullException("syntaxTree");
139
var result = new FormattingChanges(document);
140
var visitor = new FormattingVisitor(this, document, result, token);
141
syntaxTree.AcceptVisitor(visitor);
146
/// Adds a region in the document that should be formatted.
148
public void AddFormattingRegion (DomRegion region)
150
formattingRegions.Add(region);
151
if (formattingRegions.Count == 1) {
152
lastFormattingLocation = region.End;
154
lastFormattingLocation = lastFormattingLocation < region.End ? region.End : lastFormattingLocation;