예제 #1
0
        public override void Execute(IDataContext context, DelegateExecute nextExecute)
        {
            var solution = context.GetData(ProjectModel.DataContext.DataConstants.SOLUTION);

            if (solution == null)
            {
                return;
            }

            var textControl = context.GetData(TextControl.DataContext.DataConstants.TEXT_CONTROL);

            if (textControl == null)
            {
                return;
            }

            var shellLocks          = solution.GetComponent <IShellLocks>();
            var lifetimeDefinition  = Lifetimes.Define(EternalLifetime.Instance, "ZenCodingWrap");
            var lifetime            = lifetimeDefinition.Lifetime;
            var windowContextSource = context.GetData(DataConstants.PopupWindowContextSource);

            // Layouter
            // Achtung! You MUST either pass the layouter to CreatePopupWindow or dispose of it, don't let it drift off
            IPopupWindowContext ctxToUse;

            if (windowContextSource != null)
            {
                IPopupWindowContext windowContext = windowContextSource.Create(lifetime);
                var ctxTextControl = windowContext as TextControlPopupWindowContext;
                ctxToUse = ctxTextControl == null
                     ? windowContext
                     : ctxTextControl.OverrideLayouter(
                    lifetime,
                    lifetimeLayouter =>
                    new DockingLayouter(lifetimeLayouter,
                                        new TextControlAnchoringRect(
                                            lifetimeLayouter,
                                            ctxTextControl.TextControl,
                                            ctxTextControl.TextControl.Caret.Offset(),
                                            shellLocks),
                                        Anchoring2D.AnchorTopOrBottom));
            }
            else
            {
                ctxToUse = solution.GetComponent <MainWindowPopupWindowContext>().Create(lifetime);
            }

            var form = new ZenCodingWrapForm(lifetime, solution.GetComponent <IThemedIconManager>());

            // Popup support
            var window = solution.GetComponent <PopupWindowManager>().CreatePopupWindow(
                lifetimeDefinition, form, ctxToUse, HideFlags.All & ~HideFlags.Scrolling);

            window.HideMethod = FormHideMethod.Visibility;
            window.Closed    +=
                (sender, args) =>
                ReentrancyGuard.Current.ExecuteOrQueue(
                    "ZenCodingWrap",
                    () =>
            {
                if (form.DialogResult == DialogResult.Cancel)
                {
                    return;
                }

                string abbr = form.TextBox.Text.Trim();
                if (abbr.IsEmpty())
                {
                    Win32Declarations.MessageBeep(MessageBeepType.Error);
                    return;
                }

                var commandProcessor           = solution.GetComponent <ICommandProcessor>();
                var documentTransactionManager = solution.GetComponent <DocumentTransactionManager>();

                using (commandProcessor.UsingCommand("ZenCodingWrap"))
                {
                    documentTransactionManager.StartTransaction("ZenCodingWrap");

                    TextRange selection = textControl.Selection.OneDocRangeWithCaret();
                    if (!solution.IsValid())
                    {
                        return;
                    }

                    int insertPoint;
                    IProjectFile projectFile = textControl.GetProjectFile(solution);
                    string expanded          = GetEngine(solution).WrapWithAbbreviation
                                                   (abbr, textControl.Document.GetText(selection), GetDocTypeForFile(projectFile), out insertPoint);
                    CheckAndIndent(solution, projectFile, textControl, selection, expanded, insertPoint);

                    documentTransactionManager.CommitTransaction(null);
                }
            });
            window.ShowWindow();
        }
        public override void Execute(IDataContext context, DelegateExecute nextExecute)
        {
            var solution = context.GetData(IDE.DataConstants.SOLUTION);
              Assertion.AssertNotNull(solution, "solution == null");
              var textControl = context.GetData(IDE.DataConstants.TEXT_CONTROL);
              Assertion.AssertNotNull(textControl, "textControl == null");

              var windowContext = context.GetData(UI.DataConstants.POPUP_WINDOW_CONTEXT);
              // Layouter
              // Achtung! You MUST either pass the layouter to CreatePopupWindow or dispose of it, don't let it drift off
              IPopupWindowContext ctxToUse;
              IPopupLayouter layouterToUse;

              if (windowContext != null)
              {
            var ctxTextControl = windowContext as TextControlPopupWindowContext;
            if (ctxTextControl != null)
            {
              layouterToUse = new DockingLayouter(new TextControlAnchoringRect(ctxTextControl.TextControl, ctxTextControl.TextControl.Caret.Offset()), Anchoring2D.AnchorLeftOrRightOnly);
              ctxToUse = ctxTextControl;
            }
            else
            {
              layouterToUse = windowContext.CreateLayouter();
              ctxToUse = windowContext;
            }
              }
              else
              {
            ctxToUse = PopupWindowContext.Empty;
            layouterToUse = ctxToUse.CreateLayouter();
              }

              var form = new ZenCodingWrapForm();
              var window = PopupWindowManager.CreatePopupWindow(form, layouterToUse, ctxToUse, HideFlags.Escape, true);
              window.Closed += (sender, args) => ReentrancyGuard.Current.ExecuteOrQueue("ZenCodingWrap", () =>
              {
            try
            {
              if (form.DialogResult == DialogResult.Cancel)
            return;
              var abbr = form.TextBox.Text.Trim();
              if (abbr.IsEmpty())
              {
            Win32Declarations.MessageBeep(MessageBeepType.Error);
            return;
              }
              using (ReadLockCookie.Create())
              using (CommandCookie.Create("ZenCodingWrap"))
              {
            using (var cookie = DocumentManager.GetInstance(solution).EnsureWritable(textControl.Document))
            {
              if (cookie.EnsureWritableResult != EnsureWritableResult.SUCCESS)
                return;

              var selection = textControl.Selection.DocRange;
              Assertion.Assert(selection.IsValid, "selection is not valid");

              int insertPoint;
              var expanded = GetEngine(solution).WrapWithAbbreviation(
                abbr, textControl.Selection.GetSelectionText(), GetDocTypeForFile(GetProjectFile(context)), out insertPoint);
              CheckAndIndent(solution, textControl, selection, expanded, insertPoint);
            }
              }
            }
            finally
            {
              window.Dispose();
              form.Dispose();
            }
              });
              window.ShowWindow();
        }
    public override void Execute(IDataContext context, DelegateExecute nextExecute)
    {
      var solution = context.GetData(ProjectModel.DataContext.DataConstants.SOLUTION);
      if (solution == null)
        return;

      var textControl = context.GetData(TextControl.DataContext.DataConstants.TEXT_CONTROL);
      if (textControl == null)
        return;

      var shellLocks = solution.GetComponent<IShellLocks>();
      var lifetimeDefinition = Lifetimes.Define(EternalLifetime.Instance, "ZenCodingWrap");
      var lifetime = lifetimeDefinition.Lifetime;
      var windowContextSource = context.GetData(DataConstants.PopupWindowContextSource);

      // Layouter
      // Achtung! You MUST either pass the layouter to CreatePopupWindow or dispose of it, don't let it drift off
      IPopupWindowContext ctxToUse;

      if (windowContextSource != null)
      {
        IPopupWindowContext windowContext = windowContextSource.Create(lifetime);
        var ctxTextControl = windowContext as TextControlPopupWindowContext;
        ctxToUse = ctxTextControl == null
                     ? windowContext
                     : ctxTextControl.OverrideLayouter(
                       lifetime,
                       lifetimeLayouter =>
                       new DockingLayouter(lifetimeLayouter,
                                           new TextControlAnchoringRect(
                                             lifetimeLayouter,
                                             ctxTextControl.TextControl,
                                             ctxTextControl.TextControl.Caret.Offset(),
                                             shellLocks),
                                           Anchoring2D.AnchorTopOrBottom));
      }
      else
      {
        ctxToUse = solution.GetComponent<MainWindowPopupWindowContext>().Create(lifetime);
      }

      var form = new ZenCodingWrapForm(lifetime, solution.GetComponent<IThemedIconManager>());

      // Popup support
      var window = solution.GetComponent<PopupWindowManager>().CreatePopupWindow(
        lifetimeDefinition, form, ctxToUse, HideFlags.All & ~HideFlags.Scrolling);
      window.HideMethod = FormHideMethod.Visibility;
      window.Closed +=
        (sender, args) =>
        ReentrancyGuard.Current.ExecuteOrQueue(
          "ZenCodingWrap",
          () =>
            {
              if (form.DialogResult == DialogResult.Cancel)
                return;

              string abbr = form.TextBox.Text.Trim();
              if (abbr.IsEmpty())
              {
                Win32Declarations.MessageBeep(MessageBeepType.Error);
                return;
              }

              var commandProcessor = solution.GetComponent<ICommandProcessor>();
              var documentTransactionManager = solution.GetComponent<DocumentTransactionManager>();

              using (commandProcessor.UsingCommand("ZenCodingWrap"))
              {
                documentTransactionManager.StartTransaction("ZenCodingWrap");

                TextRange selection = textControl.Selection.OneDocRangeWithCaret();
                if (!solution.IsValid())
                  return;

                int insertPoint;
                IProjectFile projectFile = textControl.GetProjectFile(solution);
                string expanded = GetEngine(solution).WrapWithAbbreviation
                  (abbr, textControl.Document.GetText(selection),GetDocTypeForFile(projectFile), out insertPoint);
                CheckAndIndent(solution, projectFile, textControl, selection, expanded, insertPoint);

                documentTransactionManager.CommitTransaction(null);
              }
            });
      window.ShowWindow();
    }