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:
parent
c28b1c538d
commit
49ffb5ae81
|
|
@ -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(";");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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]
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue