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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/HelperMethods.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
// HelperMethods.cs
 
3
//  
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 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 System.Collections.Generic;
 
29
using ICSharpCode.NRefactory.CSharp;
 
30
using Mono.TextEditor;
 
31
using System.Linq;
 
32
 
 
33
namespace MonoDevelop.Refactoring
 
34
{
 
35
        public static class HelperMethods
 
36
        {
 
37
                /*static Dictionary<string, string> TypeTable = new Dictionary<string, string> ();
 
38
                static HelperMethods ()
 
39
                {
 
40
                        TypeTable[DomReturnType.Void.FullName] = "void";
 
41
                        TypeTable[DomReturnType.String.FullName] = "string";
 
42
                        TypeTable[DomReturnType.Int32.FullName] = "int";
 
43
                        TypeTable[DomReturnType.UInt32.FullName] = "uint";
 
44
                        TypeTable[DomReturnType.Int64.FullName] = "long";
 
45
                        TypeTable[DomReturnType.UInt64.FullName] = "ulong";
 
46
                        TypeTable[DomReturnType.Object.FullName] = "object";
 
47
                        TypeTable[DomReturnType.Float.FullName] = "float";
 
48
                        TypeTable[DomReturnType.Double.FullName] = "double";
 
49
                        TypeTable[DomReturnType.Byte.FullName] = "byte";
 
50
                        TypeTable[DomReturnType.SByte.FullName] = "sbyte";
 
51
                        TypeTable[DomReturnType.Int16.FullName] = "short";
 
52
                        TypeTable[DomReturnType.UInt16.FullName] = "ushort";
 
53
                        TypeTable[DomReturnType.Decimal.FullName] = "decimal";
 
54
                        TypeTable[DomReturnType.Char.FullName] = "char";
 
55
                        TypeTable[DomReturnType.Bool.FullName] = "bool";
 
56
                }
 
57
                
 
58
                public static bool IsIdentifierPart (this char ch)
 
59
                {
 
60
                        return Char.IsLetterOrDigit (ch) || ch == '_';
 
61
                }
 
62
                
 
63
                public static DocumentLocation ToDocumentLocation (this TextLocation location, Document document)
 
64
                {
 
65
                        return new DocumentLocation (location.Line, location.Column);
 
66
                }
 
67
                
 
68
                public static AstType ConvertToTypeReference (this MonoDevelop.Projects.Dom.IReturnType returnType)
 
69
                {
 
70
                        string primitiveType;
 
71
                        if (TypeTable.TryGetValue (returnType.DecoratedFullName, out primitiveType))
 
72
                                return new PrimitiveType (primitiveType);
 
73
                        
 
74
                        AstType result = null;
 
75
                        if (!string.IsNullOrEmpty (returnType.Namespace))
 
76
                                result = new SimpleType (returnType.Namespace);
 
77
                        foreach (var part in returnType.Parts) {
 
78
                                if (result == null) {
 
79
                                        var st = new SimpleType (part.Name);
 
80
                                        foreach (var type in part.GenericArguments.Select (ga => ConvertToTypeReference (ga)))
 
81
                                                st.AddChild (type, SimpleType.Roles.TypeArgument);
 
82
                                        result = st;
 
83
                                } else {
 
84
                                        var mt = new ICSharpCode.NRefactory.CSharp.MemberType () {
 
85
                                                Target = result,
 
86
                                                MemberName = part.Name
 
87
                                        };
 
88
                                        foreach (var type in part.GenericArguments.Select (ga => ConvertToTypeReference (ga)))
 
89
                                                mt.AddChild (type, SimpleType.Roles.TypeArgument);
 
90
                                        result = mt;
 
91
                                }
 
92
                        }
 
93
                        
 
94
                
 
95
                        return result;
 
96
                }
 
97
                
 
98
                public static DomReturnType ConvertToReturnType (this AstType typeRef)
 
99
                {
 
100
                        if (typeRef == null)
 
101
                                return null;
 
102
                        DomReturnType result;
 
103
                        if (typeRef is SimpleType) {
 
104
                                var st = (SimpleType)typeRef;
 
105
                                result = new DomReturnType (st.Identifier);
 
106
                                foreach (var arg in st.TypeArguments){
 
107
                                        result.AddTypeParameter (ConvertToReturnType (arg));
 
108
                                }
 
109
                        } else if (typeRef is ICSharpCode.NRefactory.CSharp.MemberType) {
 
110
                                var mt = (ICSharpCode.NRefactory.CSharp.MemberType)typeRef;
 
111
                                result = ConvertToReturnType (mt.Target);
 
112
                                result.Parts.Add (new ReturnTypePart (mt.MemberName));
 
113
                                
 
114
                                foreach (var arg in mt.TypeArguments){
 
115
                                        result.AddTypeParameter (ConvertToReturnType (arg));
 
116
                                }
 
117
                        } else if (typeRef is ComposedType) {
 
118
                                var ct = (ComposedType)typeRef;
 
119
                                result = ConvertToReturnType (ct.BaseType);
 
120
                                result.PointerNestingLevel = ct.PointerRank;
 
121
                                result.IsNullable = ct.HasNullableSpecifier;
 
122
                                
 
123
                                int arraySpecifiers = ct.ArraySpecifiers.Count;
 
124
                                if (arraySpecifiers> 0) {
 
125
                                        result.ArrayDimensions = arraySpecifiers;
 
126
                                        int i = 0;
 
127
                                        foreach (var spec in ct.ArraySpecifiers) {
 
128
                                                result.SetDimension (i, spec.Dimensions);
 
129
                                                i++;
 
130
                                        }
 
131
                                }
 
132
                        } else if (typeRef is PrimitiveType) {
 
133
                                var pt = (PrimitiveType)typeRef;
 
134
                                result = new DomReturnType (pt.Keyword);
 
135
                        } else if (typeRef.IsNull) {
 
136
                                return null;
 
137
 
 
138
                        } else { 
 
139
                                throw new InvalidOperationException ("unknown AstType:" + typeRef);
 
140
                        }
 
141
                        
 
142
                        return result;
 
143
                }
 
144
                */
 
145
        }
 
146
}