This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
CreatePropertyAction.cs
129 lines (115 loc) · 4.29 KB
/
CreatePropertyAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// CreateProperty.cs
//
// Author:
// Mike Krüger <mkrueger@novell.com>
//
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.PatternMatching;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[ContextAction("Create property", Description = "Creates a property for a undefined variable.")]
public class CreatePropertyAction : ICodeActionProvider
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
Expression identifier = context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression) as Expression;
if (identifier == null) {
yield break;
}
if (CreateFieldAction.IsInvocationTarget(identifier)) {
yield break;
}
var propertyName = GetPropertyName(identifier);
if (propertyName == null) {
yield break;
}
var statement = context.GetNode<Statement>();
if (statement == null) {
yield break;
}
if (!(context.Resolve(identifier).IsError)) {
yield break;
}
var guessedType = CreateFieldAction.GuessAstType(context, identifier);
if (guessedType == null) {
yield break;
}
var state = context.GetResolverStateBefore(identifier);
bool createInOtherType = false;
ResolveResult targetResolveResult = null;
if (identifier is MemberReferenceExpression) {
targetResolveResult = context.Resolve(((MemberReferenceExpression)identifier).Target);
createInOtherType = !state.CurrentTypeDefinition.Equals(targetResolveResult.Type.GetDefinition());
}
bool isStatic;
if (createInOtherType) {
isStatic = targetResolveResult is TypeResolveResult;
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface) {
yield break;
}
} else {
isStatic = identifier is IdentifierExpression && state.CurrentMember.IsStatic;
}
// var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
// if (service != null && !service.IsValidName(propertyName, AffectedEntity.Property, Modifiers.Private, isStatic)) {
// yield break;
// }
yield return new CodeAction(context.TranslateString("Create property"), script => {
var decl = new PropertyDeclaration() {
ReturnType = guessedType,
Name = propertyName,
Getter = new Accessor(),
Setter = new Accessor()
};
if (isStatic) {
decl.Modifiers |= Modifiers.Static;
}
if (createInOtherType) {
if (targetResolveResult.Type.Kind == TypeKind.Interface) {
decl.Modifiers = Modifiers.None;
} else {
decl.Modifiers |= Modifiers.Public;
}
script.InsertWithCursor(context.TranslateString("Create property"), decl, targetResolveResult.Type.GetDefinition());
return;
}
script.InsertWithCursor(context.TranslateString("Create property"), decl, Script.InsertPosition.Before);
});
}
static string GetPropertyName(Expression expr)
{
if (expr is IdentifierExpression) {
return ((IdentifierExpression)expr).Identifier;
}
if (expr is MemberReferenceExpression) {
return ((MemberReferenceExpression)expr).MemberName;
}
return null;
}
}
}