Skip to content

Commit 9af0823

Browse files
Only run diagnostics on PowerShell files (#1241)
* Only run diagnostics on PowerShell files * add a test and use PowerShellDocumentSelector * use fake uri instead of null * cache uri
1 parent 00c0164 commit 9af0823

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private async Task DelayThenInvokeDiagnosticsAsync(ScriptFile[] filesToAnalyze,
410410

411411
private void PublishScriptDiagnostics(ScriptFile scriptFile, IReadOnlyList<ScriptFileMarker> markers)
412412
{
413-
var diagnostics = new Diagnostic[scriptFile.DiagnosticMarkers.Count];
413+
var diagnostics = new Diagnostic[markers.Count];
414414

415415
CorrectionTableEntry fileCorrections = _mostRecentCorrectionsByFile.GetOrAdd(
416416
scriptFile,

src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
3939
};
4040
}
4141

42-
public async Task<LocationContainer> Handle(ReferenceParams request, CancellationToken cancellationToken)
42+
public Task<LocationContainer> Handle(ReferenceParams request, CancellationToken cancellationToken)
4343
{
4444
ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
4545

@@ -69,7 +69,7 @@ public async Task<LocationContainer> Handle(ReferenceParams request, Cancellatio
6969
}
7070
}
7171

72-
return new LocationContainer(locations);
72+
return Task.FromResult(new LocationContainer(locations));
7373
}
7474

7575
public void SetCapability(ReferencesCapability capability)

src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
2121
{
2222
class TextDocumentHandler : ITextDocumentSyncHandler
2323
{
24+
private static readonly Uri s_fakeUri = new Uri("Untitled:fake");
2425

2526
private readonly ILogger _logger;
2627
private readonly AnalysisService _analysisService;
@@ -82,9 +83,16 @@ public Task<Unit> Handle(DidOpenTextDocumentParams notification, CancellationTok
8283
notification.TextDocument.Uri,
8384
notification.TextDocument.Text);
8485

85-
// Kick off script diagnostics without blocking the response
86-
// TODO: Get all recently edited files in the workspace
87-
_analysisService.RunScriptDiagnostics(new ScriptFile[] { openedFile }, token);
86+
if (LspUtils.PowerShellDocumentSelector.IsMatch(new TextDocumentAttributes(
87+
// We use a fake Uri because we only want to test the LanguageId here and not if the
88+
// file ends in ps*1.
89+
s_fakeUri,
90+
notification.TextDocument.LanguageId)))
91+
{
92+
// Kick off script diagnostics if we got a PowerShell file without blocking the response
93+
// TODO: Get all recently edited files in the workspace
94+
_analysisService.RunScriptDiagnostics(new ScriptFile[] { openedFile }, token);
95+
}
8896

8997
_logger.LogTrace("Finished opening document.");
9098
return Unit.Task;

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void Dispose()
5959
Diagnostics.Clear();
6060
}
6161

62-
private string NewTestFile(string script, bool isPester = false)
62+
private string NewTestFile(string script, bool isPester = false, string languageId = "powershell")
6363
{
6464
string fileExt = isPester ? ".Tests.ps1" : ".ps1";
6565
string filePath = Path.Combine(s_binDir, Path.GetRandomFileName() + fileExt);
@@ -69,7 +69,7 @@ private string NewTestFile(string script, bool isPester = false)
6969
{
7070
TextDocument = new TextDocumentItem
7171
{
72-
LanguageId = "powershell",
72+
LanguageId = languageId,
7373
Version = 0,
7474
Text = script,
7575
Uri = new Uri(filePath)
@@ -145,6 +145,15 @@ public async Task CanReceiveDiagnosticsFromFileOpen()
145145
Assert.Equal("PSUseDeclaredVarsMoreThanAssignments", diagnostic.Code);
146146
}
147147

148+
[Fact]
149+
public async Task WontReceiveDiagnosticsFromFileOpenThatIsNotPowerShell()
150+
{
151+
NewTestFile("$a = 4", languageId: "plaintext");
152+
await Task.Delay(2000);
153+
154+
Assert.Empty(Diagnostics);
155+
}
156+
148157
[Fact]
149158
public async Task CanReceiveDiagnosticsFromFileChanged()
150159
{

0 commit comments

Comments
 (0)