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. 28
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs
  5. 42
      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 @@
<Compile Include="Configuration\AssemblyInfo.cs" /> <Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\ClassRefactoringSubmenuBuilder.cs" /> <Compile Include="Src\ClassRefactoringSubmenuBuilder.cs" />
<Compile Include="Src\ContextActions\AddUsing.cs" /> <Compile Include="Src\ContextActions\AddUsing.cs" />
<Compile Include="Src\ContextActions\DelegateAction.cs" />
<Compile Include="Src\ContextActions\Extensions.cs" /> <Compile Include="Src\ContextActions\Extensions.cs" />
<Compile Include="Src\ContextActions\FindNodesVisitor.cs" /> <Compile Include="Src\ContextActions\FindNodesVisitor.cs" />
<Compile Include="Src\ContextActions\GenerateMember.cs" /> <Compile Include="Src\ContextActions\GenerateMember.cs" />

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

@ -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 @@
// <version>$Revision: $</version> // <version>$Revision: $</version>
// </file> // </file>
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using ICSharpCode.NRefactory.Ast; using ICSharpCode.NRefactory.Ast;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Refactoring;
namespace SharpRefactoring.ContextActions namespace SharpRefactoring.ContextActions
{ {
@ -21,5 +26,19 @@ namespace SharpRefactoring.ContextActions
astTree.AcceptVisitor(findVisitor, null); astTree.AcceptVisitor(findVisitor, null);
return findVisitor.Declarations; 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();
}
}
}
} }
} }

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

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

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

@ -6,6 +6,7 @@
// </file> // </file>
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Windows; using System.Windows;
using ICSharpCode.NRefactory; using ICSharpCode.NRefactory;
using Ast = ICSharpCode.NRefactory.Ast; using Ast = ICSharpCode.NRefactory.Ast;
@ -25,43 +26,22 @@ namespace SharpRefactoring.ContextActions
{ {
public IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST) 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, // 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 // 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. // 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(); var ambience = AmbienceService.GetCurrentAmbience();
foreach (var declaration in currentLineAST.FindTypeDeclarations()) {
if (declaration.Type == Ast.ClassType.Class || declaration.Type == Ast.ClassType.Struct) { foreach (var targetClass in editorAST.GetClassesOnCurrentLine().
var rr = ParserService.Resolve(new ExpressionResult(declaration.Name), editor.Caret.Line, editor.Caret.Column, editor.FileName, editor.Document.Text); Where(c => c.ClassType == ClassType.Class || c.ClassType == ClassType.Interface)) {
var targetClass = rr.ResolvedType == null ? null : rr.ResolvedType.GetUnderlyingClass();
if (targetClass != null) { foreach (var implementAction in RefactoringService.GetImplementInterfaceActions(targetClass, false)) {
foreach (var implementAction in RefactoringService.GetImplementInterfaceActions(targetClass, false)) { var implementActionCopy = implementAction;
var implementActionCopy = implementAction; yield return new DelegateAction {
yield return new DelegateAction { Title = string.Format("Implement interface {0}", ambience.Convert(implementActionCopy.ClassToImplement)),
Title = string.Format("Implement interface {0}", ambience.Convert(implementActionCopy.ClassToImplement)), ExecuteAction = implementActionCopy.Execute
ExecuteAction = implementActionCopy.Execute };
};
}
}
} }
} }
} }
} }
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
/// </summary> /// </summary>
public IEnumerable<IContextAction> GetAvailableActions(ITextEditor editor) public IEnumerable<IContextAction> GetAvailableActions(ITextEditor editor)
{ {
yield break;
var editorAST = new EditorASTProvider(editor); var editorAST = new EditorASTProvider(editor);
// could run providers in parallel // could run providers in parallel
foreach (var provider in this.providers) { foreach (var provider in this.providers) {

Loading…
Cancel
Save