Skip to content

Commit

Permalink
Refactor signature adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosch250 committed May 18, 2015
1 parent 034d222 commit 940e79a
Showing 1 changed file with 36 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void OnOkButtonClicked(object sender, EventArgs e)
}

var interfaceImplementations = _declarations.FindInterfaceImplementationMembers()
.Where(item => item.Project.Equals(_view.Target.Project) && item.IdentifierName.Contains(_view.Target.ComponentName)))
.Where(item => item.Project.Equals(_view.Target.Project) && item.IdentifierName.Contains(_view.Target.ComponentName));
foreach (var interfaceImplentation in interfaceImplementations)
{
AdjustSignature(interfaceImplentation);
Expand Down Expand Up @@ -163,87 +163,59 @@ private void AdjustReferences()
}
}

// TODO - refactor this
// Only used for Property Letters/Setters
// Otherwise, they are caught by the try/catch block used to prevent
// value returns from crashing the program
// Extremely similar to the other AdjustReference
// One possibility is to create multiple "handler" methods
// that call another method to do the real work, passing
// "module", "argList", and "args" (I believe these are the only
// ones used
private void AdjustSignature(IdentifierReference reference)
private void AdjustSignature()
{
var proc = (dynamic)reference.Context.Parent;
var module = reference.QualifiedModuleName.Component.CodeModule;
var proc = (dynamic)_view.Target.Context;
var argList = (VBAParser.ArgListContext)proc.argList();
var args = argList.arg();
var module = _view.Target.QualifiedName.QualifiedModuleName.Component.CodeModule;

var variableIndex = 0;
for (var lineNum = argList.Start.Line; lineNum < argList.Start.Line + argList.GetSelection().LineCount; lineNum++)
// if we are reordering a property getter, check if we need to reorder a setter too
if (_view.Target.DeclarationType == DeclarationType.PropertyGet)
{
var newContent = module.Lines[lineNum, 1];
var currentStringIndex = 0;
var setter = _declarations.Items.FirstOrDefault(item => item.ParentScope == _view.Target.ParentScope &&
item.IdentifierName == _view.Target.IdentifierName &&
item.DeclarationType == DeclarationType.PropertySet);

for (var i = variableIndex; i < _view.Parameters.Count; i++)
if (setter != null)
{
var variableStringIndex = newContent.IndexOf(_view.Parameters.Find(item => item.Index == variableIndex).FullDeclaration, currentStringIndex);

if (variableStringIndex > -1)
{
var oldVariableString = _view.Parameters.Find(item => item.Index == variableIndex).FullDeclaration;
var newVariableString = _view.Parameters.ElementAt(i).FullDeclaration;
var beginningSub = newContent.Substring(0, variableStringIndex);
var replaceSub = newContent.Substring(variableStringIndex).Replace(oldVariableString, newVariableString);

newContent = beginningSub + replaceSub;

variableIndex++;
currentStringIndex = beginningSub.Length + newVariableString.Length;
}
AdjustSignature(setter);
}

module.ReplaceLine(lineNum, newContent);
}

RewriteSignature(argList, module);
}

private void AdjustSignature(Declaration reference = null)
private void AdjustSignature(IdentifierReference reference)
{
var proc = (dynamic)_view.Target.Context;
var proc = (dynamic)reference.Context.Parent;
var module = reference.QualifiedModuleName.Component.CodeModule;
var argList = (VBAParser.ArgListContext)proc.argList();
var module = _view.Target.QualifiedName.QualifiedModuleName.Component.CodeModule;

if (reference != null)
{
proc = (dynamic)reference.Context.Parent;
module = reference.QualifiedName.QualifiedModuleName.Component.CodeModule;

if (reference.DeclarationType == DeclarationType.PropertySet)
{
argList = (VBAParser.ArgListContext)proc.children[0].argList();
}
else
{
argList = (VBAParser.ArgListContext)proc.subStmt().argList();
}
}
RewriteSignature(argList, module);
}

var args = argList.arg();
private void AdjustSignature(Declaration reference)
{
var proc = (dynamic)reference.Context.Parent;
var module = reference.QualifiedName.QualifiedModuleName.Component.CodeModule;
VBAParser.ArgListContext argList;

// if we are reordering a property getter, check if we need to reorder a setter too
// only check if the passed reference is null, otherwise we recursively check and have an SO
if (reference == null && _view.Target.DeclarationType == DeclarationType.PropertyGet)
if (reference.DeclarationType == DeclarationType.PropertySet)
{
var setter = _declarations.Items.FirstOrDefault(item => item.ParentScope == _view.Target.ParentScope &&
item.IdentifierName == _view.Target.IdentifierName &&
item.DeclarationType == DeclarationType.PropertySet);

if (setter != null)
{
AdjustSignature(setter);
}
argList = (VBAParser.ArgListContext)proc.children[0].argList();
}
else
{
argList = (VBAParser.ArgListContext)proc.subStmt().argList();
}

RewriteSignature(argList, module);
}

private void RewriteSignature(VBAParser.ArgListContext argList, Microsoft.Vbe.Interop.CodeModule module)
{
var args = argList.arg();

var variableIndex = 0;
for (var lineNum = argList.Start.Line; lineNum < argList.Start.Line + argList.GetSelection().LineCount; lineNum++)
{
Expand Down

0 comments on commit 940e79a

Please sign in to comment.