Browse Source

"Implement abstract class" editor context action.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6194 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Martin Koníček 15 years ago
parent
commit
47ee9924e7
  1. 1
      src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.csproj
  2. 33
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/DelegateAction.cs
  3. 19
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs
  4. 24
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs
  5. 30
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs
  6. 1
      src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/ContextActionsService.cs

1
src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.csproj

@ -71,6 +71,7 @@ @@ -71,6 +71,7 @@
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\ClassRefactoringSubmenuBuilder.cs" />
<Compile Include="Src\ContextActions\AddUsing.cs" />
<Compile Include="Src\ContextActions\DelegateAction.cs" />
<Compile Include="Src\ContextActions\Extensions.cs" />
<Compile Include="Src\ContextActions\FindNodesVisitor.cs" />
<Compile Include="Src\ContextActions\GenerateMember.cs" />

33
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/DelegateAction.cs

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Martin Konicek" email="martin.konicek@gmail.com"/>
// <version>$Revision: $</version>
// </file>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using ICSharpCode.NRefactory;
using Ast = ICSharpCode.NRefactory.Ast;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using ICSharpCode.SharpDevelop.Refactoring;
namespace SharpRefactoring.ContextActions
{
public class DelegateAction : IContextAction
{
public string Title { get; set; }
public System.Action ExecuteAction { get; set; }
public void Execute()
{
if (this.ExecuteAction != null)
this.ExecuteAction();
}
}
}

19
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs

@ -5,8 +5,13 @@ @@ -5,8 +5,13 @@
// <version>$Revision: $</version>
// </file>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Refactoring;
namespace SharpRefactoring.ContextActions
{
@ -21,5 +26,19 @@ namespace SharpRefactoring.ContextActions @@ -21,5 +26,19 @@ namespace SharpRefactoring.ContextActions
astTree.AcceptVisitor(findVisitor, null);
return findVisitor.Declarations;
}
public static IEnumerable<IClass> GetClassesOnCurrentLine(this EditorASTProvider editorAST)
{
var currentLineAST = editorAST.CurrentLineAST;
if (currentLineAST == null)
yield break;
var editor = editorAST.Editor;
foreach (var declaration in currentLineAST.FindTypeDeclarations()) {
var rr = ParserService.Resolve(new ExpressionResult(declaration.Name), editor.Caret.Line, editor.Caret.Column, editor.FileName, editor.Document.Text);
if (rr != null && rr.ResolvedType != null) {
yield return rr.ResolvedType.GetUnderlyingClass();
}
}
}
}
}

24
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs

@ -6,7 +6,10 @@ @@ -6,7 +6,10 @@
// </file>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using ICSharpCode.SharpDevelop.Refactoring;
@ -20,19 +23,18 @@ namespace SharpRefactoring.ContextActions @@ -20,19 +23,18 @@ namespace SharpRefactoring.ContextActions
{
public IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST)
{
yield break;
}
}
var ambience = AmbienceService.GetCurrentAmbience();
public class ImplementAbstractClassAction : IContextAction
{
public string Title {
get { return "Dummy implement abstract class"; }
}
foreach (var targetClass in editorAST.GetClassesOnCurrentLine().Where(c => c.ClassType == ClassType.Class)) {
public void Execute()
{
MessageBox.Show("Dummy implement abstract class");
foreach (var implementAction in RefactoringService.GetImplementAbstractClassActions(targetClass)) {
var implementActionCopy = implementAction;
yield return new DelegateAction {
Title = string.Format("Implement abstract class {0}", ambience.Convert(implementActionCopy.ClassToImplement)),
ExecuteAction = implementActionCopy.Execute
};
}
}
}
}
}

30
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using ICSharpCode.NRefactory;
using Ast = ICSharpCode.NRefactory.Ast;
@ -25,21 +26,14 @@ namespace SharpRefactoring.ContextActions @@ -25,21 +26,14 @@ namespace SharpRefactoring.ContextActions
{
public IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST)
{
yield break; // turned off temporarily
// Using CurrentLineAST is basically OK, but when the "class" keyword is on different line than class name,
// parsing only one line never tells us that we are looking at TypeDeclaration
// Alternative solution could be to try to resolve also IdentifierExpression to see if it is class declaration.
var currentLineAST = editorAST.CurrentLineAST;
if (currentLineAST == null)
yield break;
var editor = editorAST.Editor;
var ambience = AmbienceService.GetCurrentAmbience();
foreach (var declaration in currentLineAST.FindTypeDeclarations()) {
if (declaration.Type == Ast.ClassType.Class || declaration.Type == Ast.ClassType.Struct) {
var rr = ParserService.Resolve(new ExpressionResult(declaration.Name), editor.Caret.Line, editor.Caret.Column, editor.FileName, editor.Document.Text);
var targetClass = rr.ResolvedType == null ? null : rr.ResolvedType.GetUnderlyingClass();
if (targetClass != null) {
foreach (var targetClass in editorAST.GetClassesOnCurrentLine().
Where(c => c.ClassType == ClassType.Class || c.ClassType == ClassType.Interface)) {
foreach (var implementAction in RefactoringService.GetImplementInterfaceActions(targetClass, false)) {
var implementActionCopy = implementAction;
yield return new DelegateAction {
@ -50,18 +44,4 @@ namespace SharpRefactoring.ContextActions @@ -50,18 +44,4 @@ namespace SharpRefactoring.ContextActions
}
}
}
}
}
public class DelegateAction : IContextAction
{
public string Title { get; set; }
public System.Action ExecuteAction { get; set; }
public void Execute()
{
if (this.ExecuteAction != null)
this.ExecuteAction();
}
}
}

1
src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/ContextActionsService.cs

@ -39,6 +39,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -39,6 +39,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring
/// </summary>
public IEnumerable<IContextAction> GetAvailableActions(ITextEditor editor)
{
yield break;
var editorAST = new EditorASTProvider(editor);
// could run providers in parallel
foreach (var provider in this.providers) {

Loading…
Cancel
Save