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

CSharpIntroduceVariableService.Rewriter.cs « IntroduceVariable « MonoDevelop.CSharp.Features « CSharpBinding « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f72aaf5a12dd6d005d5745e5f0b678813d96f9e7 (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
// 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.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Simplification;

namespace ICSharpCode.NRefactory6.CSharp.Features.IntroduceVariable
{
	partial class CSharpIntroduceVariableService
	{
		private class Rewriter : CSharpSyntaxRewriter
		{
			private readonly SyntaxAnnotation _replacementAnnotation = new SyntaxAnnotation ();
			private readonly SyntaxNode _replacementNode;
			private readonly ISet<ExpressionSyntax> _matches;

			private Rewriter (SyntaxNode replacementNode, ISet<ExpressionSyntax> matches)
			{
				_replacementNode = replacementNode;
				_matches = matches;
			}

			public override SyntaxNode Visit (SyntaxNode node)
			{
				var expression = node as ExpressionSyntax;
				if (expression != null &&
					_matches.Contains (expression)) {
					return _replacementNode
						.WithLeadingTrivia (expression.GetLeadingTrivia ())
						.WithTrailingTrivia (expression.GetTrailingTrivia ())
						.WithAdditionalAnnotations (_replacementAnnotation);
				}

				return base.Visit (node);
			}

			public override SyntaxNode VisitParenthesizedExpression (ParenthesizedExpressionSyntax node)
			{
				var newNode = base.VisitParenthesizedExpression (node);
				if (node != newNode &&
					newNode.IsKind (SyntaxKind.ParenthesizedExpression)) {
					var parenthesizedExpression = (ParenthesizedExpressionSyntax)newNode;
					var innerExpression = parenthesizedExpression.OpenParenToken.GetNextToken ().Parent;
					if (innerExpression.HasAnnotation (_replacementAnnotation)) {
						return newNode.WithAdditionalAnnotations (Simplifier.Annotation);
					}
				}

				return newNode;
			}

			public static SyntaxNode Visit (SyntaxNode node, SyntaxNode replacementNode, ISet<ExpressionSyntax> matches)
			{
				return new Rewriter (replacementNode, matches).Visit ((SyntaxNode)node);
			}
		}
	}
}