Update Razor directives to format correctly when entering newline.

- Existing Razor directives layout, inherits, addTagHelper, tagHelperPrefix and removeTagHelper should only ever span a single line and need to cause a re-parse when a newline is entered during design time. To do this modified their AcceptedCharacters to accept anything other than newline rather than anything.
- Updated existing tests to now expect AcceptedCharacters.AnyExceptNewLine when directives are present.
- This change also enables the model directive in Mvc since it also uses the inherit directives core parsing.

#332
This commit is contained in:
N. Taylor Mullen 2015-04-14 16:10:43 -07:00
parent 5bcda94b2c
commit fedd53aab8
3 changed files with 19 additions and 10 deletions

View File

@ -65,7 +65,7 @@ namespace Microsoft.AspNet.Razor.Parser
Span.EditHandler.EditorHints = EditorHints.LayoutPage | EditorHints.VirtualPath; Span.EditHandler.EditorHints = EditorHints.LayoutPage | EditorHints.VirtualPath;
var foundNewline = Optional(CSharpSymbolType.NewLine); var foundNewline = Optional(CSharpSymbolType.NewLine);
AddMarkerSymbolIfNecessary(); AddMarkerSymbolIfNecessary();
Output(SpanKind.MetaCode, foundNewline ? AcceptedCharacters.None : AcceptedCharacters.Any); Output(SpanKind.MetaCode, foundNewline ? AcceptedCharacters.None : AcceptedCharacters.AnyExceptNewline);
} }
protected virtual void SectionDirective() protected virtual void SectionDirective()
@ -286,7 +286,7 @@ namespace Microsoft.AspNet.Razor.Parser
// Output the span and finish the block // Output the span and finish the block
CompleteBlock(); CompleteBlock();
Output(SpanKind.Code); Output(SpanKind.Code, AcceptedCharacters.AnyExceptNewline);
} }
private void TagHelperDirective(string keyword, Func<string, ISpanCodeGenerator> buildCodeGenerator) private void TagHelperDirective(string keyword, Func<string, ISpanCodeGenerator> buildCodeGenerator)
@ -304,7 +304,7 @@ namespace Microsoft.AspNet.Razor.Parser
// If we found whitespace then any content placed within the whitespace MAY cause a destructive change // If we found whitespace then any content placed within the whitespace MAY cause a destructive change
// to the document. We can't accept it. // to the document. We can't accept it.
Output(SpanKind.MetaCode, foundWhitespace ? AcceptedCharacters.None : AcceptedCharacters.Any); Output(SpanKind.MetaCode, foundWhitespace ? AcceptedCharacters.None : AcceptedCharacters.AnyExceptNewline);
if (EndOfFile || At(CSharpSymbolType.NewLine)) if (EndOfFile || At(CSharpSymbolType.NewLine))
{ {
@ -346,7 +346,7 @@ namespace Microsoft.AspNet.Razor.Parser
// Output the span and finish the block // Output the span and finish the block
CompleteBlock(); CompleteBlock();
Output(SpanKind.Code); Output(SpanKind.Code, AcceptedCharacters.AnyExceptNewline);
} }
} }
} }

View File

@ -305,24 +305,31 @@ namespace Microsoft.AspNet.Razor.Test.Framework
public SpanConstructor AsBaseType(string baseType) public SpanConstructor AsBaseType(string baseType)
{ {
return _self.With(new SetBaseTypeCodeGenerator(baseType)); return _self
.With(new SetBaseTypeCodeGenerator(baseType))
.Accepts(AcceptedCharacters.AnyExceptNewline);
} }
public SpanConstructor AsAddTagHelper(string lookupText) public SpanConstructor AsAddTagHelper(string lookupText)
{ {
return _self.With( return _self
new AddOrRemoveTagHelperCodeGenerator(removeTagHelperDescriptors: false, lookupText: lookupText)); .With(
new AddOrRemoveTagHelperCodeGenerator(removeTagHelperDescriptors: false, lookupText: lookupText))
.Accepts(AcceptedCharacters.AnyExceptNewline);
} }
public SpanConstructor AsRemoveTagHelper(string lookupText) public SpanConstructor AsRemoveTagHelper(string lookupText)
{ {
return _self.With( return _self
new AddOrRemoveTagHelperCodeGenerator(removeTagHelperDescriptors: true, lookupText: lookupText)); .With(new AddOrRemoveTagHelperCodeGenerator(removeTagHelperDescriptors: true, lookupText: lookupText))
.Accepts(AcceptedCharacters.AnyExceptNewline);
} }
public SpanConstructor AsTagHelperPrefixDirective(string prefix) public SpanConstructor AsTagHelperPrefixDirective(string prefix)
{ {
return _self.With(new TagHelperPrefixDirectiveCodeGenerator(prefix)); return _self
.With(new TagHelperPrefixDirectiveCodeGenerator(prefix))
.Accepts(AcceptedCharacters.AnyExceptNewline);
} }
public SpanConstructor As(ISpanCodeGenerator codeGenerator) public SpanConstructor As(ISpanCodeGenerator codeGenerator)

View File

@ -37,6 +37,7 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
Factory.MetaCode("layout ").Accepts(AcceptedCharacters.None), Factory.MetaCode("layout ").Accepts(AcceptedCharacters.None),
Factory.MetaCode("Foo Bar Baz") Factory.MetaCode("Foo Bar Baz")
.With(new SetLayoutCodeGenerator("Foo Bar Baz")) .With(new SetLayoutCodeGenerator("Foo Bar Baz"))
.Accepts(AcceptedCharacters.AnyExceptNewline)
.WithEditorHints(EditorHints.VirtualPath | EditorHints.LayoutPage) .WithEditorHints(EditorHints.VirtualPath | EditorHints.LayoutPage)
) )
); );
@ -63,6 +64,7 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
Factory.EmptyCSharp() Factory.EmptyCSharp()
.AsMetaCode() .AsMetaCode()
.With(new SetLayoutCodeGenerator(string.Empty)) .With(new SetLayoutCodeGenerator(string.Empty))
.Accepts(AcceptedCharacters.AnyExceptNewline)
.WithEditorHints(EditorHints.VirtualPath | EditorHints.LayoutPage) .WithEditorHints(EditorHints.VirtualPath | EditorHints.LayoutPage)
) )
); );