How to handle first tab size on formatting?

风流意气都作罢 提交于 2019-12-12 00:34:24

问题


I create an extension, that format vs editor tabs in custom ways, using ITextParagraphPropertiesFactoryService class. Everything works just fine, expect the fact, that when user enter new line, ITextParagraphPropertiesFactoryService doesnt affect to the new line

For simplifying the problem, I create a new MEF project, add a format provider like this

[Export(typeof(ITextParagraphPropertiesFactoryService))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal class ElasticTabstopsProvider : ITextParagraphPropertiesFactoryService
{
    /// <summary>
    /// Creates an ElasticTabstopsFormatters for
    /// the provided configuration.
    /// </summary>
    public TextParagraphProperties Create(IFormattedLineSource formattedLineSource, TextFormattingRunProperties textProperties, 
        IMappingSpan line, IMappingPoint lineStart, int lineSegment)
    {
        return new TextFormattingParagraphProperties(textProperties, 1);
    }
}

And it changes all tabs width from my editor to 1. Great! This is what I want. But now when I press Enter(new line) new cursor is set under Main, however I expect tab widths to be 1.

After I start typing it goes to expected position.

The question is, how can I set new line empty line tab size? I try to override ISmartIndentProvider, but seems vs ignore that value.

Debuger stops on breakpoint in method

int? GetDesiredIndentation(ITextSnapshotLine currentLine)

of ISmartIndent, but indent stays the same no matter what value I return...


回答1:


There are at least two reasons why your ISmartIndentProvider's indent is being ignored:

First, there are lots of places with the current C# and VB language services where we explicitly set the caret position in response to certain keypresses. Enter is one of them. It's quite possible that in your scenario, we're explicitly setting the position. Short of disabling smart indenting in Tools > Options, there's nothing you can do to override that. Since you said you're getting a debugger hit in your ISmartIndentProvider, that's probably the issue here.

Second, if you're trying to define a ISmartIndentProvider for content type "text", yours won't get called if there is a language-specific provider. There's also another provider for "text" already (which calls the shimmed old language services) which might win over yours anyways.

To be honest, if you're trying to do something fancy where you don't want automatic indenting, then you really should just turn it off to ensure it's not getting in your way.



来源:https://stackoverflow.com/questions/19104387/how-to-handle-first-tab-size-on-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!