/// <summary>Creates another instance of <see cref="CSharpRazorHardCodedString"/> string with the provided arguments</summary>
        /// <param name="parent">Current object item (file)</param>
        /// <param name="start">Starting offset of the string</param>
        /// <param name="end">End offset of the string</param>
        /// <returns>A new instance of <see cref="CSharpRazorHardCodedString"/>.</returns>
        public override BaseHardCodedString CreateInstance(ProjectItem parent, int start, int end)
        {
            CSharpRazorHardCodedString newInstance = new CSharpRazorHardCodedString(parent, start, end);

            newInstance.InitializeValue();
            return(newInstance);
        }
        /// <summary>
        /// Returns an instance of BaseHardCodedString, based the type of the current document.
        /// </summary>
        /// <param name="currentDocument">The document that contains an hard-coded string.</param>
        /// <returns>A hard coded string that is file type aware.</returns>
        public static BaseHardCodedString GetHardCodedString(Document currentDocument)
        {
            BaseHardCodedString stringInstance = null;

            // Create the hard coded string instance
            switch (currentDocument.Language)
            {
            case "CSharp":
                stringInstance = new CSharpHardCodedString();
                break;

            case "Basic":
                stringInstance = new VBHardCodedString();
                break;

            case "XAML":
                stringInstance = new XamlHardCodedString();
                break;

            case "HTMLX":
                if (currentDocument.Name.EndsWith(".cshtml", StringComparison.CurrentCultureIgnoreCase))
                {
                    stringInstance = new CSharpRazorHardCodedString();
                }
                else if (currentDocument.Name.EndsWith(".vbhtml", StringComparison.CurrentCultureIgnoreCase))
                {
                    stringInstance = new VBRazorHardCodedString();
                }
                break;

            case "HTML":
                if (currentDocument.Name.EndsWith(".aspx", StringComparison.CurrentCultureIgnoreCase) || currentDocument.Name.EndsWith(".ascx", StringComparison.CurrentCultureIgnoreCase) || currentDocument.Name.EndsWith(".master", StringComparison.CurrentCultureIgnoreCase))
                {
                    stringInstance = new AspxHardCodedString();
                }
                break;
            }
            return(stringInstance);
        }
Example #3
0
        public void Exec(string CmdName, vsCommandExecOption ExecuteOption, ref object VariantIn, ref object VariantOut, ref bool Handled)
        {
            if (CmdName == null) {
                throw new ArgumentNullException("CmdName");
            }
            if (this.availableRefactorOptions.ContainsKey(CmdName)) {
                TextSelection selection = (TextSelection)(applicationObject.ActiveDocument.Selection);
                if (applicationObject.ActiveDocument.ProjectItem.Object != null) {
                    Common.BaseHardCodedString stringInstance = null;
                    /// Create the hard coded string instance
                    switch (applicationObject.ActiveDocument.Language) {
                        case "CSharp":
                            stringInstance = new Common.CSharpHardCodedString();
                            break;
                        case "Basic":
                            stringInstance = new Common.VBHardCodedString();
                            break;
                        case "XAML":
                            stringInstance = new Common.XamlHardCodedString();
                            break;
                        case "HTML":
                            if (applicationObject.ActiveDocument.Name.Contains(".cshtml")) {
                                stringInstance = new Common.CSharpRazorHardCodedString();
                            }
                            break;
                        default:
                            MessageBox.Show(
                                Strings.UnsupportedFile + " (" + applicationObject.ActiveDocument.Language + ")",
                                Strings.WarningTitle,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                            return;
                    }
                    Common.MatchResult scanResult = stringInstance.CheckForHardCodedString(
                       selection.Parent,
                       selection.AnchorPoint.AbsoluteCharOffset - 1,
                       selection.BottomPoint.AbsoluteCharOffset - 1);

                    if (!scanResult.Result && selection.AnchorPoint.AbsoluteCharOffset < selection.BottomPoint.AbsoluteCharOffset) {
                        scanResult.StartIndex = selection.AnchorPoint.AbsoluteCharOffset - 1;
                        scanResult.EndIndex = selection.BottomPoint.AbsoluteCharOffset - 1;
                        scanResult.Result = true;
                    }
                    if (scanResult.Result) {
                        stringInstance = stringInstance.CreateInstance(applicationObject.ActiveDocument.ProjectItem, scanResult.StartIndex, scanResult.EndIndex);
                        if (stringInstance != null && stringInstance.Parent != null) {
                            Common.IStringRefactorOption option = this.availableRefactorOptions[CmdName];
                            if (option.QuerySupportForString(stringInstance)) {
                                option.PerformAction(stringInstance);
                            } else {
                                MessageBox.Show(
                               Strings.UnsupportedFile,
                               Strings.WarningTitle,
                               MessageBoxButtons.OK,
                               MessageBoxIcon.Error);
                            }
                        }
                    } else {
                        MessageBox.Show(
                                Strings.NotStringLiteral,
                                Strings.WarningTitle,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                    }
                }
            }
        }