示例#1
0
 /// <summary>
 /// Edit a file that the user has double clicked on.
 /// </summary>
 /// <param name="opener">TextEditorOpener object containing the information necessary top edit a file.</param>
 /// <history>
 /// [Curtis_Beard]	   06/25/2015	Initial
 /// </history>
 public static void EditFile(TextEditorOpener opener)
 {
     if (opener != null && opener.HasValue())
     {
         EditFile(opener.Path, opener.LineNumber, opener.ColumnNumber, opener.LineText);
     }
 }
示例#2
0
        /// <summary>
        /// Open a file with a user defined text editor/executable.
        /// </summary>
        /// <param name="opener">TextEditorOpener object containing the information necessary to edit a file.</param>
        /// <history>
        /// [Theodore_Ward]     ??/??/????  Initial
        /// [Curtis_Beard]	   01/11/2005	.Net Conversion, Try/Catch
        /// [Curtis_Beard]	   06/13/2005	CHG: Used new cmd line arg specification
        /// [Curtis_Beard]	   07/20/2006	CHG: Run the text editor associated with the file's extension
        /// [Curtis_Beard]	   07/26/2006	ADD: 1512026, column position
        /// [Curtis_Beard]      09/28/2012  CHG: 3553474, support multiple file types per editor
        /// [Curtis_Beard]		04/07/2015	CHG: check for a valid line text before using
        /// [Curtis_Beard]	   04/08/2015	CHG: add logging
        /// [Curtis_Beard]	   08/20/2015	FIX: 81, use associated app instead of displaying message
        /// [Curtis_Beard]	   08/16/2016	CHG: use common class for parameters, rename from EditFile -> Open
        /// </history>
        public static void Open(TextEditorOpener opener)
        {
            if (opener != null && opener.HasValue())
            {
                try
                {
                    // pick the correct editor to use
                    System.IO.FileInfo file        = new System.IO.FileInfo(opener.Path);
                    TextEditor         editorToUse = null;

                    // find extension match
                    if (__TextEditors != null)
                    {
                        foreach (TextEditor editor in __TextEditors)
                        {
                            // handle multiple types for one editor
                            string[] types = new string[1] {
                                editor.FileType
                            };
                            if (editor.FileType.Contains(Constants.TEXT_EDITOR_TYPE_SEPARATOR))
                            {
                                types = editor.FileType.Split(Constants.TEXT_EDITOR_TYPE_SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            }

                            // loop through all types defined for this editor
                            foreach (string type in types)
                            {
                                string currentType = type;

                                // add missing start . if file type has it and the user didn't add it.
                                if (currentType != Constants.ALL_FILE_TYPES && !currentType.StartsWith(".") && file.Extension.StartsWith("."))
                                {
                                    currentType = string.Format(".{0}", currentType);
                                }

                                if (currentType.IndexOf(file.Extension, StringComparison.OrdinalIgnoreCase) > -1)
                                {
                                    // use this editor
                                    editorToUse = editor;
                                    break;
                                }
                            }

                            if (editorToUse != null)
                            {
                                break;
                            }
                        }

                        // try finding default for all types (*)
                        if (editorToUse == null)
                        {
                            foreach (TextEditor editor in __TextEditors)
                            {
                                if (editor.FileType.Equals(Constants.ALL_FILE_TYPES))
                                {
                                    // use this editor
                                    editorToUse = editor;
                                    break;
                                }
                            }
                        }

                        if (editorToUse == null)
                        {
                            // since nothing defined, just use default app associated with file type
                            OpenWithDefault(opener.Path);
                        }
                        else
                        {
                            // adjust column if tab size is set
                            if (editorToUse.TabSize > 0 && opener.ColumnNumber > 0 && !string.IsNullOrEmpty(opener.LineText))
                            {
                                // count how many tabs before found hit column index
                                int count = 0;
                                for (int i = opener.ColumnNumber - 1; i >= 0; i--)
                                {
                                    if (opener.LineText[i] == '\t')
                                    {
                                        count++;
                                    }
                                }

                                opener.ColumnNumber += ((count * editorToUse.TabSize) - count);
                            }

                            LaunchEditor(editorToUse, opener.Path, opener.LineNumber, opener.ColumnNumber, string.Empty);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Instance.Logger.Error("Unable to open text editor for file {0} at line {1}, column {2}, with text {3} and message {4}", opener.Path, opener.LineNumber, opener.ColumnNumber, opener.LineText, ex.Message);

                    MessageBox.Show(String.Format(Language.GetGenericText("TextEditorsErrorGeneric"), opener.Path, ex.Message),
                                    ProductInformation.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }