added some stuff for working with sxr output in scala

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2010-10-26 19:40:34 -04:00
parent 3a32038d5f
commit 984215aaec
2 changed files with 72 additions and 1 deletions

View File

@ -9,6 +9,7 @@ from method import Method, arg
import default import default
import urllib2 import urllib2
import os import os
import re
chr1 = '[a-zA-Z_]' chr1 = '[a-zA-Z_]'
chr2 = '[a-zA-Z_0-9]' chr2 = '[a-zA-Z_0-9]'
@ -167,6 +168,65 @@ class ScalaDocLookup(Method):
w.set_error('error looking up %s...' % name) w.set_error('error looking up %s...' % name)
class ScalaXRayBase(Method):
_is_method = False
pkg_re = re.compile('^package (.+)$')
def find_pkg(self, w):
for line in w.buffer.lines:
m = self.pkg_re.match(line)
if m:
return m.group(1)
raise Exception("no package found")
def get_sxr_dir(self, w):
# TODO: fixme fixme
path = 'target/scala_2.8.0/classes.sxr'
if os.path.exists(path):
return path
else:
raise Exception("classes.sxr not found")
def get_public_tags(self, w, base):
return os.path.join(base, 'public-tags')
def type_lookup(self, n, w):
pkg = self.find_pkg(w)
base = self.get_sxr_dir(w)
tags = os.path.join(base, pkg.replace('.', '/') + '.scala.txt')
f = open(tags, 'r')
for line in f:
toks = line.split('\t')
i = int(toks[0])
j = int(toks[1])
if i <= n and n <= j:
return toks[2]
return None
class ScalaGetType(ScalaXRayBase):
type_re = re.compile('(?:[^ \[\]{}()\.,;:]+\.)*([^ \[\]{}()\.,;:]+)')
ret_re = re.compile('\)(?=[a-zA-Z0-9_])')
def _execute(self, w, **vargs):
word = w.get_token().string
if word is None or word.strip() == "":
w.set_error('no word selected')
return
n = w.cursor_byte_offset()
t = self.type_lookup(n, w)
if t:
if w.application.config['scala.type-abbrev']:
t = self.type_re.sub(lambda m: m.group(1), t)
#t = t.replace(')', ') => ')
t = self.ret_re.sub(') => ', t)
t = t.replace(': ', ':')
t = t.replace(',', ', ')
w.set_error(t)
else:
w.set_error('%s has unknown type' % word)
class ScalaGotoDefinition(ScalaXRayBase):
def _execute(self, w, **vargs):
w.set_error('borken')
# white is for delimiters, operators, numbers # white is for delimiters, operators, numbers
default = ('default', 'default') default = ('default', 'default')
@ -205,7 +265,7 @@ class Scala(Fundamental):
tabbercls = ScalaTabber tabbercls = ScalaTabber
grammar = ScalaGrammar grammar = ScalaGrammar
commentc = '//' commentc = '//'
actions = [ScalaStart, ScalaDocBrowse, ScalaDocLookup] actions = [ScalaStart, ScalaDocBrowse, ScalaDocLookup, ScalaGetType]
opentokens = ('delimiter', 'sub.start', 'sub.sub.start', 'sub.sub.sub.start') opentokens = ('delimiter', 'sub.start', 'sub.sub.start', 'sub.sub.sub.start')
opentags = {'(': ')', '[': ']', '{': '}'} opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter', 'sub.end', 'sub.sub.end', 'sub.sub.sub.end') closetokens = ('delimiter', 'sub.end', 'sub.sub.end', 'sub.sub.sub.end')
@ -213,6 +273,7 @@ class Scala(Fundamental):
config = { config = {
'scala.api': 'http://www.scala-lang.org/api/current/allclasses.html', 'scala.api': 'http://www.scala-lang.org/api/current/allclasses.html',
'scala.api-base': 'http://www.scala-lang.org/api/current', 'scala.api-base': 'http://www.scala-lang.org/api/current',
'scala.type-abbrev': True,
} }
colors = { colors = {
@ -241,6 +302,7 @@ class Scala(Fundamental):
} }
_bindings = { _bindings = {
'scala-get-type': ('M-,',),
'close-paren': (')',), 'close-paren': (')',),
'close-brace': ('}',), 'close-brace': ('}',),
'close-bracket': (']',), 'close-bracket': (']',),

View File

@ -477,6 +477,15 @@ class Window(object):
self.first = Point(x - (x % self.width), y) self.first = Point(x - (x % self.width), y)
self.redraw() self.redraw()
# TODO: multi-byte characters!!!
def point_byte_offset(self, p):
n = 0
for i in xrange(0, p.y):
n += len(self.buffer.lines[i]) + 1
return n + p.x
def cursor_byte_offset(self):
return self.point_byte_offset(self.logical_cursor())
def last_visible_coords(self): def last_visible_coords(self):
(x, y) = self.buffer.get_buffer_end().xy() (x, y) = self.buffer.get_buffer_end().xy()
counter = 0 counter = 0