Beispiel #1
0
        protected override IDeclaredElement CreateDeclaredElement()
        {
            // the field doesn't have a name and is in a union case or in an exception
            var typeDeclaration = Parent as IFSharpTypeDeclaration;
            var typeSymbol      = typeDeclaration?.GetFSharpSymbol();

            if (typeSymbol == null)
            {
                return(null);
            }

            var result = new LocalList <IUnionCaseFieldDeclaration>();

            foreach (var child in typeDeclaration.Children())
            {
                if (child is IUnionCaseFieldDeclaration fieldDeclaration)
                {
                    result.Add(fieldDeclaration);
                }
            }

            var fieldDeclarations = result.ReadOnlyList();
            var index             = fieldDeclarations.IndexOf(this);
            var fields            = GetFields(typeSymbol);
            var caseField         =
                fields != null && index <= fields.Count
          ? fields[index]
          : null;

            return(caseField != null
        ? new FSharpUnionCaseField(this, caseField)
        : null);
        }
    public IEnumerable<RelatedFileOccurence> GetRelatedFiles(IProjectFile projectFile)
    {
      var typeNamesInFile = GetTypeNamesDefinedInFile(projectFile).ToList();

      // Look for the candidate types in the solution.
      var solution = projectFile.GetSolution();
      var candidateTypes = new LocalList<IClrDeclaredElement>();
      foreach (var candidateTypeName in GetTypeCandidates(typeNamesInFile))
      {
        var types = FindTypesByShortName(solution, candidateTypeName);
        candidateTypes.AddRange(types);
      }

      // Get the source files for each of the candidate types.
      var sourceFiles = new LocalList<IPsiSourceFile>();
      foreach (var type in candidateTypes)
      {
        var sourceFilesForCandidateType = type.GetSourceFiles();
        sourceFiles.AddRange(sourceFilesForCandidateType.ResultingList());
      }

      var elementCollector = new RecursiveElementCollector<ITypeDeclaration>();
      foreach (var psiSourceFile in sourceFiles)
      foreach (var file in psiSourceFile.EnumerateDominantPsiFiles())
      {
        elementCollector.ProcessElement(file);
      }

      var elements = elementCollector.GetResults();
      var projectFiles = elements.Select(declaration => declaration.GetSourceFile().ToProjectFile());

      var thisProjectName = projectFile.GetProject()?.Name;

      var occurences = new LocalList<RelatedFileOccurence>();

      foreach (var file in projectFiles.OfType<ProjectFileImpl>().Distinct(x => x.Location.FullPath))
      {
        // Remove all extensions (e.g.: .xaml.cs).
        var fileName = file.Name;
        var dotPos = fileName.IndexOf('.');
        if (dotPos != -1)
        {
          fileName = fileName.Substring(0, dotPos);
        }

        var relationKind = fileName.EndsWith("ViewModel") ? "ViewModel" : "View";

        var projectName = file.GetProject()?.Name;

        if (projectName != null &&
            !string.Equals(thisProjectName, projectName, StringComparison.OrdinalIgnoreCase))
        {
          relationKind += $" (in {projectName})";
        }

        occurences.Add(new RelatedFileOccurence(file, relationKind, projectFile));
      }

      return occurences.ReadOnlyList();
    }
Beispiel #3
0
        private ReferenceCollection CreateTypeNameReferences(ICSharpLiteralExpression literal,
                                                             ExpectedObjectTypeReferenceKind kind)
        {
            var literalValue = (string)literal.ConstantValue.Value;

            if (literalValue == null)
            {
                return(ReferenceCollection.Empty);
            }

            var symbolCache = literal.GetPsiServices().Symbols;

            IQualifier qualifier    = null;
            var        references   = new LocalList <IReference>();
            var        startIndex   = 0;
            var        nextDotIndex = literalValue.IndexOf('.');

            while (true)
            {
                var endIndex = nextDotIndex != -1 ? nextDotIndex : literalValue.Length;

                // startIndex + 1 to skip leading quote in tree node, which doesn't exist in literalValue
                var rangeWithin = TextRange.FromLength(startIndex + 1, endIndex - startIndex);

                // Behaviour and resolution is almost identical for each part.
                // For a single component, it is either a Unity object with an inferred namespace, a type in the global
                // namespace, or the namespace for an as yet uncompleted qualified type name
                // For a trailing component, it is a qualified reference, and could be a type, or a continuation of the
                // namespace qualification
                // For a middle component, it could be a namespace, or the user typing a new type, with the trailing
                // text being the old component
                // When there is no qualifier, resolve should match:
                // * inferred type, with expected type check
                // * type in global namespace, with expected type check
                // * namespace, with expected type check (so namespace won't be the last thing)
                // When there is a qualifier, resolve should match
                // * namespaces
                // * qualified type with expected type check
                // For the final component, resolve should match namespaces, but with the expected type check
                // At all times, completion should show both namespaces and qualified types
                // Leading and trailing space are treated as part of a name, and will cause resolve to fail
                // TODO: Handle trailing dot
                var isFinalPart = nextDotIndex == -1;
                var reference   = new UnityObjectTypeOrNamespaceReference(literal, qualifier, literal.Literal, rangeWithin,
                                                                          kind, symbolCache, isFinalPart);

                references.Add(reference);
                if (nextDotIndex == -1)
                {
                    break;
                }

                startIndex   = nextDotIndex + 1;
                nextDotIndex = literalValue.IndexOf('.', startIndex);
                qualifier    = reference;
            }

            return(new ReferenceCollection(references.ReadOnlyList()));
        }
Beispiel #4
0
    public static Boxing TryFind(
        Conversion conversion, [NotNull] IExpressionType sourceExpressionType, [NotNull] IType targetType, [NotNull] ITreeNode correspondingNode)
    {
        switch (conversion.Kind)
        {
        case ConversionKind.Boxing:
        {
            return(RefineBoxingConversionResult());
        }

        case ConversionKind.Unboxing:
        {
            return(RefineUnboxingConversionResult());
        }

        case ConversionKind.ImplicitTuple:
        case ConversionKind.ImplicitTupleLiteral:
        case ConversionKind.ExplicitTuple:
        case ConversionKind.ExplicitTupleLiteral:
        {
            var components = new LocalList <Boxing>();

            foreach (var(nested, componentIndex) in conversion.GetTopLevelNestedConversionsWithTypeInfo().WithIndexes())
            {
                var componentNode = TryGetComponentNode(correspondingNode, componentIndex) ?? correspondingNode;

                var nestedBoxing = TryFind(nested.Conversion, nested.SourceType, nested.TargetType, componentNode);
                if (nestedBoxing != null)
                {
                    components.Add(nestedBoxing);
                }
            }

            if (components.Count > 0)
            {
                return(new InsideTupleConversion(components.ReadOnlyList(), correspondingNode));
            }

            break;
        }

        case ConversionKind.ImplicitUserDefined:
        case ConversionKind.ExplicitUserDefined:
        {
            foreach (var nested in conversion.GetTopLevelNestedConversionsWithTypeInfo())
            {
                var nestedBoxing = TryFind(nested.Conversion, nested.SourceType, nested.TargetType, correspondingNode);
                if (nestedBoxing != null)
                {
                    return(nestedBoxing);
                }
            }

            break;
        }
        }

        return(null);
Beispiel #5
0
        public static IReadOnlyList <string> EnumerateKeyContainers([NotNull] string provider)
        {
            if (!CryptAcquireContext(out ProviderHandle prov, null, provider, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET | CRYPT_VERIFYCONTEXT))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var list = new LocalList <string>();
            var data = IntPtr.Zero;

            try
            {
                int flag = CRYPT_FIRST;
                int len  = 0;
                if (!CryptGetProvParam(prov, PP_ENUMCONTAINERS, IntPtr.Zero, ref len, flag))
                {
                    if (Marshal.GetLastWin32Error() != ERROR_MORE_DATA)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }

                data = Marshal.AllocHGlobal(len);
                do
                {
                    if (!CryptGetProvParam(prov, PP_ENUMCONTAINERS, data, ref len, flag))
                    {
                        if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
                        {
                            break;
                        }

                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    list.Add(Marshal.PtrToStringAnsi(data));
                    flag = CRYPT_NEXT;
                } while (true);
            }
            finally
            {
                if (data != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(data);
                }

                prov.Dispose();
            }

            return(list.ReadOnlyList());
        }
Beispiel #6
0
        private static IEnumerable <string> GetTypeCandidates([NotNull] IEnumerable <string> typeNamesInFile)
        {
            var candidates = new LocalList <string>();

            // For each type name in the file, create a list of candidates.
            foreach (var typeName in typeNamesInFile)
            {
                // If a view model...
                if (typeName.EndsWith("ViewModel", StringComparison.OrdinalIgnoreCase))
                {
                    // Remove ViewModel from end and add all the possible suffixes.
                    var baseName = typeName.Substring(0, typeName.Length - 9);
                    candidates.AddRange(ViewSuffixes.Select(suffix => baseName + suffix));

                    // Add base if it ends in one of the view suffixes.
                    if (ViewSuffixes.Any(suffix => baseName.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)))
                    {
                        candidates.Add(baseName);
                    }
                }

                foreach (var suffix in ViewSuffixes)
                {
                    if (typeName.EndsWith(suffix))
                    {
                        // Remove suffix and add ViewModel.
                        var baseName  = typeName.Substring(0, typeName.Length - suffix.Length);
                        var candidate = baseName + "ViewModel";
                        candidates.Add(candidate);

                        // Just add ViewModel
                        candidate = typeName + "ViewModel";
                        candidates.Add(candidate);
                    }
                }
            }

            return(candidates.ReadOnlyList());
        }