Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions spkl/SparkleXrm.Tasks/CodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public int RemoveExistingAttributes()
return count;
}



public void AddAttribute(CrmPluginRegistrationAttribute attribute, string className)
{
Expand All @@ -169,20 +169,36 @@ public void AddAttribute(CrmPluginRegistrationAttribute attribute, string classN
if (classLocation == null)
throw new Exception(String.Format("Cannot find class {0}", className));

var pos = _code.IndexOf(classLocation.Value);
// start index of "public class OpportunityPluign"
int classStartIndex = _code.IndexOf(classLocation.Value);

// start index of "{ public class OpportunityPluign"
int openBraceBeforeClassIndex = _code.LastIndexOf("{", classStartIndex - 1);

// Find previouse line break
var lineBreak = _code.LastIndexOf("\r\n", pos - 1);
// start index of "<EOL> { public class OpportunityPluign"
int eolBeforeClassIndex = _code.LastIndexOf("\r\n", classStartIndex - 1, classStartIndex - 1 - openBraceBeforeClassIndex);

// Indentation
var indentation = _code.Substring(lineBreak, pos - lineBreak);
// discover indentation between class and EOL (inclusive of EOL character)
string indentation = "\r\n";
if (eolBeforeClassIndex != -1)
{
indentation = _code.Substring(eolBeforeClassIndex, classStartIndex - eolBeforeClassIndex);
}

// Add the attribute
var attributeCode = attribute.GetAttributeCode(indentation);
// generate the attribut code
string attributeCode = attribute.GetAttributeCode(indentation);

// Insert
_code = _code.Insert(lineBreak, attributeCode);
if (eolBeforeClassIndex == -1)
{
// insert attribute code at class start
eolBeforeClassIndex = classStartIndex;

// add EOL between attribute code and class
attributeCode += "\r\n";
}

// insert attribute code
_code = _code.Insert(eolBeforeClassIndex, attributeCode);
}
#endregion
}
Expand Down