Getting/setting Track Changes setting property value trough DTE in Visual Studio 2019

0

I have a Visual Studio extension that gets the value and then sets a value to the Text Editor -> General -> Track Changes setting in the Options dialog.

A code that was working fine with Visual Studio 2012-2017:

DTE vsEnvironment = (DTE)GetService(typeof(DTE));
Property trackChangesProperty = vsEnvironment.Properties["TextEditor", "General"].Item("TrackChanges");

is throwing a COMException with the following message: "Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))" at EnvDTE._DTE.get_Properties(String Category, String Page) in Visual Studio 2019.

Apparently the setting is moved, so I, trying to get the new location, exported the settings to file in Visual Studio 2017 and 2019, and compared the results:

  • Visual Studio 2017:

    <ToolsOptionsCategory name="TextEditor" RegisteredName="TextEditor">
    <ToolsOptionsSubCategory name="General" RegisteredName="General" PackageName="Text Management Package">
        <PropertyValue name="TrackChanges">true</PropertyValue>
    </ToolsOptionsSubCategory>
    

  • Visual Studio 2019:

    <Category name="Text Editor_General" Category="{c178af61-531a-46f0-bd57-102d9e42c711}" Package="{e269b994-ef71-4ce0-8bcd-581c217372e8}" RegisteredName="Text Editor_General" PackageName="Microsoft.VisualStudio.Editor.Implementation.EditorPackage">
    <PropertyValue name="TrackChanges">true</PropertyValue>
    

I am still not sure how to use the information, as the indexer of the DTE.Properties accepts two parameters: Category and Page. I already tried the following:

        vsEnvironment.Properties["TextEditor", null].Item("TrackChanges");
        vsEnvironment.Properties["TextEditor", string.Empty].Item("TrackChanges");
        vsEnvironment.Properties["Text Editor_General", null].Item("TrackChanges");
        vsEnvironment.Properties["Text Editor_General", string.Empty].Item("TrackChanges");

but without success.

visual-studio
envdte
visual-studio-2019
asked on Stack Overflow Dec 7, 2018 by Borislav Ivanov • edited Dec 10, 2018 by Joy

1 Answer

1

The Microsoft staff clarified that any of the following approaches can be used:

  • Use IVsTextManager3.SetUserPreferences3(). Available in older versions of Visual Studio as well (I tested with Visual Studio 2012 - 2019), but pretty ugly API:

    IVsTextManager3 textManager = this.GetService(typeof(VsTextManagerClass)) as IVsTextManager3;
    
    VIEWPREFERENCES3[] viewPreferences3Array = new VIEWPREFERENCES3[1];
    FONTCOLORPREFERENCES2[] fontColorPreferences2Array = new FONTCOLORPREFERENCES2[1];
    FRAMEPREFERENCES2[] framePreferences2Array = new FRAMEPREFERENCES2[1];
    LANGPREFERENCES2[] langPreferences2Array = new LANGPREFERENCES2[1];
    
    textManager.GetUserPreferences3(viewPreferences3Array, framePreferences2Array, langPreferences2Array, fontColorPreferences2Array);
    
    VIEWPREFERENCES3 viewPreferences3 = viewPreferences3Array[0];
    viewPreferences3.fTrackChanges = 0;
    textManager.SetUserPreferences3(new VIEWPREFERENCES3[] { viewPreferences3 }, framePreferences2Array, langPreferences2Array, fontColorPreferences2Array);
    
  • Use IEditorOptionsFactoryService MEF service. This API is added in Visual Studio 2019 and later:

    <IEditorOptionsFactoryService>.GlobalOptions.GetOptionValue<bool>(DefaultTextViewHostOptions.ChangeTrackingId);
    <IEditorOptionsFactoryService>.GlobalOptions.SetOptionValue<bool>(DefaultTextViewHostOptions.ChangeTrackingId, <true/false>);
    
answered on Stack Overflow May 3, 2019 by Borislav Ivanov

User contributions licensed under CC BY-SA 3.0