Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/PowerShellEditorServices/PowerShellEditorServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.1" />
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.14.2" />
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.15.0" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
Expand All @@ -40,6 +40,6 @@
<PackageReference Include="System.Security.Principal" Version="4.3.0" />
<PackageReference Include="System.Security.Principal.Windows" Version="4.7.0" />
<PackageReference Include="UnixConsoleEcho" Version="0.1.0" />
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.14.2" />
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.15.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -86,11 +85,23 @@ public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request,
string diagnosticId = AnalysisService.GetUniqueIdFromDiagnostic(diagnostic);
if (corrections.TryGetValue(diagnosticId, out MarkerCorrection correction))
{
codeActions.Add(new Command()
codeActions.Add(new CodeAction
{
Title = correction.Name,
Name = "PowerShell.ApplyCodeActionEdits",
Arguments = JArray.FromObject(correction.Edits)
Kind = CodeActionKind.QuickFix,
Edit = new WorkspaceEdit
{
DocumentChanges = new Container<WorkspaceEditDocumentChange>(
new WorkspaceEditDocumentChange(
new TextDocumentEdit
{
TextDocument = new VersionedTextDocumentIdentifier
{
Uri = request.TextDocument.Uri
},
Edits = new Container<TextEdit>(correction.Edits.Select(ScriptRegion.ToTextEdit))
}))
}
});
}
}
Expand All @@ -107,14 +118,21 @@ public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request,
!ruleNamesProcessed.Contains(diagnostic.Code.String))
{
ruleNamesProcessed.Add(diagnostic.Code.String);

codeActions.Add(
new Command
var title = $"Show documentation for: {diagnostic.Code.String}";
codeActions.Add(new CodeAction
{
Title = title,
// This doesn't fix anything, but I'm adding it here so that it shows up in VS Code's
// Quick fix UI. The VS Code team is working on a way to support documentation CodeAction's better
// but this is good for now until that's ready.
Kind = CodeActionKind.QuickFix,
Command = new Command
{
Title = $"Show documentation for \"{diagnostic.Code}\"",
Title = title,
Name = "PowerShell.ShowCodeActionDocumentation",
Arguments = JArray.FromObject(new[] { diagnostic.Code })
});
Arguments = JArray.FromObject(new[] { diagnostic.Code.String })
}
});
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Management.Automation.Language;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Microsoft.PowerShell.EditorServices.Services.TextDocument
{
Expand Down Expand Up @@ -49,6 +50,27 @@ public static ScriptRegion Create(IScriptExtent scriptExtent)
scriptExtent.EndOffset);
}

internal static TextEdit ToTextEdit(ScriptRegion scriptRegion)
{
return new TextEdit
{
NewText = scriptRegion.Text,
Range = new Range
{
Start = new Position
{
Line = scriptRegion.StartLineNumber - 1,
Character = scriptRegion.StartColumnNumber - 1,
},
End = new Position
{
Line = scriptRegion.EndLineNumber - 1,
Character = scriptRegion.EndColumnNumber - 1,
}
}
};
}

#endregion

#region Constructors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,23 @@ await LanguageClient.SendRequest<CommandOrCodeActionContainer>(
}
});

Assert.Single(commandOrCodeActions,
command => command.Command.Name == "PowerShell.ApplyCodeActionEdits");
Assert.Collection(commandOrCodeActions,
command =>
{
Assert.Equal(
"Replace gci with Get-ChildItem",
command.CodeAction.Title);
Assert.Equal(
CodeActionKind.QuickFix.Kind,
command.CodeAction.Kind.Kind);
Assert.Single(command.CodeAction.Edit.DocumentChanges);
},
command =>
{
Assert.Equal(
"PowerShell.ShowCodeActionDocumentation",
command.CodeAction.Command.Name);
});
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.14.2" />
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.15.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
Expand Down