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

GenerateTypeCodeFixProvider.cs « GenerateType « MonoDevelop.CSharp.CodeFixes « CSharpBinding « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6039a17dc73d7a839af25e32b75f0dbfd4884d8f (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
// 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 System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis;
using MonoDevelop.CSharp.CodeFixes.GenerateConstructor;
using ICSharpCode.NRefactory6.CSharp.GenerateType;
using Microsoft.CodeAnalysis.CSharp;
using ICSharpCode.NRefactory6.CSharp;

namespace MonoDevelop.CSharp.CodeFixes.GenerateType
{
	[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.GenerateType), Shared]
	[ExtensionOrder(After = PredefinedCodeFixProviderNames.GenerateVariable)]
	internal class GenerateTypeCodeFixProvider : AbstractGenerateMemberCodeFixProvider
	{
		private const string CS0103 = "CS0103"; // error CS0103: The name 'Foo' does not exist in the current context
		private const string CS0117 = "CS0117"; // error CS0117: 'x' does not contain a definition for 'y'
		private const string CS0234 = "CS0234"; // error CS0234: The type or namespace name 'C' does not exist in the namespace 'N' (are you missing an assembly reference?)
		private const string CS0246 = "CS0246"; // error CS0246: The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
		private const string CS0305 = "CS0305"; // error CS0305: Using the generic type 'C<T1>' requires 1 type arguments
		private const string CS0308 = "CS0308"; // error CS0308: The non-generic type 'A' cannot be used with type arguments
		private const string CS0426 = "CS0426"; // error CS0426: The type name 'S' does not exist in the type 'Program'
		private const string CS0616 = "CS0616"; // error CS0616: 'x' is not an attribute class

		public override ImmutableArray<string> FixableDiagnosticIds
		{
			get { return ImmutableArray.Create(CS0103, CS0117, CS0234, CS0246, CS0305, CS0308, CS0426, CS0616); }
		}

		protected override bool IsCandidate(SyntaxNode node)
		{
			var qualified = node as QualifiedNameSyntax;
			if (qualified != null)
			{
				return true;
			}

			var simple = node as SimpleNameSyntax;
			if (simple != null)
			{
				return !simple.IsParentKind(SyntaxKind.QualifiedName);
			}

			var memberAccess = node as MemberAccessExpressionSyntax;
			if (memberAccess != null)
			{
				return true;
			}
			if (node.IsKind (SyntaxKind.ObjectCreationExpression))
				return true;
			return false;
		}

		protected override SyntaxNode GetTargetNode(SyntaxNode node)
		{
			if (node.IsKind (SyntaxKind.ObjectCreationExpression))
				node = ((ObjectCreationExpressionSyntax)node).Type;
			return ((ExpressionSyntax)node).GetRightmostName();
		}

		static readonly CSharpGenerateTypeService service = new CSharpGenerateTypeService ();

		protected override Task<IEnumerable<CodeAction>> GetCodeActionsAsync(Document document, SyntaxNode node, CancellationToken cancellationToken)
		{
			return service.GenerateTypeAsync(document, node, cancellationToken);
		}
	}
}