Esempio n. 1
0
        public async Task InitializeAsync()
        {
            var (client, configurationProvider) = await InitializeClientWithConfiguration(x =>
            {
                x.WithCapability(new WorkspaceEditCapability()
                {
                    DocumentChanges = true,
                    FailureHandling = FailureHandlingKind.Undo,
                    ResourceOperations = new[]
                    {
                        ResourceOperationKind.Create, ResourceOperationKind.Delete, ResourceOperationKind.Rename
                    }
                });

                x.OnPublishDiagnostics(result =>
                {
                    _diagnostics.AddOrUpdate(result.Uri, result.Diagnostics,
                        (a, b) => result.Diagnostics);
                });
                x.OnApplyWorkspaceEdit(async @params =>
                {
                    if (@params.Edit?.Changes != null)
                    {
                        foreach (var change in @params.Edit.Changes)
                        {
                            var changes = change.Value
                                .Select(change => new LinePositionSpanTextChange()
                                {
                                    NewText = change.NewText,
                                    StartColumn = Convert.ToInt32(change.Range.Start.Character),
                                    StartLine = Convert.ToInt32(change.Range.Start.Line),
                                    EndColumn = Convert.ToInt32(change.Range.End.Character),
                                    EndLine = Convert.ToInt32(change.Range.End.Line),
                                })
                                .ToArray();

                            await OmniSharpTestHost.Workspace.BufferManager.UpdateBufferAsync(new UpdateBufferRequest()
                            {
                                FileName = LanguageServerProtocol.Helpers.FromUri(change.Key),
                                Changes = changes
                            });
                        }
                    }
                    else if (@params.Edit?.DocumentChanges != null)
                    {
                        foreach (var change in @params.Edit.DocumentChanges)
                        {
                            if (change.IsTextDocumentEdit)
                            {
                                var contentChanges = change.TextDocumentEdit.Edits.ToArray();
                                if (contentChanges.Length == 1 && contentChanges[0].Range == null)
                                {
                                    var c = contentChanges[0];
                                    await OmniSharpTestHost.Workspace.BufferManager.UpdateBufferAsync(
                                        new UpdateBufferRequest()
                                        {
                                            FileName = LanguageServerProtocol.Helpers.FromUri(change.TextDocumentEdit
                                                .TextDocument.Uri),
                                            Buffer = c.NewText
                                        });
                                }
                                else
                                {
                                    var changes = contentChanges
                                        .Select(change => new LinePositionSpanTextChange()
                                        {
                                            NewText = change.NewText,
                                            StartColumn = Convert.ToInt32(change.Range.Start.Character),
                                            StartLine = Convert.ToInt32(change.Range.Start.Line),
                                            EndColumn = Convert.ToInt32(change.Range.End.Character),
                                            EndLine = Convert.ToInt32(change.Range.End.Line),
                                        })
                                        .ToArray();

                                    await OmniSharpTestHost.Workspace.BufferManager.UpdateBufferAsync(
                                        new UpdateBufferRequest()
                                        {
                                            FileName = LanguageServerProtocol.Helpers.FromUri(change.TextDocumentEdit
                                                .TextDocument.Uri),
                                            Changes = changes
                                        });
                                }
                            }

                            if (change.IsRenameFile)
                            {
                                var documents =
                                    OmniSharpTestHost.Workspace.GetDocuments(
                                        change.RenameFile.OldUri.GetFileSystemPath());
                                foreach (var oldDocument in documents)
                                {
                                    var text = await oldDocument.GetTextAsync();
                                    var newFilePath = change.RenameFile.NewUri.GetFileSystemPath();
                                    var newFileName = Path.GetFileName(newFilePath);
                                    OmniSharpTestHost.Workspace.TryApplyChanges(
                                        OmniSharpTestHost.Workspace.CurrentSolution
                                            .RemoveDocument(oldDocument.Id)
                                            .AddDocument(
                                                DocumentId.CreateNewId(oldDocument.Project.Id, newFileName),
                                                newFileName,
                                                text,
                                                oldDocument.Folders,
                                                newFilePath
                                            )
                                    );
                                }
                            }
                        }
                    }

                    await ClientEvents.SettleNext();

                    return new ApplyWorkspaceEditResponse()
                    {
                        Applied = true
                    };
                });
            });
            Client = client;

            await startUpTask;
            Configuration = new ConfigurationProvider(Server, Client, configurationProvider, CancellationToken);
        }