SixPairs

June 30, 2014

Simplest LineTransformSource

Filed under: VS Editor — Ceyhun Ciper @ 17:44
namespace Microsoft.VisualStudio.Text.Formatting
{
  using Microsoft.VisualStudio.Text.Editor;
  using Microsoft.VisualStudio.Utilities;
  using System.ComponentModel.Composition;

  [Export(typeof(ILineTransformSourceProvider))]
  [ContentType("text")]
  [TextViewRole(PredefinedTextViewRoles.Interactive)]
  public partial class LineTransformSource : ILineTransformSourceProvider, ILineTransformSource
  {
    IWpfTextView view;
    public ILineTransformSource Create(IWpfTextView textView)
    {
      this.view = textView;
      return this;
    }

    public LineTransform GetLineTransform(ITextViewLine line, double yPosition, Microsoft.VisualStudio.Text.Editor.ViewRelativePosition placement)
    {
      return GetLineTransform(line);
    }
  }
}

namespace Microsoft.VisualStudio.Text.Formatting
{
  public partial class LineTransformSource
  {
    public LineTransform GetLineTransform(ITextViewLine line)
    {
      return line.DefaultLineTransform;
    }
  }
}

Exercises

  1. Make all lines double-spaced.
    namespace Microsoft.VisualStudio.Text.Formatting
    {
      public partial class LineTransformSource
      {
        public LineTransform GetLineTransform(ITextViewLine line)
        {
          var currentTransform = line.LineTransform;
    
          // line.Height would not work here!
          var transform = new LineTransform(currentTransform.TopSpace, line.TextHeight,
            currentTransform.VerticalScale, currentTransform.Right);
    
          return transform;
        }
      }
    }
    

    Another version:

    namespace Microsoft.VisualStudio.Text.Formatting
    {
      public partial class LineTransformSource
      {
        public LineTransform GetLineTransform(ITextViewLine line)
        {
          var currentTransform = line.LineTransform;
    
          if (line.Change == TextViewLineChange.NewOrReformatted)
            return new LineTransform(currentTransform.TopSpace, line.TextHeight,
              currentTransform.VerticalScale, currentTransform.Right);
          else
            return line.DefaultLineTransform;
        }
      }
    }
    
  2. Sadece brace olan satirlari kucult.
    namespace Microsoft.VisualStudio.Text.Formatting
    {
      public partial class LineTransformSource
      {
        public LineTransform GetLineTransform(ITextViewLine line)
        {
          var s = line.Extent.GetText().Trim();
    
          if (line.Change == TextViewLineChange.NewOrReformatted &&
            (s == "{" || s == "}" || s == "};"))
          {
            return new LineTransform(0.5);
          }
    
          return line.DefaultLineTransform;
        }
      }
    }
    
  3. Collapse empty lines.
    namespace Microsoft.VisualStudio.Text.Formatting
    {
      public partial class LineTransformSource
      {
        public LineTransform GetLineTransform(ITextViewLine line)
        {
          if (string.IsNullOrWhiteSpace(line.Extent.GetText()))
            return new LineTransform(0.001);
          else
            return line.DefaultLineTransform;
        }
      }
    }
    
  4. Comment’li olan satirlari hide et.
    namespace Microsoft.VisualStudio.Text.Formatting
    {
      public partial class LineTransformSource
      {
        public LineTransform GetLineTransform(ITextViewLine line)
        {
          if (System.Text.RegularExpressions.Regex.IsMatch(line.Extent.GetText(), @"\s*//"))
            return new LineTransform(0.001);
          else
            return line.DefaultLineTransform;
        }
      }
    }
    
  5. Selection olan satirlari 1.61 (golden ratio) scale et.
    
    
  6. Caret’in oldugu line’i 1.61 (golden ratio) scale et.
    
    
  7. Satirlarin en sagina line number ekle, ama her number ayni hizada olsun.
  8. Ilk satirdan once copyright goster.
  9. Son satirdan sonra file istatistiklerini goster.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Create a free website or blog at WordPress.com.