locked
Dynamic Tab Size RRS feed

  • Question

  • I need to set tab size dynamically, according to some condition. Assume that I need to set the first tab size = 10.

    [Export(typeof(ITextParagraphPropertiesFactoryService))] 
    [ContentType("C/C++")] 
    public class MyParagraphPropertiesFactoryService : ITextParagraphPropertiesFactoryService
    {
     public TextParagraphProperties Create(IFormattedLineSource formattedLineSource, TextFormattingRunProperties textProperties,
     IMappingSpan line, IMappingPoint lineStart, int lineSegment) 
     {
      return new MyParagraphProperties(textProperties, formattedLineSource, 10); 
     }
    }
    public class MyParagraphProperties : TextFormattingParagraphProperties
    {
     public MyParagraphProperties(TextFormattingRunProperties textProperties, IFormattedLineSource formattedLineSource, int firstTabSize) 
     :
    base(textProperties, formattedLineSource.ColumnWidth * (double)formattedLineSource.TabSize) 
     {
      ColumnWidth = formattedLineSource.ColumnWidth;
      FirstTabSize = firstTabSize;
     }
     public override IList<TextTabProperties> Tabs 
     {
      get
      {
        double offset = ColumnWidth * (double)FirstTabSize + 1; 
        return new List<TextTabProperties> { 
        new TextTabProperties(TextTabAlignment.Left, offset, 0, 0) 
       };
      }
     }
     
     public double ColumnWidth { get; set; } 
     public int FirstTabSize { get; set; } 
    }

     

    Is there some way to avoid this issue?

    This code works fine, but there is one issue: when I have a line of text with tab in the beginning and press enter, the cursor is moved to position 4 of the new line (according to Tools->Options settings) and only pressing of any symbol moves it to position 10. So, "auto indenting" which adds tabs to the newly created line doesn't call MEF extension to ask the size of tab.

     

    Monday, October 25, 2010 7:02 AM

All replies

  • Hi VadimLuk,

     

    Thanks for your post.

    I suppose the indenting style is set as default,

    could you please try vsIndentStyle.vsIndentStyleSmart.

    For more information, http://msdn.microsoft.com/en-us/library/envdte.vsindentstyle.aspx

    And here is a thread tells how to turn on/off indent smart

    http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/cfb59221-9c2e-4c6b-9792-c027a50e9cea

    Hope this can help.

     

    Best Regards,

    Ziwei Chen

    Wednesday, October 27, 2010 3:45 AM
  • The problem exists for both vsIndentStyleDefault and vsIndentStyleSmart indenting styles.

    Wednesday, October 27, 2010 6:10 AM
  • As Victor suggested, you can turn off smart indentation to prevent the situation. However, if you want to keep smart indentation, you can provide your own custom indent provider to inform the editor how many spaces by default should be inserted at the beginning of a new line.

    To do so, you'll need to export an ISmartIndentProvider. Please take a look at http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.editor.ismartindentprovider.aspx

    The reason why this happens is because when a new line is inserted, the editor does not actually put any data in the buffer and therefore the formatting code does not exercise your custom tab provider. The caret is rather placed in virtual space until the user enter types a character (at which point 4 spaces or 1 tab are inserted).

    Please let us know if you run into any issues.

    Thanks,

    -Ameen

    Thursday, December 30, 2010 6:18 PM
  • I use "Block" indenting. I've tried to export an ISmartIndentProvider, but it isn't called for block indenting.
    Friday, January 14, 2011 9:28 AM