6
6
using System . Threading . Tasks ;
7
7
8
8
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 ;
12
11
using OmniSharp . Extensions . LanguageServer . Protocol . Models ;
13
- using OmniSharp . Extensions . LanguageServer . Protocol . Server ;
14
-
15
- using PSWorkspaceService = Microsoft . PowerShell . EditorServices . Services . WorkspaceService ;
16
12
17
13
namespace EditorServicesCommandSuite . EditorServices
18
14
{
19
15
internal class DocumentService : IDocumentEditProcessor
20
16
{
21
- private readonly PSWorkspaceService _workspace ;
17
+ private readonly IWorkspaceService _workspace ;
18
+
19
+ private readonly IEditorContextService _context ;
22
20
23
- private readonly MessageService _messages ;
21
+ private readonly ILanguageServerService _messages ;
24
22
25
- internal DocumentService ( PSWorkspaceService workspace , MessageService messages )
23
+ internal DocumentService (
24
+ IWorkspaceService workspace ,
25
+ IEditorContextService context ,
26
+ ILanguageServerService messages )
26
27
{
27
28
_workspace = workspace ;
29
+ _context = context ;
28
30
_messages = messages ;
29
31
}
30
32
31
33
public async Task WriteDocumentEditsAsync ( IEnumerable < DocumentEdit > edits , CancellationToken cancellationToken )
32
34
{
33
- ClientEditorContext context = await GetClientContext ( )
35
+ ILspCurrentFileContext context = await GetClientContext ( )
34
36
. ConfigureAwait ( false ) ;
35
37
36
38
// 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 ) ;
40
42
41
43
var workspaceChanges = new List < WorkspaceEditDocumentChange > ( ) ;
42
- foreach ( IGrouping < string , DocumentEdit > editGroup in orderedGroups )
44
+ foreach ( IGrouping < Uri , DocumentEdit > editGroup in orderedGroups )
43
45
{
44
- ScriptFile scriptFile ;
46
+ IEditorScriptFile scriptFile ;
45
47
try
46
48
{
47
- scriptFile = _workspace . GetFile (
48
- string . IsNullOrEmpty ( editGroup . Key ) ? context . CurrentFilePath : editGroup . Key ) ;
49
+ scriptFile = _workspace . GetFile ( editGroup . Key ?? context . Uri ) ;
49
50
}
50
51
catch ( FileNotFoundException )
51
52
{
52
53
scriptFile = await CreateNewFile ( context , editGroup . Key , cancellationToken )
53
54
. ConfigureAwait ( false ) ;
54
55
}
55
56
56
- // ScriptFile.ClientFilePath isn't always a URI.
57
- string clientFilePath = DocumentHelpers . GetPathAsClientPath ( scriptFile . ClientFilePath ) ;
58
57
var textEdits = new List < TextEdit > ( ) ;
59
58
foreach ( DocumentEdit edit in editGroup )
60
59
{
@@ -77,7 +76,7 @@ public async Task WriteDocumentEditsAsync(IEnumerable<DocumentEdit> edits, Cance
77
76
78
77
var versionedIdentifier = new VersionedTextDocumentIdentifier
79
78
{
80
- Uri = new Uri ( clientFilePath ) ,
79
+ Uri = editGroup . Key ?? context . Uri ,
81
80
Version = default ,
82
81
} ;
83
82
@@ -91,60 +90,47 @@ public async Task WriteDocumentEditsAsync(IEnumerable<DocumentEdit> edits, Cance
91
90
}
92
91
93
92
var workspaceEdit = new WorkspaceEdit { DocumentChanges = workspaceChanges } ;
94
- await _messages . Sender . Workspace . ApplyEdit (
93
+ await _messages . ApplyEdit (
95
94
new ApplyWorkspaceEditParams { Edit = workspaceEdit } )
96
95
. ConfigureAwait ( false ) ;
97
96
}
98
97
99
- internal static Position ToServerPosition ( BufferPosition position )
98
+ internal static Position ToServerPosition ( LspPosition position )
100
99
{
101
100
return new Position ( )
102
101
{
103
102
Line = position . Line - 1 ,
104
- Character = position . Column - 1 ,
103
+ Character = position . Character - 1 ,
105
104
} ;
106
105
}
107
106
108
- private async Task < ScriptFile > CreateNewFile (
109
- ClientEditorContext context ,
110
- string path ,
107
+ private async Task < IEditorScriptFile > CreateNewFile (
108
+ ILspCurrentFileContext context ,
109
+ Uri uri ,
111
110
CancellationToken cancellationToken )
112
111
{
113
112
// 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 ) ;
116
114
117
- ClientEditorContext newContext ;
115
+ ILspCurrentFileContext newContext ;
118
116
while ( true )
119
117
{
120
118
newContext = await GetClientContext ( ) . ConfigureAwait ( false ) ;
121
- if ( ! newContext . CurrentFilePath . Equals ( context . CurrentFilePath , StringComparison . OrdinalIgnoreCase ) )
119
+ if ( newContext . Uri != context . Uri )
122
120
{
123
121
break ;
124
122
}
125
123
126
124
await Task . Delay ( 200 , cancellationToken ) . ConfigureAwait ( false ) ;
127
125
}
128
126
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 ) ;
137
129
138
130
cancellationToken . ThrowIfCancellationRequested ( ) ;
139
- return _workspace . GetFile ( path ) ;
131
+ return _workspace . GetFile ( uri ) ;
140
132
}
141
133
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 ( ) ;
149
135
}
150
136
}
0 commit comments