1
0
mirror of https://github.com/gryf/pythonhelper.git synced 2025-12-19 04:20:20 +01:00

Corrected documentation.

This commit is contained in:
cheater
2012-02-08 07:23:25 +01:00
committed by cheater
parent 005a35dcf6
commit e70cca0a11

View File

@@ -126,7 +126,7 @@ 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.
""" """
# }}} # }}}
@@ -172,13 +172,13 @@ class SimplePythonTagsParser(object):
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 the format
(tagLineNumbers, tags,). (tagLineNumbers, tags,).
""" """
# }}} # }}}
# CODE {{{ # CODE {{{
# initialize the resulting list of the tag line numbers and the tag information {{{ # initialize the resulting list of tag line numbers and the tag information {{{
tagLineNumbers = [] tagLineNumbers = []
tags = {} tags = {}
# }}} # }}}
@@ -188,20 +188,20 @@ class SimplePythonTagsParser(object):
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
line = self.source.readline() line = self.source.readline()
# finish if this is the end of the source {{{ # finish when we reach the end of the source {{{
if (line == ''): if (line == ''):
break break
# }}} # }}}
# increase the line number # increase line number
lineNumber += 1 lineNumber += 1
# extract the line indentation characters and its content {{{ # extract the line's indentation characters and its content {{{
lineMatch = self.COMMENTS_INDENT_RE.match(line) lineMatch = self.COMMENTS_INDENT_RE.match(line)
lineContent = lineMatch.group(2) lineContent = lineMatch.group(2)
# }}} # }}}
@@ -233,14 +233,14 @@ class SimplePythonTagsParser(object):
# }}} # }}}
# }}} # }}}
# return the tags data for the source # return tag data for the source
return (tagLineNumbers, tags,) return (tagLineNumbers, tags,)
# }}} # }}}
def getParentTag(self, tagsStack): def getParentTag(self, tagsStack):
# DOC {{{ # DOC {{{
"""Returns the parent/enclosing tag (instance of PythonTag()) from the """Given a tag, returns its parent tag (instance of PythonTag()) from the
specified tag list. If no such parent tag exists, returns None. specified tag list. If no such parent tag exists, returns None.
Parameters Parameters
@@ -306,18 +306,18 @@ class SimplePythonTagsParser(object):
tagName -- short name of the current tag tagName -- short name of the current tag
tagTypeDecidingMethod -- reference to method that is called to tagTypeDecidingMethod -- reference to the method that is called to
determine the type of the current tag determine the type of the current tag
""" """
# }}} # }}}
# CODE {{{ # CODE {{{
# compute the indentation level # compute indentation level
indentLevel = self.computeIndentationLevel(indentChars) indentLevel = self.computeIndentationLevel(indentChars)
# get the parent tag # get parent tag
parentTag = self.getParentTag(tagsStack) parentTag = self.getParentTag(tagsStack)
# handle an enclosed tag {{{ # handle enclosed tag {{{
while (parentTag): while (parentTag):
# if the indent level of the parent tag is greater than of the current tag, use parent tag of the parent tag {{{ # if the indent level of the parent tag is greater than of the current tag, use parent tag of the parent tag {{{
if (parentTag.indentLevel >= indentLevel): if (parentTag.indentLevel >= indentLevel):
@@ -332,7 +332,7 @@ class SimplePythonTagsParser(object):
break break
# }}} # }}}
# use parent tag of the parent tag # use the parent tag of the parent tag
parentTag = self.getParentTag(tagsStack) parentTag = self.getParentTag(tagsStack)
# }}} # }}}
# handle a top-indent level tag {{{ # handle a top-indent level tag {{{
@@ -392,7 +392,7 @@ class SimplePythonTagsParser(object):
# class VimReadlineBuffer() {{{ # class VimReadlineBuffer() {{{
class VimReadlineBuffer(object): class VimReadlineBuffer(object):
# DOC {{{ # DOC {{{
"""A simple wrapper class around vim's buffer that provides readline """A simple wrapper class around a Vim buffer that provides the readline
method. method.
""" """
# }}} # }}}
@@ -406,7 +406,7 @@ class VimReadlineBuffer(object):
Parameters Parameters
vimBuffer -- VIM's buffer vimBuffer -- a Vim buffer
""" """
# }}} # }}}
@@ -423,8 +423,8 @@ class VimReadlineBuffer(object):
def readline(self): def readline(self):
# DOC {{{ # DOC {{{
"""Returns next line from the buffer. If all the buffer has been read, """Returns the next line from the buffer. If the whole buffer has been
returns empty string. read, returns an empty string.
""" """
# }}} # }}}
@@ -432,12 +432,12 @@ class VimReadlineBuffer(object):
# increase the current line counter # increase the current line counter
self.currentLine += 1 self.currentLine += 1
# notify end of file if we reached beyond the last line {{{ # tell if we reached beyond the last line {{{
if (self.currentLine == self.bufferLines): if (self.currentLine == self.bufferLines):
return '' return ''
# }}} # }}}
# return the line with an added newline (vim stores the lines without it) # Vim stores the lines without the newlines, so add one
return "%s\n" % (self.vimBuffer[self.currentLine],) return "%s\n" % (self.vimBuffer[self.currentLine],)
# }}} # }}}
@@ -448,8 +448,8 @@ class VimReadlineBuffer(object):
def getNearestLineIndex(row, tagLineNumbers): def getNearestLineIndex(row, tagLineNumbers):
# DOC {{{ # DOC {{{
"""Returns the index of line in 'tagLineNumbers' list that is nearest to the """Returns the index of 'tagLineNumbers' that contains the line nearest to
specified cursor row. the specified cursor row.
Parameters Parameters
@@ -473,28 +473,28 @@ def getNearestLineIndex(row, tagLineNumbers):
nearestLineIndex = lineIndex nearestLineIndex = lineIndex
# }}} # }}}
# if we've got past the current cursor position, let's end the search {{{ # if we've come past the current cursor position, end the search {{{
if (lineNumber >= row): if (lineNumber >= row):
break break
# }}} # }}}
# }}} # }}}
# return index of the line with the nearest tag # return the index of the line with the nearest tag
return nearestLineIndex return nearestLineIndex
# }}} # }}}
def getTags(bufferNumber, changedTick): def getTags(bufferNumber, changedTick):
# DOC {{{ # DOC {{{
"""Reads the tags for the specified buffer number. Returns a tuple """Reads the tags for the buffer specified by the number. Returns a tuple
(taglinenumber[buffer], tags[buffer],). of the format (taglinenumber[buffer], tags[buffer],).
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 -- always-increasing number used to indicate that the buffer
been modified since the last time has been modified since the last time
""" """
# }}} # }}}
@@ -531,18 +531,18 @@ def findTag(bufferNumber, changedTick):
bufferNumber -- number of the current buffer bufferNumber -- number of the current buffer
changedTick -- ever increasing number used to tell if the buffer has changedTick -- always-increasing number used to indicate that the buffer
been modified since the last time has 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 tag 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
@@ -551,15 +551,16 @@ def findTag(bufferNumber, changedTick):
# 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 a line has been found, find out if the tag is correct {{{
# (ie. the cursor can be below the last tag but on a code that has nothing # E.g. the cursor might be below the last tag, but in code that has
# to do with the tag, because it's indented differently, in such case no # nothing to do with the tag, which we know because the line is
# appropriate tag has been found.) # indented differently. In such a case no applicable 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]
# walk through all the lines in range (nearestTagLine, cursorRow) {{{ # walk through all the lines in the range (nearestTagLine, cursorRow) {{{
for lineNumber in xrange(nearestLineNumber + 1, row): for lineNumber in xrange(nearestLineNumber + 1, row):
# get the current line # get the current line
line = currentBuffer[lineNumber] line = currentBuffer[lineNumber]
@@ -588,24 +589,24 @@ def findTag(bufferNumber, changedTick):
if (i == len(line)): if (i == len(line)):
continue continue
# }}} # }}}
# if the next character is a '#' (python comment), skip the line {{{ # if the next character is a '#' (python comment), skip to the next line {{{
if (line[i] == '#'): if (line[i] == '#'):
continue continue
# }}} # }}}
# if the line's indentation starts before or at the nearest tag's one, the tag is invalid {{{ # if the line's indentation starts before or at the nearest tag's, the tag is invalid {{{
if (lineStart <= tags[nearestLineNumber].indentLevel): if (lineStart <= tags[nearestLineNumber].indentLevel):
nearestLineIndex -= 1 nearestLineIndex -= 1
break break
# }}} # }}}
# }}} # }}}
# }}} # }}}
# the tag is appropriate, so use it {{{ # the tag is correct, so use it {{{
else: else:
break break
# }}} # }}}
# }}} # }}}
# no appropriate tag has been found {{{ # no applicable tag has been found {{{
else: else:
nearestLineNumber = -1 nearestLineNumber = -1
# }}} # }}}
@@ -614,7 +615,7 @@ def findTag(bufferNumber, changedTick):
# reset the description # reset the description
tagDescription = "" tagDescription = ""
# if an appropriate tag has been found, set the description accordingly {{{ # if an applicable 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],)
@@ -645,7 +646,7 @@ def findTag(bufferNumber, changedTick):
def deleteTags(bufferNumber): def deleteTags(bufferNumber):
# DOC {{{ # DOC {{{
"""Removes tags data for the specified buffer number. """Removes tag data for the specified buffer number.
Parameters Parameters
@@ -673,23 +674,23 @@ EOS
" VIM functions {{{ " VIM functions {{{
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 change status indicator
execute 'python findTag(' . expand("<abuf>") . ', ' . b:changedtick . ')' execute 'python findTag(' . expand("<abuf>") . ', ' . b:changedtick . ')'
endfunction endfunction
function! PHBufferDelete() function! PHBufferDelete()
" set PHStatusLine for this window to empty string " set the PHStatusLine for this window to an empty string
let w:PHStatusLine = "" let w:PHStatusLine = ""
" call python function deleteTags() with the cur " call Python function findTag() with the current buffer number and change status indicator
execute 'python deleteTags(' . expand("<abuf>") . ')' execute 'python deleteTags(' . expand("<abuf>") . ')'
endfunction endfunction
@@ -698,7 +699,7 @@ function! TagInStatusLine()
" return value of w:PHStatusLine in case it's set " return value of w:PHStatusLine in case it's set
if (exists("w:PHStatusLine")) if (exists("w:PHStatusLine"))
return w:PHStatusLine return w:PHStatusLine
" otherwise just return empty string " otherwise just return an empty string
else else
return "" return ""
endif endif
@@ -737,15 +738,14 @@ endfunction
" }}} " }}}
" event binding, vim customizing {{{ " event binding, Vim customization {{{
" autocommands binding " autocommands
autocmd CursorHold * call PHCursorHold() autocmd CursorHold * call PHCursorHold()
autocmd CursorHoldI * call PHCursorHold() autocmd CursorHoldI * call PHCursorHold()
autocmd BufDelete * silent call PHBufferDelete() autocmd BufDelete * silent call PHBufferDelete()
" time that determines after how long time of no activity the CursorHold event " period of no activity after which the CursorHold event is triggered
" is fired up
set updatetime=1000 set updatetime=1000
" }}} " }}}