title: Need a better way to programmatically clear and set user settings in vscode
body: <p>So I'm writing a vscode profile management extension to both profile user settings as well as the installed extensions, but have hit a bit of a roadblock.</p>
  <p>For my extension, I need to clear out the currently set settings in the user's settings.json file, but as far as my knowledge goes there is no way through the extension API to get an exact representation of the keys in the settings.json file, which is needed to delete the settings with the <code>WorkspaceConfiguration.update(&quot;key&quot;, undefinded, ConfigurationTarget.Global)</code> function. So for that I wrote this function:</p>
  <pre class="lang-js prettyprint-override"><code>/**
   * Clears out the Global (user) settings.json of all values.
   */
  public async clearGlobalConfig() {
      // Due to limitations on updating configuration values, all possible
      // paths must be attempted for an update, from most specific to least
      const fullConfigPaths = mapObjectPaths(this.getGlobalConfig());
      // WorkspaceConfig object for doing config update operations
      const workspaceConfig = workspace.getConfiguration();
  
      // Iterate over all possible config paths
      for (const key of fullConfigPaths) {
          // Attempt to unset the value (will not unset till it matches the
          // exact path as it matches in the settings.json file)
          await workspaceConfig.update(key, undefined, ConfigurationTarget.Global);
      }
  }
  </code></pre>
  <p>However it takes about 10 - 20 seconds to run, at-least with my current settings, and the window flashes each time a setting is removed.</p>
  <p>The next issue is grabbing the settings from the settings.json to save inside of a profile config. When you do a <code>workspace.getConfguration().inspect(&quot;&quot;)?.globalValue</code>, the object that is returned is not a 1:1 with the settings.json, all of the dot notated keys are removed and the object is unflattened. Which is bad because now, again, I don't know the proper keys that need to be used with <code>WorkspaceConfiguration.update()</code> to set the setting when a profile is loaded.</p>
  <p>So I wrote this function to attempt to probe the returned object from the <code>workspace.getConfiguration()...</code> call to get a 1:1 representation of the settings.json file to save in a profile config:</p>
  <pre class="lang-js prettyprint-override"><code>/**
   * Attempts to get an object that matches the contents of the user's
   * settings.json file exactly.
   * 
   * @returns An object that should match the contents of the user 
   * settings.json
   */
  public async getGlobalSettingsFileContents(): Promise&lt;Dictionary&gt; {
      // Get the current user settings (minus this extension's settings)
      const runningConfig = this.getGlobalConfig();
      // Get all the possible full dot notated paths in the config object (from
      // most specific to least specific)
      const configPaths = mapObjectPaths(runningConfig);
  
      // Grab the WorkSpaceConfiguration object update function for reuse
      const updateFunction = workspace.getConfiguration().update;
  
      // The settings object with the proper settings.json keys to store in the
      // profile
      let actualSettings: Dictionary = {};
  
      // Previously checked config object path
      let previousPath = &quot;&quot;;
  
      // Iterate over all the possible configuration object's dot paths
      for (const configPath of configPaths) {
          // If the previous path was a valid settings key AND the current path
          // is just a less specific object path
          if (has(actualSettings, previousPath) &amp;&amp; startsWith(previousPath, configPath)) {
          continue;
          }
  
          // Get the current value before testing
          const currentValue = get(runningConfig, configPath);
  
          // Attempt to see if unsetting the settings value at this object path
          // works
          await updateFunction(configPath, undefined, ConfigurationTarget.Global);
  
          // Get the new value after the attempted unset
          const newValue = get(this.getGlobalConfig(), configPath);
  
          // If the value has changed (newValue probably being undefined)
          if (!isEqual(currentValue, newValue)) {
          // Then this was a valid settings key, save it
          actualSettings[configPath] = currentValue;
          }
  
          // Save path for checking against the next path
          previousPath = configPath;
      }
  
  
      // Get the actual settings keys that were just probed
      const actualConfigKeys = keys(actualSettings);
  
      // Iterate over the settings keys
      for (const configKey of actualConfigKeys) {
          // Attempt to re-add all the settings that were removed in the probing
          // process
          // WILL FAIL IF THERE ISN'T AN EXTENSION THAT CORRESPONDS TO THE SETTING
          await updateFunction(configKey, actualSettings[configKey], ConfigurationTarget.Global);
      }
  
      // Return what should be in the actual settings.json file
      return actualSettings;
  }
  </code></pre>
  <p>But the issue now is, after removing the settings and then trying to re-add them at the exact key they were removed from, if the corresponding extension was not installed, it will throw an error:
  <a href="https://i.stack.imgur.com/44GMC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/44GMC.png" alt="enter image description here" /></a></p>
  <p>So now my question is, is there a better way of programmatically editing the user settings.json, preferably within the vscode API and without needing to directly do filesystem operations?</p>
  <p>Link to the project: <a href="https://github.com/SLGShark6/vscode-profile-manager/tree/development" rel="nofollow noreferrer">https://github.com/SLGShark6/vscode-profile-manager/tree/development</a></p>