private async Task <CodeRefactoringContext?> GetRefactoringContext(ICodeActionRequest request, List <CodeAction> actionsDestination)
        {
            var sourceText = await _originalDocument.GetTextAsync();

            var location = GetTextSpan(request, sourceText);

            return(new CodeRefactoringContext(_originalDocument, location, (a) => actionsDestination.Add(a), CancellationToken.None));
        }
        private TextSpan GetTextSpan(ICodeActionRequest request, SourceText sourceText)
        {
            if (request.Selection != null)
            {
                return(sourceText.GetSpanFromRange(request.Selection));
            }

            var position = sourceText.GetPositionFromLineAndOffset(request.Line, request.Column);

            return(new TextSpan(position, length: 0));
        }
 private static TextSpan GetTextSpan(ICodeActionRequest request, SourceText sourceText)
 {
     if (request.Selection != null)
     {
         var startPosition = sourceText.Lines.GetPosition(new LinePosition(request.Selection.Start.Line - 1, request.Selection.Start.Column - 1));
         var endPosition = sourceText.Lines.GetPosition(new LinePosition(request.Selection.End.Line - 1, request.Selection.End.Column - 1));
         return TextSpan.FromBounds(startPosition, endPosition);
     }
     var position = sourceText.Lines.GetPosition(new LinePosition(request.Line - 1, request.Column - 1));
     return new TextSpan(position, 1);
 }
Пример #4
0
        private static TextSpan GetTextSpan(ICodeActionRequest request, SourceText sourceText)
        {
            if (request.Selection != null)
            {
                var startPosition = sourceText.Lines.GetPosition(new LinePosition(request.Selection.Start.Line - 1, request.Selection.Start.Column - 1));
                var endPosition   = sourceText.Lines.GetPosition(new LinePosition(request.Selection.End.Line - 1, request.Selection.End.Column - 1));
                return(TextSpan.FromBounds(startPosition, endPosition));
            }
            var position = sourceText.Lines.GetPosition(new LinePosition(request.Line - 1, request.Column - 1));

            return(new TextSpan(position, 1));
        }
        private TextSpan GetTextSpan(ICodeActionRequest request, SourceText sourceText)
        {
            if (request.Selection != null)
            {
                return(TextSpan.FromBounds(
                           sourceText.Lines.GetPosition(new LinePosition(request.Selection.Start.Line, request.Selection.Start.Column)),
                           sourceText.Lines.GetPosition(new LinePosition(request.Selection.End.Line, request.Selection.End.Column))));
            }

            var position = sourceText.Lines.GetPosition(new LinePosition(request.Line, request.Column));

            return(new TextSpan(position, length: 0));
        }
        private static async Task<CodeFixContext?> GetCodeFixContext(Document originalDocument, ICodeActionRequest request, List<CodeAction> actionsDestination)
        {
            var sourceText = await originalDocument.GetTextAsync();
            var semanticModel = await originalDocument.GetSemanticModelAsync();
            var diagnostics = semanticModel.GetDiagnostics();
            var location = GetTextSpan(request, sourceText);

            var pointDiagnostics = diagnostics.Where(d => d.Location.SourceSpan.Contains(location)).ToImmutableArray();
            if (pointDiagnostics.Any())
            {
                return new CodeFixContext(originalDocument, pointDiagnostics.First().Location.SourceSpan, pointDiagnostics, (a, d) => actionsDestination.Add(a), CancellationToken.None);
            }

            return null;
        }
        public static async Task<IEnumerable<CodeAction>> GetActions(OmnisharpWorkspace workspace, IEnumerable<ICodeActionProvider> codeActionProviders, ILogger logger, ICodeActionRequest request)
        {
            var actions = new List<CodeAction>();
            var originalDocument = workspace.GetDocument(request.FileName);
            if (originalDocument == null)
            {
                return actions;
            }

            var refactoringContext = await GetRefactoringContext(originalDocument, request, actions);
            var codeFixContext = await GetCodeFixContext(originalDocument, request, actions);
            await CollectRefactoringActions(codeActionProviders, logger, refactoringContext);
            await CollectCodeFixActions(codeActionProviders, logger, codeFixContext);
            actions.Reverse();
            return actions;
        }
        private async Task<IEnumerable<CodeAction>> GetActions(ICodeActionRequest request)
        {
            var actions = new List<CodeAction>();
            _originalDocument = _workspace.GetDocument(request.FileName);
            if (_originalDocument == null)
            {
                return actions;
            }

            var refactoringContext = await GetRefactoringContext(request, actions);
            var codeFixContext = await GetCodeFixContext(request, actions);
            await CollectRefactoringActions(refactoringContext);
            await CollectCodeFixActions(codeFixContext);
            actions.Reverse();
            return actions;
        }
        private async Task <CodeFixContext?> GetCodeFixContext(ICodeActionRequest request, List <CodeAction> actionsDestination)
        {
            var sourceText = await _originalDocument.GetTextAsync();

            var semanticModel = await _originalDocument.GetSemanticModelAsync();

            var diagnostics = semanticModel.GetDiagnostics();
            var location    = GetTextSpan(request, sourceText);

            var pointDiagnostics = diagnostics.Where(d => d.Location.SourceSpan.Contains(location)).ToImmutableArray();

            if (pointDiagnostics.Any())
            {
                return(new CodeFixContext(_originalDocument, pointDiagnostics.First().Location.SourceSpan, pointDiagnostics, (a, d) => actionsDestination.Add(a), CancellationToken.None));
            }

            return(null);
        }
        private async Task <IEnumerable <CodeAction> > GetActions(ICodeActionRequest request)
        {
            var actions = new List <CodeAction>();

            _originalDocument = _workspace.GetDocument(request.FileName);
            if (_originalDocument == null)
            {
                return(actions);
            }

            var refactoringContext = await GetRefactoringContext(request, actions);

            var codeFixContext = await GetCodeFixContext(request, actions);

            await CollectRefactoringActions(refactoringContext);
            await CollectCodeFixActions(codeFixContext);

            actions.Reverse();
            return(actions);
        }
        protected async Task <IEnumerable <AvailableCodeAction> > GetAvailableCodeActions(ICodeActionRequest request)
        {
            // To produce a complete list of code actions for the document wait until all projects are loaded.
            var document = await this.Workspace.GetDocumentFromFullProjectModelAsync(request.FileName);

            if (document == null)
            {
                return(Array.Empty <AvailableCodeAction>());
            }

            var codeActions = new List <CodeAction>();

            var sourceText = await document.GetTextAsync();

            var span = GetTextSpan(request, sourceText);

            await CollectCodeFixesActions(document, span, codeActions);
            await CollectRefactoringActions(document, span, codeActions);

            var distinctActions = codeActions.GroupBy(x => x.Title).Select(x => x.First());

            var availableActions = ConvertToAvailableCodeAction(distinctActions);

            return(FilterBlacklistedCodeActions(availableActions));
        }
Пример #12
0
        public static async Task <IEnumerable <CodeAction> > GetActions(OmnisharpWorkspace workspace, IEnumerable <ICodeActionProvider> codeActionProviders, ILogger logger, ICodeActionRequest request)
        {
            var actions          = new List <CodeAction>();
            var originalDocument = workspace.GetDocument(request.FileName);

            if (originalDocument == null)
            {
                return(actions);
            }

            var refactoringContext = await GetRefactoringContext(originalDocument, request, actions);

            var codeFixContext = await GetCodeFixContext(originalDocument, request, actions);

            await CollectRefactoringActions(codeActionProviders, logger, refactoringContext);
            await CollectCodeFixActions(codeActionProviders, logger, codeFixContext);

            actions.Reverse();
            return(actions);
        }
 private static async Task<CodeRefactoringContext?> GetRefactoringContext(Document originalDocument, ICodeActionRequest request, List<CodeAction> actionsDestination)
 {
     var sourceText = await originalDocument.GetTextAsync();
     var location = GetTextSpan(request, sourceText);
     return new CodeRefactoringContext(originalDocument, location, (a) => actionsDestination.Add(a), CancellationToken.None);
 }
        protected async Task <IEnumerable <AvailableCodeAction> > GetAvailableCodeActions(ICodeActionRequest request)
        {
            var document = this.Workspace.GetDocument(request.FileName);

            if (document == null)
            {
                return(Array.Empty <AvailableCodeAction>());
            }

            var codeActions = new List <CodeAction>();

            var sourceText = await document.GetTextAsync();

            var span = GetTextSpan(request, sourceText);

            await CollectCodeFixesActions(document, span, codeActions);
            await CollectRefactoringActions(document, span, codeActions);

            // Be sure to filter out any code actions that inherit from CodeActionWithOptions.
            // This isn't a great solution and might need changing later, but every Roslyn code action
            // derived from this type tries to display a dialog. For now, this is a reasonable solution.
            var availableActions = ConvertToAvailableCodeAction(codeActions)
                                   .Where(a => !a.CodeAction.GetType().GetTypeInfo().IsSubclassOf(typeof(CodeActionWithOptions)));

            return(availableActions);
        }
        protected async Task <IEnumerable <AvailableCodeAction> > GetAvailableCodeActions(ICodeActionRequest request)
        {
            // To produce a complete list of code actions for the document wait until all projects are loaded.
            var document = await this.Workspace.GetDocumentFromFullProjectModelAsync(request.FileName);

            if (document == null)
            {
                return(Array.Empty <AvailableCodeAction>());
            }

            var codeActions = new List <CodeAction>();

            var sourceText = await document.GetTextAsync();

            var span = GetTextSpan(request, sourceText);

            await CollectCodeFixesActions(document, span, codeActions);
            await CollectRefactoringActions(document, span, codeActions);

            var distinctActions = codeActions.GroupBy(x => x.Title).Select(x => x.First());

            // Be sure to filter out any code actions that inherit from CodeActionWithOptions.
            // This isn't a great solution and might need changing later, but every Roslyn code action
            // derived from this type tries to display a dialog. For now, this is a reasonable solution.
            var availableActions = ConvertToAvailableCodeAction(distinctActions)
                                   .Where(a => !a.CodeAction.GetType().GetTypeInfo().IsSubclassOf(typeof(CodeActionWithOptions)));

            return(availableActions);
        }
Пример #16
0
        private static async Task <CodeFixContext?> GetCodeFixContext(Document originalDocument, ICodeActionRequest request, List <CodeAction> actionsDestination)
        {
            var sourceText = await originalDocument.GetTextAsync();

            var semanticModel = await originalDocument.GetSemanticModelAsync();

            var diagnostics = semanticModel.GetDiagnostics();
            var span        = GetTextSpan(request, sourceText);

            // Try to find exact match
            var pointDiagnostics = diagnostics.Where(d => d.Location.SourceSpan.Equals(span)).ToImmutableArray();

            // No exact match found, try approximate match instead
            if (pointDiagnostics.Length == 0)
            {
                var firstMatchingDiagnostic = diagnostics.FirstOrDefault(d => d.Location.SourceSpan.Contains(span));
                // Try to find other matches with the same exact span as the first approximate match
                if (firstMatchingDiagnostic != null)
                {
                    pointDiagnostics = diagnostics.Where(d => d.Location.SourceSpan.Equals(firstMatchingDiagnostic.Location.SourceSpan)).ToImmutableArray();
                }
            }
            // At this point all pointDiagnostics guaranteed to have the same span
            if (pointDiagnostics.Length > 0)
            {
                return(new CodeFixContext(originalDocument, pointDiagnostics[0].Location.SourceSpan, pointDiagnostics, (a, d) => actionsDestination.Add(a), CancellationToken.None));
            }

            return(null);
        }