public async Task <RunCodeActionResponse> Handle(RunCodeActionRequest request)
        {
            var actions = await CodeActionHelper.GetActions(_workspace, _codeActionProviders, _logger, request);

            var action = actions.FirstOrDefault(a => a.GetIdentifier().Equals(request.Identifier));

            if (action == null)
            {
                return(new RunCodeActionResponse());
            }

            _logger.LogInformation("Applying " + action);
            var operations = await action.GetOperationsAsync(CancellationToken.None);

            var solution = _workspace.CurrentSolution;

            foreach (var o in operations)
            {
                o.Apply(_workspace, CancellationToken.None);
            }

            var response      = new RunCodeActionResponse();
            var directoryName = Path.GetDirectoryName(request.FileName);
            var changes       = await FileChanges.GetFileChangesAsync(_workspace.CurrentSolution, solution, directoryName, request.WantsTextChanges);

            response.Changes = changes;
            _workspace.TryApplyChanges(_workspace.CurrentSolution);
            return(response);
        }
        public async Task <FixUsingsResponse> Handle(FixUsingsRequest request)
        {
            var document = _workspace.GetDocument(request.FileName);
            var response = new FixUsingsResponse();

            if (document != null)
            {
                var fixUsingsResponse = await new FixUsingsWorker(_loggerFactory, _loader).FixUsings(_workspace.CurrentSolution, _codeActionProviders, request.FileName);
                response.AmbiguousResults = fixUsingsResponse.AmbiguousResults;

                if (request.ApplyTextChanges)
                {
                    _workspace.TryApplyChanges(fixUsingsResponse.Solution);
                }

                if (!request.WantsTextChanges)
                {
                    // return the new document
                    var docText = await fixUsingsResponse.Solution.GetDocument(document.Id).GetTextAsync();

                    response.Buffer = docText.ToString();
                }
                else
                {
                    // return the text changes
                    var changes = await fixUsingsResponse.Solution.GetDocument(document.Id).GetTextChangesAsync(document);

                    response.Changes = await LinePositionSpanTextChange.Convert(document, changes);
                }
            }

            return(response);
        }
        public async Task <RunCodeActionResponse> Handle(RunCodeActionRequest request)
        {
            var response = new RunCodeActionResponse();

            var actions = await CodeActionHelper.GetActions(_workspace, _codeActionProviders, _logger, request);

            var action = actions.FirstOrDefault(a => a.GetIdentifier().Equals(request.Identifier));

            if (action == null)
            {
                return(new RunCodeActionResponse());
            }

            _logger.LogInformation("Applying " + action);
            var operations = await action.GetOperationsAsync(CancellationToken.None);

            var solution      = _workspace.CurrentSolution;
            var changes       = Enumerable.Empty <OmniSharp.Models.ModifiedFileResponse>();
            var directoryName = Path.GetDirectoryName(request.FileName);

            foreach (var o in operations)
            {
                var applyChangesOperation = o as ApplyChangesOperation;
                if (applyChangesOperation != null)
                {
                    changes  = changes.Concat(await FileChanges.GetFileChangesAsync(applyChangesOperation.ChangedSolution, solution, directoryName, request.WantsTextChanges));
                    solution = applyChangesOperation.ChangedSolution;
                }
            }

            if (request.ApplyTextChanges)
            {
                _workspace.TryApplyChanges(solution);
            }

            response.Changes = changes;
            return(response);
        }
Exemple #4
0
        public async Task <RenameResponse> Handle(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, request.Column));

                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);
        }