Example #1
0
        private async Task<RenameResponse> SendRequest(OmnisharpWorkspace workspace,
                                                       string renameTo,
                                                       string filename,
                                                       string fileContent,
                                                       bool wantsTextChanges = false,
                                                       bool applyTextChanges = true)
        {
            var lineColumn = TestHelpers.GetLineAndColumnFromDollar(fileContent);
            var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
            var request = new RenameRequest
            {
                Line = lineColumn.Line,
                Column = lineColumn.Column,
                RenameTo = renameTo,
                FileName = filename,
                Buffer = fileContent.Replace("$", ""),
                WantsTextChanges = wantsTextChanges,
                ApplyTextChanges = applyTextChanges
            };

            var bufferFilter = new UpdateBufferFilter(workspace);
            bufferFilter.OnActionExecuting(TestHelpers.CreateActionExecutingContext(request, controller));

            return await controller.Rename(request);
        }
        private async Task<RenameResponse> SendRequest(OmnisharpWorkspace workspace,
                                                       string renameTo,
                                                       string filename,
                                                       string fileContent,
                                                       bool wantsTextChanges = false,
                                                       bool applyTextChanges = true)
        {
            var lineColumn = TestHelpers.GetLineAndColumnFromDollar(fileContent);
            var controller = new RenameService(workspace);
            var request = new RenameRequest
            {
                Line = lineColumn.Line,
                Column = lineColumn.Column,
                RenameTo = renameTo,
                FileName = filename,
                Buffer = fileContent.Replace("$", ""),
                WantsTextChanges = wantsTextChanges,
                ApplyTextChanges = applyTextChanges
            };

            await workspace.BufferManager.UpdateBuffer(request);

            return await controller.Handle(request);
        }
        public async Task<RenameResponse> Rename(RenameRequest request)
        {
            var response = new RenameResponse();

            var document = _workspace.GetDocument(request.FileName);
            if (document != null)
            {
                var sourceText = await document.GetTextAsync();
                var position = sourceText.Lines.GetPosition(new LinePosition(request.Line - 1, request.Column - 1));

                var symbol = await SymbolFinder.FindSymbolAtPositionAsync(document, position);
                Solution solution = _workspace.CurrentSolution;

                if (symbol != null)
                {
                    try
                    {
                        solution = await Renamer.RenameSymbolAsync(solution, symbol, request.RenameTo, _workspace.Options);
                    }
                    catch (ArgumentException e)
                    {
                        response.ErrorMessage = e.Message;
                    }
                }

                var changes = new Dictionary<string, ModifiedFileResponse>();
                var solutionChanges = solution.GetChanges(_workspace.CurrentSolution);

                foreach (var projectChange in solutionChanges.GetProjectChanges())
                {
                    foreach (var changedDocumentId in projectChange.GetChangedDocuments())
                    {
                        var changedDocument = solution.GetDocument(changedDocumentId);

                        ModifiedFileResponse modifiedFileResponse;
                        if (!changes.TryGetValue(changedDocument.FilePath, out modifiedFileResponse))
                        {
                            modifiedFileResponse = new ModifiedFileResponse(changedDocument.FilePath);
                            changes[changedDocument.FilePath] = modifiedFileResponse;
                        }

                        if (!request.WantsTextChanges)
                        {
                            var changedText = await changedDocument.GetTextAsync();
                            modifiedFileResponse.Buffer = changedText.ToString();
                        }
                        else
                        {
                            var originalDocument = _workspace.CurrentSolution.GetDocument(changedDocumentId);
                            var textChanges = await changedDocument.GetTextChangesAsync(originalDocument);
                            var linePositionSpanTextChanges = await LinePositionSpanTextChange.Convert(originalDocument, textChanges);

                            modifiedFileResponse.Changes = modifiedFileResponse.Changes != null
                                ? modifiedFileResponse.Changes.Union(linePositionSpanTextChanges)
                                : linePositionSpanTextChanges;
                        }
                    }
                }

                if (request.ApplyTextChanges)
                {
                    // Attempt to update the workspace
                    if (_workspace.TryApplyChanges(solution))
                    {
                        response.Changes = changes.Values;
                    }
                }
                else
                {
                    response.Changes = changes.Values;
                }
            }

            return response;
        }