You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.6 KiB

15 years ago
  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.Decompiler;
  21. using ICSharpCode.ILSpy.Options;
  22. using ICSharpCode.ILSpyX;
  23. using DecompilerSettings = ICSharpCode.ILSpyX.Settings.DecompilerSettings;
  24. namespace ICSharpCode.ILSpy
  25. {
  26. /// <summary>
  27. /// Options passed to the decompiler.
  28. /// </summary>
  29. public class DecompilationOptions
  30. {
  31. /// <summary>
  32. /// Gets whether a full decompilation (all members recursively) is desired.
  33. /// If this option is false, language bindings are allowed to show the only headers of the decompiled element's children.
  34. /// </summary>
  35. public bool FullDecompilation { get; set; }
  36. /// <summary>
  37. /// Gets/Sets the directory into which the project is saved.
  38. /// </summary>
  39. public string SaveAsProjectDirectory { get; set; }
  40. /// <summary>
  41. /// Gets/sets whether invalid identifiers should be escaped (and therefore the code be made compilable).
  42. /// This setting is ignored in case <see cref="SaveAsProjectDirectory"/> is set.
  43. /// </summary>
  44. public bool EscapeInvalidIdentifiers { get; set; }
  45. /// <summary>
  46. /// Gets the cancellation token that is used to abort the decompiler.
  47. /// </summary>
  48. /// <remarks>
  49. /// Decompilers should regularly call <c>options.CancellationToken.ThrowIfCancellationRequested();</c>
  50. /// to allow for cooperative cancellation of the decompilation task.
  51. /// </remarks>
  52. public CancellationToken CancellationToken { get; set; }
  53. /// <summary>
  54. /// Gets the progress reporter.
  55. /// </summary>
  56. /// <remarks>
  57. /// If decompilers do not implement progress reporting, an indeterminate wait bar is displayed.
  58. /// </remarks>
  59. public IProgress<DecompilationProgress> Progress { get; set; }
  60. /// <summary>
  61. /// Gets the settings for the decompiler.
  62. /// </summary>
  63. public DecompilerSettings DecompilerSettings { get; private set; }
  64. /// <summary>
  65. /// Gets/sets an optional state of a decompiler text view.
  66. /// </summary>
  67. /// <remarks>
  68. /// This state is used to restore test view's state when decompilation is started by Go Back/Forward action.
  69. /// </remarks>
  70. public TextView.DecompilerTextViewState TextViewState { get; set; }
  71. /// <summary>
  72. /// Used internally for debugging.
  73. /// </summary>
  74. internal int StepLimit = int.MaxValue;
  75. internal bool IsDebug = false;
  76. public DecompilationOptions(LanguageVersion version, DecompilerSettings settings, DisplaySettings displaySettings)
  77. {
  78. if (!Enum.TryParse(version?.Version, out Decompiler.CSharp.LanguageVersion languageVersion))
  79. languageVersion = Decompiler.CSharp.LanguageVersion.Latest;
  80. var newSettings = this.DecompilerSettings = settings.Clone();
  81. newSettings.SetLanguageVersion(languageVersion);
  82. newSettings.ExpandMemberDefinitions = displaySettings.ExpandMemberDefinitions;
  83. newSettings.ExpandUsingDeclarations = displaySettings.ExpandUsingDeclarations;
  84. newSettings.FoldBraces = displaySettings.FoldBraces;
  85. newSettings.ShowDebugInfo = displaySettings.ShowDebugInfo;
  86. newSettings.CSharpFormattingOptions.IndentationString = GetIndentationString(displaySettings);
  87. }
  88. private string GetIndentationString(DisplaySettings displaySettings)
  89. {
  90. if (displaySettings.IndentationUseTabs)
  91. {
  92. int numberOfTabs = displaySettings.IndentationSize / displaySettings.IndentationTabSize;
  93. int numberOfSpaces = displaySettings.IndentationSize % displaySettings.IndentationTabSize;
  94. return new string('\t', numberOfTabs) + new string(' ', numberOfSpaces);
  95. }
  96. return new string(' ', displaySettings.IndentationSize);
  97. }
  98. }
  99. }