|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.VisualStudio.Shell.Interop; |
| 3 | +using Microsoft.VisualStudio.Shell.TableManager; |
| 4 | +using Steroids.Contracts; |
| 5 | + |
| 6 | +namespace Steroids.CodeQuality.Extensions |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Extensions for <see cref="ITableEntry"/> objects. |
| 10 | + /// </summary> |
| 11 | + public static class ITableEntryExtensions |
| 12 | + { |
| 13 | + private const string SuppressionState = "suppressionstate"; |
| 14 | + private const string NotSuppressed = "Active"; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Creates a <see cref="DiagnosticInfo"/> from a <see cref="ITableEntriesHandle"/>. |
| 18 | + /// </summary> |
| 19 | + /// <param name="entry">The <see cref="ITableEntryHandle"/>.</param> |
| 20 | + /// <returns>The created <see cref="DiagnosticInfo"/>.</returns> |
| 21 | + public static DiagnosticInfo ToDiagnosticInfo(this ITableEntry entry) |
| 22 | + { |
| 23 | + if (!entry.TryGetValue(StandardTableKeyNames.ErrorSeverity, out __VSERRORCATEGORY errorCategory)) |
| 24 | + { |
| 25 | + errorCategory = __VSERRORCATEGORY.EC_MESSAGE; |
| 26 | + } |
| 27 | + |
| 28 | + entry.TryGetValue(StandardTableKeyNames.DocumentName, out string path); |
| 29 | + entry.TryGetValue(StandardTableKeyNames.Text, out string text); |
| 30 | + entry.TryGetValue(StandardTableKeyNames.FullText, out string fullText); |
| 31 | + entry.TryGetValue(StandardTableKeyNames.ErrorCode, out string errorCode); |
| 32 | + entry.TryGetValue(StandardTableKeyNames.HelpLink, out string helpLink); |
| 33 | + entry.TryGetValue(StandardTableKeyNames.Line, out int line); |
| 34 | + entry.TryGetValue(StandardTableKeyNames.Column, out int column); |
| 35 | + entry.TryGetValue(SuppressionState, out string suppressionState); |
| 36 | + |
| 37 | + if (string.IsNullOrWhiteSpace(fullText)) |
| 38 | + { |
| 39 | + fullText = text; |
| 40 | + } |
| 41 | + |
| 42 | + var severity = DiagnosticSeverity.Hidden; |
| 43 | + switch (errorCategory) |
| 44 | + { |
| 45 | + case __VSERRORCATEGORY.EC_ERROR: |
| 46 | + severity = DiagnosticSeverity.Error; |
| 47 | + break; |
| 48 | + case __VSERRORCATEGORY.EC_WARNING: |
| 49 | + severity = DiagnosticSeverity.Warning; |
| 50 | + break; |
| 51 | + case __VSERRORCATEGORY.EC_MESSAGE: |
| 52 | + severity = DiagnosticSeverity.Info; |
| 53 | + break; |
| 54 | + } |
| 55 | + |
| 56 | + return new DiagnosticInfo |
| 57 | + { |
| 58 | + Severity = severity, |
| 59 | + Path = path, |
| 60 | + Message = fullText, |
| 61 | + ErrorCode = errorCode, |
| 62 | + HelpUriRaw = helpLink, |
| 63 | + Line = line, |
| 64 | + Column = column, |
| 65 | + IsActive = suppressionState == NotSuppressed |
| 66 | + }; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments