Skip to content

Commit

Permalink
Remove redundant language settings related code
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Aug 18, 2024
1 parent ebda969 commit 347b89c
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 97 deletions.
24 changes: 6 additions & 18 deletions ILSpy/Analyzers/AnalyzerSearchTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,17 @@ AnalyzerTreeNode SymbolTreeNodeFactory(ISymbol symbol)
switch (symbol)
{
case IModule module:
return new AnalyzedModuleTreeNode(module) {
Language = this.Language
};
return new AnalyzedModuleTreeNode(module) { };
case ITypeDefinition td:
return new AnalyzedTypeTreeNode(td) {
Language = this.Language
};
return new AnalyzedTypeTreeNode(td) { };
case IField fd:
return new AnalyzedFieldTreeNode(fd) {
Language = this.Language
};
return new AnalyzedFieldTreeNode(fd) { };
case IMethod md:
return new AnalyzedMethodTreeNode(md) {
Language = this.Language
};
return new AnalyzedMethodTreeNode(md) { };
case IProperty pd:
return new AnalyzedPropertyTreeNode(pd) {
Language = this.Language
};
return new AnalyzedPropertyTreeNode(pd) { };
case IEvent ed:
return new AnalyzedEventTreeNode(ed) {
Language = this.Language
};
return new AnalyzedEventTreeNode(ed) { };
default:
throw new ArgumentOutOfRangeException(nameof(symbol), $"Symbol {symbol.GetType().FullName} is not supported.");
}
Expand Down
29 changes: 3 additions & 26 deletions ILSpy/Analyzers/AnalyzerTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,20 @@
// DEALINGS IN THE SOFTWARE.

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.TreeView;

namespace ICSharpCode.ILSpy.Analyzers
{
public abstract class AnalyzerTreeNode : SharpTreeNode
{
private Language language;

public Language Language {
get { return language; }
set {
if (language != value)
{
language = value;
foreach (var child in this.Children.OfType<AnalyzerTreeNode>())
child.Language = value;
}
}
}
public Language Language => SettingsService.Instance.SessionSettings.LanguageSettings.Language;

public override bool CanDelete()
{
return Parent != null && Parent.IsRoot;
return Parent is { IsRoot: true };
}

public override void DeleteCore()
Expand All @@ -56,16 +43,6 @@ public override void Delete()
DeleteCore();
}

internal protected override void OnChildrenChanged(NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (AnalyzerTreeNode a in e.NewItems.OfType<AnalyzerTreeNode>())
a.Language = this.Language;
}
base.OnChildrenChanged(e);
}

/// <summary>
/// Handles changes to the assembly list.
/// </summary>
Expand Down
20 changes: 2 additions & 18 deletions ILSpy/Analyzers/AnalyzerTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,12 @@ public class AnalyzerTreeView : SharpTreeView
public AnalyzerTreeView()
{
this.ShowRoot = false;
this.BorderThickness = new Thickness(0);
this.BorderThickness = new();
this.Root = new AnalyzerRootNode();
ContextMenuProvider.Add(this);
MessageBus<CurrentAssemblyListChangedEventArgs>.Subscribers += (sender, e) => CurrentAssemblyList_Changed(sender, e);
MessageBus<DockWorkspaceActiveTabPageChangedEventArgs>.Subscribers += DockWorkspace_ActiveTabPageChanged;
MessageBus<LanguageSettingsChangedEventArgs>.Subscribers += (sender, e) => LanguageSettings_PropertyChanged(sender, e);
}

private void DockWorkspace_ActiveTabPageChanged(object sender, EventArgs e)
{
this.Root ??= new AnalyzerRootNode { Language = MainWindow.Instance.CurrentLanguage };
}

private void LanguageSettings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "Language":
case "LanguageVersion":
((AnalyzerRootNode)this.Root).Language = MainWindow.Instance.CurrentLanguage;
break;
}
}

void CurrentAssemblyList_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
Expand Down
1 change: 0 additions & 1 deletion ILSpy/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public App()
// Add data templates registered via MEF.
Resources.MergedDictionaries.Add(DataTemplateManager.CreateDynamicDataTemplates(ExportProvider));

Languages.Initialize(ExportProvider);
EventManager.RegisterClassHandler(typeof(Window),
Hyperlink.RequestNavigateEvent,
new RequestNavigateEventHandler(Window_RequestNavigate));
Expand Down
3 changes: 2 additions & 1 deletion ILSpy/Commands/DecompileAllCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using ICSharpCode.Decompiler;
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpyX;

using TomsToolbox.Essentials;
Expand Down Expand Up @@ -94,7 +95,7 @@ sealed class Decompile100TimesCommand : SimpleCommand
public override void Execute(object parameter)
{
const int numRuns = 100;
var language = MainWindow.Instance.CurrentLanguage;
var language = SettingsService.Instance.SessionSettings.LanguageSettings.Language;
var nodes = MainWindow.Instance.SelectedNodes.ToArray();
var options = MainWindow.Instance.CreateDecompilationOptions();
Docking.DockWorkspace.Instance.RunWithCancellation(ct => Task<AvalonEditTextOutput>.Factory.StartNew(() => {
Expand Down
4 changes: 3 additions & 1 deletion ILSpy/Commands/SaveCodeContextMenuEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
using ICSharpCode.ILSpyX.TreeView;
using System.ComponentModel.Composition;

using ICSharpCode.ILSpy.Util;

namespace ICSharpCode.ILSpy.TextView
{
[ExportContextMenuEntry(Header = nameof(Resources._SaveCode), Category = nameof(Resources.Save), Icon = "Images/Save")]
Expand Down Expand Up @@ -60,7 +62,7 @@ public static bool CanExecute(IReadOnlyList<SharpTreeNode> selectedNodes)

public static void Execute(IReadOnlyList<SharpTreeNode> selectedNodes)
{
var currentLanguage = MainWindow.Instance.CurrentLanguage;
var currentLanguage = SettingsService.Instance.SessionSettings.LanguageSettings.Language;
var tabPage = Docking.DockWorkspace.Instance.ActiveTabPage;
tabPage.ShowTextView(textView => {
if (selectedNodes.Count == 1 && selectedNodes[0] is ILSpyTreeNode singleSelection)
Expand Down
12 changes: 6 additions & 6 deletions ILSpy/Controls/TreeView/SharpTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ static SharpTreeView()
RegisterCommands();
}

public static ResourceKey DefaultItemContainerStyleKey { get; private set; }
public static ResourceKey DefaultItemContainerStyleKey { get; }

public SharpTreeView()
{
SetResourceReference(ItemContainerStyleProperty, DefaultItemContainerStyleKey);
}

public static readonly DependencyProperty RootProperty =
DependencyProperty.Register("Root", typeof(SharpTreeNode), typeof(SharpTreeView));
DependencyProperty.Register(nameof(Root), typeof(SharpTreeNode), typeof(SharpTreeView));

public SharpTreeNode Root {
get { return (SharpTreeNode)GetValue(RootProperty); }
set { SetValue(RootProperty, value); }
}

public static readonly DependencyProperty ShowRootProperty =
DependencyProperty.Register("ShowRoot", typeof(bool), typeof(SharpTreeView),
DependencyProperty.Register(nameof(ShowRoot), typeof(bool), typeof(SharpTreeView),
new FrameworkPropertyMetadata(true));

public bool ShowRoot {
Expand All @@ -79,7 +79,7 @@ public bool ShowRoot {
}

public static readonly DependencyProperty ShowRootExpanderProperty =
DependencyProperty.Register("ShowRootExpander", typeof(bool), typeof(SharpTreeView),
DependencyProperty.Register(nameof(ShowRootExpander), typeof(bool), typeof(SharpTreeView),
new FrameworkPropertyMetadata(false));

public bool ShowRootExpander {
Expand All @@ -88,15 +88,15 @@ public bool ShowRootExpander {
}

public static readonly DependencyProperty AllowDropOrderProperty =
DependencyProperty.Register("AllowDropOrder", typeof(bool), typeof(SharpTreeView));
DependencyProperty.Register(nameof(AllowDropOrder), typeof(bool), typeof(SharpTreeView));

public bool AllowDropOrder {
get { return (bool)GetValue(AllowDropOrderProperty); }
set { SetValue(AllowDropOrderProperty, value); }
}

public static readonly DependencyProperty ShowLinesProperty =
DependencyProperty.Register("ShowLines", typeof(bool), typeof(SharpTreeView),
DependencyProperty.Register(nameof(ShowLines), typeof(bool), typeof(SharpTreeView),
new FrameworkPropertyMetadata(true));

public bool ShowLines {
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/LanguageSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class LanguageSettings : INotifyPropertyChanged
public LanguageSettings(XElement element)
{
this.ShowApiLevel = (ApiVisibility?)(int?)element.Element("ShowAPILevel") ?? ApiVisibility.PublicAndInternal;
this.Language = Languages.GetLanguage((string)element.Element("Language"));
this.Language = Languages.GetLanguage((string)element.Element("Language")) ?? Languages.AllLanguages.First();
this.LanguageVersion = Language.LanguageVersions.FirstOrDefault(v => v.Version == (string)element.Element("LanguageVersion"))
?? Language.LanguageVersions.LastOrDefault();
}
Expand Down
24 changes: 7 additions & 17 deletions ILSpy/Languages/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,31 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System.Collections.Generic;
using System;
using System.Collections.ObjectModel;
using System.Linq;

using Microsoft.VisualStudio.Composition;

using TomsToolbox.Composition;

namespace ICSharpCode.ILSpy
{
public static class Languages
{
// Start with a dummy list with an IL entry so that crashes
// in Initialize() (e.g. due to invalid plugins) don't lead to
// confusing follow-up errors in GetLanguage().
private static ReadOnlyCollection<Language> allLanguages = new ReadOnlyCollection<Language>(
new Language[] { new ILLanguage() });

/// <summary>
/// A list of all languages.
/// </summary>
public static ReadOnlyCollection<Language> AllLanguages {
get { return allLanguages; }
}
public static ReadOnlyCollection<Language> AllLanguages { get; } = Initialize(App.ExportProvider);

internal static void Initialize(IExportProvider ep)
static ReadOnlyCollection<Language> Initialize(IExportProvider ep)
{
List<Language> languages = new List<Language>();
languages.AddRange(ep.GetExportedValues<Language>());
languages.Sort((a, b) => a.Name.CompareTo(b.Name));
var languages = ep.GetExportedValues<Language>().ToList();

languages.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));
#if DEBUG
languages.AddRange(ILAstLanguage.GetDebugLanguages());
languages.AddRange(CSharpLanguage.GetDebugLanguages());
#endif
allLanguages = languages.AsReadOnly();
return languages.AsReadOnly();
}

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions ILSpy/TextView/DecompilerTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
using ICSharpCode.ILSpy.Options;
using ICSharpCode.ILSpy.Themes;
using ICSharpCode.ILSpy.TreeNodes;
using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpy.ViewModels;
using ICSharpCode.ILSpyX;

Expand Down Expand Up @@ -392,7 +393,7 @@ void ToolTipClosed(object? sender, EventArgs e)
if (segment.Reference is ICSharpCode.Decompiler.Disassembler.OpCodeInfo code)
{
XmlDocumentationProvider docProvider = XmlDocLoader.MscorlibDocumentation;
DocumentationUIBuilder renderer = new DocumentationUIBuilder(new CSharpAmbience(), MainWindow.Instance.CurrentLanguage.SyntaxHighlighting);
DocumentationUIBuilder renderer = new DocumentationUIBuilder(new CSharpAmbience(), SettingsService.Instance.SessionSettings.LanguageSettings.Language.SyntaxHighlighting);
renderer.AddSignatureBlock($"{code.Name} (0x{code.Code:x})");
if (docProvider != null)
{
Expand Down Expand Up @@ -442,7 +443,7 @@ void ToolTipClosed(object? sender, EventArgs e)

static FlowDocument? CreateTooltipForEntity(IEntity resolved)
{
Language currentLanguage = MainWindow.Instance.CurrentLanguage;
Language currentLanguage = SettingsService.Instance.SessionSettings.LanguageSettings.Language;
DocumentationUIBuilder renderer = new DocumentationUIBuilder(new CSharpAmbience(), currentLanguage.SyntaxHighlighting);
RichText richText = currentLanguage.GetRichTextTooltip(resolved);
if (richText == null)
Expand Down Expand Up @@ -533,7 +534,7 @@ void HighlightBrackets(object? sender, EventArgs e)
{
if (MainWindow.Instance.CurrentDisplaySettings.HighlightMatchingBraces)
{
var result = MainWindow.Instance.CurrentLanguage.BracketSearcher.SearchBracket(textEditor.Document, textEditor.CaretOffset);
var result = SettingsService.Instance.SessionSettings.LanguageSettings.Language.BracketSearcher.SearchBracket(textEditor.Document, textEditor.CaretOffset);
bracketHighlightRenderer.SetHighlight(result);
}
else
Expand Down
4 changes: 1 addition & 3 deletions ILSpy/TreeNodes/ILSpyTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public ILSpyTreeNode()

LanguageSettings LanguageSettings => SettingsService.Instance.SessionSettings.LanguageSettings;

public Language Language {
get { return LanguageSettings.Language ?? Languages.AllLanguages[0]; }
}
public Language Language => LanguageSettings.Language;

public virtual FilterResult Filter(LanguageSettings settings)
{
Expand Down
4 changes: 2 additions & 2 deletions ILSpy/Views/DebugSteps.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public DebugSteps()
MainWindow.Instance.SelectionChanged += SelectionChanged;
writingOptions.PropertyChanged += WritingOptions_PropertyChanged;

if (MainWindow.Instance.CurrentLanguage is ILAstLanguage l)
if (SettingsService.Instance.SessionSettings.LanguageSettings.Language is ILAstLanguage l)
{
l.StepperUpdated += ILAstStepperUpdated;
language = l;
Expand Down Expand Up @@ -70,7 +70,7 @@ private void LanguageSettings_PropertyChanged(object sender, System.ComponentMod
{
language.StepperUpdated -= ILAstStepperUpdated;
}
if (MainWindow.Instance.CurrentLanguage is ILAstLanguage l)
if (SettingsService.Instance.SessionSettings.LanguageSettings.Language is ILAstLanguage l)
{
l.StepperUpdated += ILAstStepperUpdated;
language = l;
Expand Down

0 comments on commit 347b89c

Please sign in to comment.