This is a migrated thread and some comments may be shown as answers.

Code cleaning extension - API ?

1 Answer 34 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
RR SoftSol
Top achievements
Rank 1
RR SoftSol asked on 29 Jul 2013, 09:25 AM
Dear Telerik,

is there an API or documentation regarding writing code cleaning extensions? The 3 examples provided in the Telerik project contain some code examples, but not everything I could use.

At the moment i'm looking for a way to insert a new generated line (region in this case) before another line (first property).
Finding the property is not an issue, I think I found it. Generating a new line and inserting It though is another issue.
Could you perhaps provide a small code example?

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Zdravko
Telerik team
answered on 31 Jul 2013, 04:43 PM
Hello Ramon,

 Thanks for contacting us.
Try with the following code:

namespace JustCode.Cleaning.Example
{
 using System;
 using System.Collections.Generic;
 using System.ComponentModel.Composition;
 using System.Linq;
 using Telerik.JustCode.CommonLanguageModel;
 
 [Export(typeof(IEngineModule))]
 [Export(typeof(ICodeCleaningStepDefinition))]
 public class InsertRegionCleaningStep : CodeCleaningStepProviderModuleBase
 {
  private const string MarkerId = "InsertRegion";
  private const string CleanOptionText = "Insert Regions";
  private const int Order = 99999;
 
 
  public override IEnumerable<CodeCleaningStep> CodeCleaningSteps
  {
   get
   {
    yield return new CodeCleaningStep(LanguageNames.CSharp, Order, CleanOptionText, MarkerId);
    yield return new CodeCleaningStep(LanguageNames.VisualBasic, Order, CleanOptionText, MarkerId);
   }
  }
 
  public override void ExecuteCodeCleaningStep(CodeCleaningStep step, FileModel fileModel, CodeSpan span)
  {
   foreach (IPropertyDeclaration property in fileModel.All<IPropertyDeclaration>())
   {
    CodeSpan regionStartCodeSpan = new CodeSpan(property.CodeSpan.StartLine, 1, property.CodeSpan.StartLine, 1);
    fileModel.ReplaceTextually(regionStartCodeSpan, string.Format("#region Test {0}", Environment.NewLine));
    CodeSpan regionEndCodeSpan = new CodeSpan(property.CodeSpan.EndLine + 2, 1, property.CodeSpan.EndLine + 2, 1);
    fileModel.ReplaceTextually(regionEndCodeSpan, string.Format("#endregion {0}", Environment.NewLine));
   }
  }
 }
}

In the code above you will have to select the first property as your target code span. 

Also I want to point you to our github JustCode extensions project where you can discover some other examples.
Thanks.

Regards,
Zdravko
Telerik
Share what you think about JustCode with us, so we can help you even better! You can use the built-in feedback tool inside JustCode, our forum, or our JustCode feedback portal.
Tags
General Discussions
Asked by
RR SoftSol
Top achievements
Rank 1
Answers by
Zdravko
Telerik team
Share this question
or