public void cref_to_uri_round_trip() { var expectedCRef = new CRefIdentifier("!:Fake{Fake.Enumerable{System.Int32}}.Method(``0)"); var uri = expectedCRef.ToUri(); CRefIdentifier actualCRef; Assert.True(CRefIdentifier.TryParse(uri, out actualCRef)); Assert.NotNull(actualCRef); Assert.Equal(expectedCRef, actualCRef); }
public void constructor_cref_to_uri_round_trip() { var expectedCRef = new CRefIdentifier("!:Fake.#ctor()"); var uri = expectedCRef.ToUri(); CRefIdentifier actualCRef; Assert.True(CRefIdentifier.TryParse(uri, out actualCRef)); Assert.NotNull(actualCRef); Assert.Equal(expectedCRef, actualCRef); }
public void check_generated_uri() { var cRef = new CRefIdentifier("!:Fake{Fake.Enumerable{System.Int32}}.Method(``0)"); var uri = cRef.ToUri(); Assert.NotNull(uri); Assert.Equal("cref",uri.Scheme); Assert.Equal( "%21%3AFake%7BFake.Enumerable%7BSystem.Int32%7D%7D.Method%28%60%600%29", uri.PathAndQuery); }
/// <summary> /// Creates a namespace from the given name. /// </summary> /// <param name="namespaceName">The name of the namespace to create.</param> /// <returns>A namespace model.</returns> private CodeDocSimpleMember CreateNamespaceFromName(string namespaceName) { Contract.Ensures(Contract.Result<CodeDocSimpleMember>() != null); string namespaceFriendlyName; if (String.IsNullOrWhiteSpace(namespaceName)) { namespaceName = String.Empty; namespaceFriendlyName = "global"; } else { namespaceFriendlyName = namespaceName; } var cRef = new CRefIdentifier("N:" + namespaceName); return new CodeDocSimpleMember(cRef) { Uri = cRef.ToUri(), FullName = namespaceFriendlyName, ShortName = namespaceFriendlyName, SubTitle = "Namespace", Title = namespaceFriendlyName, ExternalVisibility = ExternalVisibilityKind.Public }; }
/// <summary> /// Creates a code doc placeholder model for a given code reference. /// </summary> /// <param name="cRef">The code reference to construct a model for.</param> /// <returns>A code model representing the given code reference.</returns> protected ICodeDocMember CreateGeneralMemberPlaceholder(CRefIdentifier cRef) { Contract.Requires(cRef != null); Contract.Ensures(Contract.Result<ICodeDocMember>() != null); string subTitle; if ("T".Equals(cRef.TargetType, StringComparison.OrdinalIgnoreCase)) subTitle = "Type"; else if ("M".Equals(cRef.TargetType, StringComparison.OrdinalIgnoreCase)) subTitle = "Invokable"; else if ("P".Equals(cRef.TargetType, StringComparison.OrdinalIgnoreCase)) subTitle = "Property"; else if ("E".Equals(cRef.TargetType, StringComparison.OrdinalIgnoreCase)) subTitle = "Event"; else if ("F".Equals(cRef.TargetType, StringComparison.OrdinalIgnoreCase)) subTitle = "Field"; else if ("N".Equals(cRef.TargetType, StringComparison.OrdinalIgnoreCase)) subTitle = "Namespace"; else if ("A".Equals(cRef.TargetType, StringComparison.OrdinalIgnoreCase)) subTitle = "Assembly"; else subTitle = String.Empty; var cRefFullName = cRef.CoreName; return new CodeDocSimpleMember(cRef) { Uri = cRef.ToUri(), ShortName = cRefFullName, Title = cRefFullName, SubTitle = subTitle, FullName = cRefFullName, NamespaceName = String.Empty, ExternalVisibility = ExternalVisibilityKind.Public }; }
/// <summary> /// Generates a model that is extracted from MTPS data. /// </summary> /// <param name="cRef">The code reference to search for.</param> /// <param name="searchContext">The search context for locating related items.</param> /// <param name="detailLevel">The desired detail level of the generated model.</param> /// <returns>A code doc member model if found.</returns> public ICodeDocMember GetMemberModel(CRefIdentifier cRef, CodeDocRepositorySearchContext searchContext = null, CodeDocMemberDetailLevel detailLevel = CodeDocMemberDetailLevel.Full) { if(cRef == null) throw new ArgumentNullException("cRef"); Contract.EndContractBlock(); var stopwatch = new Stopwatch(); stopwatch.Start(); var tocResults = SearchToc(cRef.FullCRef); var bestTocResult = tocResults.FirstOrDefault(); stopwatch.Stop(); Debug.WriteLine("Search for {0} returned in {1}", cRef, stopwatch.Elapsed); if (bestTocResult == null) return null; Uri uri; if (!Uri.TryCreate(GetUrl(bestTocResult), UriKind.Absolute, out uri)) uri = cRef.ToUri(); CodeDocSimpleMember model = null; if (detailLevel != CodeDocMemberDetailLevel.Minimum) { var contentXml = GetContent(bestTocResult.ContentId); if (contentXml != null) { bool? isSealed = null; bool? isValueType = null; bool? isReferenceType = null; bool? isEnum = null; bool? isVirtual = null; bool? isAbstract = null; bool? isMethod = null; ExternalVisibilityKind? visibility = null; var titleDiv = contentXml.GetElementsByTagName("div") .OfType<XmlElement>() .FirstOrDefault(x => "TITLE".Equals(x.GetAttribute("class"), StringComparison.OrdinalIgnoreCase)); if (titleDiv != null) { var innerTitleText = titleDiv.InnerText.Trim(); if (innerTitleText.EndsWith("Method", StringComparison.OrdinalIgnoreCase)) { isMethod = true; } } var syntaxElement = contentXml.GetElementsByTagName("mtps:CollapsibleArea") .OfType<XmlElement>() .FirstOrDefault(x => "SYNTAX".Equals(x.GetAttribute("Title"), StringComparison.OrdinalIgnoreCase)); var codeSnippets = syntaxElement.GetElementsByTagName("mtps:CodeSnippet").OfType<XmlElement>().ToList(); foreach (var snippet in codeSnippets) { var inputSpans = snippet.GetElementsByTagName("span") .OfType<XmlElement>() .Where(e => e.GetAttribute("class") == "input") .ToList(); if (snippet.GetAttribute("Language") == "CSharp") { foreach (var inputSpan in inputSpans) { var innerText = inputSpan.InnerText; if ("PUBLIC".Equals(innerText, StringComparison.OrdinalIgnoreCase)) visibility = ExternalVisibilityKind.Public; else if ("PROTECTED".Equals(innerText, StringComparison.OrdinalIgnoreCase)) visibility = ExternalVisibilityKind.Protected; else if ("SEALED".Equals(innerText, StringComparison.OrdinalIgnoreCase)) isSealed = true; else if ("CLASS".Equals(innerText, StringComparison.OrdinalIgnoreCase)) isReferenceType = true; else if ("STRUCT".Equals(innerText, StringComparison.OrdinalIgnoreCase)) isValueType = true; else if ("ENUM".Equals(innerText, StringComparison.OrdinalIgnoreCase)) isEnum = true; else if ("VIRTUAL".Equals(innerText, StringComparison.OrdinalIgnoreCase)) isVirtual = true; else if ("ABSTRACT".Equals(innerText, StringComparison.OrdinalIgnoreCase)) isAbstract = true; } } } if (model == null) { if (isReferenceType ?? isValueType ?? isEnum ?? false) { var typeModel = new CodeDocType(cRef); typeModel.IsSealed = isSealed; typeModel.IsValueType = isValueType; typeModel.IsEnum = isEnum; model = typeModel; } else if (isMethod ?? false) { var methodModel = new CodeDocMethod(cRef); methodModel.IsSealed = isSealed; methodModel.IsVirtual = isVirtual; methodModel.IsAbstract = isAbstract; model = methodModel; } else { model = new CodeDocSimpleMember(cRef); } model.ExternalVisibility = visibility ?? ExternalVisibilityKind.Hidden; } XmlNode summaryElement = contentXml.GetElementsByTagName("div") .OfType<XmlElement>() .FirstOrDefault(x => String.Equals(x.GetAttribute("class"), "summary", StringComparison.OrdinalIgnoreCase)); if (summaryElement != null) { if (summaryElement.ChildNodes.Count == 1 && summaryElement.ChildNodes[0].Name == "p") { // unwrap the lone p tag. summaryElement = summaryElement.ChildNodes[0]; } var summaryXmlDoc = XmlDocParser.Default.Parse(summaryElement); model.SummaryContents = summaryXmlDoc.Children; } } } if (model == null) model = new CodeDocSimpleMember(cRef); if(model.ExternalVisibility == ExternalVisibilityKind.Hidden) model.ExternalVisibility = ExternalVisibilityKind.Public; model.Uri = uri; model.FullName = bestTocResult.GetFullName(); int lastTitleSpaceIndex = bestTocResult.Title.LastIndexOf(' '); if (lastTitleSpaceIndex >= 0) { model.Title = bestTocResult.Title.Substring(0, lastTitleSpaceIndex); model.SubTitle = bestTocResult.Title.Substring(lastTitleSpaceIndex + 1); } else { model.Title = bestTocResult.Title; model.SubTitle = String.Empty; } model.ShortName = model.Title; if (String.IsNullOrEmpty(model.NamespaceName)) { MtpsNodeCore namespaceNode = bestTocResult; while (namespaceNode != null && !namespaceNode.IsNamespace) { namespaceNode = namespaceNode.Parent; } if (namespaceNode != null) { model.NamespaceName = namespaceNode.GetNamespaceName(); model.Namespace = GetNamespaceByName(model.NamespaceName); } } return model; }