Welcome to mirror list, hosted at ThFree Co, Russian Federation.

InlineTemporaryCodeRefactoringProvider.ReferenceRewriter.cs « InlineTemporary « MonoDevelop.CSharp.CodeRefactorings « CSharpBinding « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16d712e672f16ff713f4c987cf6c3574fffa3107 (plain)
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
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using ICSharpCode.NRefactory6.CSharp;
using Microsoft.CodeAnalysis.CSharp;

namespace MonoDevelop.CSharp.CodeRefactorings.InlineTemporary
{
	internal partial class InlineTemporaryCodeRefactoringProvider
	{
		private class ReferenceRewriter : CSharpSyntaxRewriter
		{
			private readonly SemanticModel _semanticModel;
			private readonly ILocalSymbol _localSymbol;
			private readonly VariableDeclaratorSyntax _variableDeclarator;
			private readonly ExpressionSyntax _expressionToInline;
			private readonly CancellationToken _cancellationToken;

			private ReferenceRewriter(
				SemanticModel semanticModel,
				VariableDeclaratorSyntax variableDeclarator,
				ExpressionSyntax expressionToInline,
				CancellationToken cancellationToken)
			{
				_semanticModel = semanticModel;
				_localSymbol = (ILocalSymbol)semanticModel.GetDeclaredSymbol(variableDeclarator, cancellationToken);
				_variableDeclarator = variableDeclarator;
				_expressionToInline = expressionToInline;
				_cancellationToken = cancellationToken;
			}

			private bool IsReference(SimpleNameSyntax name)
			{
				if (name.Identifier.ValueText != _variableDeclarator.Identifier.ValueText)
				{
					return false;
				}

				var symbol = _semanticModel.GetSymbolInfo(name).Symbol;
				return symbol != null
					&& symbol.Equals(_localSymbol);
			}

			public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
			{
				_cancellationToken.ThrowIfCancellationRequested();

				if (IsReference(node))
				{
					if (HasConflict(node, _variableDeclarator))
					{
						return node.Update(node.Identifier.WithAdditionalAnnotations(CreateConflictAnnotation()));
					}

					return _expressionToInline
						.Parenthesize()
						.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation);
				}

				return base.VisitIdentifierName(node);
			}

			public override SyntaxNode VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node)
			{
				var nameEquals = node.NameEquals;
				var expression = node.Expression;
				var identifier = expression as IdentifierNameSyntax;

				if (nameEquals != null || identifier == null || !IsReference(identifier) || HasConflict(identifier, _variableDeclarator))
				{
					return base.VisitAnonymousObjectMemberDeclarator(node);
				}

				// Special case inlining into anonymous types to ensure that we keep property names:
				//
				// E.g.
				//     int x = 42;
				//     var a = new { x; };
				//
				// Should become:
				//     var a = new { x = 42; };
				nameEquals = SyntaxFactory.NameEquals(identifier);
				expression = (ExpressionSyntax)this.Visit(expression);
				return node.Update(nameEquals, expression).WithAdditionalAnnotations(Simplifier.Annotation, Formatter.Annotation);
			}

			public static SyntaxNode Visit(
				SemanticModel semanticModel,
				SyntaxNode scope,
				VariableDeclaratorSyntax variableDeclarator,
				ExpressionSyntax expressionToInline,
				CancellationToken cancellationToken)
			{
				var rewriter = new ReferenceRewriter(semanticModel, variableDeclarator, expressionToInline, cancellationToken);
				return rewriter.Visit(scope);
			}
		}
	}
}