5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.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
28
using MonoDevelop.Projects.Policies;
29
using MonoDevelop.Projects.Dom;
30
using MonoDevelop.Projects.Dom.Parser;
32
namespace MonoDevelop.Projects.Text
34
public sealed class Formatter
36
IPrettyPrinter prettyPrinter;
40
internal Formatter (string mimeType, IPrettyPrinter prettyPrinter, IFormatter formatter)
42
this.mimeType = mimeType;
43
this.prettyPrinter = prettyPrinter;
44
this.formatter = formatter;
47
public string FormatText (PolicyContainer policyParent, string input)
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);
56
public string FormatText (PolicyContainer policyParent, string mimeType, string input, int fromOffest, int toOffset)
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);
65
public bool SupportsOnTheFlyFormatting {
66
get { return prettyPrinter != null && prettyPrinter.SupportsOnTheFlyFormatting; }
70
/// Formats a text document directly with insert/remove operations.
72
/// <param name="textEditorData">
73
/// A <see cref="System.Object"/> that must be from type Mono.TextEditorData.
75
/// <param name="dom">
76
/// A <see cref="ProjectDom"/>
78
/// <param name="unit">
79
/// A <see cref="ICompilationUnit"/>
81
/// <param name="caretLocation">
82
/// A <see cref="DomLocation"/> that should be the end location to which the parsing should occur.
84
public void OnTheFlyFormat (object textEditorData, IType callingType, IMember callingMember, ProjectDom dom, ICompilationUnit unit, DomLocation endLocation)
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);
91
public bool SupportsCorrectIndenting {
92
get { return prettyPrinter != null; }
95
public void CorrectIndenting (object textEditorData, int line)
97
prettyPrinter.CorrectIndenting (textEditorData, line);