Skip to content

Commit fdb84e5

Browse files
Helpers that accept StringBuilders now are supplied one.
1 parent a05cebb commit fdb84e5

File tree

8 files changed

+112
-55
lines changed

8 files changed

+112
-55
lines changed

Compiler/CodeGeneration/SyntaxHelper.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -276,21 +276,16 @@ internal static ExpressionStatementSyntax AppendFuntionCallResult(string functio
276276
}
277277
else
278278
{
279+
//ExpressionStatementSyntax
279280
ExpressionSyntax expr = SF.InvocationExpression(SF.ParseExpression(functionName))
280281
.AddArgumentListArguments(
281282
parameters.Select(x => SF.Argument(SF.ParseExpression(x))).ToArray()
282283
);
283284
if (doAwait)
284285
{
285-
expr = SF.ParenthesizedExpression(SF.AwaitExpression(expr));
286+
expr = SF.AwaitExpression(expr);
286287
}
287-
expr = ExpressionToString(expr);
288-
289-
return
290-
SbAppend(
291-
SF.Argument(expr),
292-
encoded: encoded
293-
);
288+
return SF.ExpressionStatement(expr);
294289
}
295290
}
296291

Compiler/Introspection/RoslynIntrospector.cs

+64-15
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ public RoslynIntrospector(Project project)
3333
foreach (var addedProject in changes.GetAddedProjects())
3434
projectCompilations.Add(addedProject.Id, GetCompilationForProject(addedProject));
3535
foreach (var projectChanges in changes.GetProjectChanges())
36+
{
3637
//Bruteforce way: Just get the new compilation...
3738
//If that does not scale try adding documents to the compilation (incremental update)
38-
projectCompilations[projectChanges.ProjectId] = GetCompilationForProject(projectChanges.NewProject);
39+
//projectCompilations[projectChanges.ProjectId] = GetCompilationForProject(projectChanges.NewProject);
40+
}
3941
solution = project.Solution;
4042
}
4143
else
@@ -132,12 +134,54 @@ private INamedTypeSymbol FindClassesWithNameAndAttribute(string fullName, string
132134
(y.AttributeClass != null && y.AttributeClass.Name.Equals(attribute)))))
133135
.OrderByDescending(x => x.ContainingNamespace.ToDisplayString())
134136
.FirstOrDefault();
137+
//var y = comp.GetSymbolsWithName(x => x.Equals(name, System.StringComparison.Ordinal))
138+
// .OfType<INamedTypeSymbol>()
139+
// .Where(x => NamespaceUtility.IsPartOf(x.ToDisplayString(), fullName)
140+
// && (x.GetAttributes().Any(y =>
141+
// (y.AttributeClass != null && y.AttributeClass.Name.Equals(attributeFull)) ||
142+
// (y.AttributeClass != null && y.AttributeClass.Name.Equals(attribute)))))
143+
// .OrderByDescending(x => x.ContainingNamespace.ToDisplayString())
144+
// .ToList();
145+
146+
//if (name == "_DyloAttributes")
147+
//{
148+
// var z1 = comp.GetSymbolsWithName(x => x.Equals(name, System.StringComparison.Ordinal)).OfType<INamedTypeSymbol>().ToList();
149+
// var z2 = comp.GetSymbolsWithName(x => x.Equals(name, System.StringComparison.Ordinal)).ToList();
150+
// var z3 = comp.GetSymbolsWithName(x => x.IndexOf("_DyloAttributes", StringComparison.OrdinalIgnoreCase) != -1).ToList();
151+
// var z4 = comp.GetSymbolsWithName(n => true).ToList();
152+
// //StiftungWarentest.Website.ViewsRelaunch.Shared._DyloAttributes
153+
// var z5 = z4.Where(n => n.ContainingNamespace.ToString().StartsWith("StiftungWarentest.Website.ViewsRelaunch", StringComparison.OrdinalIgnoreCase)).ToList();
154+
// Console.WriteLine(z1.Count);
155+
//}
156+
135157
if (template != null)
136158
return template;
137159
}
138160
return null;
139161
}
140162

163+
private IMethodSymbol findHelperMethod(string funtionName, List<ITypeSymbol> parameters)
164+
{
165+
foreach (var comp in projectCompilations.Values)
166+
{
167+
var candidates = comp.GetSymbolsWithName(x => x.Equals(funtionName))
168+
.OfType<IMethodSymbol>()
169+
.Where(x => x.IsStatic &&
170+
// The check for both HelperMethodAttribute and HelperMethodAttributeFull because when loading a asp.net core project
171+
// we the attribute name is HelpermethodAttribute while when loading a .net framework project the attribute name is
172+
// HelperMethodAttribute
173+
x.GetAttributes().Any(y => y.AttributeClass.Name.Equals(StringConstants.HELPERMETHODATTRIBUTEFULL)
174+
|| y.AttributeClass.Name.Equals(StringConstants.HELPERMETHODATTRIBUTE)));
175+
var helperMethod = candidates.FirstOrDefault(x => DoParametersMatch(x, parameters));
176+
if (helperMethod != null)
177+
{
178+
return helperMethod;
179+
}
180+
}
181+
return null;
182+
}
183+
184+
141185
/// <summary>
142186
/// Searches each referenced project for helper methods.
143187
/// These must serve following conditions:
@@ -149,23 +193,23 @@ private INamedTypeSymbol FindClassesWithNameAndAttribute(string fullName, string
149193
/// <param name="funtionName">Name of the Helper as declared in the handlebars-template</param>
150194
/// <param name="parameters">Types of the Parameters that are passed to the helper method</param>
151195
/// <returns>The MethodSymbol for the called HelperMethod or null if it could not be found</returns>
152-
public IMethodSymbol GetHelperMethod(string funtionName, List<ITypeSymbol> parameters)
196+
public IMethodSymbol GetHelperMethod(string funtionName, List<ITypeSymbol> parameters, out bool acceptsStringBuilder)
153197
{
154-
foreach (var comp in projectCompilations.Values)
198+
//acceptsStringBuilder = false;
199+
//return findHelperMethod(funtionName, parameters);
200+
201+
var sbSymbol = GetStringBuilderTypeSymbol();
202+
parameters.Add(sbSymbol);
203+
204+
IMethodSymbol helperMethod = findHelperMethod(funtionName, parameters);
205+
acceptsStringBuilder = true;
206+
if (helperMethod == null)
155207
{
156-
var candidates = comp.GetSymbolsWithName(x => x.Equals(funtionName))
157-
.OfType<IMethodSymbol>()
158-
.Where(x => x.IsStatic &&
159-
// The check for both HelperMethodAttribute and HelperMethodAttributeFull because when loading a asp.net core project
160-
// we the attribute name is HelpermethodAttribute while when loading a .net framework project the attribute name is
161-
// HelperMethodAttribute
162-
x.GetAttributes().Any(y => y.AttributeClass.Name.Equals(StringConstants.HELPERMETHODATTRIBUTEFULL)
163-
|| y.AttributeClass.Name.Equals(StringConstants.HELPERMETHODATTRIBUTE)));
164-
var helperMethod = candidates.FirstOrDefault(x => DoParametersMatch(x, parameters));
165-
if (helperMethod != null)
166-
return helperMethod;
208+
acceptsStringBuilder = false;
209+
parameters.Remove(sbSymbol);
210+
helperMethod = findHelperMethod(funtionName, parameters);
167211
}
168-
return null;
212+
return helperMethod;
169213
}
170214

171215
private static bool DoParametersMatch(IMethodSymbol methodSymbol, List<ITypeSymbol> parameters)
@@ -207,5 +251,10 @@ public INamedTypeSymbol GetStringTypeSymbol()
207251
{
208252
return projectCompilations.First().Value.GetSpecialType(SpecialType.System_String);
209253
}
254+
255+
public INamedTypeSymbol GetStringBuilderTypeSymbol()
256+
{
257+
return projectCompilations.First().Value.GetTypeByMetadataName(@"System.Text.StringBuilder");
258+
}
210259
}
211260
}

Compiler/Introspection/SymbolExtensions.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ public static ITypeSymbol GetElementSymbol(this ISymbol symbol)
6060
/// <param name="name"></param>
6161
/// <returns></returns>
6262
public static bool IsString(this ITypeSymbol symbol)
63-
{
63+
{
6464
return (symbol as INamedTypeSymbol).SpecialType.HasFlag(SpecialType.System_String);
6565
}
6666

67-
private static ITypeSymbol FindMemberRec(this ITypeSymbol symbol, string name)
67+
public static bool IsTaskOfString(this ITypeSymbol symbol)
68+
{
69+
return symbol != null && symbol.ToDisplayString().Equals(@"System.Threading.Tasks.Task<string>", StringComparison.Ordinal);
70+
}
71+
72+
private static ITypeSymbol FindMemberRec(this ITypeSymbol symbol, string name)
6873
{
6974
var result = symbol.GetMembers(name).FirstOrDefault();
7075
if (result == null && symbol.BaseType != null)

Compiler/Visitors/CodeGenerationVisitor.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,23 @@ public void Visit(HelperCall astLeaf)
273273
if (param.TryEvaluate(state, out Context paramContext))
274274
paramContextList.Add(paramContext);
275275
}
276-
var helperMethod = state.Introspector.GetHelperMethod(astLeaf.FunctionName, paramContextList.Select(x => x.Symbol).ToList());
276+
277+
bool acceptsStringBuilder;
278+
var helperMethod = state.Introspector.GetHelperMethod(astLeaf.FunctionName, paramContextList.Select(x => x.Symbol).ToList(), out acceptsStringBuilder);
277279
if (helperMethod != null)
278280
{
279281
state.RegisterUsing(helperMethod.ContainingNamespace.ToDisplayString());
280-
state.PushStatement(
282+
bool returnTypeIsString = helperMethod.ReturnType.IsString() || helperMethod.ReturnType.IsTaskOfString();
283+
var paramList = paramContextList.Select(x => x.FullPath).ToList();
284+
if (acceptsStringBuilder && !returnTypeIsString)
285+
{
286+
paramList.Add("sb");
287+
}
288+
state.PushStatement(
281289
SyntaxHelper.AppendFuntionCallResult(
282290
functionName: string.Concat(helperMethod.ContainingType.Name, ".", helperMethod.Name),
283-
parameters: paramContextList.Select(x => x.FullPath).ToList(),
284-
returnTypeIsString: helperMethod.ReturnType.IsString(),
291+
parameters: paramList,
292+
returnTypeIsString: returnTypeIsString,
285293
encoded: astLeaf.Type == TokenType.Encoded,
286294
doAwait: helperMethod.Name.EndsWith("Async", StringComparison.Ordinal)));
287295
}
+23-23
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
3-
<Metadata>
4-
<Identity Id="HandlebarsCompiler.Jakob Demler.71222594-41cc-4ba2-b691-ad7c2783a83b" Version="0.4.1" Language="en-US" Publisher="Jakob Demler" />
5-
<DisplayName>HandlebarsCompiler</DisplayName>
6-
<Description xml:space="preserve">Compiles Handlebars Templates into performant and typechecked C# Code</Description>
7-
<MoreInfo>https://github.com/Noxum/CompiledHandlebars</MoreInfo>
8-
<Tags>Handlebars Html Rendering</Tags>
9-
</Metadata>
10-
<Installation>
11-
<InstallationTarget Version="[15.0,17.0)" Id="Microsoft.VisualStudio.Pro" />
12-
<InstallationTarget Version="[15.0,17.0)" Id="Microsoft.VisualStudio.Community" />
13-
<InstallationTarget Version="[15.0,17.0)" Id="Microsoft.VisualStudio.Enterprise" />
14-
</Installation>
15-
<Dependencies>
16-
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
17-
</Dependencies>
18-
<Assets>
19-
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
20-
<Asset Type="Microsoft.VisualStudio.Assembly" d:Source="File" Path="Microsoft.CodeAnalysis.Workspaces.dll" AssemblyName="Microsoft.CodeAnalysis.Workspaces, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
21-
<Asset Type="Microsoft.VisualStudio.Assembly" d:Source="File" Path="microsoft.visualstudio.languageservices.dll" AssemblyName="Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
22-
</Assets>
23-
<Prerequisites>
24-
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,)" DisplayName="Visual Studio core editor" />
25-
</Prerequisites>
3+
<Metadata>
4+
<Identity Id="HandlebarsCompiler.Jakob Demler.71222594-41cc-4ba2-b691-ad7c2783a83b" Version="0.4.2" Language="en-US" Publisher="Jakob Demler" />
5+
<DisplayName>HandlebarsCompiler</DisplayName>
6+
<Description xml:space="preserve">Compiles Handlebars Templates into performant and typechecked C# Code</Description>
7+
<MoreInfo>https://github.com/Noxum/CompiledHandlebars</MoreInfo>
8+
<Tags>Handlebars Html Rendering</Tags>
9+
</Metadata>
10+
<Installation>
11+
<InstallationTarget Version="[15.0,17.0)" Id="Microsoft.VisualStudio.Pro" />
12+
<InstallationTarget Version="[15.0,17.0)" Id="Microsoft.VisualStudio.Community" />
13+
<InstallationTarget Version="[15.0,17.0)" Id="Microsoft.VisualStudio.Enterprise" />
14+
</Installation>
15+
<Dependencies>
16+
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
17+
</Dependencies>
18+
<Assets>
19+
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
20+
<Asset Type="Microsoft.VisualStudio.Assembly" d:Source="File" Path="Microsoft.CodeAnalysis.Workspaces.dll" AssemblyName="Microsoft.CodeAnalysis.Workspaces, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
21+
<Asset Type="Microsoft.VisualStudio.Assembly" d:Source="File" Path="microsoft.visualstudio.languageservices.dll" AssemblyName="Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
22+
</Assets>
23+
<Prerequisites>
24+
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,)" DisplayName="Visual Studio core editor" />
25+
</Prerequisites>
2626
</PackageManifest>

RuntimeUtils/CompiledHandlebars.RuntimeUtils.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net45;net46;net462;netstandard2</TargetFrameworks>
55
<AssemblyName>CompiledHandlebars.RuntimeUtils</AssemblyName>
66
<RootNamespace>CompiledHandlebars.RuntimeUtils</RootNamespace>
7-
<Version>0.4.1</Version>
7+
<Version>0.4.2</Version>
88
<Description>RuntimeUtils for the HandlebarsCompiler.</Description>
99
<Authors>CurrySoftware GmbH</Authors>
1010
<Company>CurrySoftware GmbH</Company>

ViewEngine.Core/CompiledHandlebars.ViewEngine.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>netstandard2;net462</TargetFrameworks>
55
<AssemblyName>CompiledHandlebars.ViewEngine.Core</AssemblyName>
66
<RootNamespace>CompiledHandlebars.ViewEngine.Core</RootNamespace>
7-
<Version>0.4.1</Version>
7+
<Version>0.4.2</Version>
88
<Authors>CurrySoftware GmbH</Authors>
99
<Company>CurrySoftware GmbH</Company>
1010
<Product>HanldebarsCompiler</Product>

ViewEngine/CompiledHandlebars.ViewEngine.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net45;net46;net462</TargetFrameworks>
55
<AssemblyName>CompiledHandlebars.ViewEngine</AssemblyName>
66
<RootNamespace>CompiledHandlebars.ViewEngine</RootNamespace>
7-
<Version>0.4.1</Version>
7+
<Version>0.4.2</Version>
88
<Authors>CurrySoftware GmbH</Authors>
99
<Company>CurrySoftware GmbH</Company>
1010
<Product>HanldebarsCompiler</Product>

0 commit comments

Comments
 (0)