/// <summary> /// Creates a new DeclarationHeader for the declaration and assigns it to the declaration. /// </summary> /// <param name="declaration"> /// The declaration to create the header for. /// </param> /// <param name="docConfig"> /// Provides the configuration for the current ProjectFile. /// </param> /// <returns> /// A DeclarationHeader for the declaration passed in. /// </returns> public static DeclarationHeader CreateNewHeader(IDeclaration declaration, DocumentationRulesConfiguration docConfig) { IFile file = declaration.GetContainingFile(); using (WriteLockCookie.Create(file.IsPhysical())) { IDeclaration declarationTreeNode = declaration; IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, declaration.GetSolution()); bool useSingleLineDeclarationComments = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.UseSingleLineDeclarationComments); string middleText = useSingleLineDeclarationComments ? string.Empty : Environment.NewLine; string emptyDocHeader = string.Format("<summary>{0}</summary>", middleText); if (!(declarationTreeNode is IMultipleDeclarationMember)) { emptyDocHeader = CreateDocumentationForElement((IDocCommentBlockOwnerNode)declaration, docConfig); emptyDocHeader = emptyDocHeader.Substring(0, emptyDocHeader.Length - Environment.NewLine.Length); } string header = LayoutDocumentationHeader(emptyDocHeader, declaration); IDocCommentBlockNode newDocCommentNode = Utils.CreateDocCommentBlockNode(file, header); IDocCommentBlockOwnerNode docCommentBlockOwnerNode = Utils.GetDocCommentBlockOwnerNodeForDeclaration(declaration); if (docCommentBlockOwnerNode != null) { docCommentBlockOwnerNode.SetDocCommentBlockNode(newDocCommentNode); } return(new DeclarationHeader(declaration)); } }
/// <summary> /// Update dash header and footer. /// </summary> private void UpdateDashHeaderAndFooter() { if (this.headerXml == null) { return; } IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, this.File.GetSolution()); int dashCount = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.DashesCountInFileHeader); string dashes = new string('-', dashCount); XmlText xmlTextTop = this.headerXml.CreateTextNode(string.Format("// {0}{1}// ", dashes, Environment.NewLine)); if (this.headerXml.DocumentElement.FirstChild.NodeType == XmlNodeType.Text) { this.headerXml.DocumentElement.ReplaceChild(xmlTextTop, this.headerXml.DocumentElement.FirstChild); } else { this.headerXml.DocumentElement.InsertBefore(xmlTextTop, this.headerXml.DocumentElement.FirstChild); } XmlText xmlTextBottom = this.headerXml.CreateTextNode(string.Format("{0}// {1}", Environment.NewLine, dashes)); if (this.headerXml.DocumentElement.LastChild.NodeType == XmlNodeType.Text) { this.headerXml.DocumentElement.ReplaceChild(xmlTextBottom, this.headerXml.DocumentElement.LastChild); } else { this.headerXml.DocumentElement.InsertAfter(xmlTextBottom, this.headerXml.DocumentElement.LastChild); } }
/// <summary> /// Performs the QuickFix, inserts the configured modifier into the location specified by /// the violation. /// </summary> /// <param name="solution"> /// Current Solution. /// </param> /// <param name="textControl"> /// Current Text Control to modify. /// </param> public void Execute(ISolution solution, ITextControl textControl) { using (ChangeSeverityDialog dialog = new ChangeSeverityDialog()) { ////var settings = HighlightingSettingsManager.Instance.Settings.Clone(); ////var severityItem = HighlightingSettingsManager.Instance.GetSeverityItem(this.HighlightID); ////dialog.Severity = settings.GetSeverity(this.HighlightID); ////dialog.Text = "Inspection options for \"" + severityItem.Title + "\""; ////if (dialog.ShowDialog(Shell.Instance.GetComponent<UIApplication>().MainWindow) == DialogResult.OK) ////{ //// settings.SetSeverity(this.HighlightID, dialog.Severity); //// HighlightingSettingsManager.Instance.Settings = settings; //// Daemon.GetInstance(solution).Invalidate(); ////} IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solution); IContextBoundSettingsStore contextBoundSettingsStore = settingsStore.SettingsStore.BindToContextTransient(ContextRange.Smart(textControl.Document.ToDataContext())); HighlightingSettingsManager settingsManager = HighlightingSettingsManager.Instance; HighlightingSettingsManager.ConfigurableSeverityItem item = settingsManager.GetSeverityItem(this.HighlightID); dialog.Severity = settingsManager.GetConfigurableSeverity(this.HighlightID, solution); dialog.Text = string.Format("Inspection Options for \"{0}\"", item.FullTitle); dialog.CanBeError = !item.SolutionAnalysisRequired; if (dialog.ShowDialog(Shell.Instance.GetComponent <UIApplication>().MainWindow) == DialogResult.OK) { contextBoundSettingsStore.SetIndexedValue(HighlightingSettingsAccessor.InspectionSeverities, this.HighlightID, dialog.Severity); } } }
/// <summary> /// The execute inner. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="textControl"> /// The text control. /// </param> public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl) { IAttributesOwnerDeclaration declaration = Utils.GetTypeClosestToTextControl <IAttributesOwnerDeclaration>(solution, textControl); if (declaration != null) { string rulesNamespace = this.Rule.Namespace; string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name); IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solution); string justificationText = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.SuppressStyleCopAttributeJustificationText); CSharpElementFactory factory = CSharpElementFactory.GetInstance(declaration.GetPsiModule()); ITypeElement typeElement = Utils.GetTypeElement(declaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"); IAttribute attribute = factory.CreateAttribute(typeElement); ICSharpArgument newArg1 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), rulesNamespace), null); ICSharpArgument newArg2 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), ruleText), newArg1); IPropertyAssignment propertyAssignment = factory.CreatePropertyAssignment( "Justification", factory.CreateExpression("\"$0\"", justificationText)); attribute.AddPropertyAssignmentAfter(propertyAssignment, null); declaration.AddAttributeAfter(attribute, null); } }
/// <summary> /// Gets the highlight for the specified StyleCop Violation. /// </summary> /// <param name="violation"> /// The <see cref="StyleCop.ViolationEventArgs"/> instance containing the event data. /// </param> /// <param name="documentRange"> /// <see cref="DocumentRange"/>where the Violation happened. /// </param> /// <param name="fileName"> /// <see cref="CsElement"/>of the violation. /// </param> /// <param name="lineNumber"> /// Line number where the violation happened. /// </param> /// <returns> /// An <see cref="IHighlighting"/> for the specified Violation. /// </returns> public static IHighlighting GetHighlight(ViolationEventArgs violation, DocumentRange documentRange, string fileName, int lineNumber) { string ruleID = violation.Violation.Rule.CheckId; string highlightID = HighlightingRegistering.GetHighlightID(ruleID); VSSolutionManager solutionManager = Shell.Instance.GetComponent <VSSolutionManager>(); IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solutionManager.CurrentSolution); Severity severity = HighlightingSettingsManager.Instance.GetConfigurableSeverity(highlightID, settingsStore); switch (severity) { case Severity.ERROR: return(new StyleCopHighlightingError(violation, documentRange, fileName, lineNumber)); case Severity.HINT: return(new StyleCopHighlightingHint(violation, documentRange, fileName, lineNumber)); case Severity.INFO: return(new StyleCopHighlightingError(violation, documentRange, fileName, lineNumber)); case Severity.SUGGESTION: return(new StyleCopHighlightingSuggestion(violation, documentRange, fileName, lineNumber)); case Severity.WARNING: return(new StyleCopHighlightingWarning(violation, documentRange, fileName, lineNumber)); default: return(new StyleCopHighlightingDoNotShow(violation, documentRange, fileName, lineNumber)); } }
/// <summary> /// The execute inner. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="textControl"> /// The text control. /// </param> public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl) { IDeclaration declaration = Utils.GetDeclarationClosestToTextControl(solution, textControl); if (declaration != null) { string rulesNamespace = this.Rule.Namespace; string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name); IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solution); string justificationText = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.SuppressStyleCopAttributeJustificationText); IAttributesOwnerDeclaration attributesOwnerDeclaration = declaration as IAttributesOwnerDeclaration; if (attributesOwnerDeclaration != null) { CSharpElementFactory factory = CSharpElementFactory.GetInstance(declaration.GetPsiModule()); ITypeElement typeElement = Utils.GetTypeElement(declaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"); IAttribute attribute = factory.CreateAttribute(typeElement); ICSharpArgument newArg1 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), rulesNamespace), null); ICSharpArgument newArg2 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), ruleText), newArg1); attribute.AddArgumentAfter(Utils.CreateArgumentValueExpression(declaration.GetPsiModule(), "Justification = \"" + justificationText + "\""), newArg2); attributesOwnerDeclaration.AddAttributeAfter(attribute, null); IFile file = declaration.GetContainingFile(); if (file != null) { LanguageService languageService = CSharpLanguage.Instance.LanguageService(); if (languageService != null) { ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)languageService.CodeFormatter; if (codeFormatter != null) { codeFormatter.FormatFile(file, CodeFormatProfile.DEFAULT, NullProgressIndicator.Instance); } } } } } }
/// <summary> /// Takes the XmlNode and creates a formatted StringBuilder of it all formatted lovely. /// </summary> /// <param name="xml"> /// The xml to use. /// </param> /// <param name="declaration"> /// The declaration we start with. /// </param> /// <returns> /// A String all formatted. /// </returns> private static string LayoutDocumentationHeader(XmlNode xml, IDeclaration declaration) { StringBuilder pattern = new StringBuilder(); StringWriter writer = new StringWriter(pattern); bool writtenNewLine = true; IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, declaration.GetSolution()); bool useSingleLineDeclarationComments = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.UseSingleLineDeclarationComments); LayoutDocumentationXml(xml, writer, ref writtenNewLine, useSingleLineDeclarationComments); if (pattern.ToString().EndsWith(Environment.NewLine)) { pattern.Remove(pattern.Length - Environment.NewLine.Length, Environment.NewLine.Length); } if (pattern.ToString().StartsWith(Environment.NewLine)) { pattern.Remove(0, Environment.NewLine.Length); } return(pattern.ToString().Trim()); }