Skip to content

Commit

Permalink
many updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cazador481 committed Jul 7, 2017
1 parent 193bbd5 commit 5d1d118
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 426 deletions.
107 changes: 10 additions & 97 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -8,135 +8,48 @@ http://www.youtube.com/watch?v=hZ7871WcIv0
Description
===========

perl-completion plugin supports basic perl completion and Moose / DBIx::Class completion.
Deoplete source completion engine for perl includes Mo* / DBIx::Class completion.

the rules could be easily extended. see ( ftplugin/perl/perlomni.vim )
Based off of https://github.com/c9s/perlomni.vim

core rules including:

* variable name completion
* function name completion
* package method name completion
* base class method name completion
* basic Moose completion
* basic Moo completion
* basic DBIx completion

Supports deoplete completion


Requirement
===========

perlomni completion requires *vim 7.2 or newer.*
perlomni completion requires *neovim*

and filetype feature should be enabled.
https://github.com/Shougo/deoplete.nvim

Install
========

just run make:

$ make install

and add ~/.vim/bin/ to your $PATH env variable , for example, add these lines to your .bashrc or .zshrc:

export PATH=~/.vim/bin:$PATH

please make sure you've enable filetype plugin your `.vimrc`:

filetype on
filetype plugin on
filetype indent on

NOTE) If you install pathogen.vim , you can make this working without installing.

If you are you want completion as you type and are using neovim you can install https:://github.com/Shougo/deoplete.vim

Usage
=====

In insert mode , press C-x C-o to emit omni completion.

P.S. The completion works in terminal vim is faster than gvim/MacVim.

Write your perl omni completion extensions
=========================================

Perl omni completion plugin is extensible, you can extend completion rules by
simple regular expressions.

your omni completion extension should put in ~/.vim/after/ftplugin/perl/what-ever.vim.

## API Functions

**AddPerlOmniRule(rule)**, for example:

cal AddPerlOmniRule({ 'only':1, 'head': '^has\s\+\w\+' ,
\'context': '\s\+is\s*=>\s*$' ,
\'backward': '[''"]\?\w*$' ,
\'comp': function('s:CompMooseIs') } )

cal AddPerlOmniRule({
\'only':1,
\'context': '&$',
\'backward': '\<\U\w\+$',
\'comp': function('s:CompBufferFunction') })

Available Rule attributes

only:
if one rule is matched, then rest rules won't be check.

contains:
if file contains some string (can be regexp)

context:
completion context pattern

backward:
regexp for moving cursor back to the completion position.

head:
pattern that matches paragraph head.

comp:
completion function reference.


### Cache Functions

**GetCacheNS(ns,key)**

ns: cache namespace
key: cache key

**SetCacheNS(ns,key,value)**

ns: cache namespace
key: cache key
value: cache value

NeoBundle
NeoBundle 'cazador481/perlomni.vim', 'python'


Development
===========

Please feel free to ask commit bit of this. just drop me a line. :-)


Author
======
- cazador481 (Edward Ash) [email protected]

Original Author of perlomni
=====
- Cornelius (林佑安) [email protected]
- Mattn (Yasuhiro Matsumoto) [email protected]


Contibutors
==========
- cazador481 (Edward Ash) [email protected]



Samples
=======

Expand Down
36 changes: 17 additions & 19 deletions rplugin/python3/deoplete/sources/perl_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import time
#from abc import abstractmethod
from .base import Base
from deoplete import logger
import deoplete.util


class Perl_base(object):
class Perl_base(Base):

def __init__(self, vim):
Base.__init__(self, vim)
Expand All @@ -41,18 +42,17 @@ def __init__(self, vim):
self._cache = {} # g:perlomni_cache
self._cpan_mod_cache = [] # lists of classnames for completion
self._comps = []
# self.is_async = 1
self._perlomni_cache_expiry = 30 # default value
deoplete.util.set_default(self.vim, 'g:perlomni_use_cache', 1)
debug_enabled = 0
self.debug_enabled = 1

def get_complete_position(self, context):
# todo remove
loc = self.PerlComplete(1)
self.debug("Completion_pos:"+str(loc))
return loc

def debug(self, expr):
return
#deoplete.util.debug(self.vim, expr)

def PerlComplete(self, findstart): # , base):
buffer = self.vim.current.buffer
Expand Down Expand Up @@ -82,7 +82,6 @@ def PerlComplete(self, findstart): # , base):
# " let b:pcontext
# TODO ddefine parseParagraphhead
paragraph_head = self.parseParagraphHead(lnum)
self.debug("i am here")

first_bwidx = -1

Expand All @@ -92,7 +91,6 @@ def PerlComplete(self, findstart): # , base):
match = re.search(rule['backward'], lcontext)
if match:
bwidx = match.start()
self.debug('backwards match for bellow')
else:
bwidx = -1
# if backward regexp matched is empty, check if context regexp
Expand All @@ -116,21 +114,21 @@ def PerlComplete(self, findstart): # , base):
lefttext = lcontext[0:bwidx]
basetext = lcontext[bwidx:]

self.debug('--------------------')
self.debug('function:' + str(rule['comp']))
self.debug('head:' + paragraph_head)
self.debug('lefttext:' + lefttext)
self.debug('context:' + rule['context'])
self.debug('basetext:' + basetext)

# if ( has_key( rule ,'head')
# if ( has_key( rule ,'head')
# \ && b:paragraph_head =~ rule.head
# \ && lefttext =~ rule.context )
# \ || ( ! has_key(rule,'head') && lefttext =~ rule.context)
if re.search(rule['context'], lefttext):
self.debug('--------------------')
self.debug('context match')
else:
self.debug('context does not match')
self.debug('function:' + str(rule['comp']))
self.debug('head:' + paragraph_head)
self.debug('lefttext:' + lefttext)
self.debug('context:' + rule['context'])
self.debug('basetext:' + basetext)
self.debug('--------------------')
# else :
# self.debug('context does not match')
if (('head' in rule and re.search(rule['head'], paragraph_head) and re.search(rule[
'context'], lefttext))
or (not ('head' in rule)
Expand Down Expand Up @@ -161,8 +159,8 @@ def PerlComplete(self, findstart): # , base):
if first_bwidx == -1:
first_bwidx = bwidx

else:
self.debug('does not match')
# else:
# self.debug('does not match')
return first_bwidx
else:
return self._comps
Expand Down
53 changes: 41 additions & 12 deletions rplugin/python3/deoplete/sources/perlomni2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class Source(Perl_base):
def __init__(self, vim):
Perl_base.__init__(self, vim)
self.name = 'PerlOmni_py2'
self.mark = '[Py2]'
self.mark = '[Pl]'
self.min_pattern_length = 0
self.debug_enabled = 1
self.rank = 99999
self._rules = [
# {'only': 1, #TODO does not work, will not complete due to filter
# 'context': '^\s*my\s+\$self',
Expand Down Expand Up @@ -189,10 +191,10 @@ def __init__(self, vim):

def gather_candidates(self, context):
values = self.PerlComplete(0)

# self.debug(type(values).__name__)
# self.debug("size" + str(len(values)))
# self.debug(type(values[0]).__name__)
self.debug('Candidates:'+str(values))
if isinstance(
values,
list) and len(values) > 0 and isinstance(
Expand All @@ -205,8 +207,13 @@ def gather_candidates(self, context):
ret.append({'word': x})
else:
ret.append(x)
self.debug(ret)
return ret

self.debug('Candidates:'+str(ret))
return ret
if values is None:
self.debug('Candidates: None')
else:
self.debug('Candidates:'+str(values))
return values

def GetCacheNS(self, ns, cache_key):
Expand Down Expand Up @@ -237,13 +244,17 @@ def GetCacheNS(self, ns, cache_key):
return

def SetCacheNSWithExpiry(self, ns, cache_key, value, exp):
if value is None:
return value
key = ns + '_' + cache_key
self._cache[key] = value
self._cache_expiry[key] = exp
self._cache_last[key] = time.clock()
return value

def SetCacheNS(self, ns, cache_key, value):
if value is None:
return value
key = ns + "_" + cache_key
self._cache[key] = value
return value
Expand Down Expand Up @@ -373,11 +384,10 @@ def CompClassName(self, base, context):
#"returns the line number of the first line in a group of lines
def parseParagraphHead(self, fromLine):
lnum = fromLine
self.debug(str(lnum))
paragraph_head = ''
lmin=lnum-10;
lmin = lnum-10
if lmin < 1:
lmin=1
lmin = 1
for nr in range(lnum-1, lmin, -1):
line = self.vim.current.buffer[nr]
if re.match('^\s*$', line) or re.match('^\s*#', line):
Expand All @@ -390,8 +400,12 @@ def CompMooseIsa(self, base, context):
# TODO
# comps += self.CompClassName(base, context)
ret = []
for i in comps:
ret.append("'"+i+"'")
if not self.IsMoo:
for i in comps:
ret.append("'"+i+"'")
else:
ret = comps
self.debug('isa completion'+str(ret))
return ret

def CompMooseAttribute(self, base, context):
Expand Down Expand Up @@ -448,12 +462,14 @@ def findCurrentClassBaseClass(self):
return all_mods

def scanFunctionFromClass(self, mClass):
if mClass is None:
return []
classfile = self.locateClassFile(mClass)
self.debug('classfile: '+classfile)
if classfile is None:
return []
self.debug('classfile: '+classfile)
# return self.scanFunctionFromSingleClassFile(
# classfile) + self.scanFunctionFromBaseClassFile(classfile)
# classfile) + self.scanFunctionFromBaseClassFile(classfile)
return self.scanFunctionFromBaseClassFile(classfile)

def locateClassFile(self, mClass):
Expand Down Expand Up @@ -577,6 +593,8 @@ def CompArrayVariable(self, base, context):
if cache is not None:
return cache
result = self.scanArrayVariable()
if result is None:
return
return self.SetCacheNS('arrayvar', 'arrayvar', result)

def CompBufferFunction(self, base, context):
Expand All @@ -591,7 +609,7 @@ def CompBufferFunction(self, base, context):
'buf_func_all',
self.vim.current.buffer.name,
self.scanFunctionFromList()+self.find_predicate())
# self.scanFunctionFromList())
# self.scanFunctionFromList())
return funclist

# result = filter( copy(funclist),"stridx(v:val,'".a:base."') == 0 && v:val != '".a:base."'" )
Expand Down Expand Up @@ -702,3 +720,14 @@ def CompClassFunction(self, base, context):
# if g:perlomni_show_hidden_func == 0
# call filter(funclist, 'v:val !~ "^_"')
return funclist


# TODO catch
def IsMoo(self, base, context):
for line in self.vim.current.buffer[1:]:
if re.match('^\s*use Moo;', line) is not None:
return True
if re.match('\^s*use Moose;',line) is not None:
return False
# default is true
return True
Loading

0 comments on commit 5d1d118

Please sign in to comment.