Introduction
This is an extensibility of VS.NET 2003.This add-in adds try
/catch
blocks inside your method. This will save development time. Basically it checks whether it is a method and also whether a try
/catch
block already exists. If not it adds a try
/catch
inside your method. Usually programmers forget to write try
/catch
during development time and at the slast minute they will add it in all the pages. That time this will be very useful .They can just execute this add-in which will automatically add try
/catch
blocks inside your method.
Installation Steps
- Run the Setup.exe from the AddTryCatchInstall.zip.
- Select the installation folder where you want to install the setup.
- Click Close to finish the installation.
Using the Tool
After you install the add-in, it will be listed in the Tools menu automatically. To use this you have to place your cursor inside the method and click this add-in. Adding to the ToolBar: to add it to your toolbar, go to Tools -> Customize -> Select Command tab, then choose AddIn and drag AddTryCatch to the toolbar and drop it.
Source Code Overview
You have to declare this struct
which contains the caret position, function name, start and end position of the function.
private struct SFunction
{
public string FuncName;
public int StPtr;
public int EndPt;
public int CaretPos;
public bool SearchDone;
}
private SFunction OneFunction;
This method checks for the active document and calls GetWholeProcedure()
to add the try
/catch
block. Add AddingTryCatch()
method call in the Exec
method (e.g.) handler = AddingTryCatch()
;
public bool AddingTryCatch()
{
if (applicationObject.ActiveDocument !=null)
{
GetWholeProcedure();
}
return true;
}
Code listed below checks for the function in the current window and fetches the function where the cursor is placed. Then it checks if try
/catch
block exists already. Then it inserts a try
at the start of the function and catch
/finally
at the end of the function.
public void GetWholeProcedure()
{
TextSelection ts = (TextSelection) applicationObject.ActiveDocument.Selection;
EditPoint ep = ts.ActivePoint.CreateEditPoint();
OneFunction.CaretPos=ep.Line;
OneFunction.SearchDone= false;
SearchForFunctionInCurrentWindow();
if (OneFunction.SearchDone)
{
ep.MoveToLineAndOffset(OneFunction.StPtr,1);
string s = ep.GetLines(ep.Line,OneFunction.EndPt+1);
int found = s.IndexOf("try");
int foundcatch = s.IndexOf("catch(");
int position = 2;
if(found == -1 || foundcatch == -1 )
{
TextSelection ts1 =
(TextSelection)applicationObject.ActiveDocument.Selection;
if(applicationObject.ActiveDocument.Name.IndexOf("asmx.cs")
!= -1 && s.IndexOf("[WebMethod]") != -1 )
position = 3;
else
position = 2;
ts1.GotoLine((OneFunction.StPtr + position),true);
ts1.Copy();
EditPoint e =ts1.TopPoint.CreateEditPoint();
e.Insert("\t\t\ttry\n\t\t\t{\n");
ts1.Paste();
int currentposition = OneFunction.StPtr + position + 1 ;
for(int intLoop = 1 ; intLoop <=(OneFunction.EndPt -
(OneFunction.StPtr+position));intLoop++)
{
ts1.GotoLine((currentposition + intLoop),true);
if(ts1.Text.Equals("") != true )
{
ts1.SelectLine();
ts1.Indent(1);
}
}
e.LineDown(OneFunction.EndPt-(OneFunction.StPtr+position));
e.Insert("\t\t\t}\n\t\t\tcatch(Exception ex)\n\" +
"t\t\t{\n\t\t\t\tthrow ex;\n\t\t\t}\n\" +
"t\t\tfinally\n\t\t\t{\n\t\t\t}\n");
}
else
{
System.Windows.Forms.MessageBox.Show("Try/Catch Block already exists");
}
}
}
The below functions are the major things which find out the function in the active window. It checks for the function in the caret position and returns the starting and ending point of the function and sets the searchDone
flag to true
. Some of the extensibility classes used are:
ProjectItem
- Represents an item in a project.
FileCodeModel
- Allows access to programmatic constructs in a source file.
CodeElements
- Represents a code element or construct in a source file.
CodeNamespace
- Represents a namespace construct in a source file.
CodeClass
- Represents a class in source code.
private void SearchForFunctionInCurrentWindow()
{
ProjectItem pi = (ProjectItem) applicationObject.ActiveWindow.ProjectItem;
FileCodeModel fcm = pi.FileCodeModel;
if ( fcm!=null)
GetFunction(fcm.CodeElements);
}
private void GetFunction(CodeElements elements)
{
CodeElements members;
foreach (CodeElement elt in elements)
{
if(OneFunction.SearchDone)
return ;
if (elt.Kind==vsCMElement.vsCMElementFunction )
{
OneFunction.FuncName=elt.Name;
OneFunction.StPtr=elt.StartPoint.Line;
OneFunction.EndPt=elt.EndPoint.Line;
if(OneFunction.CaretPos>= OneFunction.StPtr &&
OneFunction.CaretPos <= OneFunction.EndPt)
{
OneFunction.SearchDone=true;
return;
}
}
else
{
members = GetMembers(elt);
if (members != null )
GetFunction(members);
}
}
}
private CodeElements GetMembers(CodeElement elt)
{
CodeElements members = null ;
if(elt!= null)
{
switch ( elt.Kind)
{
case (vsCMElement.vsCMElementNamespace ):
{
CodeNamespace cdeNS = (CodeNamespace) elt;
members = cdeNS.Members;
break ;
}
case (vsCMElement.vsCMElementClass):
{
CodeClass cdeCL = (CodeClass) elt;
members = cdeCL.Members;
break;
}
case (vsCMElement.vsCMElementFunction):
{
members=null;
break;
}
}
}
return members;
}
Conclusion
Hope this tool would be useful. Good day...