Code cleaning extension - API ?

2 posts, 0 answers
  1. RR SoftSol
    RR SoftSol avatar
    13 posts
    Member since:
    May 2012

    Posted 29 Jul 2013 Link to this post

    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.
  2. Zdravko
    Admin
    Zdravko avatar
    657 posts

    Posted 31 Jul 2013 Link to this post

    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.
  3. DevCraft banner
Back to Top