From 8468559e7ffe234464a91e1470944694f7f4410e Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Sat, 11 Sep 2010 10:29:56 -0400 Subject: [PATCH] started adding documentation facilities to scala mode --HG-- branch : pmacs2 --- mode/scala.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/mode/scala.py b/mode/scala.py index 3330029..a37ab18 100644 --- a/mode/scala.py +++ b/mode/scala.py @@ -4,6 +4,9 @@ from lex import Grammar, PatternRule, RegionRule, PatternMatchRule from mode.xml import XMLGrammar from mode.pipe import Pipe from method.shell import Interact +from method import Method, arg +import default +import urllib2 chr1 = '[a-zA-Z_]' chr2 = '[a-zA-Z_0-9]' @@ -28,6 +31,7 @@ class ScalaGrammar(Grammar): PatternRule('delimiter', r'(?:;|{|}|\[|\]|\(|\)|,|\.|<(?![a-zA-Z_])|>|:|/|\+|-|\*|=)'), + RegionRule('scala.inline', r'(?:^| )(?=<[a-zA-Z_])', XMLGrammar, '^[ \t]*$'), PatternRule('spaces', r'(?:\t| )+'), PatternRule('eol', r'\n'), @@ -52,8 +56,6 @@ class ScalaGrammar(Grammar): PatternRule('scala.annotation', '@[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule('scala.bareword', '[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule('scala.null', 'null'), - - RegionRule('scala.inline', r'(?:^| )(?=<[a-zA-Z_])', XMLGrammar, '^[ \t]*$'), ] class ScalaTabber(StackTabber2): @@ -89,6 +91,34 @@ class ScalaStart(Interact): def _execute(self, w, **vargs): Interact._execute(self, w, bname='*Scala*', cmd='scala') +class ScalaDocBrowse(Method): + def _execute(self, w, **vargs): + a = w.application + url = a.config['scala.api'] + Interact().execute(w, bname='*Scala-Doc*', cmd='links "%s"' % url) + +class ScalaDocLookup(Method): + args = [arg('name', t='string', p='Name: ', dv=default.current_word, + ld=True, h='The Scala name to get help on')] + def _execute(self, w, **vargs): + try: + import BeautifulSoup + except ImportError: + w.set_error('BeautifulSoup is not installed...') + return + + a = w.application + name = vargs.get('name') + path = a.getpath('cache', 'scala', 'api.html') + url = a.config['scala.api'] + + a.mkdirs('cache', 'scala') + + if not os.path.exists(path): + open(path, 'w').write(urllib2.urlopen(url).read()) + + w.set_error('looking up %s...' % name) + class Scala(Fundamental): name = 'Scala' extensions = ['.scala'] @@ -96,11 +126,14 @@ class Scala(Fundamental): tabbercls = ScalaTabber grammar = ScalaGrammar commentc = '//' - actions = [ScalaStart] + actions = [ScalaStart, ScalaDocBrowse, ScalaDocLookup] opentokens = ('delimiter',) opentags = {'(': ')', '[': ']', '{': '}'} closetokens = ('delimiter',) closetags = {')': '(', ']': '[', '}': '{'} + config = { + 'scala.api': 'http://www.scala-lang.org/api/current/allclasses.html', + } colors = { 'scala.annotation': ('magenta', 'default'),