/ILSpy/DecompilationOptions.cs

http://github.com/icsharpcode/ILSpy · C# · 108 lines · 46 code · 12 blank · 50 comment · 2 complexity · b60fd4d61e8bc7db6f48d926ad3a8c3c MD5 · raw file

  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Threading;
  20. using ICSharpCode.ILSpy.Options;
  21. namespace ICSharpCode.ILSpy
  22. {
  23. /// <summary>
  24. /// Options passed to the decompiler.
  25. /// </summary>
  26. public class DecompilationOptions
  27. {
  28. /// <summary>
  29. /// Gets whether a full decompilation (all members recursively) is desired.
  30. /// If this option is false, language bindings are allowed to show the only headers of the decompiled element's children.
  31. /// </summary>
  32. public bool FullDecompilation { get; set; }
  33. /// <summary>
  34. /// Gets/Sets the directory into which the project is saved.
  35. /// </summary>
  36. public string SaveAsProjectDirectory { get; set; }
  37. /// <summary>
  38. /// Gets/sets whether invalid identifiers should be escaped (and therefore the code be made compilable).
  39. /// This setting is ignored in case <see cref="SaveAsProjectDirectory"/> is set.
  40. /// </summary>
  41. public bool EscapeInvalidIdentifiers { get; set; }
  42. /// <summary>
  43. /// Gets the cancellation token that is used to abort the decompiler.
  44. /// </summary>
  45. /// <remarks>
  46. /// Decompilers should regularly call <c>options.CancellationToken.ThrowIfCancellationRequested();</c>
  47. /// to allow for cooperative cancellation of the decompilation task.
  48. /// </remarks>
  49. public CancellationToken CancellationToken { get; set; }
  50. /// <summary>
  51. /// Gets the settings for the decompiler.
  52. /// </summary>
  53. public Decompiler.DecompilerSettings DecompilerSettings { get; private set; }
  54. /// <summary>
  55. /// Gets/sets an optional state of a decompiler text view.
  56. /// </summary>
  57. /// <remarks>
  58. /// This state is used to restore test view's state when decompilation is started by Go Back/Forward action.
  59. /// </remarks>
  60. public TextView.DecompilerTextViewState TextViewState { get; set; }
  61. /// <summary>
  62. /// Used internally for debugging.
  63. /// </summary>
  64. internal int StepLimit = int.MaxValue;
  65. internal bool IsDebug = false;
  66. public DecompilationOptions()
  67. : this(MainWindow.Instance.CurrentLanguageVersion, DecompilerSettingsPanel.CurrentDecompilerSettings, DisplaySettingsPanel.CurrentDisplaySettings)
  68. {
  69. }
  70. public DecompilationOptions(LanguageVersion version)
  71. : this(version, DecompilerSettingsPanel.CurrentDecompilerSettings, DisplaySettingsPanel.CurrentDisplaySettings)
  72. {
  73. }
  74. public DecompilationOptions(LanguageVersion version, Decompiler.DecompilerSettings settings, Options.DisplaySettings displaySettings)
  75. {
  76. if (!Enum.TryParse(version?.Version, out Decompiler.CSharp.LanguageVersion languageVersion))
  77. languageVersion = Decompiler.CSharp.LanguageVersion.Latest;
  78. var newSettings = this.DecompilerSettings = settings.Clone();
  79. newSettings.SetLanguageVersion(languageVersion);
  80. newSettings.ExpandMemberDefinitions = displaySettings.ExpandMemberDefinitions;
  81. newSettings.ExpandUsingDeclarations = displaySettings.ExpandUsingDeclarations;
  82. newSettings.FoldBraces = displaySettings.FoldBraces;
  83. newSettings.ShowDebugInfo = displaySettings.ShowDebugInfo;
  84. newSettings.CSharpFormattingOptions.IndentationString = GetIndentationString(displaySettings);
  85. }
  86. private string GetIndentationString(DisplaySettings displaySettings)
  87. {
  88. if (displaySettings.IndentationUseTabs) {
  89. int numberOfTabs = displaySettings.IndentationSize / displaySettings.IndentationTabSize;
  90. int numberOfSpaces = displaySettings.IndentationSize % displaySettings.IndentationTabSize;
  91. return new string('\t', numberOfTabs) + new string(' ', numberOfSpaces);
  92. }
  93. return new string(' ', displaySettings.IndentationSize);
  94. }
  95. }
  96. }