mirror of
https://github.com/gryf/pythonhelper.git
synced 2025-12-19 04:20:20 +01:00
Code style. Tabs AND spaces? WTF. http://i.imgur.com/Lo5Vv.png
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
" 2. Copy script pythonhelper.vim to the $HOME/.vim/plugin directory
|
" 2. Copy script pythonhelper.vim to the $HOME/.vim/plugin directory
|
||||||
" 3. add '%{TagInStatusLine()}' yo your vimrc/statusline
|
" 3. add '%{TagInStatusLine()}' yo your vimrc/statusline
|
||||||
" 4. Run Vim and open any python file.
|
" 4. Run Vim and open any python file.
|
||||||
"
|
"
|
||||||
python << EOS
|
python << EOS
|
||||||
|
|
||||||
# import of required modules {{{
|
# import of required modules {{{
|
||||||
@@ -53,7 +53,7 @@ class PythonTag(object):
|
|||||||
|
|
||||||
|
|
||||||
# STATIC VARIABLES {{{
|
# STATIC VARIABLES {{{
|
||||||
|
|
||||||
# possible tag types {{{
|
# possible tag types {{{
|
||||||
TT_CLASS = 0
|
TT_CLASS = 0
|
||||||
TT_METHOD = 1
|
TT_METHOD = 1
|
||||||
@@ -72,13 +72,13 @@ class PythonTag(object):
|
|||||||
|
|
||||||
|
|
||||||
# METHODS {{{
|
# METHODS {{{
|
||||||
|
|
||||||
def __init__(self, type, name, fullName, lineNumber, indentLevel):
|
def __init__(self, type, name, fullName, lineNumber, indentLevel):
|
||||||
# DOC {{{
|
# DOC {{{
|
||||||
"""Initializes instances of PythonTag().
|
"""Initializes instances of PythonTag().
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
||||||
type -- tag type
|
type -- tag type
|
||||||
|
|
||||||
name -- short tag name
|
name -- short tag name
|
||||||
@@ -119,9 +119,9 @@ class PythonTag(object):
|
|||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
# class SimplePythonTagsParser() {{{
|
|
||||||
|
# class SimplePythonTagsParser() {{{
|
||||||
class SimplePythonTagsParser(object):
|
class SimplePythonTagsParser(object):
|
||||||
# DOC {{{
|
# DOC {{{
|
||||||
"""Provides a simple python tag parser.
|
"""Provides a simple python tag parser.
|
||||||
@@ -144,7 +144,7 @@ class SimplePythonTagsParser(object):
|
|||||||
|
|
||||||
|
|
||||||
# METHODS {{{
|
# METHODS {{{
|
||||||
|
|
||||||
def __init__(self, source):
|
def __init__(self, source):
|
||||||
# DOC {{{
|
# DOC {{{
|
||||||
"""Initializes instances of SimplePythonTagsParser().
|
"""Initializes instances of SimplePythonTagsParser().
|
||||||
@@ -162,12 +162,12 @@ class SimplePythonTagsParser(object):
|
|||||||
(callable(source.readline) == 0)):
|
(callable(source.readline) == 0)):
|
||||||
raise AttributeError("Source must have callable readline method.")
|
raise AttributeError("Source must have callable readline method.")
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# remember what the source is
|
# remember what the source is
|
||||||
self.source = source
|
self.source = source
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
def getTags(self):
|
def getTags(self):
|
||||||
# DOC {{{
|
# DOC {{{
|
||||||
"""Determines all the tags for the buffer. Returns a tuple in format
|
"""Determines all the tags for the buffer. Returns a tuple in format
|
||||||
@@ -185,7 +185,7 @@ class SimplePythonTagsParser(object):
|
|||||||
tagsStack = []
|
tagsStack = []
|
||||||
lineNumber = 0
|
lineNumber = 0
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# go through all the lines in the source and localize all python tags in it {{{
|
# go through all the lines in the source and localize all python tags in it {{{
|
||||||
while 1:
|
while 1:
|
||||||
# get next line
|
# get next line
|
||||||
@@ -254,7 +254,7 @@ class SimplePythonTagsParser(object):
|
|||||||
else:
|
else:
|
||||||
parentTag = None
|
parentTag = None
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# return the tag
|
# return the tag
|
||||||
return parentTag
|
return parentTag
|
||||||
# }}}
|
# }}}
|
||||||
@@ -370,7 +370,7 @@ class SimplePythonTagsParser(object):
|
|||||||
tag) for functions/methods.
|
tag) for functions/methods.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
||||||
parentTagType -- type of the enclosing/parent tag
|
parentTagType -- type of the enclosing/parent tag
|
||||||
"""
|
"""
|
||||||
# }}}
|
# }}}
|
||||||
@@ -439,7 +439,7 @@ class VimReadlineBuffer(object):
|
|||||||
return "%s\n" % (self.vimBuffer[self.currentLine],)
|
return "%s\n" % (self.vimBuffer[self.currentLine],)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
@@ -451,9 +451,9 @@ def getNearestLineIndex(row, tagLineNumbers):
|
|||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
||||||
row -- current cursor row
|
row -- current cursor row
|
||||||
|
|
||||||
tagLineNumbers -- list of tags' line numbers (ie. their position)
|
tagLineNumbers -- list of tags' line numbers (ie. their position)
|
||||||
"""
|
"""
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
@@ -465,16 +465,16 @@ def getNearestLineIndex(row, tagLineNumbers):
|
|||||||
|
|
||||||
# go through all tag line numbers and find the one nearest to the specified row {{{
|
# go through all tag line numbers and find the one nearest to the specified row {{{
|
||||||
for lineIndex, lineNumber in enumerate(tagLineNumbers):
|
for lineIndex, lineNumber in enumerate(tagLineNumbers):
|
||||||
# if the current line is nearer the current cursor position, take it {{{
|
# if the current line is nearer the current cursor position, take it {{{
|
||||||
if (nearestLineNumber < lineNumber <= row):
|
if (nearestLineNumber < lineNumber <= row):
|
||||||
nearestLineNumber = lineNumber
|
nearestLineNumber = lineNumber
|
||||||
nearestLineIndex = lineIndex
|
nearestLineIndex = lineIndex
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# if we've got past the current cursor position, let's end the search {{{
|
# if we've got past the current cursor position, let's end the search {{{
|
||||||
if (lineNumber >= row):
|
if (lineNumber >= row):
|
||||||
break
|
break
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# return index of the line with the nearest tag
|
# return index of the line with the nearest tag
|
||||||
@@ -489,10 +489,10 @@ def getTags(bufferNumber, changedTick):
|
|||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
||||||
bufferNumber -- number of the current buffer
|
bufferNumber -- number of the current buffer
|
||||||
|
|
||||||
changedTick -- ever increasing number used to tell if the buffer has
|
changedTick -- ever increasing number used to tell if the buffer has
|
||||||
been modified since the last time
|
been modified since the last time
|
||||||
"""
|
"""
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
@@ -502,7 +502,7 @@ def getTags(bufferNumber, changedTick):
|
|||||||
|
|
||||||
# return immediately if there's no need to update the tags {{{
|
# return immediately if there's no need to update the tags {{{
|
||||||
if (BUFFERTICKS.get(bufferNumber, None) == changedTick):
|
if (BUFFERTICKS.get(bufferNumber, None) == changedTick):
|
||||||
return (TAGLINENUMBERS[bufferNumber], TAGS[bufferNumber],)
|
return (TAGLINENUMBERS[bufferNumber], TAGS[bufferNumber],)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# get the tags {{{
|
# get the tags {{{
|
||||||
@@ -527,32 +527,32 @@ def findTag(bufferNumber, changedTick):
|
|||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
||||||
bufferNumber -- number of the current buffer
|
bufferNumber -- number of the current buffer
|
||||||
|
|
||||||
changedTick -- ever increasing number used to tell if the buffer has
|
changedTick -- ever increasing number used to tell if the buffer has
|
||||||
been modified since the last time
|
been modified since the last time
|
||||||
"""
|
"""
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# CODE {{{
|
# CODE {{{
|
||||||
# try to find the best tag {{{
|
# try to find the best tag {{{
|
||||||
try:
|
try:
|
||||||
# get the tags data for the current buffer
|
# get the tags data for the current buffer
|
||||||
tagLineNumbers, tags = getTags(bufferNumber, changedTick)
|
tagLineNumbers, tags = getTags(bufferNumber, changedTick)
|
||||||
|
|
||||||
# link to vim's internal data {{{
|
# link to vim's internal data {{{
|
||||||
currentBuffer = vim.current.buffer
|
currentBuffer = vim.current.buffer
|
||||||
currentWindow = vim.current.window
|
currentWindow = vim.current.window
|
||||||
row, col = currentWindow.cursor
|
row, col = currentWindow.cursor
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# get the index of the nearest line
|
# get the index of the nearest line
|
||||||
nearestLineIndex = getNearestLineIndex(row, tagLineNumbers)
|
nearestLineIndex = getNearestLineIndex(row, tagLineNumbers)
|
||||||
|
|
||||||
# if any line was found, try to find if the tag is appropriate {{{
|
# if any line was found, try to find if the tag is appropriate {{{
|
||||||
# (ie. the cursor can be below the last tag but on a code that has nothing
|
# (ie. the cursor can be below the last tag but on a code that has nothing
|
||||||
# to do with the tag, because it's indented differently, in such case no
|
# to do with the tag, because it's indented differently, in such case no
|
||||||
# appropriate tag has been found.)
|
# appropriate tag has been found.)
|
||||||
while (nearestLineIndex > -1):
|
while (nearestLineIndex > -1):
|
||||||
# get the line number of the nearest tag
|
# get the line number of the nearest tag
|
||||||
nearestLineNumber = tagLineNumbers[nearestLineIndex]
|
nearestLineNumber = tagLineNumbers[nearestLineIndex]
|
||||||
@@ -601,41 +601,41 @@ def findTag(bufferNumber, changedTick):
|
|||||||
# the tag is appropriate, so use it {{{
|
# the tag is appropriate, so use it {{{
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
# no appropriate tag has been found {{{
|
# no appropriate tag has been found {{{
|
||||||
else:
|
else:
|
||||||
nearestLineNumber = -1
|
nearestLineNumber = -1
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# describe the cursor position (what tag the cursor is on) {{{
|
# describe the cursor position (what tag the cursor is on) {{{
|
||||||
# reset the description
|
# reset the description
|
||||||
tagDescription = ""
|
tagDescription = ""
|
||||||
|
|
||||||
# if an appropriate tag has been found, set the description accordingly {{{
|
# if an appropriate tag has been found, set the description accordingly {{{
|
||||||
if (nearestLineNumber > -1):
|
if (nearestLineNumber > -1):
|
||||||
tagInfo = tags[nearestLineNumber]
|
tagInfo = tags[nearestLineNumber]
|
||||||
tagDescription = "%s (%s)" % (tagInfo.fullName, PythonTag.TAG_TYPE_NAME[tagInfo.type],)
|
tagDescription = "%s (%s)" % (tagInfo.fullName, PythonTag.TAG_TYPE_NAME[tagInfo.type],)
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# update the variable for the status line so it get updated with the new description
|
# update the variable for the status line so it get updated with the new description
|
||||||
vim.command("let w:PHStatusLine=\"%s\"" % (tagDescription,))
|
vim.command("let w:PHStatusLine=\"%s\"" % (tagDescription,))
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# handle possible exceptions {{{
|
# handle possible exceptions {{{
|
||||||
except Exception:
|
except Exception:
|
||||||
# bury into the traceback {{{
|
# bury into the traceback {{{
|
||||||
ec, ei, tb = sys.exc_info()
|
ec, ei, tb = sys.exc_info()
|
||||||
while (tb != None):
|
while (tb != None):
|
||||||
if (tb.tb_next == None):
|
if (tb.tb_next == None):
|
||||||
break
|
break
|
||||||
tb = tb.tb_next
|
tb = tb.tb_next
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# spit out the error {{{
|
# spit out the error {{{
|
||||||
print "ERROR: %s %s %s:%u" % (ec.__name__, ei, tb.tb_frame.f_code.co_filename, tb.tb_lineno,)
|
print "ERROR: %s %s %s:%u" % (ec.__name__, ei, tb.tb_frame.f_code.co_filename, tb.tb_lineno,)
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
@@ -654,7 +654,7 @@ def deleteTags(bufferNumber):
|
|||||||
# CODE {{{
|
# CODE {{{
|
||||||
# define global variables
|
# define global variables
|
||||||
global TAGS, TAGLINENUMBERS, BUFFERTICKS
|
global TAGS, TAGLINENUMBERS, BUFFERTICKS
|
||||||
|
|
||||||
# try to delete the tags for the buffer {{{
|
# try to delete the tags for the buffer {{{
|
||||||
try:
|
try:
|
||||||
del TAGS[bufferNumber]
|
del TAGS[bufferNumber]
|
||||||
@@ -673,11 +673,11 @@ EOS
|
|||||||
function! PHCursorHold()
|
function! PHCursorHold()
|
||||||
" only python is supported {{{
|
" only python is supported {{{
|
||||||
if (!exists('b:current_syntax') || (b:current_syntax != 'python'))
|
if (!exists('b:current_syntax') || (b:current_syntax != 'python'))
|
||||||
let w:PHStatusLine = ''
|
let w:PHStatusLine = ''
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
" call python function findTag() with the current buffer number and changed ticks
|
" call python function findTag() with the current buffer number and changed ticks
|
||||||
execute 'python findTag(' . expand("<abuf>") . ', ' . b:changedtick . ')'
|
execute 'python findTag(' . expand("<abuf>") . ', ' . b:changedtick . ')'
|
||||||
endfunction
|
endfunction
|
||||||
@@ -686,7 +686,7 @@ endfunction
|
|||||||
function! PHBufferDelete()
|
function! PHBufferDelete()
|
||||||
" set PHStatusLine for this window to empty string
|
" set PHStatusLine for this window to empty string
|
||||||
let w:PHStatusLine = ""
|
let w:PHStatusLine = ""
|
||||||
|
|
||||||
" call python function deleteTags() with the cur
|
" call python function deleteTags() with the cur
|
||||||
execute 'python deleteTags(' . expand("<abuf>") . ')'
|
execute 'python deleteTags(' . expand("<abuf>") . ')'
|
||||||
endfunction
|
endfunction
|
||||||
|
|||||||
Reference in New Issue
Block a user