Last active
April 6, 2020 12:38
-
-
Save tmatz/5c69fb67398b3472bd3b3db3c3fb87e5 to your computer and use it in GitHub Desktop.
Show item in ErrorList: visual studio extension sample described at https://vsxexperience.net/2010/03/23/writing-to-the-vs-errorlist/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
using Microsoft.VisualStudio; | |
using Microsoft.VisualStudio.Shell; | |
using Microsoft.VisualStudio.Shell.Interop; | |
using Microsoft.VisualStudio.TextManager.Interop; | |
using Tasks = System.Threading.Tasks; | |
namespace VSIXProject1 | |
{ | |
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] | |
[Guid(VSIXProject1Package.PackageGuidString)] | |
[ProvideAutoLoad(UIContextGuids80.SolutionHasSingleProject, PackageAutoLoadFlags.BackgroundLoad)] | |
[ProvideAutoLoad(UIContextGuids80.SolutionHasMultipleProjects, PackageAutoLoadFlags.BackgroundLoad)] | |
public sealed partial class VSIXProject1Package : AsyncPackage | |
{ | |
public const string PackageGuidString = "2f65f260-e507-48c5-8a72-11a14d2578a2"; | |
private static readonly string ProviderName = typeof(VSIXProject1Package).FullName; | |
private static readonly Guid ProviderGuid = Guid.NewGuid(); | |
protected override async Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress) | |
{ | |
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); | |
AddErrorTask(TaskCategory.User, TaskErrorCategory.Error, "Context", "Error Message", "README.txt", 1, 1); | |
await ThreadHelper.JoinableTaskFactory.RunAsync(() => Tasks.Task.Delay(TimeSpan.FromSeconds(10))); | |
} | |
internal ErrorTask AddErrorTask( | |
TaskCategory category, | |
TaskErrorCategory errorCategory, | |
string context, | |
string text, | |
string document, | |
int line, | |
int column) | |
{ | |
var errorTask = new ErrorTask() | |
{ | |
Text = text, | |
ErrorCategory = errorCategory, | |
Line = line - 1, | |
Column = column - 1, | |
Document = document, | |
Category = category, | |
}; | |
if (!string.IsNullOrEmpty(document)) | |
{ | |
errorTask.Navigate += NavigateDocument; | |
} | |
GetErrorListProvider().Tasks.Add(errorTask); | |
return errorTask; | |
} | |
private ErrorListProvider GetErrorListProvider() | |
{ | |
return new ErrorListProvider(this) | |
{ | |
ProviderName = ProviderName, | |
ProviderGuid = ProviderGuid, | |
}; | |
} | |
private void NavigateDocument(object sender, EventArgs e) | |
{ | |
ThreadHelper.ThrowIfNotOnUIThread(); | |
var task = sender as Microsoft.VisualStudio.Shell.Task ?? throw new ArgumentException(nameof(sender)); | |
OpenDocumentAndNavigateTo(task.Document, task.Line, task.Column); | |
} | |
private void OpenDocumentAndNavigateTo(string documentPath, int line, int column) | |
{ | |
ThreadHelper.ThrowIfNotOnUIThread(); | |
if (!(GetGlobalService(typeof(IVsUIShellOpenDocument)) is IVsUIShellOpenDocument openDoc)) | |
{ | |
return; | |
} | |
Guid logicalView = VSConstants.LOGVIEWID.Code_guid; | |
if (ErrorHandler.Failed(openDoc.OpenDocumentViaProject(documentPath, ref logicalView, out _, out _, out _, out var frame)) || frame == null) | |
{ | |
return; | |
} | |
if (frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var docData) != VSConstants.S_OK || docData == null) | |
{ | |
return; | |
} | |
var textBuffer = docData is IVsTextBuffer buffer ? buffer | |
: (docData is IVsTextBufferProvider provider && ErrorHandler.ThrowOnFailure(provider.GetTextBuffer(out var lines)) == VSConstants.S_OK) ? (lines as IVsTextBuffer) | |
: null; | |
if (textBuffer == null) | |
{ | |
return; | |
} | |
if (!(GetGlobalService(typeof(VsTextManagerClass)) is IVsTextManager manager)) | |
{ | |
return; | |
} | |
manager.NavigateToLineAndColumn(textBuffer, ref logicalView, line, column, line, column); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment