Overbrace in lstlisting of Scala code

I want to add an overbrace to some text in a Scala code listing (using lstlisting).

\begin{lstlisting}
object TypeClass extends FooParMain {
def newApp(args: Array[String]) = new FooParApp {
def run {
val x = new Matrix(Seq(Seq(1, 2), Seq(3, 4)))
val dSeq = Array.fill(worldSize)(x).toDistSeq

for (res <- dSeq.sumD) {
pprintln(res, " worldSize = " + worldSize, res == x * worldSize)
}
}
}
}
\end{lstlisting}


Is there any sane way of doing this without breaking the syntax highlighting? (I tried [mathescape], but it doesn't look good)

-
welcome to tex.sx. where the brace? over the whole thing or over some words in the listing? –  David Carlisle Apr 10 '13 at 11:09
In this case, overbrace on exactly "Array.fill(worldSize)(x)" and another brace over ".toDistSeq". Btw, thank you :) –  Felix Apr 10 '13 at 11:31

This uses a bit of "by eye" measuring hard to measure it automatically without knowing exactly what listings will do with the text.

(Please for future questions post complete documents showing packages used as below)

\documentclass{article}
\usepackage{listings}

\begin{document}

\small
\begin{lstlisting}[mathescape]
object TypeClass extends FooParMain {
def newApp(args: Array[String]) = new FooParApp {
def run {
val x = new Matrix(Seq(Seq(1, 2), Seq(3, 4)))
val dSeq = $\rlap{$$\overbrace{\strut\hspace{4.5cm}}$$}$Array.fill(worldSize)(x).toDistSeq

for (res <- dSeq.sumD) {
pprintln(res, " worldSize = " + worldSize, res == x * worldSize)
}
}
}
}
\end{lstlisting}

\end{document}

-
This solution messes up the layout (background and frame splits up at the line) –  Felix Apr 11 '13 at 10:49
}
\makeatother

\lstdefinestyle{Java}{
language={Java},basicstyle=\ttfamily,
moredelim=**[is][{\btHL[class name]}]{}{},
moredelim=**[is][{\btHL[important]}]{@}{@},
escapechar={§},
}

\begin{document}

A listing with {\btHL[overbrace text] highlighting of all \textbf{important} elements} looks as follows:

\begin{lstlisting}[style=Java]
public class HelloWorld {
public @static void@ main(String[] args) {
System.out.println("Hello, World");
}
}
\end{lstlisting}
\end{document}


-
Sorry for adding this so late, but I would like to be able to put text at the overbrace. Is this possible with this approach? –  Felix Apr 10 '13 at 20:22
@Felix: Please see updated code and screen shot. The issue is how to pass the overbrace text. In the updated solution, it can be given as optional parameter to the \btHL[<overbrace text>] command. Basically this means that you have to define an own moredelim for every overbrace text. Does this work for you? –  Daniel Apr 10 '13 at 21:20