PageRenderTime 51ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/IronStudio/IronRubyTools/IronRubyTools/Commands/ExecuteInReplCommand.cs

https://github.com/txdv/ironruby
C# | 145 lines | 108 code | 17 blank | 20 comment | 13 complexity | 4eb047db3b9a288891cf8235888d269d MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. * ***************************************************************************/
  14. using System;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Threading;
  18. using Microsoft.IronRubyTools.Library.Repl;
  19. using Microsoft.IronRubyTools.Navigation;
  20. using Microsoft.IronRubyTools.Project;
  21. using Microsoft.IronRubyTools.Repl;
  22. using Microsoft.IronStudio;
  23. using Microsoft.IronStudio.Repl;
  24. using Microsoft.Scripting.Utils;
  25. using Microsoft.VisualStudio;
  26. using Microsoft.VisualStudio.Shell;
  27. using Microsoft.VisualStudio.Shell.Interop;
  28. using Microsoft.VisualStudio.Text.Editor;
  29. namespace Microsoft.IronRubyTools.Commands {
  30. /// <summary>
  31. /// Provides the command for starting a file or the start item of a project in the REPL window.
  32. /// </summary>
  33. internal sealed class ExecuteInReplCommand : Command {
  34. public static ExecuteInReplCommand Instance;
  35. private static Guid _replGuid = new Guid("d9b67029-3a2b-49d1-814b-db21f733c16a");
  36. public ExecuteInReplCommand() {
  37. Instance = this;
  38. }
  39. internal static ToolWindowPane/*!*/ EnsureReplWindow() {
  40. var compModel = IronRubyToolsPackage.ComponentModel;
  41. var provider = compModel.GetExtensions<IReplWindowProvider>().First();
  42. var window = (VsReplWindow)provider.FindReplWindow(_replGuid);
  43. if (window == null) {
  44. var evaluator = new RemoteRubyVsEvaluator();
  45. evaluator.Initialize();
  46. window = (VsReplWindow)provider.CreateReplWindow(
  47. evaluator,
  48. IronRubyToolsPackage.Instance.ContentType,
  49. "IronRuby Interactive",
  50. typeof(RubyLanguageInfo).GUID,
  51. _replGuid
  52. );
  53. window.UseSmartUpDown = IronRubyToolsPackage.Instance.OptionsPage.ReplSmartHistory;
  54. }
  55. return window;
  56. }
  57. internal static VsReplWindow TryGetReplWindow() {
  58. var compModel = IronRubyToolsPackage.ComponentModel;
  59. var provider = compModel.GetExtensions<IReplWindowProvider>().First();
  60. return (VsReplWindow)provider.FindReplWindow(_replGuid);
  61. }
  62. public override EventHandler BeforeQueryStatus {
  63. get {
  64. return QueryStatusMethod;
  65. }
  66. }
  67. private void QueryStatusMethod(object sender, EventArgs args) {
  68. var oleMenu = sender as OleMenuCommand;
  69. IWpfTextView textView;
  70. var rbProj = CommonPackage.GetStartupProject() as RubyProjectNode;
  71. if (rbProj != null) {
  72. // startup project, enabled in Start in REPL mode.
  73. oleMenu.Visible = true;
  74. oleMenu.Enabled = true;
  75. oleMenu.Supported = true;
  76. oleMenu.Text = "Execute Project in IronRuby Interactive";
  77. } else if ((textView = CommonPackage.GetActiveTextView()) != null &&
  78. textView.TextBuffer.ContentType == IronRubyToolsPackage.Instance.ContentType) {
  79. // enabled in Execute File mode...
  80. oleMenu.Visible = true;
  81. oleMenu.Enabled = true;
  82. oleMenu.Supported = true;
  83. oleMenu.Text = "Execute File in IronRuby Interactive";
  84. } else {
  85. oleMenu.Visible = false;
  86. oleMenu.Enabled = false;
  87. oleMenu.Supported = false;
  88. }
  89. }
  90. public override void DoCommand(object sender, EventArgs args) {
  91. var window = (IReplWindow)EnsureReplWindow();
  92. IVsWindowFrame windowFrame = (IVsWindowFrame)((ToolWindowPane)window).Frame;
  93. string filename, dir;
  94. if (!CommonPackage.TryGetStartupFileAndDirectory(out filename, out dir)) {
  95. // TODO: Error reporting
  96. return;
  97. }
  98. ErrorHandler.ThrowOnFailure(windowFrame.Show());
  99. window.Focus();
  100. ((RemoteRubyEvaluator)window.Evaluator).Reset();
  101. window.WriteLine(String.Format("Running {0}", filename));
  102. string scopeName = Path.GetFileNameWithoutExtension(filename);
  103. // now execute the current file in the REPL
  104. var engine = ((RemoteRubyEvaluator)window.Evaluator).Engine;
  105. ThreadPool.QueueUserWorkItem(
  106. _ => {
  107. try {
  108. var src = engine.CreateScriptSourceFromFile(filename, StringUtils.DefaultEncoding, Scripting.SourceCodeKind.Statements);
  109. src.Compile().Execute(((RemoteRubyEvaluator)window.Evaluator).CurrentScope);
  110. } catch (Exception e) {
  111. window.WriteLine(String.Format("Exception: {0}", e));
  112. }
  113. }
  114. );
  115. }
  116. private RubyProjectNode GetRubyStartupProject() {
  117. var buildMgr = (IVsSolutionBuildManager)Package.GetGlobalService(typeof(IVsSolutionBuildManager));
  118. IVsHierarchy hierarchy;
  119. if (ErrorHandler.Succeeded(buildMgr.get_StartupProject(out hierarchy)) && hierarchy != null) {
  120. return hierarchy.GetProject().GetRubyProject();
  121. }
  122. return null;
  123. }
  124. public override int CommandId {
  125. get { return (int)PkgCmdIDList.cmdidExecuteFileInRepl; }
  126. }
  127. }
  128. }