/// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// </summary>
        /// <param name="Sender">Event sender.</param>
        /// <param name="Args">Event args.</param>
        private void GenerateMacroCallback(object Sender, EventArgs Args)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // マクロの種類を取得
            EditorSelecter SelecterDialog = new EditorSelecter();

            SelecterDialog.ShowDialog();

            if (SelecterDialog.DialogResult == DialogResult.OK)
            {
                // エディタを起動
                DialogResult DialogResult = DialogResult.None;
                string       EditResult   = string.Empty;
                switch (XmlFunctionLibrary.GetEditorType(SelecterDialog.MacroType))
                {
                case EditorType.MacroEditor:
                    MacroEditor MacroEditor = new MacroEditor(SelecterDialog.MacroType);
                    MacroEditor.ShowDialog();
                    DialogResult = MacroEditor.DialogResult;
                    EditResult   = MacroEditor.MacroString;
                    break;

                case EditorType.LogEditor:
                    LogEditor LogEditor = new LogEditor(SelecterDialog.MacroType);
                    LogEditor.ShowDialog();
                    DialogResult = LogEditor.DialogResult;
                    EditResult   = LogEditor.MacroString;
                    break;

                case EditorType.DelegateEditor:
                    DelegateEditor DelegateEditor = new DelegateEditor(SelecterDialog.MacroType);
                    DelegateEditor.ShowDialog();
                    DialogResult = DelegateEditor.DialogResult;
                    EditResult   = DelegateEditor.MacroString;
                    break;
                }

                if (DialogResult == DialogResult.OK && !string.IsNullOrEmpty(EditResult))
                {
                    // カーソル位置に結果の文字列を挿入
                    DTE Dte = (DTE)ServiceProvider.GetService(typeof(DTE));
                    Assumes.Present(Dte);
                    var Selection = (TextSelection)Dte.ActiveDocument.Selection;
                    Selection.Text = EditResult;

                    // カーソルの行を更新
                    Selection.SelectLine();
                    Selection.SmartFormat();
                    Selection.EndOfLine();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// </summary>
        /// <param name="Sender">Event sender.</param>
        /// <param name="Args">Event args.</param>
        private void EditMacroCallback(object Sender, EventArgs Args)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            DTE Dte = (DTE)ServiceProvider.GetService(typeof(DTE));

            Assumes.Present(Dte);
            var ActiveDocument = Dte.ActiveDocument;

            if (ActiveDocument != null)
            {
                // 一旦カーソル位置の行を全て選択
                var Selection = (TextSelection)ActiveDocument.Selection;
                Selection.SelectLine();

                // マクロの名前だけ選択
                string        TargetType = string.Empty;
                List <string> MacroTypes = new List <string>(XmlFunctionLibrary.GetMacroTypes(true, true, false));
                foreach (var MacroType in MacroTypes)
                {
                    if (Selection.Text.Contains(MacroType))
                    {
                        TargetType = MacroType;
                        break;
                    }
                }

                Selection.StartOfLine();
                Selection.LineUp();

                while (true)
                {
                    if (Selection.Text.Contains(TargetType))
                    {
                        break;
                    }
                    Selection.WordRight(true);
                }
                Selection.WordLeft();
                Selection.WordRight(true);

                TargetType = Selection.Text;

                // サポートしてなかったらエラー
                MacroTypes.Clear();
                MacroTypes.AddRange(XmlFunctionLibrary.GetMacroTypes(false, false, true));
                if (!MacroTypes.Contains(TargetType))
                {
                    string SupportedMacros = string.Empty;
                    for (int Index = 0; Index < MacroTypes.Count; Index++)
                    {
                        SupportedMacros += MacroTypes[Index] + "\r\n";
                    }

                    MessageBox.Show(
                        TargetType + " is not a supported macro",
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error
                        );
                    return;
                }

                // マクロの中身だけ選択する
                string TargetParameters = string.Empty;
                bool   bIsInString      = false;
                int    Depth            = 0;
                while (true)
                {
                    Selection.CharRight(true);

                    var LastChar = Selection.Text[Selection.Text.Length - 1];

                    // マクロ名以降のみを取得
                    TargetParameters += LastChar;

                    // 文字列中はカウントしない
                    if (LastChar == '\"')
                    {
                        bIsInString = !bIsInString;
                    }

                    // カッコの深さをカウント
                    if (!bIsInString && LastChar == '(')
                    {
                        Depth++;
                    }
                    else if (!bIsInString && LastChar == ')')
                    {
                        Depth--;
                    }

                    // カッコの数が合ったら終了
                    if (Depth <= 0)
                    {
                        break;
                    }

                    // カッコの数が合わなかった時の対策
                    if (Selection.ActivePoint.AtEndOfLine)
                    {
                        break;
                    }
                }

                // 文字列中に"が入っている場合はエラー
                if (StringFunctionLibrary.CountOfChar(TargetParameters, '\"') % 2 != 0)
                {
                    MessageBox.Show(
                        "The string literal contains double quotes\r\n" +
                        "Remove the double quotes inside the string literal to edit the macro",
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error
                        );
                    return;
                }

                if (!string.IsNullOrEmpty(TargetParameters))
                {
                    // エディタを起動
                    DialogResult DialogResult = DialogResult.None;
                    string       EditResult   = string.Empty;
                    switch (XmlFunctionLibrary.GetEditorType(TargetType))
                    {
                    case EditorType.MacroEditor:
                        MacroEditor MacroEditor = new MacroEditor(TargetType, TargetParameters);
                        MacroEditor.ShowDialog();
                        DialogResult = MacroEditor.DialogResult;
                        EditResult   = MacroEditor.MacroString;
                        break;

                    case EditorType.LogEditor:
                        LogEditor LogEditor = new LogEditor(TargetType, TargetParameters);
                        LogEditor.ShowDialog();
                        DialogResult = LogEditor.DialogResult;
                        EditResult   = LogEditor.MacroString;
                        break;

                    case EditorType.DelegateEditor:
                        DelegateEditor DelegateEditor = new DelegateEditor(TargetType, TargetParameters);
                        DelegateEditor.ShowDialog();
                        DialogResult = DelegateEditor.DialogResult;
                        EditResult   = DelegateEditor.MacroString;
                        break;
                    }

                    if (ActiveDocument != null && DialogResult == DialogResult.OK && !string.IsNullOrEmpty(EditResult))
                    {
                        // カーソル位置に結果の文字列を挿入
                        Selection.Text = EditResult;

                        // カーソルの行を更新
                        Selection.SelectLine();
                        Selection.SmartFormat();
                        Selection.EndOfLine();
                    }
                }
            }
        }