Map semicolon's in using statements.

This allows for users to write "@using System;" and still have proper intellisense and mapping.  Also removed some legacy code that I came across when running tests.
This commit is contained in:
N. Taylor Mullen 2014-02-24 17:29:52 -08:00
parent c28b1c538d
commit 49ffb5ae81
3 changed files with 28 additions and 11 deletions

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
@ -14,14 +16,32 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
protected override void Visit(UsingChunk chunk) protected override void Visit(UsingChunk chunk)
{ {
ImportedUsings.Add(chunk.Namespace); string documentContent = ((Span)chunk.Association).Content.Trim();
bool mapSemicolon = false;
using (Writer.BuildLineMapping(chunk.Start, chunk.Association.Length, Context.SourceFile)) if (documentContent.LastOrDefault() == ';')
{ {
Writer.WriteUsing(chunk.Namespace, endLine: false); mapSemicolon = true;
} }
Writer.WriteLine(";"); ImportedUsings.Add(chunk.Namespace);
// Depending on if the user has a semicolon in their @using statement we have to conditionally decide
// to include the semicolon in the line mapping.
using (Writer.BuildLineMapping(chunk.Start, documentContent.Length, Context.SourceFile))
{
Writer.WriteUsing(chunk.Namespace, endLine: false);
if (mapSemicolon)
{
Writer.Write(";");
}
}
if (!mapSemicolon)
{
Writer.WriteLine(";");
}
} }
} }
} }

View File

@ -91,10 +91,7 @@ namespace Microsoft.AspNet.Razor.Test.Generator
Directory.CreateDirectory("./tests"); Directory.CreateDirectory("./tests");
} }
RunTest(testType, onResults: (results) => RunTest(testType);
{
File.WriteAllText(String.Format("./tests/{0}.cs", testType), results.GeneratedCode);
});
} }
[Fact] [Fact]

View File

@ -12,12 +12,12 @@ namespace Microsoft.AspNet.Razor.Test.Generator.CodeTree
[Fact] [Fact]
public void CodeTreeWithUsings() public void CodeTreeWithUsings()
{ {
var syntaxTreeNode = Mock.Of<SyntaxTreeNode>(); var syntaxTreeNode = new Mock<Span>(new SpanBuilder());
var language = new CSharpRazorCodeLanguage(); var language = new CSharpRazorCodeLanguage();
RazorEngineHost host = new RazorEngineHost(language); RazorEngineHost host = new RazorEngineHost(language);
var context = CodeGeneratorContext.Create(host, "TestClass", "TestNamespace", "Foo.cs", shouldGenerateLinePragmas: false); var context = CodeGeneratorContext.Create(host, "TestClass", "TestNamespace", "Foo.cs", shouldGenerateLinePragmas: false);
context.CodeTreeBuilder.AddUsingChunk("FakeNamespace1", syntaxTreeNode); context.CodeTreeBuilder.AddUsingChunk("FakeNamespace1", syntaxTreeNode.Object);
context.CodeTreeBuilder.AddUsingChunk("FakeNamespace2.SubNamespace", syntaxTreeNode); context.CodeTreeBuilder.AddUsingChunk("FakeNamespace2.SubNamespace", syntaxTreeNode.Object);
CodeBuilder codeBuilder = language.CreateBuilder(context); CodeBuilder codeBuilder = language.CreateBuilder(context);
// Act // Act