//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SqlTools.Hosting.Utility;
using Newtonsoft.Json;
namespace Microsoft.SqlTools.CoreServices.SqlContext
{
///
/// Handles backwards compatibility of settings by checking for settings in a priority list. If a settings
/// group such as Intellisense is defined on a serialized setting it's used in the order of mssql, then sql, then
/// falls back to a default value.
///
public class CompoundToolsSettingsValues: ISqlToolsSettingsValues
{
private List priorityList = new List();
private SqlToolsSettingsValues defaultValues;
public CompoundToolsSettingsValues(ISqlToolsSettingsValues mssql, ISqlToolsSettingsValues all)
{
Validate.IsNotNull(nameof(mssql), mssql);
Validate.IsNotNull(nameof(all), all);
priorityList.Add(mssql);
priorityList.Add(all);
// Always add in a fallback which has default values to be used.
defaultValues = new SqlToolsSettingsValues(createDefaults: true);
priorityList.Add(defaultValues);
}
private T GetSettingOrDefault(Func lookup)
where T : new()
{
T value = priorityList.Select( (settings) => lookup(settings)).Where(val => val != null).FirstOrDefault();
return value != null ? value : new T();
}
///
/// Gets or sets the detailed IntelliSense settings
///
public IntelliSenseSettings IntelliSense
{
get
{
return GetSettingOrDefault((settings) => settings.IntelliSense);
}
set
{
priorityList[0].IntelliSense = value;
}
}
// ///
// /// Gets or sets the query execution settings
// ///
// public QueryExecutionSettings QueryExecutionSettings
// {
// get
// {
// return GetSettingOrDefault((settings) => settings.QueryExecutionSettings);
// }
// set
// {
// priorityList[0].QueryExecutionSettings = value;
// }
// }
// ///
// /// Gets or sets the formatter settings
// ///
// public FormatterSettings Format
// {
// get
// {
// return GetSettingOrDefault((settings) => settings.Format);
// }
// set
// {
// priorityList[0].Format = value;
// }
// }
///
/// Gets or sets the object explorer settings
///
public ObjectExplorerSettings ObjectExplorer
{
get
{
return GetSettingOrDefault((settings) => settings.ObjectExplorer);
}
set
{
priorityList[0].ObjectExplorer = value;
}
}
}
}