private void GetLiveProjectAndDocumentErrors(
            out IDictionary <ProjectId, IList <DiagnosticData> > projectErrors,
            out IDictionary <DocumentId, IList <DiagnosticData> > documentErrors)
        {
            projectErrors  = null;
            documentErrors = null;

            foreach (var diagnostic in _diagnosticService.GetDiagnostics(_workspace, id: null, projectId: null, documentId: null, cancellationToken: CancellationToken.None))
            {
                if (diagnostic.DocumentId != null)
                {
                    documentErrors = documentErrors ?? new Dictionary <DocumentId, IList <DiagnosticData> >();

                    var errors = documentErrors.GetOrAdd(diagnostic.DocumentId, _ => new List <DiagnosticData>());
                    errors.Add(diagnostic);
                    continue;
                }

                if (diagnostic.ProjectId != null)
                {
                    projectErrors = projectErrors ?? new Dictionary <ProjectId, IList <DiagnosticData> >();

                    var errors = projectErrors.GetOrAdd(diagnostic.ProjectId, _ => new List <DiagnosticData>());
                    errors.Add(diagnostic);
                    continue;
                }

                Contract.Requires(false, "shouldn't happen");
            }

            projectErrors  = projectErrors ?? SpecializedCollections.EmptyDictionary <ProjectId, IList <DiagnosticData> >();
            documentErrors = documentErrors ?? SpecializedCollections.EmptyDictionary <DocumentId, IList <DiagnosticData> >();
        }
        private IDictionary <INamespaceOrTypeSymbol, IAliasSymbol> CreateAliasMap()
        {
            if (!this.IsMinimizing)
            {
                return(SpecializedCollections.EmptyDictionary <
                           INamespaceOrTypeSymbol,
                           IAliasSymbol
                           >());
            }

            // Walk up the ancestors from the current position. If this is a speculative
            // model, walk up the corresponding ancestors in the parent model.
            SemanticModel semanticModel;
            int           position;

            if (semanticModelOpt.IsSpeculativeSemanticModel)
            {
                semanticModel = semanticModelOpt.ParentModel;
                position      = semanticModelOpt.OriginalPositionForSpeculation;
            }
            else
            {
                semanticModel = semanticModelOpt;
                position      = positionOpt;
            }

            var token     = semanticModel.SyntaxTree.GetRoot().FindToken(position);
            var startNode = token.Parent;

            // NOTE(cyrusn): If we're currently in a block of usings, then we want to collect the
            // aliases that are higher up than this block.  Using aliases declared in a block of
            // usings are not usable from within that same block.
            var usingDirective = GetAncestorOrThis <UsingDirectiveSyntax>(startNode);

            if (usingDirective != null)
            {
                startNode = usingDirective.Parent.Parent;
            }

            var usingAliases = GetAncestorsOrThis <NamespaceDeclarationSyntax>(startNode)
                               .SelectMany(n => n.Usings)
                               .Concat(
                GetAncestorsOrThis <CompilationUnitSyntax>(startNode).SelectMany(c => c.Usings)
                )
                               .Where(u => u.Alias != null)
                               .Select(u => semanticModel.GetDeclaredSymbol(u) as IAliasSymbol)
                               .Where(u => u != null);

            var builder = ImmutableDictionary.CreateBuilder <INamespaceOrTypeSymbol, IAliasSymbol>();

            foreach (var alias in usingAliases)
            {
                if (!builder.ContainsKey(alias.Target))
                {
                    builder.Add(alias.Target, alias);
                }
            }

            return(builder.ToImmutable());
        }
Esempio n. 3
0
        public StructuralTypeDisplayInfo GetTypeDisplayInfo(
            ISymbol orderSymbol,
            ImmutableArray <INamedTypeSymbol> directStructuralTypeReferences,
            SemanticModel semanticModel,
            int position)
        {
            if (directStructuralTypeReferences.Length == 0)
            {
                return(new StructuralTypeDisplayInfo(
                           SpecializedCollections.EmptyDictionary <INamedTypeSymbol, string>(),
                           SpecializedCollections.EmptyList <SymbolDisplayPart>()));
            }

            var transitiveStructuralTypeReferences = GetTransitiveStructuralTypeReferences(directStructuralTypeReferences);

            transitiveStructuralTypeReferences = OrderStructuralTypes(transitiveStructuralTypeReferences, orderSymbol);

            IList <SymbolDisplayPart> typeParts = new List <SymbolDisplayPart>();

            typeParts.Add(PlainText(FeaturesResources.Types_colon));
            typeParts.AddRange(LineBreak());

            for (var i = 0; i < transitiveStructuralTypeReferences.Length; i++)
            {
                if (i != 0)
                {
                    typeParts.AddRange(LineBreak());
                }

                var structuralType = transitiveStructuralTypeReferences[i];
                typeParts.AddRange(Space(count: 4));

                var kind =
                    structuralType.IsValueType ? SymbolDisplayPartKind.StructName :
                    structuralType.IsDelegateType() ? SymbolDisplayPartKind.DelegateName : SymbolDisplayPartKind.ClassName;

                typeParts.Add(Part(kind, structuralType, structuralType.Name));
                typeParts.AddRange(Space());
                typeParts.Add(PlainText(FeaturesResources.is_));
                typeParts.AddRange(Space());

                if (structuralType.IsValueType)
                {
                    typeParts.AddRange(structuralType.ToMinimalDisplayParts(semanticModel, position));
                }
                else
                {
                    typeParts.AddRange(GetAnonymousTypeParts(structuralType, semanticModel, position));
                }
            }

            // Finally, assign a name to all the anonymous types.
            var structuralTypeToName = GenerateStructuralTypeNames(transitiveStructuralTypeReferences);

            typeParts = StructuralTypeDisplayInfo.ReplaceStructuralTypes(
                typeParts, structuralTypeToName, semanticModel, position);

            return(new StructuralTypeDisplayInfo(structuralTypeToName, typeParts));
        }
        public async Task <IDictionary <SymbolDescriptionGroups, ImmutableArray <SymbolDisplayPart> > > ToDescriptionGroupsAsync(Workspace workspace, SemanticModel semanticModel, int position, ImmutableArray <ISymbol> symbols, CancellationToken cancellationToken)
        {
            if (symbols.Length == 0)
            {
                return(SpecializedCollections.EmptyDictionary <SymbolDescriptionGroups, ImmutableArray <SymbolDisplayPart> >());
            }

            var builder = CreateDescriptionBuilder(workspace, semanticModel, position, cancellationToken);

            return(await builder.BuildDescriptionSectionsAsync(symbols).ConfigureAwait(false));
        }
        public AnonymousTypeDisplayInfo GetNormalAnonymousTypeDisplayInfo(
            ISymbol orderSymbol,
            IEnumerable <INamedTypeSymbol> directNormalAnonymousTypeReferences,
            SemanticModel semanticModel,
            int position,
            ISymbolDisplayService displayService)
        {
            if (!directNormalAnonymousTypeReferences.Any())
            {
                return(new AnonymousTypeDisplayInfo(
                           SpecializedCollections.EmptyDictionary <INamedTypeSymbol, string>(),
                           SpecializedCollections.EmptyList <SymbolDisplayPart>()));
            }

            var transitiveNormalAnonymousTypeReferences = GetTransitiveNormalAnonymousTypeReferences(directNormalAnonymousTypeReferences.ToSet());

            transitiveNormalAnonymousTypeReferences = OrderAnonymousTypes(transitiveNormalAnonymousTypeReferences, orderSymbol);

            IList <SymbolDisplayPart> anonymousTypeParts = new List <SymbolDisplayPart>();

            anonymousTypeParts.Add(PlainText(FeaturesResources.Anonymous_Types_colon));
            anonymousTypeParts.AddRange(LineBreak());

            for (int i = 0; i < transitiveNormalAnonymousTypeReferences.Count; i++)
            {
                if (i != 0)
                {
                    anonymousTypeParts.AddRange(LineBreak());
                }

                var anonymousType = transitiveNormalAnonymousTypeReferences[i];
                anonymousTypeParts.AddRange(Space(count: 4));
                anonymousTypeParts.Add(Part(SymbolDisplayPartKind.ClassName, anonymousType, anonymousType.Name));
                anonymousTypeParts.AddRange(Space());
                anonymousTypeParts.Add(PlainText(FeaturesResources.is_));
                anonymousTypeParts.AddRange(Space());
                anonymousTypeParts.AddRange(GetAnonymousTypeParts(anonymousType, semanticModel, position, displayService));
            }

            // Now, inline any delegate anonymous types we've got.
            anonymousTypeParts = this.InlineDelegateAnonymousTypes(anonymousTypeParts, semanticModel, position, displayService);

            // Finally, assign a name to all the anonymous types.
            var anonymousTypeToName = GenerateAnonymousTypeNames(transitiveNormalAnonymousTypeReferences);

            anonymousTypeParts = AnonymousTypeDisplayInfo.ReplaceAnonymousTypes(anonymousTypeParts, anonymousTypeToName);

            return(new AnonymousTypeDisplayInfo(anonymousTypeToName, anonymousTypeParts));
        }
        protected void GetCompilationReferences(
            TCompilation compilation,
            DiagnosticBag diagnostics,
            List <MetadataReference> references,
            out IDictionary <string, MetadataReference> boundReferenceDirectives,
            out List <CommonLocation> referenceDirectiveLocations)
        {
            referenceDirectiveLocations = null;
            boundReferenceDirectives    = null;

            foreach (var referenceDirective in compilation.ReferenceDirectives)
            {
                // we already successfully bound #r with the same value:
                if (boundReferenceDirectives != null && boundReferenceDirectives.ContainsKey(referenceDirective.File))
                {
                    continue;
                }

                MetadataReference boundReference = ResolveReferenceDirective(referenceDirective.File, referenceDirective.Location, compilation);
                if (boundReference == null)
                {
                    diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_MetadataFileNotFound, referenceDirective.Location, referenceDirective.File));
                    continue;
                }

                if (boundReferenceDirectives == null)
                {
                    referenceDirectiveLocations = new List <CommonLocation>();
                    boundReferenceDirectives    = new Dictionary <string, MetadataReference>();
                }

                references.Add(boundReference);
                referenceDirectiveLocations.Add(referenceDirective.Location);
                boundReferenceDirectives.Add(referenceDirective.File, boundReference);
            }

            if (boundReferenceDirectives == null)
            {
                // no directive references resolved successfully:
                boundReferenceDirectives = SpecializedCollections.EmptyDictionary <string, MetadataReference>();
            }

            // add external reference at the end, so that they are processed first:
            references.AddRange(compilation.ExternalReferences);
        }
Esempio n. 7
0
        private IDictionary <INamespaceOrTypeSymbol, IAliasSymbol> CreateAliasMap()
        {
            if (!this.IsMinimizing)
            {
                return(SpecializedCollections.EmptyDictionary <INamespaceOrTypeSymbol, IAliasSymbol>());
            }

            var token     = semanticModelOpt.SyntaxTree.GetRoot().FindToken(positionOpt);
            var startNode = token.Parent;

            // NOTE(cyrusn): If we're currently in a block of usings, then we want to collect the
            // aliases that are higher up than this block.  Using aliases declared in a block of
            // usings are not usable from within that same block.
            var usingDirective = GetAncestorOrThis <JavaImportDeclarationSyntax>(startNode);

            if (usingDirective != null)
            {
                startNode = usingDirective.Parent.Parent;
            }

            //var usingAliases = GetAncestorsOrThis<JavaPackageDeclarationSyntax>(startNode)
            //	.SelectMany(n => n.Usings)
            //	.Concat(GetAncestorsOrThis<CompilationUnitSyntax>(startNode).SelectMany(c => c.Usings))
            //	.Where(u => u.Alias != null)
            //	.Select(u => semanticModelOpt.GetDeclaredSymbol(u) as IAliasSymbol)
            //	.Where(u => u != null);

            var builder = ImmutableDictionary.CreateBuilder <INamespaceOrTypeSymbol, IAliasSymbol>();

            //foreach (var alias in usingAliases)
            //{
            //	if (!builder.ContainsKey(alias.Target))
            //	{
            //		builder.Add(alias.Target, alias);
            //	}
            //}

            return(builder.ToImmutable());
        }
Esempio n. 8
0
        protected void GetCompilationReferences(
            TCompilation compilation,
            DiagnosticBag diagnostics,
            out ImmutableArray <MetadataReference> references,
            out IDictionary <ValueTuple <string, string>, MetadataReference> boundReferenceDirectives,
            out ImmutableArray <Location> referenceDirectiveLocations)
        {
            boundReferenceDirectives = null;

            ArrayBuilder <MetadataReference> referencesBuilder = ArrayBuilder <MetadataReference> .GetInstance();

            ArrayBuilder <Location> referenceDirectiveLocationsBuilder = null;

            try
            {
                foreach (var referenceDirective in compilation.ReferenceDirectives)
                {
                    if (compilation.Options.MetadataReferenceResolver == null)
                    {
                        diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_MetadataReferencesNotSupported, referenceDirective.Location));
                        break;
                    }

                    // we already successfully bound #r with the same value:
                    if (boundReferenceDirectives != null && boundReferenceDirectives.ContainsKey(ValueTuple.Create(referenceDirective.Location.SourceTree.FilePath, referenceDirective.File)))
                    {
                        continue;
                    }

                    MetadataReference boundReference = ResolveReferenceDirective(referenceDirective.File, referenceDirective.Location, compilation);
                    if (boundReference == null)
                    {
                        diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_MetadataFileNotFound, referenceDirective.Location, referenceDirective.File));
                        continue;
                    }

                    if (boundReferenceDirectives == null)
                    {
                        boundReferenceDirectives           = new Dictionary <ValueTuple <string, string>, MetadataReference>();
                        referenceDirectiveLocationsBuilder = ArrayBuilder <Location> .GetInstance();
                    }

                    referencesBuilder.Add(boundReference);
                    referenceDirectiveLocationsBuilder.Add(referenceDirective.Location);
                    boundReferenceDirectives.Add(ValueTuple.Create(referenceDirective.Location.SourceTree.FilePath, referenceDirective.File), boundReference);
                }

                // add external reference at the end, so that they are processed first:
                referencesBuilder.AddRange(compilation.ExternalReferences);

                // Add all explicit references of the previous script compilation.
                var previousScriptCompilation = compilation.ScriptCompilationInfo?.PreviousScriptCompilation;
                if (previousScriptCompilation != null)
                {
                    referencesBuilder.AddRange(previousScriptCompilation.GetBoundReferenceManager().ExplicitReferences);
                }

                if (boundReferenceDirectives == null)
                {
                    // no directive references resolved successfully:
                    boundReferenceDirectives = SpecializedCollections.EmptyDictionary <ValueTuple <string, string>, MetadataReference>();
                }

                references = referencesBuilder.ToImmutable();
                referenceDirectiveLocations = referenceDirectiveLocationsBuilder?.ToImmutableAndFree() ?? ImmutableArray <Location> .Empty;
            }
            finally
            {
                // Put this in a finally because we have tests that (intentionally) cause ResolveReferenceDirective to throw and
                // we don't want to clutter the test output with leak reports.
                referencesBuilder.Free();
            }
        }
        protected void GetCompilationReferences(
            TCompilation compilation,
            DiagnosticBag diagnostics,
            out ImmutableArray <MetadataReference> references,
            out IDictionary <string, MetadataReference> boundReferenceDirectives,
            out ImmutableArray <Location> referenceDirectiveLocations)
        {
            boundReferenceDirectives = null;

            ArrayBuilder <MetadataReference> referencesBuilder = ArrayBuilder <MetadataReference> .GetInstance();

            ArrayBuilder <Location> referenceDirectiveLocationsBuilder = null;

            try
            {
                foreach (var referenceDirective in compilation.ReferenceDirectives)
                {
                    if (compilation.Options.MetadataReferenceResolver == null || compilation.Options.MetadataReferenceProvider == null)
                    {
                        diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_MetadataReferencesNotSupported, referenceDirective.Location));
                        break;
                    }

                    // we already successfully bound #r with the same value:
                    if (boundReferenceDirectives != null && boundReferenceDirectives.ContainsKey(referenceDirective.File))
                    {
                        continue;
                    }

                    MetadataReference boundReference = ResolveReferenceDirective(referenceDirective.File, referenceDirective.Location, compilation);
                    if (boundReference == null)
                    {
                        diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_MetadataFileNotFound, referenceDirective.Location, referenceDirective.File));
                        continue;
                    }

                    if (boundReferenceDirectives == null)
                    {
                        boundReferenceDirectives           = new Dictionary <string, MetadataReference>();
                        referenceDirectiveLocationsBuilder = ArrayBuilder <Location> .GetInstance();
                    }

                    referencesBuilder.Add(boundReference);
                    referenceDirectiveLocationsBuilder.Add(referenceDirective.Location);
                    boundReferenceDirectives.Add(referenceDirective.File, boundReference);
                }

                // Workaround for bug #797360: include facades if we reference a Portable Library:
                referencesBuilder.AddRange(ResolveFacadeReferences(compilation));

                // add external reference at the end, so that they are processed first:
                referencesBuilder.AddRange(compilation.ExternalReferences);

                if (boundReferenceDirectives == null)
                {
                    // no directive references resolved successfully:
                    boundReferenceDirectives = SpecializedCollections.EmptyDictionary <string, MetadataReference>();
                }

                references = referencesBuilder.ToImmutable();
                referenceDirectiveLocations = referenceDirectiveLocationsBuilder == null ? ImmutableArray <Location> .Empty : referenceDirectiveLocationsBuilder.ToImmutableAndFree();
            }
            finally
            {
                // Put this in a finally because we have tests that (intentionally) cause ResolveReferenceDirective to throw and
                // we don't want to clutter the test output with leak reports.
                referencesBuilder.Free();
            }
        }
Esempio n. 10
0
 public EditorAnalyzerConfigOptions(IEditorOptions editorOptions)
 {
     _configOptions = (editorOptions.GetOptionValue(DefaultOptions.RawCodingConventionsSnapshotOptionName) as IDictionary <string, object>) ??
                      SpecializedCollections.EmptyDictionary <string, object>();
 }