Example #1
0
 /// <summary>
 /// Creates a new <see cref="FixAllContext"/>.
 /// Use this overload when applying fix all to a diagnostic with a source location.
 /// </summary>
 /// <param name="document">Document within which fix all occurrences was triggered.</param>
 /// <param name="codeFixProvider">Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.</param>
 /// <param name="scope"><see cref="FixAllScope"/> to fix all occurrences.</param>
 /// <param name="codeActionEquivalenceKey">The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.</param>
 /// <param name="diagnosticIds">Diagnostic Ids to fix.</param>
 /// <param name="fixAllDiagnosticProvider">
 /// <see cref="DiagnosticProvider"/> to fetch document/project diagnostics to fix in a <see cref="FixAllContext"/>.
 /// </param>
 /// <param name="cancellationToken">Cancellation token for fix all computation.</param>
 public FixAllContext(
     Document document,
     CodeFixProvider codeFixProvider,
     FixAllScope scope,
     string codeActionEquivalenceKey,
     IEnumerable <string> diagnosticIds,
     DiagnosticProvider fixAllDiagnosticProvider,
     CancellationToken cancellationToken
     )
     : this(
         new FixAllState(
             null,
             document,
             codeFixProvider,
             scope,
             codeActionEquivalenceKey,
             diagnosticIds,
             fixAllDiagnosticProvider
             ),
         new ProgressTracker(),
         cancellationToken
         )
 {
     if (document == null)
     {
         throw new ArgumentNullException(nameof(document));
     }
 }
Example #2
0
        private static async Task <FixAllContext> GetFixAllContextAsync(
            Solution solution,
            CodeFixProvider codeFixProvider,
            ImmutableDictionary <Project, ImmutableArray <Diagnostic> > diagnosticsByProject,
            CancellationToken cancellationToken)
        {
            var diagnosticProvider = new DiagnosticProvider(diagnosticsByProject);

            var firstDiagnostic = diagnosticsByProject
                                  .SelectMany(kvp => kvp.Value)
                                  .FirstOrDefault();

            RoslynDebug.AssertNotNull(firstDiagnostic?.Location?.SourceTree);

            var document = solution.GetRequiredDocument(firstDiagnostic.Location.SourceTree);

            // This will allow us access to the equivalence key
            CodeAction?action  = null;
            var        context = new CodeFixContext(
                document,
                firstDiagnostic,
                (a, _) => action ??= a,
                cancellationToken);
            await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);

            return(new FixAllContext(
                       document: document,
                       codeFixProvider: codeFixProvider,
                       scope: FixAllScope.Solution,
                       codeActionEquivalenceKey: action?.EquivalenceKey !, // FixAllState supports null equivalence key. This should still be supported.
                       diagnosticIds: codeFixProvider.FixableDiagnosticIds,
                       fixAllDiagnosticProvider: diagnosticProvider,
                       cancellationToken: cancellationToken));
        }
Example #3
0
        public DocumentId AddDocument(SourceTextContainer sourceTextContainer, string workingDirectory,
                                      Action <DiagnosticsUpdatedArgs> onDiagnosticsUpdated, Action <SourceText> onTextUpdated)
        {
            if (sourceTextContainer == null)
            {
                throw new ArgumentNullException(nameof(sourceTextContainer));
            }

            var workspace = new RoslynWorkspace(_host, _nuGetConfiguration, this);

            if (onTextUpdated != null)
            {
                workspace.ApplyingTextChange += (d, s) => onTextUpdated(s);
            }

            DiagnosticProvider.Enable(workspace, DiagnosticProvider.Options.Semantic);

            var currentSolution = workspace.CurrentSolution;
            var project         = CreateSubmissionProject(currentSolution,
                                                          CreateCompilationOptions(workspace, workingDirectory));
            var currentDocument = SetSubmissionDocument(workspace, sourceTextContainer, project);

            _workspaces.TryAdd(currentDocument.Id, workspace);

            if (onDiagnosticsUpdated != null)
            {
                _diagnosticsUpdatedNotifiers.TryAdd(currentDocument.Id, onDiagnosticsUpdated);
            }

            return(currentDocument.Id);
        }
Example #4
0
        public RoslynWorkspace(HostServices hostServices, string workspaceKind = WorkspaceKind.Host, RoslynHost roslynHost = null)
            : base(hostServices, workspaceKind)
        {
            DiagnosticProvider.Enable(this, DiagnosticProvider.Options.Semantic);

            RoslynHost = roslynHost;
        }
Example #5
0
 protected override void Dispose(bool finalize)
 {
     DiagnosticProvider.Disable(this);
     _sourceTextChangedHandlers.Clear();
     _diagnosticsUpdatedNotifiers.Clear();
     base.Dispose(finalize);
 }
Example #6
0
        protected internal RoslynWorkspace(HostServices host, RoslynHost roslynHost)
            : base(host, WorkspaceKind.Host)
        {
            RoslynHost = roslynHost;

            DiagnosticProvider.Enable(this, DiagnosticProvider.Options.Semantic);
        }
Example #7
0
        protected override void Dispose(bool finalize)
        {
            base.Dispose(finalize);

            ApplyingTextChange = null;

            DiagnosticProvider.Disable(this);
        }
Example #8
0
        //

        public WorkspaceImpl(HostServices hostServices, WorkspaceType workspaceType)          //bool isCSharpScript )
            : base(hostServices, WorkspaceKind.Host)
        {
            this.workspaceType = workspaceType;
            //this.isCSharpScript = isCSharpScript;

            DiagnosticProvider.Enable(this, DiagnosticProvider.Options.Semantic);
        }
 public void StartListening(Workspace workspace, object serviceOpt)
 {
     // misc workspace will enable syntax errors and semantic errors for script files for
     // all participating projects in the workspace
     DiagnosticProvider.Enable(
         workspace,
         DiagnosticProvider.Options.Syntax | DiagnosticProvider.Options.ScriptSemantic
         );
 }
 public void StartListening(Workspace workspace, object serviceOpt)
 {
     if (_globalOptions.GetOption(SolutionCrawlerRegistrationService.EnableSolutionCrawler))
     {
         // misc workspace will enable syntax errors and semantic errors for script files for
         // all participating projects in the workspace
         DiagnosticProvider.Enable(workspace, DiagnosticProvider.Options.Syntax | DiagnosticProvider.Options.ScriptSemantic);
     }
 }
Example #11
0
 /// <summary>
 /// Creates a new <see cref="FixAllContext"/>.
 /// Use this overload when applying fix all to a diagnostic with a source location.
 /// </summary>
 /// <param name="document">Document within which fix all occurrences was triggered.</param>
 /// <param name="codeFixProvider">Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.</param>
 /// <param name="scope"><see cref="FixAllScope"/> to fix all occurrences.</param>
 /// <param name="codeActionEquivalenceKey">The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.</param>
 /// <param name="diagnosticIds">Diagnostic Ids to fix.</param>
 /// <param name="fixAllDiagnosticProvider">
 /// <see cref="DiagnosticProvider"/> to fetch document/project diagnostics to fix in a <see cref="FixAllContext"/>.
 /// </param>
 /// <param name="cancellationToken">Cancellation token for fix all computation.</param>
 public FixAllContext(
     Document document,
     CodeFixProvider codeFixProvider,
     FixAllScope scope,
     string?codeActionEquivalenceKey,
     IEnumerable <string> diagnosticIds,
     DiagnosticProvider fixAllDiagnosticProvider,
     CancellationToken cancellationToken)
     : this(new FixAllState(
Example #12
0
 public void CloseDocument(DocumentId documentId)
 {
     if (_workspaces.TryGetValue(documentId, out var workspace))
     {
         DiagnosticProvider.Disable(workspace);
         workspace.Dispose();
         _workspaces.TryRemove(documentId, out workspace);
     }
     _diagnosticsUpdatedNotifiers.TryRemove(documentId, out _);
 }
Example #13
0
        public void CloseDocument(DocumentId documentId)
        {
            RoslynWorkspace workspace;

            if (_workspaces.TryGetValue(documentId, out workspace))
            {
                DiagnosticProvider.Disable(workspace);
                workspace.Dispose();
                _workspaces.TryRemove(documentId, out workspace);
            }
            Action <DiagnosticsUpdatedArgs> notifier;

            _diagnosticsUpdatedNotifiers.TryRemove(documentId, out notifier);
        }
Example #14
0
 /// <summary>
 /// Creates a new <see cref="FixAllContext"/>.
 /// Use this overload when applying fix all to a diagnostic with a source location.
 /// </summary>
 /// <param name="document">Document within which fix all occurrences was triggered.</param>
 /// <param name="codeFixProvider">Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.</param>
 /// <param name="scope"><see cref="FixAllScope"/> to fix all occurrences.</param>
 /// <param name="codeActionEquivalenceKey">The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.</param>
 /// <param name="diagnosticIds">Diagnostic Ids to fix.</param>
 /// <param name="fixAllDiagnosticProvider">
 /// <see cref="DiagnosticProvider"/> to fetch document/project diagnostics to fix in a <see cref="FixAllContext"/>.
 /// </param>
 /// <param name="cancellationToken">Cancellation token for fix all computation.</param>
 public FixAllContext(
     Document document,
     CodeFixProvider codeFixProvider,
     FixAllScope scope,
     string codeActionEquivalenceKey,
     IEnumerable<string> diagnosticIds,
     DiagnosticProvider fixAllDiagnosticProvider,
     CancellationToken cancellationToken)
     : this(document, document.Project, codeFixProvider, scope, codeActionEquivalenceKey, diagnosticIds, fixAllDiagnosticProvider, cancellationToken)
 {
     if (document == null)
     {
         throw new ArgumentNullException(nameof(document));
     }
 }
Example #15
0
 /// <summary>
 /// Creates a new <see cref="FixAllContext"/>.
 /// Use this overload when applying fix all to a diagnostic with no source location, i.e. <see cref="Location.None"/>.
 /// </summary>
 /// <param name="project">Project within which fix all occurrences was triggered.</param>
 /// <param name="codeFixProvider">Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.</param>
 /// <param name="scope"><see cref="FixAllScope"/> to fix all occurrences.</param>
 /// <param name="codeActionEquivalenceKey">The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.</param>
 /// <param name="diagnosticIds">Diagnostic Ids to fix.</param>
 /// <param name="fixAllDiagnosticProvider">
 /// <see cref="DiagnosticProvider"/> to fetch document/project diagnostics to fix in a <see cref="FixAllContext"/>.
 /// </param>
 /// <param name="cancellationToken">Cancellation token for fix all computation.</param>
 public FixAllContext(
     Project project,
     CodeFixProvider codeFixProvider,
     FixAllScope scope,
     string codeActionEquivalenceKey,
     IEnumerable <string> diagnosticIds,
     DiagnosticProvider fixAllDiagnosticProvider,
     CancellationToken cancellationToken)
     : this(null, project, codeFixProvider, scope, codeActionEquivalenceKey, diagnosticIds, fixAllDiagnosticProvider, cancellationToken)
 {
     if (project == null)
     {
         throw new ArgumentNullException(nameof(project));
     }
 }
Example #16
0
        public static void EnableDiagnostics(this Workspace workspace, DiagnosticOptions options)
        {
            var diagnosticProviderOptions = (DiagnosticProvider.Options) 0;

            if ((options & DiagnosticOptions.Syntax) != 0)
            {
                diagnosticProviderOptions |= DiagnosticProvider.Options.Syntax;
            }
            if ((options & DiagnosticOptions.Semantic) != 0)
            {
                diagnosticProviderOptions |= DiagnosticProvider.Options.Semantic;
            }

            DiagnosticProvider.Enable(workspace, diagnosticProviderOptions);
        }
Example #17
0
        public ProjectExternalErrorReporter(ProjectId projectId, string errorCodePrefix, VisualStudioWorkspaceImpl workspace)
        {
            Debug.Assert(projectId != null);
            Debug.Assert(errorCodePrefix != null);
            Debug.Assert(workspace != null);

            _projectId       = projectId;
            _errorCodePrefix = errorCodePrefix;
            _workspace       = workspace;

            KnownUIContexts.SolutionBuildingContext.WhenActivated(() =>
            {
                KnownUIContexts.SolutionBuildingContext.UIContextChanged += OnSolutionBuild;
                DiagnosticProvider.OnSolutionBuildStarted();
            });
        }
Example #18
0
        /// <summary>
        /// Creates a new <see cref="FixAllContext"/>.
        /// Use this overload when applying fix all to a diagnostic with a source location.
        /// <para>
        /// This overload cannot be used with <see cref="FixAllScope.ContainingMember"/> or
        /// <see cref="FixAllScope.ContainingType"/> value for the <paramref name="scope"/>.
        /// For those fix all scopes, use the <see cref="FixAllContext"/> constructor that
        /// takes a 'diagnosticSpan' parameter to identify the containing member or type based
        /// on this span.
        /// </para>
        /// </summary>
        /// <param name="document">Document within which fix all occurrences was triggered.</param>
        /// <param name="codeFixProvider">Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.</param>
        /// <param name="scope"><see cref="FixAllScope"/> to fix all occurrences.</param>
        /// <param name="codeActionEquivalenceKey">The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.</param>
        /// <param name="diagnosticIds">Diagnostic Ids to fix.</param>
        /// <param name="fixAllDiagnosticProvider">
        /// <see cref="DiagnosticProvider"/> to fetch document/project diagnostics to fix in a <see cref="FixAllContext"/>.
        /// </param>
        /// <param name="cancellationToken">Cancellation token for fix all computation.</param>
        public FixAllContext(
            Document document,
            CodeFixProvider codeFixProvider,
            FixAllScope scope,
            string?codeActionEquivalenceKey,
            IEnumerable <string> diagnosticIds,
            DiagnosticProvider fixAllDiagnosticProvider,
            CancellationToken cancellationToken)
#pragma warning disable RS0030 // Do not used banned APIs - It is fine to invoke the public FixAllContext constructor here.
            : this(document, diagnosticSpan : null, codeFixProvider, scope,
                   codeActionEquivalenceKey, diagnosticIds, fixAllDiagnosticProvider, cancellationToken)
#pragma warning restore RS0030 // Do not used banned APIs
        {
            if (scope is FixAllScope.ContainingMember or FixAllScope.ContainingType)
            {
                throw new ArgumentException(WorkspacesResources.FixAllScope_ContainingType_and_FixAllScope_ContainingMember_are_not_supported_with_this_constructor,
                                            nameof(scope));
            }
        }
Example #19
0
        internal RoslynWorkspace(HostServices host, NuGetConfiguration nuGetConfiguration, CompositionHost compositionContext, string dotnetPath, string sdkPath)
            : base(host, WorkspaceKind.Host)
        {
            this.dotnetPath     = dotnetPath;
            this.sdkPath        = sdkPath;
            _nuGetConfiguration = nuGetConfiguration;

            _openDocumentTextLoaders     = new Dictionary <DocumentId, AvalonEditTextContainer>();
            _diagnosticsUpdatedNotifiers = new ConcurrentDictionary <DocumentId, Action <DiagnosticsUpdatedArgs> >();

            _compositionContext = compositionContext;

            _buildNodes = new BlockingCollection <MSBuildHost>();

            DiagnosticProvider.Enable(this, DiagnosticProvider.Options.Semantic | DiagnosticProvider.Options.Syntax);

            //this.EnableDiagnostics(DiagnosticOptions.Semantic | DiagnosticOptions.Syntax);

            GetService <IDiagnosticService>().DiagnosticsUpdated += OnDiagnosticsUpdated;
        }
Example #20
0
        private FixAllContext(
            Document document,
            Project project,
            CodeFixProvider codeFixProvider,
            FixAllScope scope,
            string codeActionEquivalenceKey,
            IEnumerable <string> diagnosticIds,
            DiagnosticProvider fixAllDiagnosticProvider,
            CancellationToken cancellationToken)
        {
            Contract.ThrowIfNull(project);

            if (codeFixProvider == null)
            {
                throw new ArgumentNullException(nameof(codeFixProvider));
            }

            if (diagnosticIds == null)
            {
                throw new ArgumentNullException(nameof(diagnosticIds));
            }

            if (diagnosticIds.Any(d => d == null))
            {
                throw new ArgumentException(WorkspacesResources.DiagnosticCannotBeNull, nameof(diagnosticIds));
            }

            if (fixAllDiagnosticProvider == null)
            {
                throw new ArgumentNullException(nameof(fixAllDiagnosticProvider));
            }

            this.Document                 = document;
            this.Project                  = project;
            this.CodeFixProvider          = codeFixProvider;
            this.Scope                    = scope;
            this.CodeActionEquivalenceKey = codeActionEquivalenceKey;
            this.DiagnosticIds            = ImmutableHashSet.CreateRange(diagnosticIds);
            _diagnosticProvider           = fixAllDiagnosticProvider;
            this.CancellationToken        = cancellationToken;
        }
Example #21
0
        public void SubscribeToDiagnosticsUpdateNotification(DocumentId documentId, Action <DiagnosticsUpdatedArgs> onDiagnosticsUpdated)
        {
            if (null == documentId)
            {
                throw new ArgumentNullException(nameof(documentId));
            }
            if (null == onDiagnosticsUpdated)
            {
                throw new ArgumentNullException(nameof(onDiagnosticsUpdated));
            }

            if (!_diagnosticsUpdatedNotifiers.TryAdd(documentId, onDiagnosticsUpdated))
            {
                if (_diagnosticsUpdatedNotifiers.TryGetValue(documentId, out var handler))
                {
                    handler -= onDiagnosticsUpdated; // in case it is already registed
                    handler += onDiagnosticsUpdated;
                    throw new NotImplementedException("The previous code needs verification");
                }
            }

            // enable diagnostics now, if not already enabled
            DiagnosticProvider.Enable(this, DiagnosticProvider.Options.Semantic);
        }
Example #22
0
 /// <summary>
 /// Creates a new <see cref="FixAllContext"/>.
 /// Use this overload when applying fix all to a diagnostic with no source location, i.e. <see cref="Location.None"/>.
 /// </summary>
 /// <param name="project">Project within which fix all occurrences was triggered.</param>
 /// <param name="codeFixProvider">Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.</param>
 /// <param name="scope"><see cref="FixAllScope"/> to fix all occurrences.</param>
 /// <param name="codeActionEquivalenceKey">The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.</param>
 /// <param name="diagnosticIds">Diagnostic Ids to fix.</param>
 /// <param name="fixAllDiagnosticProvider">
 /// <see cref="DiagnosticProvider"/> to fetch document/project diagnostics to fix in a <see cref="FixAllContext"/>.
 /// </param>
 /// <param name="cancellationToken">Cancellation token for fix all computation.</param>
 public FixAllContext(
     Project project,
     CodeFixProvider codeFixProvider,
     FixAllScope scope,
     string codeActionEquivalenceKey,
     IEnumerable<string> diagnosticIds,
     DiagnosticProvider fixAllDiagnosticProvider,
     CancellationToken cancellationToken)
     : this(new FixAllState(null, project, codeFixProvider, scope, codeActionEquivalenceKey, diagnosticIds, fixAllDiagnosticProvider),
           new ProgressTracker(), cancellationToken)
 {
     if (project == null)
     {
         throw new ArgumentNullException(nameof(project));
     }
 }
 private void StopSolutionCrawler()
 => DiagnosticProvider.Disable(this);
 private void StartSolutionCrawler()
 => DiagnosticProvider.Enable(this, DiagnosticProvider.Options.Syntax);
Example #25
0
     > GetDocumentDiagnosticsToFixAsync() =>
 DiagnosticProvider.GetDocumentDiagnosticsToFixAsync(this);
Example #26
0
     > GetProjectDiagnosticsToFixAsync() =>
 DiagnosticProvider.GetProjectDiagnosticsToFixAsync(this);
Example #27
0
 public static void DisableDiagnostics(this Workspace workspace)
 {
     DiagnosticProvider.Disable(workspace);
 }
Example #28
0
 internal Task <ImmutableDictionary <Document, ImmutableArray <Diagnostic> > > GetDocumentDiagnosticsToFixAsync()
 => DiagnosticProvider.GetDocumentDiagnosticsToFixAsync(this);
 internal void StopSolutionCrawler()
 {
     DiagnosticProvider.Disable(this);
 }
 internal void StartSolutionCrawler()
 {
     DiagnosticProvider.Enable(this, DiagnosticProvider.Options.Syntax);
 }
 public void StopListening(Workspace workspace)
 => DiagnosticProvider.Disable(workspace);
Example #32
0
        private FixAllContext(
            Document document,
            Project project,
            CodeFixProvider codeFixProvider,
            FixAllScope scope,
            string codeActionEquivalenceKey,
            IEnumerable<string> diagnosticIds,
            DiagnosticProvider fixAllDiagnosticProvider,
            CancellationToken cancellationToken)
        {
            Contract.ThrowIfNull(project);

            if (codeFixProvider == null)
            {
                throw new ArgumentNullException(nameof(codeFixProvider));
            }

            if (diagnosticIds == null)
            {
                throw new ArgumentNullException(nameof(diagnosticIds));
            }

            if (diagnosticIds.Any(d => d == null))
            {
                throw new ArgumentException(WorkspacesResources.DiagnosticCannotBeNull, nameof(diagnosticIds));
            }

            if (fixAllDiagnosticProvider == null)
            {
                throw new ArgumentNullException(nameof(fixAllDiagnosticProvider));
            }

            this.Document = document;
            this.Project = project;
            this.CodeFixProvider = codeFixProvider;
            this.Scope = scope;
            this.CodeActionEquivalenceKey = codeActionEquivalenceKey;
            this.DiagnosticIds = ImmutableHashSet.CreateRange(diagnosticIds);
            _diagnosticProvider = fixAllDiagnosticProvider;
            this.CancellationToken = cancellationToken;
        }