5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2011 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 ICSharpCode.NRefactory.PatternMatching;
30
using System.Threading;
31
using System.Collections.Generic;
33
namespace ICSharpCode.NRefactory.CSharp.Refactoring
35
[ContextAction("Generate getter", Description = "Generates a getter for a field.")]
36
public class GenerateGetterAction : ICodeActionProvider
38
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
40
var initializer = GetVariableInitializer(context);
41
if (initializer == null || !initializer.NameToken.Contains(context.Location.Line, context.Location.Column)) {
44
var type = initializer.Parent.Parent as TypeDeclaration;
48
foreach (var member in type.Members) {
49
if (member is PropertyDeclaration && ContainsGetter((PropertyDeclaration)member, initializer)) {
53
var field = initializer.Parent as FieldDeclaration;
58
yield return new CodeAction(context.TranslateString("Create getter"), script => {
59
script.InsertWithCursor(
60
context.TranslateString("Create getter"),
61
Script.InsertPosition.After,
62
GeneratePropertyDeclaration(context, field, initializer));
66
static PropertyDeclaration GeneratePropertyDeclaration (RefactoringContext context, FieldDeclaration field, VariableInitializer initializer)
68
var mod = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
69
if (field.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Static))
70
mod |= ICSharpCode.NRefactory.CSharp.Modifiers.Static;
72
return new PropertyDeclaration () {
74
Name = context.GetNameProposal (initializer.Name, false),
75
ReturnType = field.ReturnType.Clone (),
76
Getter = new Accessor () {
77
Body = new BlockStatement () {
78
new ReturnStatement (new IdentifierExpression (initializer.Name))
84
bool ContainsGetter (PropertyDeclaration property, VariableInitializer initializer)
86
if (property.Getter.IsNull || property.Getter.Body.Statements.Count () != 1)
88
var ret = property.Getter.Body.Statements.Single () as ReturnStatement;
91
return ret.Expression.IsMatch (new IdentifierExpression (initializer.Name)) ||
92
ret.Expression.IsMatch (new MemberReferenceExpression (new ThisReferenceExpression (), initializer.Name));
95
VariableInitializer GetVariableInitializer (RefactoringContext context)
97
return context.GetNode<VariableInitializer> ();