Skip to content

Commit

Permalink
procedure rename works across modules
Browse files Browse the repository at this point in the history
  • Loading branch information
retailcoder committed Mar 26, 2015
1 parent ebcf17f commit 1cb51f4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
9 changes: 9 additions & 0 deletions RetailCoder.VBE/UI/Refactorings/Rename/RenamePresenter.cs
Expand Up @@ -84,10 +84,19 @@ private string GetReplacementLine(string content, string target, string newName)
{
result = result.Replace(target + '.', newName + '.');
}
else if (result.Contains('.' + target))
{
result = result.Replace('.' + target, '.'+ newName);
}

if (result.Contains(target + '!'))
{
result = result.Replace(target + '!', newName + '!');
}
else if (result.Contains('!' + target))
{
result = result.Replace('!' + target, '!' + newName);
}

return result.Substring(1);
}
Expand Down
22 changes: 12 additions & 10 deletions Rubberduck.Parsing/QualifiedModuleName.cs
@@ -1,38 +1,40 @@

using Microsoft.Vbe.Interop;

namespace Rubberduck.Parsing
{
public struct QualifiedModuleName
{
public QualifiedModuleName(string project, string module, int projectHashCode, int contentHashCode)
public QualifiedModuleName(string projectName, string module, VBProject project, int contentHashCode)
{
_projectHash = projectHashCode;
_contentHashCode = contentHashCode;
_project = project;
_contentHashCode = contentHashCode;
_projectName = projectName;
_module = module;
}

public static QualifiedModuleName Empty { get { return new QualifiedModuleName(string.Empty, string.Empty, default(int), default(int)); } }
public static QualifiedModuleName Empty { get { return new QualifiedModuleName(string.Empty, string.Empty, null, default(int)); } }

private readonly int _projectHash;
public int ProjectHashCode { get { return _projectHash; } }
private readonly VBProject _project;
public VBProject Project { get { return _project; } }

private readonly int _contentHashCode;
public int ContentHashCode { get { return _contentHashCode; } }

private readonly string _project;
public string ProjectName { get { return _project; } }
private readonly string _projectName;
public string ProjectName { get { return _projectName; } }

private readonly string _module;
public string ModuleName { get { return _module; } }

public override string ToString()
{
return _project + "." + _module;
return _projectName + "." + _module;
}

public override int GetHashCode()
{
return (_projectHash.ToString() + _contentHashCode.ToString() + ToString()).GetHashCode();
return (_project.ToString() + _contentHashCode.ToString() + ToString()).GetHashCode();
}

public override bool Equals(object obj)
Expand Down
5 changes: 3 additions & 2 deletions Rubberduck.Parsing/Symbols/Declaration.cs
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Antlr4.Runtime;
using Microsoft.Vbe.Interop;

namespace Rubberduck.Parsing.Symbols
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public void AddReference(IdentifierReference reference)
/// <remarks>
/// This property is intended to differenciate identically-named VBProjects.
/// </remarks>
public int ProjectHashCode { get { return _qualifiedName.QualifiedModuleName.ProjectHashCode; } }
public VBProject Project { get { return _qualifiedName.QualifiedModuleName.Project; } }

/// <summary>
/// Gets the name of the VBProject the declaration is made in.
Expand Down Expand Up @@ -141,7 +142,7 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return string.Concat(ProjectHashCode.ToString(), ProjectName, ComponentName, _parentScope, _identifierName).GetHashCode();
return string.Concat(Project.ToString(), ProjectName, ComponentName, _parentScope, _identifierName).GetHashCode();
}
}
}
24 changes: 22 additions & 2 deletions Rubberduck.Parsing/Symbols/IdentifierReferenceListener.cs
Expand Up @@ -133,8 +133,28 @@ private void EnterIdentifier(RuleContext context, Selection selection)
}
}

private static readonly DeclarationType[] ProcedureDeclarations = new[]
{
DeclarationType.Procedure,
DeclarationType.Function,
DeclarationType.PropertyGet,
DeclarationType.PropertyLet,
DeclarationType.PropertySet
};

private bool IsInScope(Declaration declaration)
{
if (ProcedureDeclarations.Contains(declaration.DeclarationType))
{
if (declaration.Accessibility == Accessibility.Public)
{
var result = declaration.Project.Equals(_qualifiedName.Project);
return result;
}

return declaration.QualifiedName.QualifiedModuleName == _qualifiedName;
}

return declaration.Scope == _currentScope
|| declaration.Scope == ModuleScope
|| IsGlobalField(declaration)
Expand Down Expand Up @@ -176,7 +196,7 @@ private bool IsGlobalField(Declaration declaration)
var modules = moduleMatches.Where(match => match.DeclarationType == DeclarationType.Module);

// Friend members are only visible within the same project.
var isSameProject = declaration.ProjectHashCode == _qualifiedName.ProjectHashCode;
var isSameProject = declaration.Project == _qualifiedName.Project;

// todo: verify that this isn't overkill. Friend modifier has restricted legal usage.
return modules.Any()
Expand All @@ -198,7 +218,7 @@ private bool IsGlobalProcedure(Declaration declaration)
}

// Friend members are only visible within the same project.
var isSameProject = declaration.ProjectHashCode == _qualifiedName.ProjectHashCode;
var isSameProject = declaration.Project == _qualifiedName.Project;

// implicit (unspecified) accessibility makes a member Public,
// so if it's in the same project, it's public whenever it's not explicitly Private:
Expand Down
3 changes: 1 addition & 2 deletions Rubberduck.Parsing/VBComponentExtensions.cs
Expand Up @@ -13,10 +13,9 @@ public static QualifiedModuleName QualifiedName(this VBComponent component)
{
var moduleName = component.Name;
var project = component.Collection.Parent;
var hash = project.GetHashCode();
var code = component.CodeModule.Lines().GetHashCode();

return new QualifiedModuleName(project.Name, moduleName, hash, code);
return new QualifiedModuleName(project.Name, moduleName, project, code);
}

public static string Lines(this CodeModule module)
Expand Down

0 comments on commit 1cb51f4

Please sign in to comment.