/// <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); }
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); } } } }