Skip to content

Commit c9f3582

Browse files
Update for PSES preview 9 (#49)
- Refactor to use new API's - Fix some inconsistencies with ExpandMemberExpression - Fix a bug where the drop namespace refactor would not work with generic type names - Add a refactor to change named block kinds
1 parent 62944c0 commit c9f3582

34 files changed

+566
-491
lines changed

EditorServicesCommandSuite.build.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ task CopyToRelease {
155155
$srcBase = "$CSharpPath/$ModuleName"
156156
Copy-Item "$srcBase/bin/$Configuration/$Framework/publish/EditorServicesCommandSuite.*" -Destination $ReleasePath
157157
Copy-Item "$srcBase.EditorServices/bin/$Configuration/$Framework/publish/EditorServicesCommandSuite.*" -Destination $ReleasePath
158+
Copy-Item "$srcBase.EditorServices/bin/$Configuration/$Framework/publish/*.dll" -Destination $ReleasePath
158159

159160
$psrlReleaseItems =
160161
'EditorServicesCommandSuite.*',

module/EditorServicesCommandSuite.psd1

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ FunctionsToExport = 'Add-ModuleQualification',
6161
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
6262
CmdletsToExport = 'Get-CommandSuiteSetting',
6363
'Set-CommandSuiteSetting',
64-
'New-CommandSuiteSettingFile',
65-
'Get-RefactorOption'
64+
'New-CommandSuiteSettingFile'
6665

6766
# Variables to export from this module
6867
VariablesToExport = @()
@@ -121,6 +120,3 @@ PrivateData = @{
121120
} # End of PrivateData hashtable
122121

123122
}
124-
125-
126-

module/EditorServicesCommandSuite.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Update-FormatData -AppendPath $PSScriptRoot/EditorServicesCommandSuite.format.ps
44

55
if ($null -ne $psEditor) {
66
if ($PSVersionTable.PSVersion.Major -ge 6) {
7-
$psesLoadContext = [System.Runtime.Loader.AssemblyLoadContext]::GetLoadContext(
8-
[Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorObject].Assembly)
7+
$extensionService = [Microsoft.PowerShell.EditorServices.Extensions.EditorObjectExtensions]::
8+
GetExtensionServiceProvider($psEditor)
99

10-
$assembly = $psesLoadContext.LoadFromAssemblyPath((
10+
$assembly = $extensionService.LoadAssemblyInPsesLoadContext((
1111
Join-Path $PSScriptRoot -ChildPath 'EditorServicesCommandSuite.EditorServices.dll'))
1212

1313
$type = $assembly.GetType('EditorServicesCommandSuite.EditorServices.Internal.CommandSuite')

src/EditorServicesCommandSuite.EditorServices/ContextService.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
using System.Linq;
12
using System.Management.Automation;
23
using System.Threading;
34
using System.Threading.Tasks;
45

56
using EditorServicesCommandSuite.Internal;
67
using EditorServicesCommandSuite.Utility;
7-
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
8-
9-
using PSWorkspaceService = Microsoft.PowerShell.EditorServices.Services.WorkspaceService;
8+
using Microsoft.PowerShell.EditorServices.Extensions.Services;
109

1110
namespace EditorServicesCommandSuite.EditorServices
1211
{
1312
internal class ContextService : DocumentContextProvider
1413
{
15-
private readonly MessageService _messages;
14+
private readonly IWorkspaceService _workspace;
1615

17-
private readonly PSWorkspaceService _workspace;
16+
private readonly IEditorContextService _context;
1817

19-
internal ContextService(PSWorkspaceService workspace, MessageService messages)
18+
internal ContextService(IWorkspaceService workspace, IEditorContextService context)
2019
{
2120
_workspace = workspace;
22-
_messages = messages;
21+
_context = context;
2322
}
2423

2524
internal override string Workspace => _workspace.WorkspacePath;
@@ -29,13 +28,9 @@ internal override async Task<DocumentContextBase> GetDocumentContextAsync(
2928
CancellationToken cancellationToken,
3029
ThreadController threadController)
3130
{
32-
var context = await _messages.SendRequestAsync(
33-
Messages.GetEditorContext,
34-
new GetEditorContextRequest())
35-
.ConfigureAwait(false);
36-
37-
var scriptFile = _workspace.GetFile(context.CurrentFilePath);
38-
return GetContextBuilder(scriptFile.ScriptAst, scriptFile.ScriptTokens)
31+
var context = await _context.GetCurrentLspFileContextAsync().ConfigureAwait(false);
32+
var scriptFile = _workspace.GetFile(context.Uri);
33+
return GetContextBuilder(scriptFile.Ast, scriptFile.Tokens.ToArray())
3934
.AddCursorPosition(
4035
(int)context.CursorPosition.Line + 1,
4136
(int)context.CursorPosition.Character + 1)

src/EditorServicesCommandSuite.EditorServices/DiagnosticsService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Management.Automation;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
89
using EditorServicesCommandSuite.Internal;
910
using EditorServicesCommandSuite.Utility;
1011

src/EditorServicesCommandSuite.EditorServices/DocumentService.cs

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,54 @@
66
using System.Threading.Tasks;
77

88
using EditorServicesCommandSuite.Internal;
9-
using Microsoft.PowerShell.EditorServices.Handlers;
10-
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
11-
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
9+
using Microsoft.PowerShell.EditorServices.Extensions;
10+
using Microsoft.PowerShell.EditorServices.Extensions.Services;
1211
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
13-
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
14-
15-
using PSWorkspaceService = Microsoft.PowerShell.EditorServices.Services.WorkspaceService;
1612

1713
namespace EditorServicesCommandSuite.EditorServices
1814
{
1915
internal class DocumentService : IDocumentEditProcessor
2016
{
21-
private readonly PSWorkspaceService _workspace;
17+
private readonly IWorkspaceService _workspace;
18+
19+
private readonly IEditorContextService _context;
2220

23-
private readonly MessageService _messages;
21+
private readonly ILanguageServerService _messages;
2422

25-
internal DocumentService(PSWorkspaceService workspace, MessageService messages)
23+
internal DocumentService(
24+
IWorkspaceService workspace,
25+
IEditorContextService context,
26+
ILanguageServerService messages)
2627
{
2728
_workspace = workspace;
29+
_context = context;
2830
_messages = messages;
2931
}
3032

3133
public async Task WriteDocumentEditsAsync(IEnumerable<DocumentEdit> edits, CancellationToken cancellationToken)
3234
{
33-
ClientEditorContext context = await GetClientContext()
35+
ILspCurrentFileContext context = await GetClientContext()
3436
.ConfigureAwait(false);
3537

3638
// Order by empty file names first so the first group processed is the current file.
37-
IOrderedEnumerable<IGrouping<string, DocumentEdit>> orderedGroups = edits
38-
.GroupBy(e => e.FileName)
39-
.OrderByDescending(g => string.IsNullOrEmpty(g.Key));
39+
IOrderedEnumerable<IGrouping<Uri, DocumentEdit>> orderedGroups = edits
40+
.GroupBy(e => e.Uri)
41+
.OrderByDescending(g => g.Key == null);
4042

4143
var workspaceChanges = new List<WorkspaceEditDocumentChange>();
42-
foreach (IGrouping<string, DocumentEdit> editGroup in orderedGroups)
44+
foreach (IGrouping<Uri, DocumentEdit> editGroup in orderedGroups)
4345
{
44-
ScriptFile scriptFile;
46+
IEditorScriptFile scriptFile;
4547
try
4648
{
47-
scriptFile = _workspace.GetFile(
48-
string.IsNullOrEmpty(editGroup.Key) ? context.CurrentFilePath : editGroup.Key);
49+
scriptFile = _workspace.GetFile(editGroup.Key ?? context.Uri);
4950
}
5051
catch (FileNotFoundException)
5152
{
5253
scriptFile = await CreateNewFile(context, editGroup.Key, cancellationToken)
5354
.ConfigureAwait(false);
5455
}
5556

56-
// ScriptFile.ClientFilePath isn't always a URI.
57-
string clientFilePath = DocumentHelpers.GetPathAsClientPath(scriptFile.ClientFilePath);
5857
var textEdits = new List<TextEdit>();
5958
foreach (DocumentEdit edit in editGroup)
6059
{
@@ -77,7 +76,7 @@ public async Task WriteDocumentEditsAsync(IEnumerable<DocumentEdit> edits, Cance
7776

7877
var versionedIdentifier = new VersionedTextDocumentIdentifier
7978
{
80-
Uri = new Uri(clientFilePath),
79+
Uri = editGroup.Key ?? context.Uri,
8180
Version = default,
8281
};
8382

@@ -91,60 +90,47 @@ public async Task WriteDocumentEditsAsync(IEnumerable<DocumentEdit> edits, Cance
9190
}
9291

9392
var workspaceEdit = new WorkspaceEdit { DocumentChanges = workspaceChanges };
94-
await _messages.Sender.Workspace.ApplyEdit(
93+
await _messages.ApplyEdit(
9594
new ApplyWorkspaceEditParams { Edit = workspaceEdit })
9695
.ConfigureAwait(false);
9796
}
9897

99-
internal static Position ToServerPosition(BufferPosition position)
98+
internal static Position ToServerPosition(LspPosition position)
10099
{
101100
return new Position()
102101
{
103102
Line = position.Line - 1,
104-
Character = position.Column - 1,
103+
Character = position.Character - 1,
105104
};
106105
}
107106

108-
private async Task<ScriptFile> CreateNewFile(
109-
ClientEditorContext context,
110-
string path,
107+
private async Task<IEditorScriptFile> CreateNewFile(
108+
ILspCurrentFileContext context,
109+
Uri uri,
111110
CancellationToken cancellationToken)
112111
{
113112
// Path parameter doesn't actually do anything currently. The new file will be untitled.
114-
await _messages.SendRequestAsync(Messages.NewFile, path)
115-
.ConfigureAwait(false);
113+
await _context.OpenNewUntitledFileAsync().ConfigureAwait(false);
116114

117-
ClientEditorContext newContext;
115+
ILspCurrentFileContext newContext;
118116
while (true)
119117
{
120118
newContext = await GetClientContext().ConfigureAwait(false);
121-
if (!newContext.CurrentFilePath.Equals(context.CurrentFilePath, StringComparison.OrdinalIgnoreCase))
119+
if (newContext.Uri != context.Uri)
122120
{
123121
break;
124122
}
125123

126124
await Task.Delay(200, cancellationToken).ConfigureAwait(false);
127125
}
128126

129-
ScriptFile scriptFile = _workspace.GetFile(newContext.CurrentFilePath);
130-
await _messages.SendRequestAsync(
131-
Messages.SaveFile,
132-
new SaveFileDetails()
133-
{
134-
FilePath = scriptFile.ClientFilePath,
135-
NewPath = path,
136-
}).ConfigureAwait(false);
127+
IEditorScriptFile scriptFile = _workspace.GetFile(newContext.Uri);
128+
await _context.SaveFileAsync(scriptFile.Uri, uri).ConfigureAwait(false);
137129

138130
cancellationToken.ThrowIfCancellationRequested();
139-
return _workspace.GetFile(path);
131+
return _workspace.GetFile(uri);
140132
}
141133

142-
private async Task<ClientEditorContext> GetClientContext()
143-
{
144-
return await _messages.SendRequestAsync(
145-
Messages.GetEditorContext,
146-
new GetEditorContextRequest())
147-
.ConfigureAwait(false);
148-
}
134+
private Task<ILspCurrentFileContext> GetClientContext() => _context.GetCurrentLspFileContextAsync();
149135
}
150136
}

0 commit comments

Comments
 (0)