Browse Source
Added "bookmark" for classes. Added support for finding references to classes and renaming classes. Renamed the Erbauer/Auswerter classes. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@260 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
59 changed files with 2671 additions and 2318 deletions
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,155 @@
@@ -0,0 +1,155 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt">2002-2005 AlphaSierraPapa</copyright>
|
||||
// <license see="prj:///doc/license.txt">GNU General Public License</license>
|
||||
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Drawing; |
||||
using System.Text; |
||||
using System.Windows.Forms; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.TextEditor; |
||||
using ICSharpCode.TextEditor.Document; |
||||
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using ICSharpCode.SharpDevelop.Bookmarks; |
||||
using ICSharpCode.SharpDevelop.Gui; |
||||
using SearchAndReplace; |
||||
using ICSharpCode.SharpDevelop.DefaultEditor.Commands; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands |
||||
{ |
||||
/// <summary>
|
||||
/// Build context menu for class members in the text editor.
|
||||
/// </summary>
|
||||
public class ClassBookmarkMenuBuilder : ParserBookmarkMenuBuilderBase, ISubmenuBuilder |
||||
{ |
||||
public ToolStripItem[] BuildSubmenu(Codon codon, object owner) |
||||
{ |
||||
MenuCommand cmd; |
||||
ClassBookmark bookmark = (ClassBookmark)owner; |
||||
IClass c = bookmark.Class; |
||||
List<ToolStripItem> list = new List<ToolStripItem>(); |
||||
|
||||
cmd = new MenuCommand("&Rename", Rename); |
||||
cmd.Tag = c; |
||||
list.Add(cmd); |
||||
|
||||
if (c.BaseTypes.Count > 0) { |
||||
cmd = new MenuCommand("Go to &base class", GoToBase); |
||||
cmd.Tag = c; |
||||
list.Add(cmd); |
||||
} |
||||
if (!c.IsSealed && !c.IsStatic) { |
||||
cmd = new MenuCommand("Find &derived classes", FindDerivedClasses); |
||||
cmd.Tag = c; |
||||
list.Add(cmd); |
||||
} |
||||
|
||||
cmd = new MenuCommand("&Find references", FindReferences); |
||||
cmd.Tag = c; |
||||
list.Add(cmd); |
||||
|
||||
return list.ToArray(); |
||||
} |
||||
|
||||
void GoToBase(object sender, EventArgs e) |
||||
{ |
||||
MenuCommand item = (MenuCommand)sender; |
||||
IClass c = (IClass)item.Tag; |
||||
IClass baseClass = c.BaseClass; |
||||
if (baseClass != null) { |
||||
string fileName = baseClass.CompilationUnit.FileName; |
||||
if (fileName != null) { |
||||
FileService.JumpToFilePosition(fileName, baseClass.Region.BeginLine - 1, baseClass.Region.BeginColumn - 1); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
void Rename(object sender, EventArgs e) |
||||
{ |
||||
MenuCommand item = (MenuCommand)sender; |
||||
IClass c = (IClass)item.Tag; |
||||
c = c.DefaultReturnType.GetUnderlyingClass(); // get compound class if class is partial
|
||||
string newName = MessageService.ShowInputBox("Rename", "Enter the new name of the class", c.Name); |
||||
if (!CheckName(newName)) return; |
||||
|
||||
List<Reference> list = RefactoringService.FindReferences(c, null); |
||||
if (list == null) return; |
||||
|
||||
// Add the class declaration(s)
|
||||
foreach (IClass part in GetClassParts(c)) { |
||||
AddDeclarationAsReference(list, part.CompilationUnit.FileName, part.Region, part.Name); |
||||
} |
||||
|
||||
// Add the constructors
|
||||
foreach (IMethod m in c.Methods) { |
||||
if (m.IsConstructor) { |
||||
AddDeclarationAsReference(list, m.DeclaringType.CompilationUnit.FileName, m.Region, c.Name); |
||||
} |
||||
} |
||||
|
||||
RenameReferences(list, newName); |
||||
} |
||||
|
||||
void AddDeclarationAsReference(List<Reference> list, string fileName, IRegion region, string name) |
||||
{ |
||||
if (fileName == null) |
||||
return; |
||||
ProvidedDocumentInformation documentInformation = GetDocumentInformation(fileName); |
||||
int offset = documentInformation.Document.PositionToOffset(new Point(region.BeginColumn - 1, region.BeginLine - 1)); |
||||
string text = documentInformation.TextBuffer.GetText(offset, Math.Min(name.Length + 30, documentInformation.TextBuffer.Length - offset - 1)); |
||||
int offsetChange = text.IndexOf(name); |
||||
if (offsetChange < 0) |
||||
return; |
||||
offset += offsetChange; |
||||
foreach (Reference r in list) { |
||||
if (r.Offset == offset) |
||||
return; |
||||
} |
||||
list.Add(new Reference(fileName, offset, name.Length, name, null)); |
||||
} |
||||
|
||||
List<IClass> GetClassParts(IClass c) |
||||
{ |
||||
List<IClass> list; |
||||
CompoundClass cc = c as CompoundClass; |
||||
if (cc != null) { |
||||
list = cc.Parts; |
||||
} else { |
||||
list = new List<IClass>(1); |
||||
list.Add(c); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
void FindDerivedClasses(object sender, EventArgs e) |
||||
{ |
||||
MenuCommand item = (MenuCommand)sender; |
||||
IClass c = (IClass)item.Tag; |
||||
List<IClass> derivedClasses = RefactoringService.FindDerivedClasses(c, ParserService.AllProjectContents, false); |
||||
|
||||
List<SearchResult> results = new List<SearchResult>(); |
||||
foreach (IClass derivedClass in derivedClasses) { |
||||
if (derivedClass.CompilationUnit == null) continue; |
||||
if (derivedClass.CompilationUnit.FileName == null) continue; |
||||
|
||||
SearchResult res = new SimpleSearchResult(derivedClass.FullyQualifiedName, new Point(derivedClass.Region.BeginColumn - 1, derivedClass.Region.BeginLine - 1)); |
||||
res.ProvidedDocumentInformation = GetDocumentInformation(derivedClass.CompilationUnit.FileName); |
||||
results.Add(res); |
||||
} |
||||
SearchReplaceInFilesManager.ShowSearchResults("Classes deriving from " + c.Name, results); |
||||
} |
||||
|
||||
void FindReferences(object sender, EventArgs e) |
||||
{ |
||||
MenuCommand item = (MenuCommand)sender; |
||||
IClass c = (IClass)item.Tag; |
||||
ShowAsSearchResults("References to " + c.Name, RefactoringService.FindReferences(c, null)); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,164 @@
@@ -0,0 +1,164 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt">2002-2005 AlphaSierraPapa</copyright>
|
||||
// <license see="prj:///doc/license.txt">GNU General Public License</license>
|
||||
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Drawing; |
||||
using System.Text; |
||||
using System.Windows.Forms; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.TextEditor; |
||||
using ICSharpCode.TextEditor.Document; |
||||
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using ICSharpCode.SharpDevelop.Bookmarks; |
||||
using ICSharpCode.SharpDevelop.Gui; |
||||
using SearchAndReplace; |
||||
using ICSharpCode.SharpDevelop.DefaultEditor.Commands; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands |
||||
{ |
||||
public class ParserBookmarkMenuBuilderBase |
||||
{ |
||||
protected ProvidedDocumentInformation GetDocumentInformation(string fileName) |
||||
{ |
||||
foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) { |
||||
if (content is ITextEditorControlProvider && |
||||
content.FileName != null && |
||||
FileUtility.IsEqualFileName(content.FileName, fileName)) |
||||
{ |
||||
return new ProvidedDocumentInformation(((ITextEditorControlProvider)content).TextEditorControl.Document, fileName, 0); |
||||
} |
||||
} |
||||
ITextBufferStrategy strategy = StringTextBufferStrategy.CreateTextBufferFromFile(fileName); |
||||
return new ProvidedDocumentInformation(strategy, fileName, 0); |
||||
} |
||||
|
||||
protected TextEditorControl JumpToDefinition(IMember member) |
||||
{ |
||||
IViewContent viewContent = null; |
||||
ICompilationUnit cu = member.DeclaringType.CompilationUnit; |
||||
if (cu != null) { |
||||
string fileName = cu.FileName; |
||||
if (fileName != null) { |
||||
if (member.Region != null && member.Region.BeginLine > 0) { |
||||
viewContent = FileService.JumpToFilePosition(fileName, member.Region.BeginLine - 1, member.Region.BeginColumn - 1); |
||||
} else { |
||||
FileService.OpenFile(fileName); |
||||
} |
||||
} |
||||
} |
||||
ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.ITextEditorControlProvider tecp = viewContent as ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.ITextEditorControlProvider; |
||||
return (tecp == null) ? null : tecp.TextEditorControl; |
||||
} |
||||
|
||||
protected TextEditorControl JumpBehindDefinition(IMember member) |
||||
{ |
||||
IViewContent viewContent = null; |
||||
ICompilationUnit cu = member.DeclaringType.CompilationUnit; |
||||
if (cu != null) { |
||||
string fileName = cu.FileName; |
||||
if (fileName != null) { |
||||
if (member.Region != null && member.Region.EndLine > 0) { |
||||
viewContent = FileService.JumpToFilePosition(fileName, member.Region.EndLine, 0); |
||||
} else { |
||||
FileService.OpenFile(fileName); |
||||
} |
||||
} |
||||
} |
||||
ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.ITextEditorControlProvider tecp = viewContent as ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.ITextEditorControlProvider; |
||||
return (tecp == null) ? null : tecp.TextEditorControl; |
||||
} |
||||
|
||||
protected bool CheckName(string name) |
||||
{ |
||||
if (name == null || name.Length == 0) |
||||
return false; |
||||
if (!char.IsLetter(name, 0) && name[0] != '_') { |
||||
MessageService.ShowError("Names must start with a letter or underscore."); |
||||
return false; |
||||
} |
||||
for (int i = 1; i < name.Length; i++) { |
||||
if (!char.IsLetterOrDigit(name, i) && name[i] != '_') { |
||||
MessageService.ShowError("Names may only contain letters, digits or underscores."); |
||||
return false; |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
protected struct Modification |
||||
{ |
||||
public IDocument Document; |
||||
public int Offset; |
||||
public int LengthDifference; |
||||
|
||||
public Modification(IDocument document, int offset, int lengthDifference) |
||||
{ |
||||
this.Document = document; |
||||
this.Offset = offset; |
||||
this.LengthDifference = lengthDifference; |
||||
} |
||||
} |
||||
|
||||
protected void ModifyDocument(List<Modification> modifications, IDocument doc, int offset, int length, string newName) |
||||
{ |
||||
foreach (Modification m in modifications) { |
||||
if (m.Document == doc) { |
||||
if (m.Offset < offset) |
||||
offset += m.LengthDifference; |
||||
} |
||||
} |
||||
int lengthDifference = newName.Length - length; |
||||
doc.Replace(offset, length, newName); |
||||
if (lengthDifference != 0) { |
||||
for (int i = 0; i < modifications.Count; ++i) { |
||||
Modification m = modifications[i]; |
||||
if (m.Document == doc) { |
||||
if (m.Offset > offset) { |
||||
m.Offset += lengthDifference; |
||||
modifications[i] = m; // Modification is a value type
|
||||
} |
||||
} |
||||
} |
||||
modifications.Add(new Modification(doc, offset, lengthDifference)); |
||||
} |
||||
} |
||||
|
||||
protected void ShowAsSearchResults(string pattern, List<Reference> list) |
||||
{ |
||||
if (list == null) return; |
||||
List<SearchResult> results = new List<SearchResult>(list.Count); |
||||
foreach (Reference r in list) { |
||||
SearchResult res = new SearchResult(r.Offset, r.Length); |
||||
res.ProvidedDocumentInformation = GetDocumentInformation(r.FileName); |
||||
results.Add(res); |
||||
} |
||||
SearchReplaceInFilesManager.ShowSearchResults(pattern, results); |
||||
} |
||||
|
||||
protected void RenameReferences(List<Reference> list, string newName) |
||||
{ |
||||
List<IViewContent> modifiedContents = new List<IViewContent>(); |
||||
List<Modification> modifications = new List<Modification>(); |
||||
foreach (Reference r in list) { |
||||
FileService.OpenFile(r.FileName); |
||||
IViewContent viewContent = FileService.GetOpenFile(r.FileName).ViewContent; |
||||
if (!modifiedContents.Contains(viewContent)) { |
||||
modifiedContents.Add(viewContent); |
||||
} |
||||
ITextEditorControlProvider p = viewContent as ITextEditorControlProvider; |
||||
if (p != null) { |
||||
ModifyDocument(modifications, p.TextEditorControl.Document, r.Offset, r.Length, newName); |
||||
} |
||||
} |
||||
foreach (IViewContent viewContent in modifiedContents) { |
||||
ParserService.ParseViewContent(viewContent); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue