mirror of
https://github.com/gryf/pythonhelper.git
synced 2026-04-25 15:21:26 +02:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2caa97d75f | - | ||
| b0841be7ee | - | ||
| ab9d0e5e69 | - | ||
| f5b70c33bb | - | ||
| 91b230cb51 | - | ||
| 9f5c2d1dc2 | - |
+565
-164
@@ -1,68 +1,452 @@
|
|||||||
" File: pythonhelper.vim
|
" File: pythonhelper.vim
|
||||||
" Author: Michal Vitecek <fuf-at-mageo-dot-cz>
|
" Author: Michal Vitecek <fuf-at-mageo-dot-cz>
|
||||||
" Version: 0.7
|
" Version: 0.83
|
||||||
" Last Modified: Oct 2, 2002
|
" Last Modified: Jan 4, 2010
|
||||||
"
|
"
|
||||||
" Overview
|
" Overview
|
||||||
" --------
|
" --------
|
||||||
" Vim script to help moving around in larger Python source files. It displays
|
" Vim script to help moving around in larger Python source files. It displays
|
||||||
" current class, method or function the cursor is placed in in the status
|
" current class, method or function the cursor is placed in on the status
|
||||||
" line for every python file. It's more clever than Yegappan Lakshmanan's
|
" line for every python file. It's more clever than Yegappan Lakshmanan's
|
||||||
" taglist.vim because it takes into account indetation and comments to
|
" taglist.vim because it takes into account indetation and comments to
|
||||||
" determine what tag the cursor is placed in.
|
" determine what tag the cursor is placed in.
|
||||||
"
|
"
|
||||||
" Requirements
|
" Requirements
|
||||||
" ------------
|
" ------------
|
||||||
" This script needs VIM compiled with Python interpreter and relies on
|
" This script needs only VIM compiled with Python interpreter. It doesn't rely
|
||||||
" exuberant ctags utility to generate the tag listing. You can determine
|
" on exuberant ctags utility. You can determine whether your VIM has Python
|
||||||
" whether your VIM has Python support by issuing command :ver and looking for
|
" support by issuing command :ver and looking for +python in the list of
|
||||||
" +python in the list of features.
|
" features.
|
||||||
"
|
|
||||||
" The exuberant ctags can be downloaded from http://ctags.sourceforge.net/ and
|
|
||||||
" should be reasonably new version (tested with 5.3).
|
|
||||||
"
|
|
||||||
" Note: The script doesn't display current tag on the status line only in
|
|
||||||
" NORMAL mode. This is because CursorHold event is fired up only in this mode.
|
|
||||||
" However if you badly need to know what tag you are on even in INSERT or
|
|
||||||
" VISUAL mode, contact me on the above specified email address and I'll send
|
|
||||||
" you patch that enables it.
|
|
||||||
"
|
"
|
||||||
" Installation
|
" Installation
|
||||||
" ------------
|
" ------------
|
||||||
" 1. Make sure your Vim has python feature on (+python). If not, you will need
|
" 1. Make sure your Vim has python feature on (+python). If not, you will need
|
||||||
" to recompile it with --with-pythoninterp option to the configure script
|
" to recompile it with --with-pythoninterp option to the configure script
|
||||||
" 2. Copy script pythonhelper.vim to the $HOME/.vim/plugin directory
|
" 2. Copy script pythonhelper.vim to the $HOME/.vim/plugin directory
|
||||||
" 3. Edit the script and modify the location of your exuberant tags utility
|
" 3. Run Vim and open any python file.
|
||||||
" (variable CTAGS_PROGRAM).
|
|
||||||
" 4. Run Vim and open any python file.
|
|
||||||
"
|
"
|
||||||
python << EOS
|
python << EOS
|
||||||
|
|
||||||
# import of required modules {{{
|
# import of required modules {{{
|
||||||
import vim
|
import re
|
||||||
import os
|
|
||||||
import popen2
|
|
||||||
import time
|
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
import vim
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
# CTAGS program and parameters {{{
|
|
||||||
CTAGS_PROGRAM = "/usr/local/bin/ctags"
|
|
||||||
CTAGS_PARAMETERS = "--language-force=python --format=2 --sort=0 --fields=+nK -L - -f - "
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
# global dictionaries of tags and their line numbers, keys are buffer numbers {{{
|
# global dictionaries of tags and their line numbers, keys are buffer numbers {{{
|
||||||
TAGS = {}
|
TAGS = {}
|
||||||
TAGLINENUMBERS = {}
|
TAGLINENUMBERS = {}
|
||||||
BUFFERTICKS = {}
|
BUFFERTICKS = {}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# class PythonTag() {{{
|
||||||
|
class PythonTag(object):
|
||||||
|
# DOC {{{
|
||||||
|
"""A simple storage class representing a python tag.
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# STATIC VARIABLES {{{
|
||||||
|
|
||||||
|
# possible tag types {{{
|
||||||
|
TT_CLASS = 0
|
||||||
|
TT_METHOD = 1
|
||||||
|
TT_FUNCTION = 2
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# tag type names {{{
|
||||||
|
TAG_TYPE_NAME = {
|
||||||
|
TT_CLASS : "class",
|
||||||
|
TT_METHOD : "method",
|
||||||
|
TT_FUNCTION : "function",
|
||||||
|
}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# METHODS {{{
|
||||||
|
|
||||||
|
def __init__(self, type, name, fullName, lineNumber, indentLevel):
|
||||||
|
# DOC {{{
|
||||||
|
"""Initializes instances of PythonTag().
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
type -- tag type
|
||||||
|
|
||||||
|
name -- short tag name
|
||||||
|
|
||||||
|
fullName -- full tag name (in dotted notation)
|
||||||
|
|
||||||
|
lineNumber -- line number on which the tag starts
|
||||||
|
|
||||||
|
indentLevel -- indentation level of the tag
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# remember the settings {{{
|
||||||
|
self.type = type
|
||||||
|
self.name = name
|
||||||
|
self.fullName = fullName
|
||||||
|
self.lineNumber = lineNumber
|
||||||
|
self.indentLevel = indentLevel
|
||||||
|
# }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
# DOC {{{
|
||||||
|
"""Returns a string representation of the tag.
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
return "%s (%s) [%s, %u, %u]" % (self.name, PythonTag.TAG_TYPE_NAME[self.type],
|
||||||
|
self.fullName, self.lineNumber, self.indentLevel,)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
__repr__ = __str__
|
||||||
|
|
||||||
|
|
||||||
|
# }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# class SimplePythonTagsParser() {{{
|
||||||
|
class SimplePythonTagsParser(object):
|
||||||
|
# DOC {{{
|
||||||
|
"""Provides a simple python tag parser.
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# STATIC VARIABLES {{{
|
||||||
|
|
||||||
|
# how many chars a single tab represents (visually)
|
||||||
|
TABSIZE = 8
|
||||||
|
# regexp used to extract indentation and strip comments
|
||||||
|
COMMENTS_INDENT_RE = re.compile('([ \t]*)([^\n#]*).*')
|
||||||
|
# regexp used to extract a class name
|
||||||
|
CLASS_RE = re.compile('class[ \t]+([^(:]+).*')
|
||||||
|
# regexp used to extract a method or function name
|
||||||
|
METHOD_RE = re.compile('def[ \t]+([^(]+).*')
|
||||||
|
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# METHODS {{{
|
||||||
|
|
||||||
|
def __init__(self, source):
|
||||||
|
# DOC {{{
|
||||||
|
"""Initializes instances of SimplePythonTagsParser().
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
source -- source for which the tags will be generated. It must
|
||||||
|
provide callable method readline (i.e. as file objects do).
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# make sure source has readline() method {{{
|
||||||
|
if ((hasattr(source, 'readline') == 0) or
|
||||||
|
(callable(source.readline) == 0)):
|
||||||
|
raise AttributeError("Source must have callable readline method.")
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# remember what the source is
|
||||||
|
self.source = source
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
def getTags(self):
|
||||||
|
# DOC {{{
|
||||||
|
"""Determines all the tags for the buffer. Returns a tuple in format
|
||||||
|
(tagLineNumbers, tags,).
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# initialize the resulting list of the tag line numbers and the tag information {{{
|
||||||
|
tagLineNumbers = []
|
||||||
|
tags = {}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# initalize local auxiliary variables {{{
|
||||||
|
tagsStack = []
|
||||||
|
lineNumber = 0
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# go through all the lines in the source and localize all python tags in it {{{
|
||||||
|
while 1:
|
||||||
|
# get next line
|
||||||
|
line = self.source.readline()
|
||||||
|
|
||||||
|
# finish if this is the end of the source {{{
|
||||||
|
if (line == ''):
|
||||||
|
break
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# increase the line number
|
||||||
|
lineNumber += 1
|
||||||
|
|
||||||
|
# extract the line indentation characters and its content {{{
|
||||||
|
lineMatch = self.COMMENTS_INDENT_RE.match(line)
|
||||||
|
lineContent = lineMatch.group(2)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# handle the class tag {{{
|
||||||
|
# match for the class tag
|
||||||
|
tagMatch = self.CLASS_RE.match(lineContent)
|
||||||
|
|
||||||
|
# if the class tag has been found, store some information on it {{{
|
||||||
|
if (tagMatch):
|
||||||
|
currentTag = self.getPythonTag(tagsStack, lineNumber, lineMatch.group(1),
|
||||||
|
tagMatch.group(1), self.tagClassTypeDecidingMethod)
|
||||||
|
tagLineNumbers.append(lineNumber)
|
||||||
|
tags[lineNumber] = currentTag
|
||||||
|
# }}}
|
||||||
|
# }}}
|
||||||
|
# handle the function/method/none tag {{{
|
||||||
|
else:
|
||||||
|
# match for the method/function tag
|
||||||
|
tagMatch = self.METHOD_RE.match(lineContent)
|
||||||
|
|
||||||
|
# if the method/function tag has been found, store some information on it {{{
|
||||||
|
if (tagMatch):
|
||||||
|
currentTag = self.getPythonTag(tagsStack, lineNumber, lineMatch.group(1),
|
||||||
|
tagMatch.group(1), self.tagFunctionTypeDecidingMethod)
|
||||||
|
tagLineNumbers.append(lineNumber)
|
||||||
|
tags[lineNumber] = currentTag
|
||||||
|
# }}}
|
||||||
|
# }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# return the tags data for the source
|
||||||
|
return (tagLineNumbers, tags,)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
def getParentTag(self, tagsStack):
|
||||||
|
# DOC {{{
|
||||||
|
"""Returns the parent/enclosing tag (instance of PythonTag()) from the
|
||||||
|
specified tag list. If no such parent tag exists, returns None.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
tagsStack -- list (stack) of currently open PythonTag() instances
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# determine the parent tag {{{
|
||||||
|
if (len(tagsStack)):
|
||||||
|
parentTag = tagsStack[-1]
|
||||||
|
else:
|
||||||
|
parentTag = None
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# return the tag
|
||||||
|
return parentTag
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
def computeIndentationLevel(indentChars):
|
||||||
|
# DOC {{{
|
||||||
|
"""Computes the indentation level from the specified string.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
indentChars -- white space before any other character on line
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# initialize the indentation level
|
||||||
|
indentLevel = 0
|
||||||
|
|
||||||
|
# compute the indentation level (expand tabs) {{{
|
||||||
|
for char in indentChars:
|
||||||
|
if (char == '\t'):
|
||||||
|
indentLevel += SimplePythonTagsParser.TABSIZE
|
||||||
|
else:
|
||||||
|
indentLevel += 1
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# return the computed indentation level
|
||||||
|
return indentLevel
|
||||||
|
# }}}
|
||||||
|
computeIndentationLevel = staticmethod(computeIndentationLevel)
|
||||||
|
|
||||||
|
|
||||||
|
def getPythonTag(self, tagsStack, lineNumber, indentChars, tagName, tagTypeDecidingMethod):
|
||||||
|
# DOC {{{
|
||||||
|
"""Returns instance of PythonTag() based on the specified data.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
tagsStack -- list (stack) of tags currently active. Note: Modified
|
||||||
|
in this method!
|
||||||
|
|
||||||
|
lineNumber -- current line number
|
||||||
|
|
||||||
|
indentChars -- characters making up the indentation level of the
|
||||||
|
current tag
|
||||||
|
|
||||||
|
tagName -- short name of the current tag
|
||||||
|
|
||||||
|
tagTypeDecidingMethod -- reference to method that is called to
|
||||||
|
determine the type of the current tag
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# compute the indentation level
|
||||||
|
indentLevel = self.computeIndentationLevel(indentChars)
|
||||||
|
# get the parent tag
|
||||||
|
parentTag = self.getParentTag(tagsStack)
|
||||||
|
|
||||||
|
# handle an enclosed tag {{{
|
||||||
|
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 (parentTag.indentLevel >= indentLevel):
|
||||||
|
del tagsStack[-1]
|
||||||
|
# }}}
|
||||||
|
# otherwise we have all information on the current tag and can return it {{{
|
||||||
|
else:
|
||||||
|
# create the tag
|
||||||
|
tag = PythonTag(tagTypeDecidingMethod(parentTag.type), tagName, "%s.%s" % (parentTag.fullName, tagName,), lineNumber, indentLevel)
|
||||||
|
|
||||||
|
# break the loop
|
||||||
|
break
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# use parent tag of the parent tag
|
||||||
|
parentTag = self.getParentTag(tagsStack)
|
||||||
|
# }}}
|
||||||
|
# handle a top-indent level tag {{{
|
||||||
|
else:
|
||||||
|
# create the tag
|
||||||
|
tag = PythonTag(tagTypeDecidingMethod(None), tagName, tagName, lineNumber, indentLevel)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# add the tag to the list of tags
|
||||||
|
tagsStack.append(tag)
|
||||||
|
|
||||||
|
# return the tag
|
||||||
|
return tag
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
def tagClassTypeDecidingMethod(self, parentTagType):
|
||||||
|
# DOC {{{
|
||||||
|
"""Returns tag type of the current tag based on its previous tag (super
|
||||||
|
tag) for classes.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
parentTagType -- type of the enclosing/parent tag
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# is always class no matter what
|
||||||
|
return PythonTag.TT_CLASS
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
def tagFunctionTypeDecidingMethod(self, parentTagType):
|
||||||
|
# DOC {{{
|
||||||
|
"""Returns tag type of the current tag based on its previous tag (super
|
||||||
|
tag) for functions/methods.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
parentTagType -- type of the enclosing/parent tag
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
if (parentTagType == PythonTag.TT_CLASS):
|
||||||
|
return PythonTag.TT_METHOD
|
||||||
|
else:
|
||||||
|
return PythonTag.TT_FUNCTION
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# class VimReadlineBuffer() {{{
|
||||||
|
class VimReadlineBuffer(object):
|
||||||
|
# DOC {{{
|
||||||
|
"""A simple wrapper class around vim's buffer that provides readline
|
||||||
|
method.
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# METHODS {{{
|
||||||
|
|
||||||
|
def __init__(self, vimBuffer):
|
||||||
|
# DOC {{{
|
||||||
|
"""Initializes instances of VimReadlineBuffer().
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
|
||||||
|
vimBuffer -- VIM's buffer
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# remember the settings
|
||||||
|
self.vimBuffer = vimBuffer
|
||||||
|
|
||||||
|
# initialize instance attributes {{{
|
||||||
|
self.currentLine = -1
|
||||||
|
self.bufferLines = len(vimBuffer)
|
||||||
|
# }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
def readline(self):
|
||||||
|
# DOC {{{
|
||||||
|
"""Returns next line from the buffer. If all the buffer has been read,
|
||||||
|
returns empty string.
|
||||||
|
"""
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# CODE {{{
|
||||||
|
# increase the current line counter
|
||||||
|
self.currentLine += 1
|
||||||
|
|
||||||
|
# notify end of file if we reached beyond the last line {{{
|
||||||
|
if (self.currentLine == self.bufferLines):
|
||||||
|
return ''
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# return the line with an added newline (vim stores the lines without it)
|
||||||
|
return "%s\n" % (self.vimBuffer[self.currentLine],)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
def getNearestLineIndex(row, tagLineNumbers):
|
def getNearestLineIndex(row, tagLineNumbers):
|
||||||
# DOC {{{
|
# DOC {{{
|
||||||
"""Returns index of line in tagLineNumbers list that is nearest to the
|
"""Returns the index of line in 'tagLineNumbers' list that is nearest to the
|
||||||
current cursor row.
|
specified cursor row.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
||||||
@@ -73,29 +457,34 @@ def getNearestLineIndex(row, tagLineNumbers):
|
|||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# CODE {{{
|
# CODE {{{
|
||||||
nearestLineNumber = -1
|
# initialize local auxiliary variables {{{
|
||||||
nearestLineIndex = -1
|
nearestLineNumber = -1
|
||||||
i = 0
|
nearestLineIndex = -1
|
||||||
for lineNumber in tagLineNumbers:
|
# }}}
|
||||||
|
|
||||||
|
# go through all tag line numbers and find the one nearest to the specified row {{{
|
||||||
|
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 = i
|
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
|
||||||
# }}}
|
# }}}
|
||||||
i += 1
|
# }}}
|
||||||
|
|
||||||
|
# return 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. It does so by executing
|
"""Reads the tags for the specified buffer number. Returns a tuple
|
||||||
the CTAGS program and parsing its output. Returns tuple
|
(taglinenumber[buffer], tags[buffer],).
|
||||||
(taglinenumber[buffer], tags[buffer]).
|
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|
||||||
@@ -107,92 +496,27 @@ def getTags(bufferNumber, changedTick):
|
|||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# CODE {{{
|
# CODE {{{
|
||||||
global CTAGS_PROGRAM, CTAGS_PARAMETERS
|
# define global variables
|
||||||
global TAGLINENUMBERS, TAGS, BUFFERTICKS
|
global TAGLINENUMBERS, TAGS, BUFFERTICKS
|
||||||
|
|
||||||
|
|
||||||
# return immediately if there's no need to update the tags {{{
|
# return immediately if there's no need to update the tags {{{
|
||||||
if ((BUFFERTICKS.has_key(bufferNumber)) and (BUFFERTICKS[bufferNumber] == changedTick)):
|
if (BUFFERTICKS.get(bufferNumber, None) == changedTick):
|
||||||
return (TAGLINENUMBERS[bufferNumber], TAGS[bufferNumber],)
|
return (TAGLINENUMBERS[bufferNumber], TAGS[bufferNumber],)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# read the tags and fill the global variables {{{
|
# get the tags {{{
|
||||||
currentBuffer = vim.current.buffer
|
simpleTagsParser = SimplePythonTagsParser(VimReadlineBuffer(vim.current.buffer))
|
||||||
currentWindow = vim.current.window
|
tagLineNumbers, tags = simpleTagsParser.getTags()
|
||||||
row, col = currentWindow.cursor
|
|
||||||
|
|
||||||
# create a temporary file with the current content of the buffer {{{
|
|
||||||
fileName = "/tmp/.%s.%u.ph" % (os.path.basename(currentBuffer.name), os.getpid(),)
|
|
||||||
f = open(fileName, "w")
|
|
||||||
|
|
||||||
for line in currentBuffer:
|
|
||||||
f.write(line)
|
|
||||||
f.write('\n')
|
|
||||||
f.close()
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
# run ctags on it {{{
|
|
||||||
try:
|
|
||||||
ctagsOutPut, ctagsInPut = popen2.popen4("%s %s" % (CTAGS_PROGRAM, CTAGS_PARAMETERS,))
|
|
||||||
ctagsInPut.write(fileName + "\n")
|
|
||||||
ctagsInPut.close()
|
|
||||||
except:
|
|
||||||
os.unlink(fileName)
|
|
||||||
return
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
# parse the ctags' output {{{
|
|
||||||
tagLineNumbers = []
|
|
||||||
tags = {}
|
|
||||||
while 1:
|
|
||||||
line = ctagsOutPut.readline()
|
|
||||||
# if empty line has been read, it's the end of the file {{{
|
|
||||||
if (line == ''):
|
|
||||||
break
|
|
||||||
# }}}
|
|
||||||
# if the line starts with !, then it's a comment line {{{
|
|
||||||
if (line[0] == '!'):
|
|
||||||
continue
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
# split the line into parts and parse the data {{{
|
|
||||||
# the format is: [0]tagName [1]fileName [2]tagLine [3]tagType [4]tagLineNumber [[5]tagOwner]
|
|
||||||
tagData = line.split('\t')
|
|
||||||
name = tagData[0]
|
|
||||||
# get the tag's indentation {{{
|
|
||||||
start = 2
|
|
||||||
j = 2
|
|
||||||
while ((j < len(tagData[2])) and (tagData[2][j].isspace())):
|
|
||||||
if (tagData[2][j] == '\t'):
|
|
||||||
start += 8
|
|
||||||
else:
|
|
||||||
start += 1
|
|
||||||
j += 1
|
|
||||||
# }}}
|
|
||||||
type = tagData[3]
|
|
||||||
line = int(tagData[4][5:])
|
|
||||||
if (len(tagData) == 6):
|
|
||||||
owner = tagData[5].strip()
|
|
||||||
else:
|
|
||||||
owner = None
|
|
||||||
# }}}
|
|
||||||
tagLineNumbers.append(line)
|
|
||||||
tags[line] = (name, type, owner, start)
|
|
||||||
ctagsOutPut.close()
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
# clean up the now unnecessary stuff {{{
|
|
||||||
os.unlink(fileName)
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# update the global variables {{{
|
# update the global variables {{{
|
||||||
TAGS[bufferNumber] = tags
|
TAGS[bufferNumber] = tags
|
||||||
TAGLINENUMBERS[bufferNumber] = tagLineNumbers
|
TAGLINENUMBERS[bufferNumber] = tagLineNumbers
|
||||||
BUFFERTICKS[bufferNumber] = changedTick
|
BUFFERTICKS[bufferNumber] = changedTick
|
||||||
# }}}
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
return (TAGLINENUMBERS[bufferNumber], TAGS[bufferNumber],)
|
# return the tuple (tagLineNumbers, tags,)
|
||||||
|
return (tagLineNumbers, tags,)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
@@ -210,11 +534,12 @@ def findTag(bufferNumber, changedTick):
|
|||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# CODE {{{
|
# CODE {{{
|
||||||
|
# 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 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
|
||||||
@@ -222,74 +547,97 @@ 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 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.)
|
||||||
if (nearestLineIndex > -1):
|
while (nearestLineIndex > -1):
|
||||||
nearestLineNumber = tagLineNumbers[nearestLineIndex]
|
# get the line number of the nearest tag
|
||||||
# walk through all the lines in range (nearestTagLine, cursorRow) {{{
|
nearestLineNumber = tagLineNumbers[nearestLineIndex]
|
||||||
for i in xrange(nearestLineNumber + 1, row):
|
|
||||||
line = currentBuffer[i]
|
# walk through all the lines in range (nearestTagLine, cursorRow) {{{
|
||||||
# count the indentation of the line, if it's lower that the tag's, the found tag is wrong {{{
|
for lineNumber in xrange(nearestLineNumber + 1, row):
|
||||||
if (len(line)):
|
# get the current line
|
||||||
|
line = currentBuffer[lineNumber]
|
||||||
|
|
||||||
|
# count the indentation of the line, if it's lower than the tag's, the tag is invalid {{{
|
||||||
|
if (len(line)):
|
||||||
|
# initialize local auxiliary variables {{{
|
||||||
|
lineStart = 0
|
||||||
|
i = 0
|
||||||
|
# }}}
|
||||||
|
|
||||||
# compute the indentation of the line {{{
|
# compute the indentation of the line {{{
|
||||||
lineStart = 0
|
while ((i < len(line)) and (line[i].isspace())):
|
||||||
j = 0
|
# move the start of the line code {{{
|
||||||
while ((j < len(line)) and (line[j].isspace())):
|
if (line[i] == '\t'):
|
||||||
if (line[j] == '\t'):
|
lineStart += SimplePythonTagsParser.TABSIZE
|
||||||
lineStart += 8
|
else:
|
||||||
else:
|
lineStart += 1
|
||||||
lineStart += 1
|
# }}}
|
||||||
j += 1
|
|
||||||
# if the line contains only spaces, it doesn't count {{{
|
# go to the next character on the line
|
||||||
if (j == len(line)):
|
i += 1
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# if the line contains only spaces, skip it {{{
|
||||||
|
if (i == len(line)):
|
||||||
continue
|
continue
|
||||||
# }}}
|
# }}}
|
||||||
# if the next character is # (python comment), this line doesn't count {{{
|
# if the next character is a '#' (python comment), skip the line {{{
|
||||||
if (line[j] == '#'):
|
if (line[i] == '#'):
|
||||||
continue
|
continue
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
# if the line's indentation starts before or at the nearest tag's one, the tag is invalid {{{
|
||||||
|
if (lineStart <= tags[nearestLineNumber].indentLevel):
|
||||||
|
nearestLineIndex -= 1
|
||||||
|
break
|
||||||
# }}}
|
# }}}
|
||||||
# if the line's indentation starts before the nearest tag's one, the tag is wrong {{{
|
# }}}
|
||||||
if (lineStart < tags[nearestLineNumber][3]):
|
# }}}
|
||||||
nearestLineNumber = -1
|
# the tag is appropriate, so use it {{{
|
||||||
break
|
else:
|
||||||
# }}}
|
break
|
||||||
# }}}
|
|
||||||
# }}}
|
# }}}
|
||||||
|
# }}}
|
||||||
|
# no appropriate tag has been found {{{
|
||||||
else:
|
else:
|
||||||
nearestLineNumber = -1
|
nearestLineNumber = -1
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# describe the cursor position (what tag it's in) {{{
|
# describe the cursor position (what tag the cursor is on) {{{
|
||||||
|
# reset the description
|
||||||
tagDescription = ""
|
tagDescription = ""
|
||||||
|
|
||||||
|
# if an appropriate tag has been found, set the description accordingly {{{
|
||||||
if (nearestLineNumber > -1):
|
if (nearestLineNumber > -1):
|
||||||
tagInfo = tags[nearestLineNumber]
|
tagInfo = tags[nearestLineNumber]
|
||||||
# use the owner if any exists {{{
|
tagDescription = "[in %s (%s)]" % (tagInfo.fullName, PythonTag.TAG_TYPE_NAME[tagInfo.type],)
|
||||||
if (tagInfo[2] != None):
|
# }}}
|
||||||
fullTagName = "%s.%s()" % (tagInfo[2].split(':')[1], tagInfo[0],)
|
|
||||||
# }}}
|
|
||||||
# otherwise use just the tag name {{{
|
|
||||||
else:
|
|
||||||
fullTagName = tagInfo[0]
|
|
||||||
# }}}
|
|
||||||
tagDescription = "[in %s (%s)]" % (fullTagName, tagInfo[1],)
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# update the variable for the status line so it will be updated next time
|
# 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,))
|
||||||
except:
|
# }}}
|
||||||
# spit out debugging information {{{
|
|
||||||
|
# handle possible exceptions {{{
|
||||||
|
except Exception:
|
||||||
|
# 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 {{{
|
||||||
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)
|
||||||
# }}}
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
def deleteTags(bufferNumber):
|
def deleteTags(bufferNumber):
|
||||||
@@ -303,8 +651,10 @@ def deleteTags(bufferNumber):
|
|||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# CODE {{{
|
# CODE {{{
|
||||||
|
# define global variables
|
||||||
global TAGS, TAGLINENUMBERS, BUFFERTICKS
|
global TAGS, TAGLINENUMBERS, BUFFERTICKS
|
||||||
|
|
||||||
|
# try to delete the tags for the buffer {{{
|
||||||
try:
|
try:
|
||||||
del TAGS[bufferNumber]
|
del TAGS[bufferNumber]
|
||||||
del TAGLINENUMBERS[bufferNumber]
|
del TAGLINENUMBERS[bufferNumber]
|
||||||
@@ -312,14 +662,16 @@ def deleteTags(bufferNumber):
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
# }}}
|
# }}}
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
" 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
|
||||||
@@ -331,15 +683,62 @@ endfunction
|
|||||||
|
|
||||||
|
|
||||||
function! PHBufferDelete()
|
function! PHBufferDelete()
|
||||||
|
" set PHStatusLine for this window to empty string
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
function! TagInStatusLine()
|
||||||
|
" return value of w:PHStatusLine in case it's set
|
||||||
|
if (exists("w:PHStatusLine"))
|
||||||
|
return w:PHStatusLine
|
||||||
|
" otherwise just return empty string
|
||||||
|
else
|
||||||
|
return ""
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! PHPreviousClassMethod()
|
||||||
|
call search('^[ \t]*\(class\|def\)\>', 'bw')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! PHNextClassMethod()
|
||||||
|
call search('^[ \t]*\(class\|def\)\>', 'w')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! PHPreviousClass()
|
||||||
|
call search('^[ \t]*class\>', 'bw')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! PHNextClass()
|
||||||
|
call search('^[ \t]*class\>', 'w')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! PHPreviousMethod()
|
||||||
|
call search('^[ \t]*def\>', 'bw')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! PHNextMethod()
|
||||||
|
call search('^[ \t]*def\>', 'w')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
|
||||||
|
" event binding, vim customizing {{{
|
||||||
|
|
||||||
" autocommands binding
|
" autocommands binding
|
||||||
autocmd CursorHold * silent call PHCursorHold()
|
autocmd CursorHold * call PHCursorHold()
|
||||||
autocmd BufWinEnter * silent 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
|
" time that determines after how long time of no activity the CursorHold event
|
||||||
@@ -352,7 +751,9 @@ highlight User1 gui=bold guifg=cyan guibg=black
|
|||||||
highlight User2 gui=bold guifg=black guibg=red
|
highlight User2 gui=bold guifg=black guibg=red
|
||||||
" the status line will be displayed for every window
|
" the status line will be displayed for every window
|
||||||
set laststatus=2
|
set laststatus=2
|
||||||
" set the status variable for the current window
|
|
||||||
let w:PHStatusLine = ''
|
|
||||||
" set the status line to display some useful information
|
" set the status line to display some useful information
|
||||||
set stl=%-f%r\ %2*%m%*\ \ \ \ %1*%{w:PHStatusLine}%*%=[%l:%c]\ \ \ \ [buf\ %n]
|
set stl=%-f%r\ %2*%m%*\ \ \ \ %1*%{TagInStatusLine()}%*%=[%l:%c]\ \ \ \ [buf\ %n]
|
||||||
|
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" vim:foldmethod=marker
|
||||||
|
|||||||
Reference in New Issue
Block a user