-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #2960: Only allow one inserted apply per tree #2962
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.glavo.dotty { | ||
|
||
import scala.collection.mutable | ||
|
||
sealed trait Node { | ||
def mkString(n: Int): String | ||
} | ||
|
||
class Tag(val name: String, | ||
val attributes: mutable.LinkedHashMap[Symbol, String] = mutable.LinkedHashMap(), | ||
val children: mutable.Buffer[Node] = mutable.Buffer()) extends Node { | ||
|
||
override def mkString(n: Int): String = { | ||
Tag.spaces(n) + s"<$name ${attributes.map(_.name + "=" + Tag.unescape(_)).mkString(" ")}>" + | ||
(if(children.isEmpty) "\n" | ||
else children.map(_.mkString(n + 4)).mkString("\n", "\n", "\n")) + | ||
Tag.spaces(n) + s"</$name>" | ||
} | ||
|
||
def apply(attrs: (Symbol, String)*): this.type = { | ||
attributes ++= attrs | ||
this | ||
} | ||
|
||
def apply[U](f: implicit Tag => U)(implicit t: Tag = null): this.type = { | ||
if(t != null) t.children += this | ||
f(this) | ||
this | ||
} | ||
} | ||
|
||
object Tag { | ||
def spaces(n: Int = 0): String = { | ||
if(n == 0) "" | ||
else { | ||
val cs = new Array[Char](n) | ||
for (i <- 0 until n) | ||
cs(i) = 0 | ||
|
||
new String(cs) | ||
} | ||
} | ||
|
||
def unescape(str: String): String = { | ||
"\"" + str + "\"" | ||
} | ||
|
||
implicit def symbolToTag(symbol: Symbol): Tag = | ||
new Tag(symbol.name) | ||
|
||
implicit class PairMaker(val tag: Symbol) extends AnyVal { | ||
def :=(value: String): (Symbol, String) = (tag, value) | ||
} | ||
} | ||
|
||
class Text(val value: String) extends Node { | ||
override def mkString(n: Int): String = { | ||
Tag.spaces(n) + value | ||
} | ||
} | ||
} | ||
|
||
object Test { | ||
import org.glavo.dotty._ | ||
import org.glavo.dotty.Tag._ | ||
'html{} // error | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is an attachment needed instead of checking if the tree selects
nme.apply
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. It depends how we want to specity it:
1st choice: An
apply
is inserted on any expression in function position unless that expression is already an inserted apply.2nd choice: An
apply
is inserted on any expression in function position unless that expression is itself anapply
selection or call.Which way should we go? Here's where it makes a difference. Consider the case where we want to expand to
If we give only one
apply
, which one should be the inserted one? According to the PR, it must be the secondapply
. If we followed 2nd choice, this would be rejected instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One reason to keep the current PR is that the first
apply
could be macro generated where the macro returns a map, say. Then it would seem sensical to insert the 2nd apply automatically, as for any other map.