diff options
author | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
commit | 95408b1a41b31b645f3a8c3323ab7d3ca4bff26b (patch) | |
tree | f8f137cddcb9a40499d303be95722fd2c6081bce /main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeFixes/GenerateVariable/GenerateVariableCodeFixProvider.cs | |
parent | b70e4e04b4e41b8ec28d7e4b220b401ecf467e7a (diff) | |
parent | de043d330a8bf5855d4d983c804cd36727cb2406 (diff) |
Merge remote-tracking branch 'origin/roslyn' into new-project-model
Diffstat (limited to 'main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeFixes/GenerateVariable/GenerateVariableCodeFixProvider.cs')
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeFixes/GenerateVariable/GenerateVariableCodeFixProvider.cs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeFixes/GenerateVariable/GenerateVariableCodeFixProvider.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeFixes/GenerateVariable/GenerateVariableCodeFixProvider.cs new file mode 100644 index 0000000000..11e589fc05 --- /dev/null +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeFixes/GenerateVariable/GenerateVariableCodeFixProvider.cs @@ -0,0 +1,59 @@ +// 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.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CodeActions; +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.GenerateMember.GenerateVariable; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis; +using MonoDevelop.CSharp.CodeFixes.GenerateConstructor; + +namespace MonoDevelop.CSharp.CodeFixes.GenerateVariable +{ + [ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.GenerateVariable), Shared] + [ExtensionOrder(After = PredefinedCodeFixProviderNames.GenerateMethod)] + internal class GenerateVariableCodeFixProvider : AbstractGenerateMemberCodeFixProvider + { + private const string CS1061 = "CS1061"; // error CS1061: 'C' does not contain a definition for 'Foo' and no extension method 'Foo' accepting a first argument of type 'C' could be found + private const string CS0103 = "CS0103"; // error CS0103: The name 'Foo' does not exist in the current context + private const string CS0117 = "CS0117"; // error CS0117: 'TestNs.Program' does not contain a definition for 'blah' + private const string CS0539 = "CS0539"; // error CS0539: 'Class.SomeProp' in explicit interface declaration is not a member of interface + private const string CS0246 = "CS0246"; // error CS0246: The type or namespace name 'Version' could not be found + + public override ImmutableArray<string> FixableDiagnosticIds + { + get { return ImmutableArray.Create(CS1061, CS0103, CS0117, CS0539, CS0246); } + } + + protected override bool IsCandidate(SyntaxNode node) + { + return node is SimpleNameSyntax || node is PropertyDeclarationSyntax || node is MemberBindingExpressionSyntax; + } + + protected override SyntaxNode GetTargetNode(SyntaxNode node) + { + if (node.IsKind(SyntaxKind.MemberBindingExpression)) + { + var nameNode = node.ChildNodes().FirstOrDefault(n => n.IsKind(SyntaxKind.IdentifierName)); + if (nameNode != null) + { + return nameNode; + } + } + + return base.GetTargetNode(node); + } + static readonly CSharpGenerateVariableService service = new CSharpGenerateVariableService (); + + protected override Task<IEnumerable<CodeAction>> GetCodeActionsAsync(Document document, SyntaxNode node, CancellationToken cancellationToken) + { + return service.GenerateVariableAsync(document, node, cancellationToken); + } + } +} |