Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit bc7241d

Browse files
committed
Changes to show intellisense for @Inject directive
1 parent 7f34709 commit bc7241d

File tree

4 files changed

+117
-17
lines changed

4 files changed

+117
-17
lines changed

src/Microsoft.AspNet.Mvc.Razor.Host/InjectChunkVisitor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ protected override void Visit([NotNull] InjectChunk chunk)
3838
if (Context.Host.DesignTimeMode && chunk.Association != null)
3939
{
4040
Writer.WriteLine("public");
41-
var code = string.Format(CultureInfo.InvariantCulture,
42-
"{0} {1}",
43-
chunk.TypeName,
44-
chunk.MemberName);
41+
42+
var code = string.IsNullOrEmpty(chunk.MemberName) ?
43+
chunk.TypeName :
44+
chunk.TypeName + ' ' + chunk.MemberName;
4545
var csharpVisitor = new CSharpCodeVisitor(Writer, Context);
4646
csharpVisitor.CreateExpressionCodeMapping(code, chunk);
4747
Writer.WriteLine("{ get; private set; }");

src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorCodeParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected virtual void InjectDirective()
107107
var propertyStartLocation = CurrentLocation;
108108
AcceptWhile(IsSpacingToken(includeNewLines: false, includeComments: true));
109109

110-
if (!hasTypeError && At(CSharpSymbolType.NewLine))
110+
if (!hasTypeError && (EndOfFile || At(CSharpSymbolType.NewLine)))
111111
{
112112
// Add an error for the property name only if we successfully read the type name
113113
Context.OnError(propertyStartLocation,
@@ -131,6 +131,7 @@ protected virtual void InjectDirective()
131131
propertyName.Trim());
132132

133133
// Output the span and finish the block
134+
CompleteBlock();
134135
Output(SpanKind.Code);
135136
}
136137

test/Microsoft.AspNet.Mvc.Razor.Host.Test/InjectChunkVisitorTest.cs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using Microsoft.AspNet.Razor;
@@ -71,18 +72,54 @@ public void Visit_GeneratesProperties_ForInjectChunks()
7172
public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForInjectChunks()
7273
{
7374
// Arrange
74-
var expected = @"[Microsoft.AspNet.Mvc.ActivateAttribute]
75-
public
76-
#line 1 """"
77-
MyType1 MyPropertyName1
75+
var expected = string.Join(Environment.NewLine,
76+
@"[Microsoft.AspNet.Mvc.ActivateAttribute]",
77+
@"public",
78+
@"#line 1 """"",
79+
@"MyType1 MyPropertyName1",
80+
"",
81+
@"#line default",
82+
@"#line hidden",
83+
@"{ get; private set; }",
84+
@"[Microsoft.AspNet.Mvc.ActivateAttribute]",
85+
@"public",
86+
@"#line 1 """"",
87+
@"MyType2 @MyPropertyName2",
88+
"",
89+
@"#line default",
90+
@"#line hidden",
91+
@"{ get; private set; }",
92+
"");
93+
var writer = new CSharpCodeWriter();
94+
var context = CreateContext();
95+
context.Host.DesignTimeMode = true;
7896

79-
#line default
80-
#line hidden
81-
{ get; private set; }
82-
[Microsoft.AspNet.Mvc.ActivateAttribute]
97+
var visitor = new InjectChunkVisitor(writer, context, "Microsoft.AspNet.Mvc.ActivateAttribute");
98+
var factory = SpanFactory.CreateCsHtml();
99+
var node = (Span)factory.Code("Some code")
100+
.As(new InjectParameterGenerator("MyType", "MyPropertyName"));
101+
102+
// Act
103+
visitor.Accept(new Chunk[]
104+
{
105+
new LiteralChunk(),
106+
new InjectChunk("MyType1", "MyPropertyName1") { Association = node },
107+
new InjectChunk("MyType2", "@MyPropertyName2") { Association = node }
108+
});
109+
var code = writer.GenerateCode();
110+
111+
// Assert
112+
Assert.Equal(expected, code);
113+
}
114+
115+
[Fact]
116+
public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForPartialInjectChunks()
117+
{
118+
// Arrange
119+
var expected = @"[Microsoft.AspNet.Mvc.ActivateAttribute]
83120
public
84121
#line 1 """"
85-
MyType2 @MyPropertyName2
122+
MyType1
86123
87124
#line default
88125
#line hidden
@@ -101,8 +138,7 @@ MyType2 @MyPropertyName2
101138
visitor.Accept(new Chunk[]
102139
{
103140
new LiteralChunk(),
104-
new InjectChunk("MyType1", "MyPropertyName1") { Association = node },
105-
new InjectChunk("MyType2", "@MyPropertyName2") { Association = node }
141+
new InjectChunk("MyType1", string.Empty) { Association = node },
106142
});
107143
var code = writer.GenerateCode();
108144

test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcCSharpRazorCodeParserTest.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public void ParseInjectKeyword_ErrorOnMissingTypeName()
309309
{
310310
// Arrange
311311
var errors = new List<RazorError>();
312-
var documentContent = "@inject \r\nBar";
312+
var documentContent = "@inject " + Environment.NewLine + "Bar";
313313
var factory = SpanFactory.CreateCsHtml();
314314
var expectedSpans = new Span[]
315315
{
@@ -337,6 +337,37 @@ public void ParseInjectKeyword_ErrorOnMissingTypeName()
337337
Assert.Equal(expectedErrors, errors);
338338
}
339339

340+
[Fact]
341+
public void ParseInjectKeyword_ErrorOnMissingTypeName_WhenTypeNameEndsWithEOF()
342+
{
343+
// Arrange
344+
var errors = new List<RazorError>();
345+
var documentContent = "@inject ";
346+
var factory = SpanFactory.CreateCsHtml();
347+
var expectedSpans = new Span[]
348+
{
349+
factory.EmptyHtml(),
350+
factory.CodeTransition(SyntaxConstants.TransitionString)
351+
.Accepts(AcceptedCharacters.None),
352+
factory.MetaCode("inject ")
353+
.Accepts(AcceptedCharacters.None),
354+
factory.Code(" ")
355+
.As(new InjectParameterGenerator(string.Empty, string.Empty)),
356+
};
357+
var expectedErrors = new[]
358+
{
359+
new RazorError("The 'inject' keyword must be followed by a type name on the same line.",
360+
new SourceLocation(11, 0, 11), 1)
361+
};
362+
363+
// Act
364+
var spans = ParseDocument(documentContent, errors);
365+
366+
// Assert
367+
Assert.Equal(expectedSpans, spans);
368+
Assert.Equal(expectedErrors, errors);
369+
}
370+
340371
[Fact]
341372
public void ParseInjectKeyword_ErrorOnMissingPropertyName()
342373
{
@@ -371,6 +402,38 @@ public void ParseInjectKeyword_ErrorOnMissingPropertyName()
371402
Assert.Equal(expectedErrors, errors);
372403
}
373404

405+
[Fact]
406+
public void ParseInjectKeyword_ErrorOnMissingPropertyName_WhenTypeNameEndsWithEOF()
407+
{
408+
// Arrange
409+
var errors = new List<RazorError>();
410+
var documentContent = "@inject IMyServi";
411+
var factory = SpanFactory.CreateCsHtml();
412+
var expectedSpans = new Span[]
413+
{
414+
factory.EmptyHtml(),
415+
factory.CodeTransition(SyntaxConstants.TransitionString)
416+
.Accepts(AcceptedCharacters.None),
417+
factory.MetaCode("inject ")
418+
.Accepts(AcceptedCharacters.None),
419+
factory.Code(" IMyServi")
420+
.As(new InjectParameterGenerator("IMyServi", string.Empty)),
421+
};
422+
var expectedErrors = new[]
423+
{
424+
new RazorError("A property name must be specified when using the 'inject' statement. " +
425+
"Format for a 'inject' statement is '@inject <Type Name> <Property Name>'.",
426+
new SourceLocation(19, 0, 19), 1)
427+
};
428+
429+
// Act
430+
var spans = ParseDocument(documentContent, errors);
431+
432+
// Assert
433+
Assert.Equal(expectedSpans, spans);
434+
Assert.Equal(expectedErrors, errors);
435+
}
436+
374437
private static List<Span> ParseDocument(string documentContents,
375438
List<RazorError> errors = null,
376439
List<LineMapping> lineMappings = null)

0 commit comments

Comments
 (0)