Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6391 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61pull/1/head
9 changed files with 191 additions and 57 deletions
@ -0,0 +1,81 @@ |
|||||||
|
// <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.IO; |
||||||
|
using System.Linq; |
||||||
|
using ICSharpCode.Core; |
||||||
|
using ICSharpCode.SharpDevelop.Dom; |
||||||
|
using ICSharpCode.SharpDevelop.Editor.Commands; |
||||||
|
using ICSharpCode.SharpDevelop.Refactoring; |
||||||
|
|
||||||
|
namespace SharpRefactoring.ContextActions |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Description of CacheClassAtCaret.
|
||||||
|
/// </summary>
|
||||||
|
public class CacheClassAtCaret : IContextActionCache |
||||||
|
{ |
||||||
|
public IClass Class { get; private set; } |
||||||
|
|
||||||
|
public bool IsClassFileNameCorrect { get; private set; } |
||||||
|
|
||||||
|
public string CorrectClassFileName { get; private set; } |
||||||
|
|
||||||
|
public bool IsCorrectClassFileNameAvailable { get; private set; } |
||||||
|
|
||||||
|
public bool IsClassReadOnly { get; private set; } |
||||||
|
|
||||||
|
public bool IsCaretAtClassDeclaration { get; private set; } |
||||||
|
|
||||||
|
public bool IsFixClassFileNameAvailable |
||||||
|
{ |
||||||
|
get { |
||||||
|
return !IsClassReadOnly && IsCaretAtClassDeclaration && !IsClassFileNameCorrect && IsCorrectClassFileNameAvailable; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void Initialize(EditorContext context) |
||||||
|
{ |
||||||
|
this.Class = GetClass(context.CurrentSymbol); |
||||||
|
if (this.Class == null) |
||||||
|
return; |
||||||
|
var c = this.Class; |
||||||
|
|
||||||
|
// TODO cache
|
||||||
|
var classDecls = context.GetClassDeclarationsOnCurrentLine().ToList(); |
||||||
|
this.IsCaretAtClassDeclaration = classDecls.Count == 1 && (classDecls[0].FullyQualifiedName == c.FullyQualifiedName); |
||||||
|
|
||||||
|
this.IsClassFileNameCorrect = (c.IsInnerClass() || (!c.IsUserCode()) || |
||||||
|
c.Name.Equals(Path.GetFileNameWithoutExtension(c.CompilationUnit.FileName), StringComparison.OrdinalIgnoreCase)); |
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(c.CompilationUnit.FileName)) { |
||||||
|
// Cannot get path
|
||||||
|
this.CorrectClassFileName = null; |
||||||
|
this.IsCorrectClassFileNameAvailable = false; |
||||||
|
return; |
||||||
|
} |
||||||
|
this.CorrectClassFileName = Path.Combine(Path.GetDirectoryName(c.CompilationUnit.FileName), |
||||||
|
c.Name + Path.GetExtension(c.CompilationUnit.FileName)); |
||||||
|
|
||||||
|
this.IsCorrectClassFileNameAvailable = (FileUtility.IsValidPath(CorrectClassFileName) |
||||||
|
&& Path.IsPathRooted(CorrectClassFileName) |
||||||
|
&& !File.Exists(CorrectClassFileName)); |
||||||
|
|
||||||
|
this.IsClassReadOnly = FindReferencesAndRenameHelper.IsReadOnly(this.Class); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
IClass GetClass(ResolveResult currentSymbol) |
||||||
|
{ |
||||||
|
if (currentSymbol == null || currentSymbol.ResolvedType == null) |
||||||
|
return null; |
||||||
|
IClass c = currentSymbol.ResolvedType.GetUnderlyingClass(); |
||||||
|
c = c.ProjectContent.GetClass(c.FullyQualifiedName, c.TypeParameters.Count, c.ProjectContent.Language, GetClassOptions.LookForInnerClass); |
||||||
|
return ClassBookmarkSubmenuBuilder.GetCurrentPart(c); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
// <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.IO; |
||||||
|
using ICSharpCode.Core; |
||||||
|
using ICSharpCode.SharpDevelop.Refactoring; |
||||||
|
|
||||||
|
namespace SharpRefactoring.ContextActions |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Description of MoveClassToFile.
|
||||||
|
/// </summary>
|
||||||
|
public class MoveClassToFile : ContextAction |
||||||
|
{ |
||||||
|
public CacheClassAtCaret ClassAtCaret |
||||||
|
{ |
||||||
|
get { return this.Context.GetCached<CacheClassAtCaret>(); } |
||||||
|
} |
||||||
|
|
||||||
|
public override string Title { |
||||||
|
get { |
||||||
|
var fileName = Path.GetFileName(ClassAtCaret.CorrectClassFileName); |
||||||
|
return StringParser.Parse("${res:SharpDevelop.Refactoring.MoveClassToFile}", new StringTagPair("FileName", fileName)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override bool IsAvailable(EditorContext context) |
||||||
|
{ |
||||||
|
if (ClassAtCaret.Class == null) return false; |
||||||
|
return (ClassAtCaret.IsFixClassFileNameAvailable && ClassAtCaret.Class.CompilationUnit.Classes.Count != 1); |
||||||
|
} |
||||||
|
|
||||||
|
public override void Execute(EditorContext context) |
||||||
|
{ |
||||||
|
FindReferencesAndRenameHelper.MoveClassToFile(ClassAtCaret.Class, ClassAtCaret.CorrectClassFileName); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
// <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.IO; |
||||||
|
using ICSharpCode.Core; |
||||||
|
using ICSharpCode.SharpDevelop.Project; |
||||||
|
using ICSharpCode.SharpDevelop.Refactoring; |
||||||
|
|
||||||
|
namespace SharpRefactoring.ContextActions |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Description of RenameFileToMatchClassFileName.
|
||||||
|
/// </summary>
|
||||||
|
public class RenameFileToMatchClassName : ContextAction |
||||||
|
{ |
||||||
|
public CacheClassAtCaret ClassAtCaret |
||||||
|
{ |
||||||
|
get { return this.Context.GetCached<CacheClassAtCaret>(); } |
||||||
|
} |
||||||
|
|
||||||
|
public override string Title { |
||||||
|
get { |
||||||
|
var fileName = Path.GetFileName(ClassAtCaret.CorrectClassFileName); |
||||||
|
return StringParser.Parse("${res:SharpDevelop.Refactoring.RenameFileTo}", new StringTagPair("FileName", fileName)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override bool IsAvailable(EditorContext context) |
||||||
|
{ |
||||||
|
if (ClassAtCaret.Class == null) return false; |
||||||
|
return (ClassAtCaret.IsFixClassFileNameAvailable && ClassAtCaret.Class.CompilationUnit.Classes.Count == 1); |
||||||
|
} |
||||||
|
|
||||||
|
public override void Execute(EditorContext context) |
||||||
|
{ |
||||||
|
IProject project = (IProject)ClassAtCaret.Class.ProjectContent.Project; |
||||||
|
RefactoringHelpers.RenameFile(project, ClassAtCaret.Class.CompilationUnit.FileName, ClassAtCaret.CorrectClassFileName); |
||||||
|
if (project != null) { |
||||||
|
project.Save(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue