Esempio n. 1
0
        /// <summary>
        /// Adds a commands in the assembly to the plugin menu.
        /// </summary>
        public void AddCommand()
        {
            foreach (CommandClass cmdClass in _classes)
            {
                var classSortAttrib = cmdClass.classType.TryGetAttribute <NppSortOrderAttribute>();
                int classSortOrder  = classSortAttrib != null ? classSortAttrib.SortOrder : Int32.MaxValue;

                var classMenuAttrib = cmdClass.classType.TryGetAttribute <NppMenuAttribute>();

                foreach (CommandMethod cmdMethod in cmdClass.methods)
                {
                    PluginCommand cmd = new PluginCommand(cmdMethod.name);
                    cmd.Tag            = cmdMethod;
                    cmd.Execute       += Execute;
                    cmd.ClassSortOrder = classSortOrder;

                    foreach (NppShortcutAttribute s in cmdMethod.method.GetCustomAttributes(typeof(NppShortcutAttribute), false))
                    {
                        cmd.Shortcut = s.Shortcut;
                    }

                    cmd.SortOrder = Int32.MaxValue;
                    foreach (NppSortOrderAttribute so in cmdMethod.method.GetCustomAttributes(typeof(NppSortOrderAttribute), false))
                    {
                        cmd.SortOrder = so.SortOrder;
                    }

                    foreach (NppSeparatorAttribute s in cmdMethod.method.GetCustomAttributes(typeof(NppSeparatorAttribute), false))
                    {
                        cmd.Separator = true;
                    }

                    foreach (NppToolbarIconAttribute t in cmdMethod.method.GetCustomAttributes(typeof(NppToolbarIconAttribute), false))
                    {
                        cmd.ShowInToolbar = true;
                        cmd.ToolbarIcon   = t.LoadIcon(cmdClass.instance, _fileName);
                        if (cmd.ToolbarIcon == null)
                        {
                            cmd.ToolbarIcon = Res.DefaultToolbarIcon;
                        }
                    }

                    var menuAttrib = (from a in cmdMethod.method.GetCustomAttributes(typeof(NppMenuAttribute), false).Cast <NppMenuAttribute>() where !string.IsNullOrWhiteSpace(a.Name) select a).FirstOrDefault();
                    if (classMenuAttrib != null && !string.IsNullOrWhiteSpace(classMenuAttrib.Name))
                    {
                        cmd.MenuName         = menuAttrib != null && !string.IsNullOrWhiteSpace(menuAttrib.Name) ? string.Concat(classMenuAttrib.Name, "|", menuAttrib.Name) : classMenuAttrib.Name;
                        cmd.MenuInsertBefore = classMenuAttrib.InsertBefore;
                    }
                    else if (menuAttrib != null && !string.IsNullOrWhiteSpace(menuAttrib.Name))
                    {
                        cmd.MenuName         = menuAttrib.Name;
                        cmd.MenuInsertBefore = menuAttrib.InsertBefore;
                    }

                    cmdMethod.commandIndex = Plugin.AddCommand(cmd);
                }
            }
        }
Esempio n. 2
0
        private static void InitCommands()
        {
            PluginCommand cmd = new PluginCommand("Show &Output Window");

            cmd.Execute  += OnShowOutputWindow;
            cmd.SortOrder = -4;
            _npp.SetShowOutputWindowCommandIndex(AddCommand(cmd));

            cmd           = new PluginCommand("NppSharp &Settings");
            cmd.Execute  += OnSettings;
            cmd.SortOrder = -3;
            AddCommand(cmd);

            cmd           = new PluginCommand("Show &Help File");
            cmd.Execute  += OnShowHelpFile;
            cmd.SortOrder = -2;
            AddCommand(cmd);

            var topSeparator = new PluginCommand("-");

            topSeparator.SortOrder = -1;
            AddCommand(topSeparator);

            ScriptManager.AddCommands();

            // Sort for the desired order as specified in the scripts.
            _commands.Sort(new PluginCommandComparer());

            // Add separators
            List <PluginCommand> cmdList = new List <PluginCommand>();
            bool firstInNppSharpMenu     = true;

            foreach (PluginCommand c in _commands)
            {
                if (c.Separator && !firstInNppSharpMenu &&
                    string.IsNullOrWhiteSpace(c.MenuName))                      // If c.MenuName is set, the this command would just be deleted from the NppSharp menu anyway, so don't add a separator as a command.
                {
                    cmdList.Add(new PluginCommand("-"));
                }
                cmdList.Add(c);
                if (string.IsNullOrWhiteSpace(c.MenuName))
                {
                    firstInNppSharpMenu = false;
                }
            }
            _commands = cmdList;

            if (!_commands.Any(c => string.IsNullOrWhiteSpace(c.MenuName) && c.SortOrder >= 0))
            {
                _commands.Remove(topSeparator);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Creates the event arguments object.
 /// </summary>
 /// <param name="cmd">The plugin command to be executed.</param>
 public ExecuteCommandEventArgs(PluginCommand cmd)
 {
     _cmd = cmd;
 }
Esempio n. 4
0
 /// <summary>
 /// Creates the event arguments object.
 /// </summary>
 /// <param name="cmd">The plugin command to be executed.</param>
 public ExecuteCommandEventArgs(PluginCommand cmd)
 {
     _cmd = cmd;
 }
Esempio n. 5
0
        /// <summary>
        /// Adds a commands in the assembly to the plugin menu.
        /// </summary>
        public void AddCommand()
        {
            foreach (CommandClass cmdClass in _classes)
            {
                int classSortOrder = Int32.MaxValue;
                foreach (NppSortOrderAttribute so in cmdClass.classType.GetCustomAttributes(typeof(NppSortOrderAttribute), false))
                {
                    classSortOrder = so.SortOrder;
                }

                foreach (CommandMethod cmdMethod in cmdClass.methods)
                {
                    PluginCommand cmd = new PluginCommand(cmdMethod.name);
                    cmd.Tag = cmdMethod;
                    cmd.Execute += Execute;
                    cmd.ClassSortOrder = classSortOrder;

                    foreach (NppShortcutAttribute s in cmdMethod.method.GetCustomAttributes(typeof(NppShortcutAttribute), false))
                    {
                        cmd.Shortcut = s.Shortcut;
                    }

                    cmd.SortOrder = Int32.MaxValue;
                    foreach (NppSortOrderAttribute so in cmdMethod.method.GetCustomAttributes(typeof(NppSortOrderAttribute), false))
                    {
                        cmd.SortOrder = so.SortOrder;
                    }

                    foreach (NppSeparatorAttribute s in cmdMethod.method.GetCustomAttributes(typeof(NppSeparatorAttribute), false))
                    {
                        cmd.Separator = true;
                    }

                    foreach (NppToolbarIconAttribute t in cmdMethod.method.GetCustomAttributes(typeof(NppToolbarIconAttribute), false))
                    {
                        cmd.ShowInToolbar = true;
                        cmd.ToolbarIcon = t.LoadIcon(cmdClass.instance, _fileName);
                        if (cmd.ToolbarIcon == null) cmd.ToolbarIcon = Res.DefaultToolbarIcon;
                    }

                    cmdMethod.commandIndex = Plugin.AddCommand(cmd);
                }
            }
        }
Esempio n. 6
0
 internal static int AddCommand(PluginCommand cmd)
 {
     _commands.Add(cmd);
     return(_commands.Count - 1);
 }