Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow markup in @functions #317

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
3 participants
@rynowak
Copy link
Member

rynowak commented Mar 8, 2019

This change allows markup elements to appear in inside methods in the
functions block using template syntax.

The template codegen is replaced (for the functions block) with the
normal Razor codegen for the render methods. So instead of returning an
IHtmlContent, it's void/Task.

Two ugly things:

  • You have to wrap in a codeblock to invoke in some cases because it's
    void-returning
  • Design-time experience doesn't tell you that you have an error if you
    use tag helpers in the method and declare the method as void. This is a
    new problem now that allow tag helpers is appear here.
Allow markup in @functions
This change allows markup elements to appear in inside methods in the
functions block using template syntax.

The template codegen is replaced (for the functions block) with the
normal Razor codegen for the render methods. So instead of returning an
IHtmlContent, it's void/Task.

Two ugly things:
- You have to wrap in a codeblock to invoke in some cases because it's
void-returning
- Design-time experience doesn't tell you that you have an error if you
use tag helpers in the method and declare the method as void. This is a
new problem now that allow tag helpers is appear here.

@rynowak rynowak requested review from ajaybhargavb and NTaylorMullen Mar 8, 2019

@rynowak

This comment has been minimized.

Copy link
Member Author

rynowak commented Mar 8, 2019

@@ -103,7 +103,7 @@ internal static bool IsAcceptableInsertion(SourceChange change)
// Internal for testing
internal static bool ContainsInvalidContent(SourceChange change)
{
if (change.NewText.IndexOfAny(new[] { '{', '}' }) >= 0)
if (change.NewText.IndexOfAny(new[] { '{', '}', '@', '<', '*', }) >= 0)

This comment has been minimized.

@rynowak

rynowak Mar 8, 2019

Author Member

@NTaylorMullen - looking for your advice on this, plus any ideas you have if I'm missing tests. I had to add all of these various characters to make partial-parsing work but other than that it was straightforward.

I considered making these conditional on the version, but functions doesn't get much usage outside of 3.0 scenarios anyway....

This comment has been minimized.

@NTaylorMullen

NTaylorMullen Mar 8, 2019

Member

Played around with this a bit and it looks like it doesn't work exactly how you'd expect. For instance if you have

@functions  {
    
    |

    @<p></p>
}

Typing anything at the pipe wont end up hitting the codeblock edit handler. Seems as though bits don't get classified properly when the functions block already has a template in it.

RazorMetaCode - [13..14)::1 - Gen<None> - AutoCompleteEditHandler;Accepts:None,AutoComplete:[<null>];AtEnd
LeftBrace;[{];
CSharpCodeBlock - [14..79)::65
CSharpStatementLiteral - [14..79)::65 - [LF void Announcment(string message) => "@<h3>@message</h3>";LF] - Gen<Stmt> - CodeBlockEditHandler;Accepts:Any;CodeBlock

This comment has been minimized.

@rynowak

rynowak Mar 8, 2019

Author Member

doesn't parse templates inside a string.

RazorMetaCode - [13..14)::1 - Gen<None> - AutoCompleteEditHandler;Accepts:None,AutoComplete:[<null>];AtEnd
LeftBrace;[{];
CSharpCodeBlock - [14..96)::82
CSharpStatementLiteral - [14..96)::82 - [LF void Announcment(string message)LF {LF @<h3>@message</h3>LF }LF] - Gen<Stmt> - CodeBlockEditHandler;Accepts:Any;CodeBlock

This comment has been minimized.

@rynowak

rynowak Mar 8, 2019

Author Member

parsing behaviour is unchanged on downlevel.

@m0sa

This comment has been minimized.

Copy link

m0sa commented Mar 8, 2019

This made me think that this would have to work with local functions, too... e.g.:

@functions {
   void Foo(int[] number) {
     int x = 0;
     @<ul>
        @foreach(var n in numbers) { x = n; Bar(); }
     </ul>

     // Bar is a Local Function inside of Foo
     void Bar() { @<li>@x</li> }
   }
}
@NTaylorMullen

This comment has been minimized.

Copy link
Member

NTaylorMullen commented Mar 14, 2019

Taking a different spin on this here #334

@rynowak

This comment has been minimized.

Copy link
Member Author

rynowak commented Mar 15, 2019

Closing in favour of #334

@rynowak rynowak closed this Mar 15, 2019

@rynowak rynowak deleted the rynowak/function-templates branch Mar 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.