Skip to content

Commit

Permalink
+ Added filter to import part of syntax highlight themes (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Dec 26, 2018
1 parent 7fdd2cf commit f7926ea
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 104 deletions.
48 changes: 41 additions & 7 deletions Codist/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,24 @@ public static Config InitConfig() {
return config;
}
try {
return InternalLoadConfig(ConfigPath, false);
return InternalLoadConfig(ConfigPath, StyleFilters.None);
}
catch (Exception ex) {
Debug.WriteLine(ex.ToString());
return GetDefaultConfig();
}
}

public static void LoadConfig(string configPath, bool stylesOnly = false) {
public static void LoadConfig(string configPath, StyleFilters styleFilter = StyleFilters.None) {
if (Interlocked.Exchange(ref _LoadingConfig, 1) != 0) {
return;
}
Debug.WriteLine("Load config: " + configPath);
try {
Instance = InternalLoadConfig(configPath, stylesOnly);
Instance = InternalLoadConfig(configPath, styleFilter);
//TextEditorHelper.ResetStyleCache();
Loaded?.Invoke(Instance, EventArgs.Empty);
Updated?.Invoke(Instance, new ConfigUpdatedEventArgs(stylesOnly ? Features.SyntaxHighlight : Features.All));
Updated?.Invoke(Instance, new ConfigUpdatedEventArgs(styleFilter != StyleFilters.None ? Features.SyntaxHighlight : Features.All));
}
catch(Exception ex) {
Debug.WriteLine(ex.ToString());
Expand All @@ -102,7 +102,7 @@ public static void LoadConfig(string configPath, bool stylesOnly = false) {
}
}

static Config InternalLoadConfig(string configPath, bool stylesOnly) {
static Config InternalLoadConfig(string configPath, StyleFilters styleFilter) {
var configContent = configPath == LightTheme ? Properties.Resources.Light
: configPath == DarkTheme ? Properties.Resources.Dark
: configPath == SimpleTheme ? Properties.Resources.Simple
Expand All @@ -114,7 +114,7 @@ static Config InternalLoadConfig(string configPath, bool stylesOnly) {
args.ErrorContext.Handled = true; // ignore json error
}
});
if (stylesOnly == false) {
if (styleFilter == StyleFilters.None) {
var l = config.Labels;
for (var i = l.Count - 1; i >= 0; i--) {
if (String.IsNullOrWhiteSpace(l[i].Label)) {
Expand All @@ -132,7 +132,7 @@ static Config InternalLoadConfig(string configPath, bool stylesOnly) {
LoadStyleEntries<CSharpStyle, CSharpStyleTypes>(config.CodeStyles, removeFontNames);
LoadStyleEntries<XmlCodeStyle, XmlStyleTypes>(config.XmlCodeStyles, removeFontNames);
LoadStyleEntries<SymbolMarkerStyle, SymbolMarkerStyleTypes>(config.SymbolMarkerStyles, removeFontNames);
if (stylesOnly) {
if (styleFilter == StyleFilters.All) {
// don't override other settings if loaded from predefined themes or syntax config file
ResetCodeStyle(Instance.GeneralStyles, config.GeneralStyles);
ResetCodeStyle(Instance.CommentStyles, config.CommentStyles);
Expand All @@ -144,6 +144,18 @@ static Config InternalLoadConfig(string configPath, bool stylesOnly) {
_LastLoaded = DateTime.Now;
return Instance;
}
else if (styleFilter != StyleFilters.None) {
MergeCodeStyle(Instance.GeneralStyles, config.GeneralStyles, styleFilter);
MergeCodeStyle(Instance.CommentStyles, config.CommentStyles, styleFilter);
MergeCodeStyle(Instance.CodeStyles, config.CodeStyles, styleFilter);
MergeCodeStyle(Instance.CppStyles, config.CppStyles, styleFilter);
MergeCodeStyle(Instance.XmlCodeStyles, config.XmlCodeStyles, styleFilter);
MergeCodeStyle(Instance.SymbolMarkerStyles, config.SymbolMarkerStyles, styleFilter);
//MergeCodeStyle(Instance.MarkerSettings, config.MarkerSettings, styleFilter);
ResetCodeStyle(Instance.MarkerSettings, config.MarkerSettings);
_LastLoaded = DateTime.Now;
return Instance;
}
_LastLoaded = DateTime.Now;
Debug.WriteLine("Config loaded");
return config;
Expand Down Expand Up @@ -307,6 +319,17 @@ static void ResetCodeStyle<TStyle>(List<TStyle> source, IEnumerable<TStyle> targ
source.Clear();
source.AddRange(target);
}
static void MergeCodeStyle<TStyle>(List<TStyle> source, IEnumerable<TStyle> target, StyleFilters styleFilters) where TStyle : StyleBase {
foreach (var item in target) {
if (item.IsSet == false) {
continue;
}
var s = source.Find(i => i.Id == item.Id);
if (s != null) {
item.CopyTo(s, styleFilters);
}
}
}
internal static CommentStyle[] GetDefaultCommentStyles() {
return new CommentStyle[] {
new CommentStyle(CommentStyleTypes.Emphasis, Constants.CommentColor) { Bold = true, FontSize = 10 },
Expand Down Expand Up @@ -476,6 +499,17 @@ public enum SpecialHighlightOptions
AllBraces = DeclarationBrace | ParameterBrace | BranchBrace | LoopBrace | ResourceBrace | SpecialPunctuation
}

[Flags]
public enum StyleFilters
{
None,
Color = 1,
FontFamily = 1 << 1,
FontSize = 1 << 2,
FontStyle = 1 << 3,
All = Color | FontFamily | FontSize | FontStyle
}

[Flags]
public enum MarkerOptions
{
Expand Down
Loading

0 comments on commit f7926ea

Please sign in to comment.