alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (KeyBinding.scala)

This example Scala source code file (KeyBinding.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

int, keybinding, keybinding, list, list, nil, nil, string, string

The Scala KeyBinding.scala source code

/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools.nsc
package interpreter

case class KeyBinding(name: String, code: Int, aliases: List[String], description: String) {
  def nameString = if (aliases.nonEmpty) aliases mkString ", " else name
  override def toString = "%3d %s: %s".format(code, nameString, description)
}

object KeyBinding {
  def parse(bindings: String): List[KeyBinding] = {
    def loop(xs: List[String]): List[KeyBinding] = {
      val (comment, lines) = xs span (_ startsWith "#")
      val description = comment map (_ drop 1 trim) mkString " "
      val (aliases, desc) = description span (_ != ':') match {
        case (x, y) => (
          x split ',' map (_.trim) toList, 
          if (y == "") "" else y.tail.trim
        )
      }
      lines match {
        case Nil      => Nil
        case hd :: tl =>
          val kb = (hd indexOf '=') match {
            case -1   => KeyBinding(hd, -1, aliases, desc)
            case idx  => KeyBinding(hd drop idx + 1, hd take idx toInt, aliases, desc)
          }
          kb :: loop(tl)
      }
    }
    // This is verrrrrrrry specific to the current contents
    // of the keybindings.properties in jline.
    loop(bindings split "\\n" map (_.trim) dropWhile (_ != "") filterNot (_ == "") toList) sortBy (_.code)
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala KeyBinding.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.