Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only run diagnostics on PowerShell files #1241

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ private async Task DelayThenInvokeDiagnosticsAsync(ScriptFile[] filesToAnalyze,

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

CorrectionTableEntry fileCorrections = _mostRecentCorrectionsByFile.GetOrAdd(
scriptFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()
};
}

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

Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task<LocationContainer> Handle(ReferenceParams request, Cancellatio
}
}

return new LocationContainer(locations);
return Task.FromResult(new LocationContainer(locations));
}

public void SetCapability(ReferencesCapability capability)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ public Task<Unit> Handle(DidOpenTextDocumentParams notification, CancellationTok
notification.TextDocument.Uri,
notification.TextDocument.Text);

// Kick off script diagnostics without blocking the response
// TODO: Get all recently edited files in the workspace
_analysisService.RunScriptDiagnostics(new ScriptFile[] { openedFile }, token);
if (LspUtils.PowerShellDocumentSelector.IsMatch(new TextDocumentAttributes(
// We use a fake Uri because we only want to test the LanguageId here and not if the
// file ends in ps*1.
new Uri("Untitled:file"),
TylerLeonhardt marked this conversation as resolved.
Show resolved Hide resolved
notification.TextDocument.LanguageId)))
{
// Kick off script diagnostics if we got a PowerShell file without blocking the response
// TODO: Get all recently edited files in the workspace
_analysisService.RunScriptDiagnostics(new ScriptFile[] { openedFile }, token);
}

_logger.LogTrace("Finished opening document.");
return Unit.Task;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void Dispose()
Diagnostics.Clear();
}

private string NewTestFile(string script, bool isPester = false)
private string NewTestFile(string script, bool isPester = false, string languageId = "powershell")
{
string fileExt = isPester ? ".Tests.ps1" : ".ps1";
string filePath = Path.Combine(s_binDir, Path.GetRandomFileName() + fileExt);
Expand All @@ -69,7 +69,7 @@ private string NewTestFile(string script, bool isPester = false)
{
TextDocument = new TextDocumentItem
{
LanguageId = "powershell",
LanguageId = languageId,
Version = 0,
Text = script,
Uri = new Uri(filePath)
Expand Down Expand Up @@ -145,6 +145,15 @@ public async Task CanReceiveDiagnosticsFromFileOpen()
Assert.Equal("PSUseDeclaredVarsMoreThanAssignments", diagnostic.Code);
}

[Fact]
public async Task WontReceiveDiagnosticsFromFileOpenThatIsNotPowerShell()
{
NewTestFile("$a = 4", languageId: "plaintext");
await Task.Delay(2000);

Assert.Empty(Diagnostics);
}

[Fact]
public async Task CanReceiveDiagnosticsFromFileChanged()
{
Expand Down