未验证 提交 4e2cb832 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #47846 from jasonmalinowski/fix-issues-with-opening-source-generated-files

Fix issues with opening source generated files
......@@ -20,6 +20,7 @@
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.LanguageServices.Implementation.Extensions;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
......@@ -41,6 +42,7 @@ internal sealed class SourceGeneratedFileManager : IRunningDocumentTableEventLis
private readonly IAsynchronousOperationListener _listener;
private readonly IVsRunningDocumentTable _runningDocumentTable;
private readonly ITextDocumentFactoryService _textDocumentFactoryService;
private readonly VisualStudioDocumentNavigationService _visualStudioDocumentNavigationService;
private readonly RunningDocumentTableEventTracker _runningDocumentTableEventTracker;
......@@ -63,6 +65,7 @@ internal sealed class SourceGeneratedFileManager : IRunningDocumentTableEventLis
IVsEditorAdaptersFactoryService editorAdaptersFactoryService,
ITextDocumentFactoryService textDocumentFactoryService,
VisualStudioWorkspace visualStudioWorkspace,
VisualStudioDocumentNavigationService visualStudioDocumentNavigationService,
IAsynchronousOperationListenerProvider listenerProvider)
{
_serviceProvider = serviceProvider;
......@@ -70,6 +73,7 @@ internal sealed class SourceGeneratedFileManager : IRunningDocumentTableEventLis
_textDocumentFactoryService = textDocumentFactoryService;
_temporaryDirectory = Path.Combine(Path.GetTempPath(), "VisualStudioSourceGeneratedDocuments");
_visualStudioWorkspace = visualStudioWorkspace;
_visualStudioDocumentNavigationService = visualStudioDocumentNavigationService;
Directory.CreateDirectory(_temporaryDirectory);
......@@ -99,7 +103,12 @@ public void NavigateToSourceGeneratedFile(Project project, ISourceGenerator gene
// The file name we generate here is chosen to match the compiler's choice, so the debugger can recognize the files should match.
// This can only be changed if the compiler changes the algorithm as well.
var temporaryFilePath = Path.Combine(projectDirectory, $"{generatorType.Module.ModuleVersionId}_{generatorType.FullName}_{generatedSourceHintName}");
File.WriteAllText(temporaryFilePath, "");
// Don't write to the file if it's already there, as that potentially triggers a file reload
if (!File.Exists(temporaryFilePath))
{
File.WriteAllText(temporaryFilePath, "");
}
var openDocumentService = _serviceProvider.GetService<SVsUIShellOpenDocument, IVsUIShellOpenDocument>();
var hr = openDocumentService.OpenDocumentViaProject(
......@@ -114,6 +123,12 @@ public void NavigateToSourceGeneratedFile(Project project, ISourceGenerator gene
{
windowFrame.Show();
}
// We should have the file now, so navigate to the right span
if (_openFiles.TryGetValue(temporaryFilePath, out var openFile))
{
openFile.NavigateToSpan(sourceSpan);
}
}
public bool TryParseGeneratedFilePath(
......@@ -425,6 +440,12 @@ private void EnsureWindowFrameInfoBarUpdated()
_currentWindowFrameImageMoniker = _windowFrameImageMonikerToShow;
_currentWindowFrameInfoBarElement = infoBarUI;
}
public void NavigateToSpan(TextSpan sourceSpan)
{
var sourceText = _textBuffer.CurrentSnapshot.AsText();
_fileManager._visualStudioDocumentNavigationService.NavigateTo(_textBuffer, sourceText.GetVsTextSpanForSpan(sourceSpan));
}
}
}
}
......@@ -4,6 +4,7 @@
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading;
......@@ -11,6 +12,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Navigation;
using Microsoft.CodeAnalysis.Options;
......@@ -19,7 +21,6 @@
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.LanguageServices.Implementation.Extensions;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
using Microsoft.VisualStudio.LanguageServices.Implementation.Venus;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
......@@ -32,12 +33,16 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation
{
using Workspace = Microsoft.CodeAnalysis.Workspace;
[ExportWorkspaceService(typeof(IDocumentNavigationService), ServiceLayer.Host), Shared]
[Export(typeof(VisualStudioDocumentNavigationService))]
internal sealed class VisualStudioDocumentNavigationService : ForegroundThreadAffinitizedObject, IDocumentNavigationService
{
private readonly IServiceProvider _serviceProvider;
private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService;
private readonly IVsRunningDocumentTable4 _runningDocumentTable;
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioDocumentNavigationService(
IThreadingContext threadingContext,
SVsServiceProvider serviceProvider,
......@@ -357,7 +362,7 @@ private static Document OpenDocument(Workspace workspace, DocumentId documentId)
return workspace.CurrentSolution.GetDocument(documentId);
}
private bool NavigateTo(ITextBuffer textBuffer, VsTextSpan vsTextSpan)
public bool NavigateTo(ITextBuffer textBuffer, VsTextSpan vsTextSpan)
{
using (Logger.LogBlock(FunctionId.NavigationService_VSDocumentNavigationService_NavigateTo, CancellationToken.None))
{
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Composition;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Navigation;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
namespace Microsoft.VisualStudio.LanguageServices.Implementation
{
[ExportWorkspaceServiceFactory(typeof(IDocumentNavigationService), ServiceLayer.Host), Shared]
internal sealed class VisualStudioDocumentNavigationServiceFactory : IWorkspaceServiceFactory
{
private readonly IDocumentNavigationService _singleton;
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioDocumentNavigationServiceFactory(
IThreadingContext threadingContext,
SVsServiceProvider serviceProvider,
IVsEditorAdaptersFactoryService editorAdaptersFactoryService)
{
_singleton = new VisualStudioDocumentNavigationService(threadingContext, serviceProvider, editorAdaptersFactoryService);
}
public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
=> _singleton;
}
}
......@@ -27,8 +27,6 @@ public override async Task InitializeAsync()
{
await base.InitializeAsync();
// Right now source generators are still restricted to /langver:preview
VisualStudio.SolutionExplorer.SetLanguageVersion(new ProjectUtils.Project(ProjectName), "preview");
VisualStudio.SolutionExplorer.AddAnalyzerReference(typeof(IntegrationTestSourceGenerator).Assembly.Location, new ProjectUtils.Project(ProjectName));
}
......@@ -47,6 +45,7 @@ public static void Main()
VisualStudio.Editor.PlaceCaret(IntegrationTestSourceGenerator.GeneratedClassName);
VisualStudio.Editor.GoToDefinition();
Assert.Equal($"{IntegrationTestSourceGenerator.GeneratedClassName}.cs {ServicesVSResources.generated_suffix}", VisualStudio.Shell.GetActiveWindowCaption());
Assert.Equal(IntegrationTestSourceGenerator.GeneratedClassName, VisualStudio.Editor.GetSelectedText());
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册