未验证 提交 c239147f 编写于 作者: M Manish Vasani 提交者: GitHub

Merge pull request #39892 from mavasani/ClosedFileDiagnostics

Remove diagnostics from error list on document close or reset for tha…
......@@ -156,7 +156,8 @@ public async Task DocumentCloseAsync(Document document, CancellationToken cancel
// let other components knows about this event
ClearCompilationsWithAnalyzersCache();
await _stateManager.OnDocumentClosedAsync(stateSets, document).ConfigureAwait(false);
var documentHadDiagnostics = await _stateManager.OnDocumentClosedAsync(stateSets, document).ConfigureAwait(false);
RaiseDiagnosticsRemovedIfRequiredForClosedOrResetDocument(document, stateSets, documentHadDiagnostics);
}
}
......@@ -168,12 +169,35 @@ public Task DocumentResetAsync(Document document, CancellationToken cancellation
// let other components knows about this event
ClearCompilationsWithAnalyzersCache();
_stateManager.OnDocumentReset(stateSets, document);
var documentHadDiagnostics = _stateManager.OnDocumentReset(stateSets, document);
RaiseDiagnosticsRemovedIfRequiredForClosedOrResetDocument(document, stateSets, documentHadDiagnostics);
}
return Task.CompletedTask;
}
private void RaiseDiagnosticsRemovedIfRequiredForClosedOrResetDocument(Document document, IEnumerable<StateSet> stateSets, bool documentHadDiagnostics)
{
// if there was no diagnostic reported for this document OR Full solution analysis is enabled, nothing to clean up
if (!documentHadDiagnostics ||
FullAnalysisEnabled(document.Project, forceAnalyzerRun: false))
{
// this is Perf to reduce raising events unnecessarily.
return;
}
var removeDiagnosticsOnDocumentClose = document.Project.Solution.Options.GetOption(ServiceFeatureOnOffOptions.RemoveDocumentDiagnosticsOnDocumentClose, document.Project.Language);
// TODO: Remove the below hard-coded check for TypeScript once they update their code to explicitly set the above option.
if (document.Project.Language != "TypeScript" &&
!removeDiagnosticsOnDocumentClose)
{
return;
}
RaiseDiagnosticsRemovedForDocument(document.Id, stateSets);
}
public void RemoveDocument(DocumentId documentId)
{
using (Logger.LogBlock(FunctionId.Diagnostics_RemoveDocument, GetRemoveLogMessage, documentId, CancellationToken.None))
......@@ -191,19 +215,25 @@ public void RemoveDocument(DocumentId documentId)
return;
}
// remove all diagnostics for the document
AnalyzerService.RaiseBulkDiagnosticsUpdated(raiseEvents =>
{
foreach (var stateSet in stateSets)
{
RaiseDiagnosticsRemoved(documentId, solution: null, stateSet, AnalysisKind.Syntax, raiseEvents);
RaiseDiagnosticsRemoved(documentId, solution: null, stateSet, AnalysisKind.Semantic, raiseEvents);
RaiseDiagnosticsRemoved(documentId, solution: null, stateSet, AnalysisKind.NonLocal, raiseEvents);
}
});
RaiseDiagnosticsRemovedForDocument(documentId, stateSets);
}
}
private void RaiseDiagnosticsRemovedForDocument(DocumentId documentId, IEnumerable<StateSet> stateSets)
{
// remove all diagnostics for the document
AnalyzerService.RaiseBulkDiagnosticsUpdated(raiseEvents =>
{
foreach (var stateSet in stateSets)
{
// clear all doucment diagnostics
RaiseDiagnosticsRemoved(documentId, solution: null, stateSet, AnalysisKind.Syntax, raiseEvents);
RaiseDiagnosticsRemoved(documentId, solution: null, stateSet, AnalysisKind.Semantic, raiseEvents);
RaiseDiagnosticsRemoved(documentId, solution: null, stateSet, AnalysisKind.NonLocal, raiseEvents);
}
});
}
public void RemoveProject(ProjectId projectId)
{
using (Logger.LogBlock(FunctionId.Diagnostics_RemoveProject, GetRemoveLogMessage, projectId, CancellationToken.None))
......
......@@ -11,5 +11,12 @@ internal static class ServiceFeatureOnOffOptions
/// </summary>
[Obsolete("Currently used by TypeScript - should move to the new option SolutionCrawlerOptions.BackgroundAnalysisScopeOption")]
public static readonly PerLanguageOption<bool?> ClosedFileDiagnostic = SolutionCrawlerOptions.ClosedFileDiagnostic;
/// <summary>
/// This option is used by TypeScript.
/// </summary>
public static readonly PerLanguageOption<bool> RemoveDocumentDiagnosticsOnDocumentClose = new PerLanguageOption<bool>(
"ServiceFeatureOnOffOptions", "RemoveDocumentDiagnosticsOnDocumentClose", defaultValue: false,
storageLocations: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.Specific.RemoveDocumentDiagnosticsOnDocumentClose"));
}
}
......@@ -127,5 +127,47 @@ static void Main(string[] args)
actualContents = VisualStudio.ErrorList.GetErrorListContents();
Assert.Equal(expectedContents, actualContents);
}
public virtual void ErrorsAfterClosingFile()
{
VisualStudio.Editor.SetText(@"
class Program2
{
static void Main(string[] args)
{
int aa = 7;
int a = aa;
}
}
");
VisualStudio.ErrorList.ShowErrorList();
var expectedContents = new ErrorListItem[] { };
var actualContents = VisualStudio.ErrorList.GetErrorListContents();
Assert.Equal(expectedContents, actualContents);
VisualStudio.Editor.Activate();
VisualStudio.Editor.PlaceCaret("a = aa", charsOffset: -1);
VisualStudio.Editor.SendKeys("a");
VisualStudio.ErrorList.ShowErrorList();
expectedContents = new[] {
new ErrorListItem(
severity: "Error",
description: "A local variable or function named 'aa' is already defined in this scope",
project: "TestProj.csproj",
fileName: "Class1.cs",
line: 7,
column: 13)
};
actualContents = VisualStudio.ErrorList.GetErrorListContents();
Assert.Equal(expectedContents, actualContents);
// Close the current document and verify diagnostics for closed document are not removed from error list.
VisualStudio.SolutionExplorer.SaveAll();
VisualStudio.Editor.SendExplicitFocus();
VisualStudio.Editor.SendKeys(new KeyPress(VirtualKey.F4, ShiftState.Ctrl));
VisualStudio.ErrorList.ShowErrorList();
actualContents = VisualStudio.ErrorList.GetErrorListContents();
Assert.Equal(expectedContents, actualContents);
}
}
}
......@@ -33,5 +33,12 @@ public override void ErrorsDuringMethodBodyEditing()
{
base.ErrorsDuringMethodBodyEditing();
}
[WpfFact, Trait(Traits.Feature, Traits.Features.ErrorList)]
[WorkItem(39902, "https://github.com/dotnet/roslyn/issues/39902")]
public override void ErrorsAfterClosingFile()
{
base.ErrorsAfterClosingFile();
}
}
}
......@@ -39,5 +39,14 @@ public override void ErrorsDuringMethodBodyEditing()
{
base.ErrorsDuringMethodBodyEditing();
}
[WpfFact(Skip = "https://github.com/dotnet/roslyn/issues/39588")]
[Trait(Traits.Feature, Traits.Features.ErrorList)]
[Trait(Traits.Feature, Traits.Features.NetCore)]
[WorkItem(39902, "https://github.com/dotnet/roslyn/issues/39902")]
public override void ErrorsAfterClosingFile()
{
base.ErrorsAfterClosingFile();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册