~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Projects.Text/Formatter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// Formatter.cs
3
 
//  
4
 
// Author:
5
 
//       Lluis Sanchez Gual <lluis@novell.com>
6
 
// 
7
 
// Copyright (c) 2010 Novell, Inc (http://www.novell.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
 
 
27
 
using System;
28
 
using MonoDevelop.Projects.Policies;
29
 
using MonoDevelop.Projects.Dom;
30
 
using MonoDevelop.Projects.Dom.Parser;
31
 
 
32
 
namespace MonoDevelop.Projects.Text
33
 
{
34
 
        public sealed class Formatter
35
 
        {
36
 
                IPrettyPrinter prettyPrinter;
37
 
                IFormatter formatter;
38
 
                string mimeType;
39
 
                
40
 
                internal Formatter (string mimeType, IPrettyPrinter prettyPrinter, IFormatter formatter)
41
 
                {
42
 
                        this.mimeType = mimeType;
43
 
                        this.prettyPrinter = prettyPrinter;
44
 
                        this.formatter = formatter;
45
 
                }
46
 
                
47
 
                public string FormatText (PolicyContainer policyParent, string input)
48
 
                {
49
 
                        if (policyParent == null)
50
 
                                policyParent = PolicyService.DefaultPolicies;
51
 
                        if (prettyPrinter != null)
52
 
                                return prettyPrinter.FormatText (policyParent, mimeType, input);
53
 
                        return formatter.FormatText (policyParent, mimeType, input);
54
 
                }
55
 
                
56
 
                public string FormatText (PolicyContainer policyParent, string mimeType, string input, int fromOffest, int toOffset)
57
 
                {
58
 
                        if (policyParent == null)
59
 
                                policyParent = PolicyService.DefaultPolicies;
60
 
                        if (prettyPrinter != null)
61
 
                                return prettyPrinter.FormatText (policyParent, mimeType, input, fromOffest, toOffset);
62
 
                        return formatter.FormatText (policyParent, mimeType, input, fromOffest, toOffset);
63
 
                }
64
 
                
65
 
                public bool SupportsOnTheFlyFormatting {
66
 
                        get { return prettyPrinter != null && prettyPrinter.SupportsOnTheFlyFormatting; }
67
 
                }
68
 
                
69
 
                /// <summary>
70
 
                /// Formats a text document directly with insert/remove operations.
71
 
                /// </summary>
72
 
                /// <param name="textEditorData">
73
 
                /// A <see cref="System.Object"/> that must be from type Mono.TextEditorData.
74
 
                /// </param>
75
 
                /// <param name="dom">
76
 
                /// A <see cref="ProjectDom"/>
77
 
                /// </param>
78
 
                /// <param name="unit">
79
 
                /// A <see cref="ICompilationUnit"/>
80
 
                /// </param>
81
 
                /// <param name="caretLocation">
82
 
                /// A <see cref="DomLocation"/> that should be the end location to which the parsing should occur.
83
 
                /// </param>
84
 
                public void OnTheFlyFormat (object textEditorData, IType callingType, IMember callingMember, ProjectDom dom, ICompilationUnit unit, DomLocation endLocation)
85
 
                {
86
 
                        if (prettyPrinter == null || !prettyPrinter.SupportsOnTheFlyFormatting)
87
 
                                throw new InvalidOperationException ("On the fly formatting not supported");
88
 
                        prettyPrinter.OnTheFlyFormat (textEditorData, callingType, callingMember, dom, unit, endLocation);
89
 
                }
90
 
                
91
 
                public bool SupportsCorrectIndenting {
92
 
                        get { return prettyPrinter != null; }
93
 
                }
94
 
                
95
 
                public void CorrectIndenting (object textEditorData, int line)
96
 
                {
97
 
                        prettyPrinter.CorrectIndenting (textEditorData, line);
98
 
                }
99
 
        }
100
 
}
101