Browse Source

CSharpSymbolSearch: use newCode.

pull/45/merge
Daniel Grunwald 12 years ago
parent
commit
3af9b3f842
  1. 52
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/CSharpSymbolSearch.cs
  2. 7
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CSharpCodeGenerator.cs
  3. 14
      src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs
  4. 1
      src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/IEntity.cs
  5. 6
      src/Main/Base/Project/Dom/IEntityModel.cs
  6. 16
      src/Main/Base/Project/Dom/IMemberModel.cs
  7. 2
      src/Main/Base/Project/Dom/IMethodModel.cs
  8. 5
      src/Main/Base/Project/Refactoring/CodeGenerator.cs
  9. 13
      src/Main/Base/Project/Src/Services/RefactoringService/FindReferenceService.cs
  10. 16
      src/Main/SharpDevelop/Dom/MemberModel.cs
  11. 4
      src/Main/SharpDevelop/Dom/TypeDefinitionModel.cs

52
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/CSharpSymbolSearch.cs

@ -8,10 +8,12 @@ using System.Linq; @@ -8,10 +8,12 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.AvalonEdit.Document;
using CSharpBinding.Parser;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.CSharp.TypeSystem;
@ -142,7 +144,7 @@ namespace CSharpBinding @@ -142,7 +144,7 @@ namespace CSharpBinding
var cancellationToken = args.ProgressMonitor.CancellationToken;
return Task.Run(
() => {
bool isNameValid = true; // TODO : check name!
bool isNameValid = Mono.CSharp.Tokenizer.IsValidIdentifier(args.NewName);
for (int i = 0; i < searchScopes.Count; i++) {
IFindReferenceSearchScope searchScope = searchScopes[i];
object progressLock = new object();
@ -177,7 +179,7 @@ namespace CSharpBinding @@ -177,7 +179,7 @@ namespace CSharpBinding
return;
ReadOnlyDocument document = null;
IHighlighter highlighter = null;
List<SearchResultMatch> results = new List<SearchResultMatch>();
List<RenameResultMatch> results = new List<RenameResultMatch>();
// Grab the unresolved file matching the compilation version
// (this may differ from the version created by re-parsing the project)
@ -193,20 +195,26 @@ namespace CSharpBinding @@ -193,20 +195,26 @@ namespace CSharpBinding
new[] { searchScope }, args.NewName, resolver,
delegate (RenameCallbackArguments callbackArgs) {
var node = callbackArgs.NodeToReplace;
string newCode = callbackArgs.NewNode.ToString();
if (document == null) {
document = new ReadOnlyDocument(textSource, fileName);
highlighter = SD.EditorControlService.CreateHighlighter(document);
highlighter.BeginHighlighting();
if (args.ProvideHighlightedLine) {
highlighter = SD.EditorControlService.CreateHighlighter(document);
highlighter.BeginHighlighting();
}
}
var startLocation = node.StartLocation;
var endLocation = node.EndLocation;
int offset = document.GetOffset(startLocation);
int length = document.GetOffset(endLocation) - offset;
if (args.ProvideHighlightedLine) {
var builder = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter);
var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null;
results.Add(new RenameResultMatch(fileName, startLocation, endLocation, offset, length, newCode, builder, defaultTextColor));
} else {
results.Add(new RenameResultMatch(fileName, startLocation, endLocation, offset, length, newCode));
}
Identifier identifier = node.GetChildByRole(Roles.Identifier);
if (!identifier.IsNull)
node = identifier;
var region = new DomRegion(fileName, node.StartLocation, node.EndLocation);
int offset = document.GetOffset(node.StartLocation);
int length = document.GetOffset(node.EndLocation) - offset;
var builder = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter);
var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null;
results.Add(new SearchResultMatch(fileName, node.StartLocation, node.EndLocation, offset, length, builder, defaultTextColor));
},
errorCallback, cancellationToken);
if (highlighter != null) {
@ -214,16 +222,28 @@ namespace CSharpBinding @@ -214,16 +222,28 @@ namespace CSharpBinding
}
if (results.Count > 0) {
if (!isNameValid) {
errorCallback(new Error(ErrorType.Error, string.Format("The name '{0}' is not valid in the current context!", args.NewName), 0, 0));
errorCallback(new Error(ErrorType.Error, string.Format("The name '{0}' is not valid in the current context!", args.NewName),
new DomRegion(fileName, results[0].StartLocation)));
return;
}
IDocument changedDocument = new TextDocument(document);
var oldVersion = changedDocument.Version;
foreach (var result in results.OrderByDescending(m => m.StartOffset)) {
changedDocument.Replace(result.StartOffset, result.Length, args.NewName);
changedDocument.Replace(result.StartOffset, result.Length, result.newCode);
}
callback(new PatchedFile(fileName, results, oldVersion, changedDocument.Version));
}
}
class RenameResultMatch : SearchResultMatch
{
internal readonly string newCode;
public RenameResultMatch(FileName fileName, TextLocation startLocation, TextLocation endLocation, int offset, int length, string newCode, HighlightedInlineBuilder builder = null, HighlightingColor defaultTextColor = null)
: base(fileName, startLocation, endLocation, offset, length, builder, defaultTextColor)
{
this.newCode = newCode;
}
}
}
}

7
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CSharpCodeGenerator.cs

@ -127,7 +127,12 @@ namespace CSharpBinding.Refactoring @@ -127,7 +127,12 @@ namespace CSharpBinding.Refactoring
fieldDecl.Variables.Add(new VariableInitializer(name));
script.InsertWithCursor("Add field: " + name, Script.InsertPosition.End, fieldDecl);
}
base.AddField(declaringType, accessibility, fieldType, name);
}
public override void ChangeAccessibility(IEntity entity, Accessibility newAccessiblity)
{
// TODO script.ChangeModifiers(...)
throw new NotImplementedException();
}
}
}

14
src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs

@ -500,15 +500,15 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -500,15 +500,15 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
return null;
}
if (node is TypeDeclaration)
if (node is TypeDeclaration)
node = ((TypeDeclaration)node).NameToken;
if (node is DelegateDeclaration)
if (node is DelegateDeclaration)
node = ((DelegateDeclaration)node).NameToken;
if (node is EntityDeclaration)
if (node is EntityDeclaration)
node = ((EntityDeclaration)node).NameToken;
if (node is ParameterDeclaration)
if (node is ParameterDeclaration)
node = ((ParameterDeclaration)node).NameToken;
if (node is ConstructorDeclaration)
node = ((ConstructorDeclaration)node).NameToken;
@ -531,8 +531,8 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -531,8 +531,8 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
Action<RenameCallbackArguments> callback, Action<Error> errorCallback, CancellationToken cancellationToken = default (CancellationToken))
{
FindReferencesInFile(
searchScopes,
resolver,
searchScopes,
resolver,
delegate(AstNode astNode, ResolveResult result) {
var nodeToReplace = GetNodeToReplace(astNode);
if (nodeToReplace == null) {
@ -540,7 +540,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -540,7 +540,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
return;
}
callback (new RenameCallbackArguments(nodeToReplace, Identifier.Create(newName)));
},
},
cancellationToken);
}
#endregion

1
src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/IEntity.cs

@ -166,6 +166,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -166,6 +166,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary>
/// Gets whether this member is declared to be shadowing another member with the same name.
/// (C# 'new' keyword)
/// </summary>
bool IsShadowing { get; }

6
src/Main/Base/Project/Dom/IEntityModel.cs

@ -37,6 +37,12 @@ namespace ICSharpCode.SharpDevelop.Dom @@ -37,6 +37,12 @@ namespace ICSharpCode.SharpDevelop.Dom
/// <remarks>Static classes also count as sealed classes.</remarks>
bool IsSealed { get; }
/// <summary>
/// Gets whether this member is declared to be shadowing another member with the same name.
/// (C# 'new' keyword)
/// </summary>
bool IsShadowing { get; }
/// <summary>
/// Resolves the entity in the current solution snapshot.
/// Returns null if the entity could not be resolved.

16
src/Main/Base/Project/Dom/IMemberModel.cs

@ -28,5 +28,21 @@ namespace ICSharpCode.SharpDevelop.Dom @@ -28,5 +28,21 @@ namespace ICSharpCode.SharpDevelop.Dom
/// Updates the member model with the specified new member.
/// </summary>
void Update(IUnresolvedMember newMember);
/// <summary>
/// Gets if the member is virtual. Is true only if the "virtual" modifier was used, but non-virtual
/// members can be overridden, too; if they are abstract or overriding a method.
/// </summary>
bool IsVirtual { get; }
/// <summary>
/// Gets whether this member is overriding another member.
/// </summary>
bool IsOverride { get; }
/// <summary>
/// Gets if the member can be overridden. Returns true when the member is "abstract", "virtual" or "override" but not "sealed".
/// </summary>
bool IsOverridable { get; }
}
}

2
src/Main/Base/Project/Dom/IMethodModel.cs

@ -7,6 +7,6 @@ namespace ICSharpCode.SharpDevelop.Dom @@ -7,6 +7,6 @@ namespace ICSharpCode.SharpDevelop.Dom
{
public interface IMethodModel : IMemberModel
{
int TypeParameterCount { get; }
}
}

5
src/Main/Base/Project/Refactoring/CodeGenerator.cs

@ -81,5 +81,10 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -81,5 +81,10 @@ namespace ICSharpCode.SharpDevelop.Refactoring
{
throw new NotSupportedException("Feature not supported!");
}
public virtual void MakeVirtual(IMember member)
{
throw new NotSupportedException("Feature not supported!");
}
}
}

13
src/Main/Base/Project/Src/Services/RefactoringService/FindReferenceService.cs

@ -209,8 +209,9 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -209,8 +209,9 @@ namespace ICSharpCode.SharpDevelop.Refactoring
foreach (ISymbolSearch s in symbolSearches) {
progressMonitor.CancellationToken.ThrowIfCancellationRequested();
using (var childProgressMonitor = progressMonitor.CreateSubTask(s.WorkAmount / totalWorkAmount)) {
await s.RenameAsync(new SymbolRenameArgs(newName, childProgressMonitor, parseableFileContentFinder),
file => changes.Add(file), error => errors.Add(error));
var args = new SymbolRenameArgs(newName, childProgressMonitor, parseableFileContentFinder);
args.ProvideHighlightedLine = false;
await s.RenameAsync(args, file => changes.Add(file), error => errors.Add(error));
}
workDone += s.WorkAmount;
@ -252,6 +253,13 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -252,6 +253,13 @@ namespace ICSharpCode.SharpDevelop.Refactoring
get { return this.ProgressMonitor.CancellationToken; }
}
/// <summary>
/// Specifies whether the symbol search should pass a HighlightedInlineBuilder
/// for the matching line to the SearchResultMatch.
/// The default value is <c>true</c>.
/// </summary>
public bool ProvideHighlightedLine { get; set; }
public ParseableFileContentFinder ParseableFileContentFinder { get; private set; }
public SymbolSearchArgs(IProgressMonitor progressMonitor, ParseableFileContentFinder parseableFileContentFinder)
@ -262,6 +270,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -262,6 +270,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring
throw new ArgumentNullException("parseableFileContentFinder");
this.ProgressMonitor = progressMonitor;
this.ParseableFileContentFinder = parseableFileContentFinder;
this.ProvideHighlightedLine = true;
}
}

16
src/Main/SharpDevelop/Dom/MemberModel.cs

@ -106,5 +106,21 @@ namespace ICSharpCode.SharpDevelop.Dom @@ -106,5 +106,21 @@ namespace ICSharpCode.SharpDevelop.Dom
public bool IsSealed {
get { return member.IsSealed; }
}
public bool IsShadowing {
get { return member.IsShadowing; }
}
public bool IsVirtual {
get { return member.IsVirtual; }
}
public bool IsOverride {
get { return member.IsOverride; }
}
public bool IsOverridable {
get { return member.IsOverridable; }
}
}
}

4
src/Main/SharpDevelop/Dom/TypeDefinitionModel.cs

@ -330,5 +330,9 @@ namespace ICSharpCode.SharpDevelop.Dom @@ -330,5 +330,9 @@ namespace ICSharpCode.SharpDevelop.Dom
public bool IsSealed {
get { return parts.Any(p => p.IsSealed); }
}
public bool IsShadowing {
get { return parts.Any(p => p.IsShadowing); }
}
}
}

Loading…
Cancel
Save