public void CommentCodeTypeIsTranslatedToClassAttribute() { var ch = new AnnotatedCodeChunk { TextValue = "// some comment", CodeType = CodeType.Comment }; var chHtmlized = new CodeChunkHtmlizer().HtmlizeChunkText(1, ch); Assert.AreEqual("<span class='c'>// some comment</span>", chHtmlized); }
public void PlainChunkIsNotAnnotated() { var ch = new AnnotatedCodeChunk { TextValue = "just text" }; var chHtmlized = new CodeChunkHtmlizer().HtmlizeChunkText(1, ch); Assert.AreEqual("just text", chHtmlized); }
public string HtmlizeChunkText(int chunkId, AnnotatedCodeChunk chunk) { var baseValue = chunk.TextValue; // <span onmouseout="hideTip(event, 'fs3', 3)" onmouseover="showTip(event, 'fs3', 3)" class="t">Fit</span> var mouseAttributes = ""; if (!string.IsNullOrWhiteSpace(chunk.TooltipValue)) { mouseAttributes = $" onmouseout='hideTip(event, \"cs{chunkId}\")', {chunkId} onmouseover='showTip(event, \"cs{chunkId}\", {chunkId})'"; } switch (chunk.CodeType) { case CodeType.Comment: baseValue = $"<span{mouseAttributes} class='c'>{baseValue}</span>"; break; case CodeType.Operator: baseValue = $"<span{mouseAttributes} class='o'>{baseValue}</span>"; break; case CodeType.Keyword: baseValue = $"<span{mouseAttributes} class='k'>{baseValue}</span>"; break; case CodeType.Type: baseValue = $"<span{mouseAttributes} class='t'>{baseValue}</span>"; break; case CodeType.Method: baseValue = $"<span{mouseAttributes} class='f'>{baseValue}</span>"; break; default: if (mouseAttributes != "") { baseValue = $"<span{mouseAttributes}>{baseValue}</span>"; } break; } return baseValue; }
// <div class="tip" id="fs1">val a : float<br /><br />Full name: Regression.a</div> public string HtmlizeChunkTooltip(int chunkId, AnnotatedCodeChunk chunk) => $"<div class='tip' id='cs{chunkId}'>{chunk.TooltipValue}</div>";
public override void VisitToken(SyntaxToken token) { var itc = new AnnotatedCodeChunk {TextValue = token.Text}; var node = token.Parent; if (token.IsKind(SyntaxKind.IdentifierToken)) { if (node is IdentifierNameSyntax) // var, or variable mention { var symbol = SemanticModel.GetSymbolInfo(node).Symbol; if (symbol is INamedTypeSymbol) // var, or variable mention { if (node.ToString() == "var") { itc.CodeType = CodeType.Keyword; } else { itc.CodeType = CodeType.Type; } itc.TooltipValue = GetTooltipForType(symbol as INamedTypeSymbol); } else if (symbol is IFieldSymbol) // variable mention { itc.CodeType = CodeType.Variable; itc.TooltipValue = GetTooltipForType((symbol as IFieldSymbol).Type); } else if (symbol is IMethodSymbol) // method call { itc.CodeType = CodeType.Method; itc.TooltipValue = GetTooltipForMethod(symbol as IMethodSymbol); } else if (symbol is INamespaceSymbol) { itc.CodeType = CodeType.Namespace; itc.TooltipValue = GetTooltipForNamespace(symbol as INamespaceSymbol); } else if (symbol is IPropertySymbol) { itc.CodeType = CodeType.Property; itc.TooltipValue = GetTooltipForProperty(symbol as IPropertySymbol); } } if (node is VariableDeclaratorSyntax) // variable name declaration { var symbol = SemanticModel.GetDeclaredSymbol(node); var typeSymbol = symbol as IFieldSymbol; itc.CodeType = CodeType.Variable; itc.TooltipValue = GetTooltipForType(typeSymbol?.Type); } if (node is GenericNameSyntax) { var symbol = SemanticModel.GetSymbolInfo(node).Symbol; if (symbol is INamedTypeSymbol) { itc.CodeType = CodeType.Type; itc.TooltipValue = GetTooltipForType(symbol as INamedTypeSymbol); } } } else if (node is PredefinedTypeSyntax) // "int" { var type = SemanticModel.GetTypeInfo(node).Type; itc.CodeType = CodeType.Keyword; itc.TooltipValue = GetTooltipForType(type); } if (token.IsKeyword()) { itc.CodeType = CodeType.Keyword; } AddItcAction(itc); base.VisitToken(token); }
public override void VisitTrivia(SyntaxTrivia trivia) { var atch = new AnnotatedCodeChunk { TextValue = trivia.ToString() }; var triviaKind = trivia.Kind(); if (triviaKind == SyntaxKind.SingleLineCommentTrivia || triviaKind == SyntaxKind.MultiLineCommentTrivia || triviaKind == SyntaxKind.XmlComment) { atch.CodeType = CodeType.Comment; } AddItcAction(atch); base.VisitTrivia(trivia); }