Skip to content

Commit

Permalink
Add a Command-T based class opener
Browse files Browse the repository at this point in the history
This displays the list of classes from g:as_locations in Command-T when
<Leader>o is pressed.  Selecting one opens it via the same mechanism as
jump.

To use this, you'll need the master branch of Command-T at
https://github.com/groves/Command-T for the time being.
  • Loading branch information
groves committed Oct 14, 2011
1 parent 7e40298 commit 1098056
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
5 changes: 5 additions & 0 deletions vim/ftplugin/actionscript/aspirin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ if !exists("b:did_aspirinplugin")
endif
noremap <buffer> <silent> <unique> <Plug>AspirinJump :call AspirinJump()<CR>
if !hasmapto('<Plug>AspirinOpenClass')
map <buffer> <unique> <LocalLeader>o <Plug>AspirinOpenClass
endif
noremap <buffer> <silent> <unique> <Plug>AspirinOpenClass :call AspirinOpenClass()<CR>
if !hasmapto('<Plug>AspirinImport')
map <buffer> <unique> <LocalLeader>i <Plug>AspirinImport
endif
Expand Down
34 changes: 24 additions & 10 deletions vim/plugin/aspirin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Majority of the functionality of the aspirin plugin. It's out in its own module to namespace it
# from Vim's shared Python interpreter
import os, re, sys, subprocess, vim, zipfile
import itertools, os, re, sys, subprocess, vim, zipfile
from xml.etree import ElementTree

def jump(classname):
found = choose_lookup(classname)
if found:
if found[1].endswith('.html'):
subprocess.check_call([vim.eval("g:aspirin_open"), found[1]])
else:
vim.command("edit " + found[1])
openclass(found)

def openclass(fullclassname):
path = full_to_path[fullclassname]
if path.endswith('.html'):
subprocess.check_call([vim.eval("g:aspirin_open"), path])
else:
vim.command("edit " + path)

class ClassImportContext(object):
def __init__(self, helperclasses, startline=1):
Expand Down Expand Up @@ -37,7 +41,7 @@ def addMissingImports(self):
inpackage = False
if not self.package is None:
for full in fulls:
if full[0] == self.package + "." + unknown:
if full == self.package + "." + unknown:
inpackage = True
break
if not inpackage and addimport(unknown, self.startline, scan_if_not_found=False):
Expand Down Expand Up @@ -116,15 +120,17 @@ def addimport(classname, packageline=None, scan_if_not_found=True):
else:
vim.command("let packageline = %s" % packageline)
vim.command('let s:startpos = getpos(".")')
vim.command('let ignored=append(packageline + 1, "import %s;")' % found[0])
vim.command('let ignored=append(packageline + 1, "import %s;")' % found)
vim.command("let ignored=cursor(s:startpos[1] + 1, s:startpos[2])")
return found

def valexists(val):
return bool(int(vim.eval('exists("%s")' % val)))

last_lookup_paths = []
classname_to_full = {}
classname_to_full = {} # Simple classname to set<full classname>
full_to_path = {}
fullclassnames = []
def lookup(classname, scan_if_not_found=True):
global last_lookup_paths
if not valexists("g:as_locations"):
Expand All @@ -150,14 +156,19 @@ def choose_lookup(classname, scan=True):
else:
print "Multiple classes found for", classname
for idx, full in enumerate(fulls):
print idx + 1, full[0]
print idx + 1, full
vim.command('let idx=input("Class number or blank to abort: ")')
idx = vim.eval("idx").strip()
if not idx:
return None
return fulls[int(idx) - 1]

def listclasses():
lookup("NonsenseClass", False)
return fullclassnames

def scan(locs):
full_to_path.clear()
classname_to_full.clear()
for path in locs:
path = os.path.expanduser(path)
Expand All @@ -171,13 +182,16 @@ def scan(locs):
addclasses(scan_dir(path))
else:
print "Don't know how to handle", path
global fullclassnames
fullclassnames = sorted(list(itertools.chain.from_iterable(classname_to_full.itervalues())))

def addclasses(classes):
for fullname, path in classes:
simplename = fullname.split('.')[-1]
if simplename not in classname_to_full:
classname_to_full[simplename] = set()
classname_to_full[simplename].add((fullname, path))
classname_to_full[simplename].add(fullname)
full_to_path[fullname] = path

pathtofull = lambda path: '.'.join(path.split('/'))

Expand Down
34 changes: 34 additions & 0 deletions vim/plugin/aspirin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,37 @@ endfunction
function! AspirinLastEx()
python aspirin.send_ex_to_quickfix()
endfunction

function! AspirinOpenClass()
call s:AspirinLoadCommandT()
python vim.command('let paths=%s' % aspirin.listclasses())
ruby $command_t.show_finder AsClassFinder.new(AsListScanner.new VIM::evaluate("paths"))
endfunction

function! s:AspirinLoadCommandT()
if exists("g:loaded_aspirin_command_t")
return
endif
let g:loaded_scim_command_t = 1
ruby << EOF
require 'command-t/scanner'
require 'command-t/finder/basic_finder'

class AsClassFinder < CommandT::BasicFinder
def open selection, options
::VIM::command("python aspirin.openclass('#{selection}')")
end

def bufferBased?
false
end
end

class AsListScanner < CommandT::Scanner
attr_accessor :paths
def initialize paths
@paths = paths
end
end
EOF
endfunction

0 comments on commit 1098056

Please sign in to comment.