32
32
using ICSharpCode.NRefactory;
34
34
using MonoDevelop.CSharp.Formatting;
35
using MonoDevelop.Projects.Policies;
36
using MonoDevelop.Ide.Gui.Content;
37
using MonoDevelop.Projects.Text;
38
using System.Reflection;
39
using System.Collections.Generic;
40
using MonoDevelop.Ide;
36
42
namespace MonoDevelop.CSharp.Refactoring
50
56
public string OutputNode (ProjectDom dom, INode node, string indent)
52
58
CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor ();
53
CSharpFormatter.SetFormatOptions (outputVisitor, dom != null && dom.Project != null ? dom.Project.Policies : null);
54
int col = CSharpFormatter.GetColumn (indent, 0, 4);
59
SetFormatOptions (outputVisitor, dom != null && dom.Project != null ? dom.Project.Policies : null);
60
int col = GetColumn (indent, 0, 4);
55
61
outputVisitor.OutputFormatter.IndentationLevel = System.Math.Max (0, col / 4);
56
62
node.AcceptVisitor (outputVisitor, null);
57
63
return outputVisitor.Text;
66
static void SetFormatOptions (CSharpOutputVisitor outputVisitor, PolicyContainer policyParent)
68
IEnumerable<string> types = DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType);
69
TextStylePolicy currentPolicy = policyParent != null ? policyParent.Get<TextStylePolicy> (types) : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<TextStylePolicy> (types);
70
CSharpFormattingPolicy codePolicy = policyParent != null ? policyParent.Get<CSharpFormattingPolicy> (types) : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<CSharpFormattingPolicy> (types);
72
outputVisitor.Options.IndentationChar = currentPolicy.TabsToSpaces ? ' ' : '\t';
73
outputVisitor.Options.TabSize = currentPolicy.TabWidth;
74
outputVisitor.Options.IndentSize = currentPolicy.TabWidth;
76
outputVisitor.Options.EolMarker = TextStylePolicy.GetEolMarker(currentPolicy.EolMarker);
77
Type optionType = outputVisitor.Options.GetType ();
79
foreach (var property in typeof (CSharpFormattingPolicy).GetProperties ()) {
80
PropertyInfo info = optionType.GetProperty (property.Name);
83
object val = property.GetValue (codePolicy, null);
85
if (info.PropertyType.IsEnum) {
86
cval = Enum.Parse (info.PropertyType, val.ToString ());
87
} else if (info.PropertyType == typeof(bool)) {
88
cval = Convert.ToBoolean (val);
90
cval = Convert.ChangeType (val, info.PropertyType);
92
info.SetValue (outputVisitor.Options, cval, null);
96
public static int GetColumn (string wrapper, int i, int tabSize)
100
for (; j < wrapper.Length && (wrapper[j] == ' ' || wrapper[j] == '\t'); j++) {
101
if (wrapper[j] == ' ') {
104
col = GetNextTabstop (col, tabSize);
109
static int GetNextTabstop (int currentColumn, int tabSize)
111
int result = currentColumn + tabSize;
112
return (result / tabSize) * tabSize;
60
115
public ICSharpCode.NRefactory.Parser.Errors LastErrors {