Writing Visual Studio settings in an extension do not stay
Asked Answered
F

2

6

In my extension that I am writing for Visual Studio 2015 I want to change the tab size and indent size as at work we have a different setting as when I am developing for opensource project (company history dating our C period). I have written the following code in my command class:

private const string CollectionPath = @"Text Editor\CSharp";
private void MenuItemCallback(object sender, EventArgs e)
{
  var settingsManager = new ShellSettingsManager(ServiceProvider);
  var settingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
  var tabSize = settingsStore.GetInt32(CollectionPath, "Tab Size", -1);
  var indentSize = settingsStore.GetInt32(CollectionPath, "Indent Size", -1);
  if (tabSize != -1 && indentSize != -1)
  {
    settingsStore.SetInt32(CollectionPath, "Tab Size", 2);
    settingsStore.SetInt32(CollectionPath, "Indent Size", 2);
  }
}

When testing in an experimental hive it changes it when you step through the method but when you open the Options dialog it stays the original values. When you debug again the values stay the original.

What did I forget or did wrong?

Florenceflorencia answered 10/5, 2016 at 6:19 Comment(6)
I believe you need to do settingsStore.CommitChanges() or something like that. :)Selfexecuting
@Dr.Stitch that does not exist.Florenceflorencia
how about this "settingsStore.Settings.Default.Save();" ? sorry about that.Selfexecuting
@Dr.Stitch I think you are confused with application settings of Windows Forms. I want to write into the settings of Visual Studio. Not in an application.Florenceflorencia
Missing SaveChanges() or some functions like that ??Outdated
what settings of Visual Studio? you mean like properties on a project and then Settings.... if so this is held in the .config file and simply modifying this, it would then persist it for you.Cellarer
M
4

Directly access the Visual Studio options via the Properties functionality in the EnvDTE assembly.

private void ChangeTabs(DTE vs, int newTabSize, int newIndentSize)
{
    var cSharp = vs.Properties["TextEditor", "CSharp"];

    EnvDTE.Property lTabSize = cSharp.Item("TabSize");
    EnvDTE.Property lIndentSize = cSharp.Item("IndentSize");

    lTabSize.Value = newTabSize;
    lIndentSize.Value = newIndentSize;
}

private void ChangeSettings()
{
   DTE vs = (DTE)GetService(typeof(DTE)); 
   ChangeTabs(vs, 3, 3);
}

For reference: Controlling Options Settings

Mcdougall answered 12/5, 2016 at 7:16 Comment(3)
This works as this answer is a little bit the same #14616024 but the changes do not stay when you restart Visual Studio.Florenceflorencia
I have tested this with Visual Studio 2013. And the changed settings survive after you restart Visual Studio.Mcdougall
Must have been an experimental hive issue. Works now. See my answer to be complete. (Format document).Florenceflorencia
F
1

To be complete. This is the correct answer:

In the constructor you need to add

_dte2 = (DTE2) ServiceProvider.GetService(typeof (DTE));

And with the command it is like this

    _dte2.Properties["TextEditor", "CSharp"].Item("TabSize").Value = 2;
    _dte2.Properties["TextEditor", "CSharp"].Item("IndentSize").Value = 2;
    _dte2.Commands.Raise(VSConstants.CMDSETID.StandardCommandSet2K_string, (int)VSConstants.VSStd2KCmdID.FORMATDOCUMENT, null, null);

There is an issue that the changed options are overridden with the defaults when you restart Visual Studio.

Florenceflorencia answered 12/5, 2016 at 7:24 Comment(1)
You shouldn't write the values with both dte.properties and settingsstore. Using dte.properties alone is enough.Mcdougall

© 2022 - 2024 — McMap. All rights reserved.