public void Invoke(CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } string fixedCode = string.Empty; Suppressor supp = new Suppressor(_code, _snapshot.ContentType.TypeName); if (_rule == null) { fixedCode = supp.SuppressAll(_suppDate); } else { fixedCode = supp.SuppressRule(_rule.Id, _suppDate); } _span.TextBuffer.Replace(_span.GetSpan(_snapshot), fixedCode); }
/// <summary> /// Test given text for issues /// </summary> /// <param name="text">Source code</param> /// <param name="contenttype">Visual Studio content type</param> /// <returns>MatchRecord with infomartion of identified issue</returns> private MatchResult FindMatch(string text, string contenttype) { // Get rules for the given content type IEnumerable <Rule> rules = GetRulesForContentType(contenttype); MatchResult result = new MatchResult() { Success = false }; // Go through each rule foreach (Rule r in rules) { // Go through each matching pattern of the rule foreach (PatternRecord p in r.Patterns) { // Type == Substring if (p.Type == PatternType.Substring) { result.Location = text.ToLower().IndexOf(p.Pattern.ToLower()); result.Length = p.Pattern.Length; if (result.Location > -1) { result.Success = true; result.Rule = r; break; // from pattern loop } } // Type == Regex else if (p.Type == PatternType.Regex) { RegexOptions reopt = RegexOptions.None; if (p.Modifiers != null) { reopt |= (p.Modifiers.Contains("ignorecase")) ? RegexOptions.IgnoreCase : RegexOptions.None; reopt |= (p.Modifiers.Contains("multiline")) ? RegexOptions.Multiline : RegexOptions.None; } Regex patRegx = new Regex(p.Pattern, reopt); Match m = patRegx.Match(text); if (m.Success) { result.Success = true; result.Rule = r; result.Location = m.Index; result.Length = m.Length; break; // from pattern loop } } } // We got matching rule. Let's see if we have a supression on the line if (result.Success) { Suppressor supp = new Suppressor(_textLine, contenttype); // If rule is being suppressed then clear the MatchResult if (supp.IsRuleSuppressed(result.Rule.Id)) { result = new MatchResult(); } // Otherwise break out of the loop as we found an issue. // So, no need to scan for more. else { break; } } } return(result); }