~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormatter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
26
using System;
 
27
using ICSharpCode.NRefactory.Editor;
 
28
using System.Threading;
 
29
using System.Linq;
 
30
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
using System.Collections.Generic;
 
33
 
 
34
namespace 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="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)
 
96
                {
 
97
                        if (policy == null)
 
98
                                throw new ArgumentNullException("policy");
 
99
                        this.policy = policy;
 
100
                        this.options = options ?? TextEditorOptions.Default;
 
101
                }
 
102
 
 
103
                /// <summary>
 
104
                /// Format the specified document and gives back the formatted text as result.
 
105
                /// </summary>
 
106
                public string Format(IDocument document)
 
107
                {
 
108
                        return InternalFormat (new StringBuilderDocument (document.Text));
 
109
                }
 
110
 
 
111
                /// <summary>
 
112
                /// Format the specified text and gives back the formatted text as result.
 
113
                /// </summary>
 
114
                public string Format(string text)
 
115
                {
 
116
                        return InternalFormat (new StringBuilderDocument (text));
 
117
                }
 
118
 
 
119
                string InternalFormat(IDocument document)
 
120
                {
 
121
                        var syntaxTree = SyntaxTree.Parse (document, document.FileName);
 
122
                        var changes = AnalyzeFormatting(document, syntaxTree);
 
123
                        changes.ApplyChanges();
 
124
                        return document.Text;
 
125
                }
 
126
 
 
127
                /// <summary>
 
128
                /// Analyzes the formatting of a given document and syntax tree.
 
129
                /// </summary>
 
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))
 
134
                {
 
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);
 
142
                        return result;
 
143
                }
 
144
 
 
145
                /// <summary>
 
146
                /// Adds a region in the document that should be formatted.
 
147
                /// </summary>
 
148
                public void AddFormattingRegion (DomRegion region)
 
149
                {
 
150
                        formattingRegions.Add(region);
 
151
                        if (formattingRegions.Count == 1) {
 
152
                                lastFormattingLocation = region.End;
 
153
                        } else {
 
154
                                lastFormattingLocation = lastFormattingLocation < region.End ? region.End : lastFormattingLocation;
 
155
                        }
 
156
                }
 
157
 
 
158
        }
 
159
}
 
160