Scala enhancements

General Discussion on RSyntaxTextArea.

Moderator: robert

Scala enhancements

Postby Guest » Thu Nov 08, 2012 1:29 pm

Hi,

I've been using RSTA/Autocomplete with very good success (for my own work and to teach kids) within Kojo (http://www.kogics.net/webkojo).
Kojo is based on Scala, and for the most part, Scala support within RSTA is good. But I have identified a few areas that need improvement:
    * support for triple quoted multiline strings
    * xml literal enhancement
      * highlighting of xml tags
      * no scala keyword highlighting, except within scala escape blocks - { }
    * code folding

Here's a small Kojo program that shows these issues in action:

scala code:

val truetext = """ 
Yes!
"""
val falsetext = """
No!!
"""

val pg = Page(
name = "",
body =
<body>
Some xml literal based text with Scala keywords that should not be highlighted - if, else <br/>
Within Scala escape blocks (below), keywords should be highlighted
(as they are) <br/>
{ if (true) <em>{truetext}</em> else <em>falsetext</em> }
</body>
)

stClear()
stPlayStory(Story(pg))


How difficult is this to pull off? Pointers appreciated...

Thanks,
- Lalit
Guest
 

Re: Scala enhancements

Postby robert » Thu Nov 08, 2012 2:26 pm

These should all be easy enough to add, I'll take a look. I hope to make the 2.0.5 release in a week or two, it should be ready by then.

The only one I'm not sure of is code folding. Can you give me guidance on what should be considered a foldable region (what the start and end markers would be)?
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: Scala enhancements

Postby Guest » Thu Nov 08, 2012 4:40 pm

These should all be easy enough to add, I'll take a look. I hope to make the 2.0.5 release in a week or two, it should be ready by then.


Awesome!

The only one I'm not sure of is code folding. Can you give me guidance on what should be considered a foldable region (what the start and end markers would be)?


This will go a long way:

def x[...](...) : ... {
// foldable region, including closing brace below
}

class X[...](...) {
// foldable region, including closing brace below
}

object X {
// foldable region, including closing brace below
}

trait X {
// foldable region, including closing brace below
}

stuff in [] == type params
stuff in () == value params

If that's not precise enough, please let me know, and I'll come up with a better formulation.

Thanks,
- Lalit
Guest
 

Re: Scala enhancements

Postby Guest » Fri Nov 09, 2012 3:55 am

Looking at my list of foldables, it seems that it can be generalized to:

start marker: ' { '
end marker: matching ' } '

i.e. all blocks are foldable

Also, good to have (but not essentail) would be:

start marker: ' """ '
end marker: next ' """ '

i.e. multi line strings are foldable

Thanks,
- Lalit
Guest
 

Re: Scala enhancements

Postby robert » Sun Nov 11, 2012 4:25 am

Code folding and multi-line comment highlighting were added to SVN in commit 699. They'll be in the 2.0.5 release, which will hopefully be in a week.

As for XML literals, this will be tougher. RSTA already supports this for JavaScript (e4x), but we have an easy way to know when the XML literal is terminated there - stop on the first ';' character. In the case of Scala, this doesn't seem to be something we can rely on. The only way I can think of implementing this is to require the XML literal to be a single element with child content, i.e. this would be valid:

scala code:

val foo = <html>
<body>
Hello world!
</body>
</html>


But this would not be:

scala code:

val2 = <script>
doSomething();
</script>
<script>
doSomethingElse();
</script>


For all I know, this is also enforced by the actual Scala compiler. If it actually does handle the second scenario, then I congratulate them on writing such an intelligent parser. :P But it's probably more than RSTA's parsing capabilities can currently handle.

Can you confirm that the first scenario is all that would need to be handled? If so, the parser will probably count the number of opening tags and closing tags, and when the net amount == 0, assume the XML literal has ended.
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: Scala enhancements

Postby Guest » Sun Nov 11, 2012 5:19 pm

Code folding and multi-line comment highlighting were added to SVN in commit 699. They'll be in the 2.0.5 release, which will hopefully be in a week.

Great. I'll try them out.

Can you confirm that the first scenario is all that would need to be handled?


I have never used anything other than the first scenario, but it turns out that Scala supports the second scenario too!
For all practical purposes, though, if the first scenario is supported, that should be good enough.

Thanks,
- Lalit
Guest
 

Re: Scala enhancements

Postby robert » Wed Nov 14, 2012 3:48 am

Next question: Does Scala accept "incomplete" tags?

scala code:

val anotherOne = <opened>
<child1/>
<child2/>
<child3>Parent is not closed!</child3>


Just want to make sure; I'm not sure if the above scenario is common, but if it's possible and the syntax highlighting doesn't recognize it, it would keep highlighting future (non-markup) tokens as XML tags, which would be bad. If it does not accept this, then we're golden.
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: Scala enhancements

Postby Guest » Wed Nov 14, 2012 5:34 am

Does Scala accept "incomplete" tags?


No it does not. Without the closing tag, Scala keeps interpreting tokens as xml content, and eventually runs into a compile time error.

Thanks,
- Lalit
Guest
 

Re: Scala enhancements

Postby robert » Sat Nov 17, 2012 4:01 pm

I thought more about this, and I believe it's simply too difficult for RSTA to parse in its current form. Only because you can nest Scala snippets in XML literals and vice versa, to unlimited depths, and each piece of XML can also be of unlimited "depth." RSTA can only remember 32 bits of information per line. What we need to remember is

  • Whether we're in Scala or XML
    • If in Scala, remember if we're in a comment or multi-line string
    • If in XML, remember if we're in an attribute, a tag, or just in a text node
    • If in XML, remember how many still-open XML tags we've come across in the current level.
  • How many levels deep in Scala we are
  • How many levels deep in XML literals we are

I don't see how we can code this information into 32 bits without a tiny (unusable) limit on how nested we can go. For this kind of parsing we really need a stack of states.

Ideas (and patches!) welcome of course, but for the next release (hopefully this week) I don't see XML literals happening in Scala, unfortunately.
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: Scala enhancements

Postby Lalit Pant » Sat Nov 17, 2012 6:25 pm

I can see how this is pretty complicated.

Can we do one level of Scala within XML (with no further XML levels within the nested Scala)? This would go a long way.
If you have (almost) working code for this, and are not inclined to try to include this within the next release, would you mind sharing this code - to help get me jump started trying to get some basic XML literal support in there.

Thanks,
- Lalit
Lalit Pant
 

Next

Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 4 guests