/sztools/python/jde.py
Python | 1794 lines | 1791 code | 3 blank | 0 comment | 3 complexity | ab10c048be5ec4f7c335482d83afd900 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- import socket
- import vim
- import os.path
- import time
- import re
- import traceback
- import StringIO
- import shutil
- from subprocess import Popen
- from string import Template
- import difflib
- from common import output,SzToolsConfig,MiscUtil,VimUtil,BasicTalker,ZipUtil
- from pyparsing import *
- from xml.etree.ElementTree import *
- HOST = 'localhost'
- PORT = 9527
- END_TOKEN = "==end=="
- MAX_CPT_COUNT = 200
- bp_data = {}
- lastProjectRoot = None
- class ProjectManager(object):
- @staticmethod
- def getProjectRoot(filePath, useLastRoot = True):
- global lastProjectRoot
- projectRoot = None
- parent = filePath
- if not filePath :
- return None
- while True :
- tmpdir = os.path.dirname(parent)
- if tmpdir == "" or tmpdir == "/" or tmpdir == parent :
- break
- parent = tmpdir
- fullname = lambda name : os.path.join(parent,name)
- prj_names =[fullname(name) for name in [".project",".classpath"]]
- if os.path.exists(prj_names[0]) and os.path.exists(prj_names[1]):
- projectRoot = parent
- break
- if projectRoot != None :
- lastProjectRoot = projectRoot
- elif useLastRoot :
- projectRoot = lastProjectRoot
- return projectRoot
- @staticmethod
- def getSrcLocations(filePath):
- tree = ElementTree()
- classpathXml = ProjectManager.getClassPathXml(filePath)
- if os.path.isdir(classpathXml) :
- return [ classpathXml ]
- project_root = ProjectManager.getProjectRoot(filePath)
- tree.parse(classpathXml)
- entries = tree.findall("classpathentry")
- src_locs = []
- for entry in entries :
- if entry.get("kind") == "src" :
- abpath = os.path.normpath(os.path.join(project_root,entry.get("path")))
- src_locs.append(abpath)
- return src_locs
- @staticmethod
- def getClassPathXml(filePath):
- projectRoot = ProjectManager.getProjectRoot(filePath)
- if not projectRoot :
- parent = os.path.dirname(filePath)
- return parent
- return os.path.join(projectRoot,".classpath")
- @staticmethod
- def getAntBuildXml(filePath):
- projectRoot = ProjectManager.getProjectRoot(filePath)
- if not projectRoot : return None
- antBuildXml = os.path.join(projectRoot,"build.xml")
- if os.path.exists(antBuildXml):
- return antBuildXml
- return None
- @staticmethod
- def projectInit():
- pwd = os.getcwd()
- projectName = os.path.basename(os.getcwd())
- examples_dir = os.path.join(SzToolsConfig.getShareHome(),"examples")
- projectInitXml = os.path.join(examples_dir,"project.xml")
- classpathInitXml = os.path.join(examples_dir,"classpath.xml")
- jdeInitXml = os.path.join(examples_dir,"jde.xml")
- if not os.path.exists("src") :
- os.mkdir("src")
- if not os.path.exists("lib") :
- os.mkdir("lib")
- if not os.path.exists("dst") :
- os.mkdir("dst")
- lines = open(projectInitXml).readlines()
- f = open(".project","w")
- for line in lines :
- line=line.replace("test",projectName)
- f.write(line)
- f.close()
- shutil.copy2(classpathInitXml, "./.classpath")
- shutil.copy2(jdeInitXml, "./.jde")
- print "project initialized in current dir succeed."
- @staticmethod
- def projectClean():
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- Talker.projectClean(classPathXml)
- @staticmethod
- def projectOpen():
- classPathXml = os.path.join(os.getcwd(),".classpath")
- if not os.path.exists(classPathXml) :
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- if current_file_name != None and os.path.exists(current_file_name):
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- else :
- classPathXml = ProjectManager.getClassPathXml(os.getcwd())
- #if we can't find .classpath, defer the project init process later .
- if classPathXml.endswith(".classpath") :
- Talker.projectOpen(classPathXml)
- @staticmethod
- def loadJarMeta():
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- Talker.loadJarMeta(classPathXml)
- class Talker(BasicTalker):
-
- @staticmethod
- def getPackageList(pkgname,xmlPath):
- params = dict()
- params["cmd"]="completion"
- params["completionType"] = "package"
- params["pkgname"] = pkgname
- params["classPathXml"] = xmlPath
- data = Talker.send(params)
- return data
- @staticmethod
- def getClassList(classNameStart,xmlPath,ignoreCase="false"):
- params = dict()
- params["cmd"]="completion"
- params["completionType"] = "class"
- params["className"] = classNameStart
- params["classPathXml"] = xmlPath
- params["ignoreCase"] = ignoreCase
- data = Talker.send(params)
- return data
- @staticmethod
- def locateSource(className, xmlPath, sourceType="declare"):
- "sourceType in { 'declare', 'impl] }"
- params = dict()
- params["cmd"]="locateSource"
- params["className"] = className
- params["classPathXml"] = xmlPath
- params["sourceType"] = sourceType
- data = Talker.send(params)
- return data
- @staticmethod
- def getMemberList(args):
- params = dict()
- sourceFile ,classnameList,xmlPath,memberType,expTokens = args
- params["cmd"]="completion"
- params["sourceFile"] = sourceFile
- params["completionType"] = memberType
- params["classnames"] = ",".join(classnameList)
- params["classPathXml"] = xmlPath
- params["expTokens"] = ",".join(expTokens)
- data = Talker.send(params)
- return data
- @staticmethod
- def getMethodDefs(args):
- "print all methods of a variable"
- params = dict()
- sourceFile ,classnameList,xmlPath,expTokens,memberName= args
- params["cmd"]="getMethodDefs"
- params["sourceFile"] = sourceFile
- params["classnames"] = ",".join(classnameList)
- params["classPathXml"] = xmlPath
- params["expTokens"] = ",".join(expTokens)
- params["memberName"] = memberName
- data = Talker.send(params)
- return data
- @staticmethod
- def getMethodDefClass(args):
- "get whic class defines a method"
- params = dict()
- sourceFile ,classnameList,xmlPath,expTokens,memberName,sourceType= args
- params["cmd"]="getMethodDefClass"
- params["sourceFile"] = sourceFile
- params["classnames"] = ",".join(classnameList)
- params["classPathXml"] = xmlPath
- params["expTokens"] = ",".join(expTokens)
- params["memberName"] = memberName
- params["sourceType"] = sourceType
- data = Talker.send(params)
- return data
- @staticmethod
- def searchRef(xmlPath,sourceFile,memberDesc):
- params = dict()
- params["cmd"]="searchRef"
- params["classPathXml"] = xmlPath
- params["sourceFile"] = sourceFile
- params["memberDesc"] = memberDesc
- data = Talker.send(params)
- return data
- @staticmethod
- def getConstructDefs(sourceFile,classnameList,xmlPath):
- params = dict()
- params["cmd"]="getConstructDefs"
- params["sourceFile"] = sourceFile
- params["classnames"] = ",".join(classnameList)
- params["classPathXml"] = xmlPath
- data = Talker.send(params)
- return data
- @staticmethod
- def compileFile(xmlPath,sourceFile):
- params = dict()
- params["cmd"]="compile"
- params["classPathXml"] = xmlPath
- params["sourceFile"] = sourceFile
- data = Talker.send(params)
- return data
- @staticmethod
- def typeHierarchy(xmlPath,sourceFile):
- params = dict()
- params["cmd"]="typeHierarchy"
- params["classPathXml"] = xmlPath
- params["sourceFile"] = sourceFile
- data = Talker.send(params)
- return data
- @staticmethod
- def copyResource(xmlPath,sourceFile):
- params = dict()
- params["cmd"]="copyResource"
- params["classPathXml"] = xmlPath
- params["sourceFile"] = sourceFile
- data = Talker.send(params)
- return data
- @staticmethod
- def runFile(xmlPath,sourceFile,vimServer,bufname,runCmd="run"):
- params = dict()
- params["cmd"]=runCmd
- params["classPathXml"] = xmlPath
- params["sourceFile"] = sourceFile
- params["vimServer"] = vimServer
- params["bufname"] = bufname
- data = Talker.send(params)
- return data
- @staticmethod
- def autoImport(xmlPath,varNames,pkgName):
- params = dict()
- params["cmd"]="autoimport"
- params["classPathXml"] = xmlPath
- params["varNames"] = varNames
- params["pkgName"] = pkgName
- data = Talker.send(params)
- return data
- @staticmethod
- def dumpClass(xmlPath,classnameList):
- params = dict()
- params["cmd"]="dumpClass"
- params["classPathXml"] = xmlPath
- params["dumpClassNames"] = ",".join(classnameList)
- data = Talker.send(params)
- return data
- @staticmethod
- def overideMethod(xmlPath,varNames):
- params = dict()
- params["cmd"]="overide"
- params["classPathXml"] = xmlPath
- params["varNames"] = ",".join(varNames)
- data = Talker.send(params)
- return data
- @staticmethod
- def runAntBuild(vimServer,cmdName,runInShell):
- params = dict()
- params["cmd"]="runSys"
- params["vimServer"] = vimServer
- params["cmdName"] = cmdName
- params["runInShell"] = runInShell
- data = Talker.send(params)
- return data
- @staticmethod
- def projectClean(xmlPath):
- params = dict()
- params["cmd"]="projectClean"
- params["classPathXml"] = xmlPath
- data = Talker.send(params)
- return data
- @staticmethod
- def projectOpen(xmlPath):
- params = dict()
- params["cmd"]="projectOpen"
- params["classPathXml"] = xmlPath
- data = Talker.send(params)
- return data
- @staticmethod
- def loadJarMeta(xmlPath):
- params = dict()
- params["cmd"]="loadJarMeta"
- params["classPathXml"] = xmlPath
- data = Talker.send(params)
- return data
- class EditUtil(object):
- @staticmethod
- def createSkeleton():
- vim_buffer = vim.current.buffer
- if len(vim_buffer) > 10 :
- return
- buf_content = "\n".join(vim_buffer)
- if not re.match("^\s*$",buf_content) :
- return
- cur_file = vim_buffer.name
- if cur_file.startswith("jar:"):
- return
- if os.path.exists(cur_file) :
- file_content ="\n".join(open(cur_file,"r").readlines())
- if not re.match("^\s*$",file_content) :
- return
- cur_path = os.path.dirname(cur_file)
- prj_root = ProjectManager.getProjectRoot(cur_file)
- src_locs = ProjectManager.getSrcLocations(cur_file)
- pkg = ""
- for abs_src in src_locs :
- if cur_path.startswith(abs_src) :
- pkg = cur_path[ len(abs_src)+1 : ]
- if pkg != "" :
- vim_buffer.append("package %s;" % pkg.replace(os.path.sep,"."))
- vim_buffer.append("")
- class_name = os.path.splitext(os.path.basename(cur_file))[0]
- s = Template("public class $name {\n\n}")
- skeleton = s.substitute(name=class_name)
- for line in skeleton.split("\n"):
- vim_buffer.append(line)
- del vim_buffer[0]
- @staticmethod
- def generateGseter():
- jdef_parser = Parser.getJavaVarDefParser(None,False)
- statement = OneOrMore(Group(jdef_parser))
- statement.ignore( javaStyleComment )
- startCol,endCol,startLine,endLine=MiscUtil.getVisualArea()
- vim_buffer = vim.current.buffer
- selectedText = "\n".join(vim_buffer[startLine-1:endLine])
- results = statement.parseString(selectedText)
- template = """
- public void set$1($2 $3) {
- this.$3=$3;
- }
- public $2 get$1() {
- return this.$3;
- }
- """
- sb = []
- for item in results :
- tmp = []
- for type_token in item[0]:
- if type_token not in "<>[]" :
- if len(tmp) > 0 and tmp[-1] not in "<>[]":
- tmp.append(",")
- tmp.append(type_token)
- vartype = "".join(tmp)
- varname = item[1]
- result = template.replace("$1",varname[0].upper() + varname[1:])
- result = result.replace("$2",vartype)
- result = result.replace("$3",varname)
- sb.append(result)
- if sb == [] :
- return
- endline = Parser.parseClassEnd()
- del vim_buffer[endline]
- output(sb,append=True)
- output("}",append=True)
- #format the getter,setter code
- vim.command("normal %sGV200j=" % endline)
- @staticmethod
- def overideMethod():
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- if not classPathXml : return
- allFullClassNames = Parser.getAllSuperClassFullNames()
- resultText = Talker.overideMethod(classPathXml,allFullClassNames)
- VimUtil.writeToSzToolBuffer("JdeConsole",resultText)
- @staticmethod
- def locateDefinition(sourceType):
- (row,col) = vim.current.window.cursor
- vim_buffer = vim.current.buffer
- line = vim_buffer[row-1]
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- dotExpParser = Parser.getJavaDotExpParser()
- tokenEndCol = col
-
- for char in line[col:] :
- if not re.match("\w",char) :
- break
- tokenEndCol += 1
- #if current line starts with "." or last line ends with "." . join the line
- if re.match(r"^\s*\..*", line) or re.match(r".*\.$",vim_buffer[row-2]) :
- line = vim_buffer[row-2].strip() + line[0:tokenEndCol+1]
- tokenEndCol = len(line) - 1
- #if locate the class source
- classNamePat = r".*\b(?P<name>[A-Z]\w+)$"
- searchResult = re.search(classNamePat, line[0:tokenEndCol])
- if searchResult :
- className = searchResult.group("name")
- tmpLine = line[0:tokenEndCol]
- #check if the exp is some member name started with uppercase letter.
- if tmpLine[len(tmpLine) - len(className) - 1] != "." :
- classNameList = Parser.getFullClassNames(className)
- for className in classNameList :
- sourcePath = Talker.locateSource(className, classPathXml,sourceType)
- if sourcePath != "None" :
- sourcePath, className = sourcePath.split("\n")
- matchedLine = EditUtil.searchClassDefLineNum(className, sourcePath)
- vim.command("edit +%s %s" % (matchedLine, sourcePath ))
- break
- return
- #locate the member of class
- expTokens = dotExpParser.searchString(line[0:tokenEndCol])[0]
- if not expTokens :
- return
- varName = expTokens[0]
- endTokenIndex = 0 if len(expTokens)==1 else -1
- if len(expTokens) == 1 or (len(expTokens) == 3 and varName == "this"):
- if len(expTokens) ==1 :
- memberName = expTokens[0]
- else :
- memberName = expTokens[2]
- #search in visible scope(only upward)
- var_type, var_type_row = Parser.getVarTypeInfo(memberName,row-1)
- if var_type != None :
- vim.command("let @/='\<%s\>'" % memberName)
- vim.command("normal %sG" % str(var_type_row + 1))
- return
- #search in class member info
- members = Parser.parseAllMemberInfo(vim_buffer)
- for name,mtype,rtntype,param,lineNum in members :
- if name == memberName :
- matched_row = lineNum
- vim.command("let @/='\<%s\>'" % memberName)
- vim.command("normal %sG" % str(matched_row))
- return
- else :
- expTokens = expTokens[1:]
- memberName = expTokens[-1]
- if line[tokenEndCol] == "(":
- expTokens[endTokenIndex] = expTokens[endTokenIndex] + "()"
- superClass = Parser.getSuperClass()
- if varName[0].isupper():
- classname = varName
- elif varName == "this" :
- classname = "this"
- elif varName.endswith("()") :
- classname = "this"
- expTokens.insert(0,varName)
- elif varName == "super" :
- classname = superClass
- else :
- classname = Parser.getVarType(varName,row-1)
- if not classname :
- if not superClass : return
- expTokens.insert(0,varName)
- classname = superClass
- classNameList = Parser.getFullClassNames(classname)
- expTokens = expTokens[:-1]
- tmpName = memberName + "()" if line[tokenEndCol] == "(" else memberName
- #get the param count of method, match method by name and count of param first
- param_count = -1
- if line[tokenEndCol] == "(":
- matched_count = -1
- param_names = None
- for index, ch in enumerate(line[tokenEndCol+1:]) :
- if "(" == ch :
- matched_count = matched_count - 1
- if ")" == ch :
- matched_count = matched_count + 1
- if matched_count == 0 :
- param_names = line[tokenEndCol+1 : tokenEndCol+1+index]
- break
- if param_names != None :
- param_count = len(param_names.split(",")) if param_names.strip() != "" else 0
- params =(current_file_name,classNameList,classPathXml,expTokens,tmpName,sourceType)
- sourcePath = Talker.getMethodDefClass(params)
- if sourcePath != "None" :
- matchedLine = EditUtil.searchMemeberLineNum(memberName, sourcePath,param_count)
- vim.command("let @/='\<%s\>'" % memberName)
- vim.command("edit +%s %s" % (matchedLine, sourcePath ))
- else :
- print "cant' locate the source code"
- return
- @staticmethod
- def reprDictInDoubleQuote(dic):
- pairs = []
- for item in dic.keys() :
- value = re.escape(dic[item])
- pair = '"'+str(item)+'"'+":"+'"'+value+'"'
- pairs.append(pair)
- return "{"+",".join(pairs)+"}"
- @staticmethod
- def searchRef():
- (row,col) = vim.current.window.cursor
- vim_buffer = vim.current.buffer
- line = vim_buffer[row-1]
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- result = Parser.parseCurrentMethod()
- if result == None :
- print "current line not contains a method declaration."
- return
- rtntype,name,param = result
- memberDesc = rtntype + " " + name + "(" + param +")"
- resultText = Talker.searchRef(classPathXml,current_file_name,memberDesc)
- hltype = "R"
- HighlightManager.removeHighlightType(hltype)
- qflist = []
- for line in resultText.split("\n"):
- if line.strip() == "" : continue
- try :
- filename,lnum,text= line.split("::")
- bufnr=str(vim.eval("bufnr('%')"))
- absname = os.path.normpath(filename)
- filename = Compiler.relpath(absname)
- if filename != Compiler.relpath(current_file_name) :
- qfitem = dict(filename=filename,lnum=lnum,text=text)
- else :
- qfitem = dict(bufnr=bufnr,lnum=lnum,text=text)
- lstart = text.find(name)
- lend = lstart + len(name) + 2
- HighlightManager.addHighlightInfo(absname,lnum,hltype,text,lstart, lend)
- qflist.append(qfitem)
- except Exception , e:
- fp = StringIO.StringIO()
- traceback.print_exc(file=fp)
- message = fp.getvalue()
- logging.debug(message)
- HighlightManager.highlightCurrentBuf()
- if len(qflist) > 0 :
- #since vim use single quote string as literal string, the escape char will not
- #been handled, so repr the dict in a double quoted string
- qflist_str = "[" + ",".join([EditUtil.reprDictInDoubleQuote(item) for item in qflist])+"]"
- vim.command("call setqflist(%s)" % qflist_str)
- vim.command("cwindow")
- else :
- print "can't find any reference location."
- @staticmethod
- def searchMemeberLineNum(memberName,sourcePath,paramCount = -1):
- if sourcePath.startswith("jar:") :
- lines = ZipUtil.read_zip_entry(sourcePath)
- else :
- lines = open(sourcePath).readlines()
- matched_row = 1
- members = Parser.parseAllMemberInfo(lines)
- nameMatches = [1]
- gotExactMatch = False
- for name,mtype,rtntype,param,lineNum in members :
- if name == memberName :
- tmp_count = len(param.split(",")) if param.strip() != "" else 0
- if paramCount == -1 or paramCount == tmp_count :
- matched_row = lineNum
- gotExactMatch = True
- break
- else :
- nameMatches.append(lineNum)
- if gotExactMatch :
- return str(matched_row)
- else :
- return str(nameMatches[-1])
- @staticmethod
- def searchClassDefLineNum(className, sourcePath):
- if sourcePath.startswith("jar:") :
- lines = ZipUtil.read_zip_entry(sourcePath)
- else :
- lines = open(sourcePath).readlines()
- matched_row = 1
- clsPat = re.compile(r"\s*((public|private|protected)\s+)?"
- "((abstract|static|final|strictfp)\s+)?"
- "(class|interface)\s"+className+r"\b")
- for index,line in enumerate(lines):
- if clsPat.match(line):
- matched_row = index + 1
- break
-
- return str(matched_row)
- @staticmethod
- def searchAndEdit(current_file_name, className,memberName, mode="local",param_count=-1):
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- sourcePath = Talker.locateSource(className, classPathXml)
- if mode == "local" :
- editCmd="edit"
- elif mode == "buffer" :
- vim.command("split")
- elif mode == "tab" :
- vim.command("tabnew")
- if sourcePath != "None" :
- sourcePath, className = sourcePath.split("\n")
- if memberName.strip() != "" :
- matchedLine = EditUtil.searchMemeberLineNum(memberName, sourcePath,param_count)
- else :
- matchedLine = EditUtil.searchClassDefLineNum(className, sourcePath)
- vim.command("edit +%s %s" % (matchedLine, sourcePath ))
-
- return
- @staticmethod
- def tipMethodParameter():
- (row,col) = vim.current.window.cursor
- vim_buffer = vim.current.buffer
- line = vim_buffer[row-1]
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- tokenEndCol = line[0:col+1].rfind("(")
- if tokenEndCol < 0 : return
- newClassPat = r".*\bnew\s+(?P<name>[A-Z]\w+)$"
- searchResult = re.search(newClassPat, line[0:tokenEndCol])
- if searchResult :
- className = searchResult.group("name")
- classNameList = Parser.getFullClassNames(className)
- constructDefs = Talker.getConstructDefs(current_file_name,classNameList,classPathXml)
- if constructDefs == "" : return
- VimUtil.writeToSzToolBuffer("JdeConsole",constructDefs)
- return
- dotExpParser = Parser.getJavaDotExpParser()
- expTokens = dotExpParser.searchString(line[0:tokenEndCol])[0]
- if not expTokens : return
- varName = expTokens[0]
- endTokenIndex = 0 if len(expTokens)==1 else -1
- if len(expTokens) == 1 or (len(expTokens) == 3 and varName == "this"):
- if len(expTokens) ==1 :
- memberName = expTokens[0]
- else :
- memberName = expTokens[2]
- members = Parser.parseAllMemberInfo(vim_buffer)
- for name,mtype,rtntype,param,lineNum in members :
- if name == memberName :
- matched_row = lineNum
- vim.command("normal %sG" % str(matched_row))
- return
- else :
- expTokens = expTokens[1:]
- memberName = expTokens[-1]
- if varName[0].isupper():
- classname = varName
- elif varName == "this" :
- classname = "this"
- else :
- classname = Parser.getVarType(varName,row-1)
- superClass = Parser.getSuperClass()
- if not classname :
- if not superClass : return
- expTokens.insert(0,varName)
- classname = superClass
- classNameList = Parser.getFullClassNames(classname)
- expTokens = expTokens[:-1]
- params =(current_file_name,classNameList,classPathXml,expTokens,memberName)
- methodDefs = Talker.getMethodDefs(params)
- if methodDefs == "" :
- return
- VimUtil.writeToSzToolBuffer("JdeConsole",methodDefs)
-
- return
- @staticmethod
- def dumpClassInfo():
- classname = vim.eval("expand('<cword>')")
- if classname == "" : return
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- if not classPathXml : return
- classNameList = Parser.getFullClassNames(classname)
- result = Talker.dumpClass(classPathXml,classNameList)
- VimUtil.writeToSzToolBuffer("JdeConsole",result)
- @staticmethod
- def toggleBreakpoint():
- global bp_data
- file_name = vim.current.buffer.name
- (row,col) = vim.current.window.cursor
- bp_set = bp_data.get(file_name)
- if bp_set == None :
- bp_set = set()
- mainClassName = Parser.getMainClass()
- class_path_xml = ProjectManager.getClassPathXml(file_name)
- serverName = vim.eval("v:servername")
- bufnr=str(vim.eval("bufnr('%')"))
- if row in bp_set :
- cmdline = "breakpoint_remove %s %s" % (mainClassName,row)
- data = JdbTalker.submit(cmdline,class_path_xml,serverName)
- if data == "success" :
- bp_set.remove(row)
- HighlightManager.removeSign(file_name,row,"B")
- else :
- print "remove breakpoint error : msgs "+data
- else :
- cmdline = "breakpoint_add %s %s" % (mainClassName,row)
- data = JdbTalker.submit(cmdline,class_path_xml,serverName)
- if data == "success" :
- HighlightManager.addSign(file_name,row, "B")
- bp_set.add(row)
- bp_data[file_name] = bp_set
- else :
- print "can't create breakpoint here"
- @staticmethod
- def addConditionalBreakpoint(lineNum):
- global bp_data
- file_name = vim.current.buffer.name
- (row,col) = vim.current.window.cursor
- bp_set = bp_data.get(file_name)
- if bp_set == None :
- bp_set = set()
- HighlightManager.addSign(file_name,lineNum, "B")
- bp_set.add(row)
- bp_data[file_name] = bp_set
- @staticmethod
- def syncBreakpointInfo():
- global bp_data
- current_file = vim.current.buffer.name
- bp_set = bp_data.get(current_file)
- if bp_set :
- for row_num in bp_set :
- HighlightManager.removeSign(current_file,row_num,"B")
- source_file_path = vim.current.buffer.name
- serverName = vim.eval("v:servername")
- class_path_xml = ProjectManager.getClassPathXml(source_file_path)
- data = JdbTalker.submit("syncbps",class_path_xml,serverName)
- bp_data = {}
- if data :
- for line in data.split("\n"):
- if line.strip() == "" or line.find(" ") < 0 :
- continue
- class_name, line_num = line.split(" ")
- rlt_path = class_name.replace(".", os.path.sep)+".java"
- src_locs = ProjectManager.getSrcLocations(source_file_path)
- matched_file = None
- for src_loc in src_locs :
- abs_path = os.path.normpath(os.path.join(src_loc, rlt_path))
- if os.path.exists(abs_path) :
- matched_file = abs_path
- break
- if matched_file != None :
- bp_set = bp_data.get(matched_file)
- if bp_set == None :
- bp_set = set()
- bp_set.add(int(line_num))
- bp_data[matched_file] = bp_set
- if matched_file == vim.current.buffer.name :
- HighlightManager.addSign(matched_file,line_num,"B")
- @staticmethod
- def getTypeHierarchy():
- (row,col) = vim.current.window.cursor
- vim_buffer = vim.current.buffer
- line = vim_buffer[row-1]
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- if not classPathXml :
- return
- resultText = Talker.typeHierarchy(classPathXml,current_file_name)
- return resultText
- class HighlightManager(object):
- class Info(object):
- def __init__(self,msg,lstart,lend,hltype):
- self.msg = msg
- self.lstart = lstart
- self.lend = lend
- self.hltype = hltype
- def __str__(self):
- return "(%s,%s)" % (self.hltype,self.msg)
- def __repr__(self):
- return "(%s,%s)" % (self.hltype,self.msg)
- @staticmethod
- def removeHighlightType(removed_hltype):
- global all_hl_info
- all_hl_info = globals().get("all_hl_info")
- if all_hl_info == None :
- return
- empty_buf = []
- for file_path in all_hl_info :
- buf_hl_info = all_hl_info.get(file_path)
- empty_line =[]
- for line_num in buf_hl_info :
- infos = [info for info in buf_hl_info.get(line_num) if info.hltype != removed_hltype]
- if len(infos) > 0 :
- buf_hl_info[line_num] = infos
- else :
- empty_line.append(line_num)
- for line_num in empty_line:
- del buf_hl_info[line_num]
- if len(buf_hl_info) == 0 :
- empty_buf.append(file_path)
- for file_path in empty_buf :
- del all_hl_info[file_path]
- @staticmethod
- def addHighlightInfo(abs_path,lnum,hltype,msg="",lstart=-1,lend=-1):
- global all_hl_info
- lnum=int(lnum)
- all_hl_info = globals().get("all_hl_info")
- if all_hl_info == None :
- all_hl_info = {}
- buf_hl_info = all_hl_info.get(abs_path)
- if buf_hl_info == None :
- buf_hl_info = {}
- cur_hl_info = HighlightManager.Info(msg,lstart,lend,hltype)
- line_hl_info = buf_hl_info.get(lnum)
- if line_hl_info == None :
- line_hl_info = []
- line_hl_info.append(cur_hl_info)
- buf_hl_info[lnum] = line_hl_info
- all_hl_info[abs_path] = buf_hl_info
- @staticmethod
- def addSign(abs_path, lnum,hltype,bufnr=None):
- lnum=int(lnum)
- HighlightManager.addHighlightInfo(abs_path,lnum,hltype)
- if bufnr == None :
- bufnr=str(vim.eval("bufnr('%')"))
- global all_hl_info
- all_hl_info = globals().get("all_hl_info")
- buf_hl_info = all_hl_info.get(abs_path)
- line_hl_info = buf_hl_info.get(lnum)
- hltypes = "".join([info.hltype for info in line_hl_info])
- group = HighlightManager._getGroupName(hltypes)
- HighlightManager._signErrorGroup(lnum,group,bufnr)
- @staticmethod
- def removeSign(abs_path, lnum, hltype,bufnr = None):
- global all_hl_info
- lnum = int(lnum)
- all_hl_info = globals().get("all_hl_info")
- if all_hl_info == None :
- return
- buf_hl_info = all_hl_info.get(abs_path)
- if buf_hl_info == None :
- return
- line_hl_info = buf_hl_info.get(lnum)
- if line_hl_info == None :
- return
- line_hl_info = [info for info in line_hl_info if info.hltype != hltype]
- if bufnr == None :
- bufnr=str(vim.eval("bufnr('%')"))
- unsigncmd = "sign unplace %s buffer=%s" % (str(lnum),bufnr)
- unsigncmd = "sign unplace %s buffer=%s" % (str(lnum),bufnr)
- vim.command(unsigncmd)
- if len(line_hl_info) == 0 :
- del buf_hl_info[lnum]
- else :
- buf_hl_info[lnum] = line_hl_info
- hltypes = "".join([info.hltype for info in line_hl_info])
- group = HighlightManager._getGroupName(hltypes)
- signcmd=Template("sign place ${id} line=${lnum} name=${name} buffer=${nr}")
- signcmd =signcmd.substitute(id=lnum,lnum=lnum,name=group, nr=bufnr)
- vim.command(signcmd)
- @staticmethod
- def displayMsg():
- global all_hl_info
- all_hl_info = globals().get("all_hl_info")
- if all_hl_info == None :
- return
- vim_buffer = vim.current.buffer
- buf_hl_info = all_hl_info.get(vim_buffer.name)
- if buf_hl_info == None :
- return
- (row,col) = vim.current.window.cursor
- if buf_hl_info.get(row) != None :
- fist_hl_info = buf_hl_info.get(row)[0]
- msg = fist_hl_info.msg
- vim.command("call DisplayMsg('%s')" % msg)
- else :
- vim.command("call DisplayMsg('%s')" % "")
- @staticmethod
- def highlightCurrentBuf():
- global all_hl_info
- HighlightManager._clearHighlightInVim()
- all_hl_info = globals().get("all_hl_info")
- if all_hl_info == None :
- return
- file_name = vim.current.buffer.name
- buf_hl_info = all_hl_info.get(file_name)
- if buf_hl_info == None :
- return
- bufnr=str(vim.eval("bufnr('%')"))
- for lnum in buf_hl_info :
- hltypes =[]
- for info in buf_hl_info.get(lnum) :
- group = HighlightManager._getGroupName(info.hltype)
- HighlightManager._highlightErrorGroup(lnum,info.lstart,info.lend,group)
- hltypes.append(info.hltype)
- group = HighlightManager._getGroupName("".join(hltypes))
- HighlightManager._signErrorGroup(lnum,group,bufnr)
- @staticmethod
- def _getGroupName(highlightType):
- infos = {"W":"SzjdeWarning","E":"SzjdeError","R":"SzjdeReference",\
- "B":"SzjdeBreakPoint","S":"SuspendLine", "SB":"SuspendLineBP"}
- if "S" in highlightType and "B" in highlightType :
- group = "SuspendLineBP"
- elif "S" in highlightType:
- group = "SuspendLine"
- elif "B" in highlightType:
- group = "SzjdeBreakPoint"
- elif "E" in highlightType:
- group = "SzjdeError"
- elif "W" in highlightType :
- group = "SzjdeWarning"
- elif "R" in highlightType:
- group = "SzjdeReference"
- return group
- @staticmethod
- def _highlightErrorGroup(errorRow,start,end,group):
- errorRow,start,end = int(errorRow), int(start), int(end)
- if start < 0 or end < 0 or errorRow < 0 :
- return
- vim_buffer = vim.current.buffer
- charCount = 0
- fileformat = vim.eval("&fileformat")
- newLineCount =1
- if fileformat == "dos" :
- newLineCount = 2
- if group=="SzjdeError" or group == "SzjdeWarning" :
- for row in vim_buffer[0:errorRow-1] :
- charCount += len(unicode(row)) + newLineCount
- rowStart = 0 if start - charCount < 0 else start - charCount
- #TODO : shit! where does that magic number come from ? don't fucing rember.
- rowEnd = end - charCount + 3
- if rowEnd < 0 :
- rowEnd = rowStart + len(unicode(vim_buffer[errorRow]))
- else :
- rowStart = start
- rowEnd = end
- syncmd = """syn match %s "\%%%sl\%%>%sc.\%%<%sc" """ %(group, errorRow, rowStart, rowEnd)
- vim.command(syncmd)
- @staticmethod
- def _signErrorGroup(errorRow,group,bufnr):
- signcmd=Template("sign place ${id} line=${lnum} name=${name} buffer=${nr}")
- signcmd =signcmd.substitute(id=errorRow,lnum=errorRow,name=group, nr=bufnr)
- vim.command(signcmd)
- @staticmethod
- def initBreakpointSign():
- HighlightManager.highlightCurrentBuf()
- return
-
- @staticmethod
- def _clearHighlightInVim():
- vim.command("syntax clear SzjdeError")
- vim.command("syntax clear SzjdeWarning")
- vim.command("syntax clear SzjdeReference")
- pat = re.compile(r".*id=(?P<name>\d+)\b.*$")
- bufnr=str(vim.eval("bufnr('%')"))
- vim.command("redir => g:current_signplace")
- vim.command("silent sign place buffer=%s" % bufnr )
- vim.command("redir END")
- output = vim.eval("g:current_signplace")
- lines = output.split("\n")
- for line in lines :
- sign_ids = pat.findall(line)
- if (len(sign_ids) == 1 ) :
- vim.command("silent sign unplace %s buffer=%s" % (sign_ids[0], bufnr ))
-
- class Compiler(object):
- @staticmethod
- def relpath(path):
- if path.startswith(os.getcwd()) :
- return os.path.relpath(path)
- else :
- return path
- @staticmethod
- def compileCurrentFile(buildProject = False):
- (row,col) = vim.current.window.cursor
- vim_buffer = vim.current.buffer
- line = vim_buffer[row-1]
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- if not classPathXml :
- return
- need_compiled = False
- src_locs = ProjectManager.getSrcLocations(current_file_name)
- for abs_src in src_locs :
- if current_file_name.startswith(abs_src) :
- need_compiled = True
- break
- if not need_compiled :
- return
- if buildProject :
- current_file_name = "All"
- print "build project can take a while, please wait....."
- resultText = Talker.compileFile(classPathXml,current_file_name)
- allsrcFiles, errorMsgList = resultText.split("$$$$$")
- allsrcFiles = allsrcFiles.split("\n")
- errorMsgList = errorMsgList.split("\n")
- qflist = []
- #clear highlight type E and W
- HighlightManager.removeHighlightType("E")
- HighlightManager.removeHighlightType("W")
- error_files = []
- for line in errorMsgList:
- if line.strip() == "" : continue
- try :
- errorType,filename,lnum,text,lstart,lend = line.split("::")
- if errorType == "E" :
- error_files.append(filename)
- bufnr=str(vim.eval("bufnr('%')"))
- absname = os.path.normpath(filename)
- filename = Compiler.relpath(absname)
- if filename != Compiler.relpath(current_file_name) :
- qfitem = dict(filename=filename,lnum=lnum,text=text,type=errorType)
- else :
- qfitem = dict(bufnr=bufnr,lnum=lnum,text=text,type=errorType)
- HighlightManager.addHighlightInfo(absname,lnum,errorType,text,lstart,lend)
- qflist.append(qfitem)
- except Exception , e:
- fp = StringIO.StringIO()
- traceback.print_exc(file=fp)
- message = fp.getvalue()
- logging.debug(message)
- EditUtil.syncBreakpointInfo()
- HighlightManager.highlightCurrentBuf()
- pathflags =[(filename.replace("\n",""),False) for filename in allsrcFiles]
- pathflags.extend([(filename,True) for filename in error_files])
- Compiler.set_error_flags(pathflags)
- vim.command("call setqflist(%s)" % qflist)
- if len(error_files) > 0 :
- vim.command("cwindow")
- else :
- vim.command("cclose")
- @staticmethod
- def set_error_flags(pathflags):
- if "projectTree" not in globals() :
- return
-
- for path,flag in pathflags :
- node = projectTree.find_node(path)
- if node != None :
- node.set_error_flag(flag)
- if not VimUtil.isSzToolBufferVisible('ProjectTree'):
- return
- vim.command("call SwitchToSzToolView('ProjectTree')" )
- (row,col) = vim.current.window.cursor
- projectTree.render_tree()
- vim.current.window.cursor = (row,col)
- vim.command("exec 'wincmd w'")
- @staticmethod
- def copyResource():
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- if not current_file_name : return
- if current_file_name.endswith(".java") : return
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- if not classPathXml : return
- resultText = Talker.copyResource(classPathXml,current_file_name)
- VimUtil.writeToSzToolBuffer("JdeConsole",resultText)
- class Runner(object):
- @staticmethod
- def runCurrentFile(runCmd="run"):
- (row,col) = vim.current.window.cursor
- vim_buffer = vim.current.buffer
- line = vim_buffer[row-1]
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- if not classPathXml : return
- serverName = vim.eval("v:servername")
- resultText = Talker.runFile(classPathXml,current_file_name,serverName,"JdeConsole",runCmd)
- VimUtil.writeToSzToolBuffer("JdeConsole",resultText)
- @staticmethod
- def runAntBuild(target=None):
- serverName = vim.eval("v:servername")
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- antBuildXml = ProjectManager.getAntBuildXml(current_file_name)
- if antBuildXml :
- cmdName = "ant -file "+antBuildXml
- runInShell = "false"
- if os.name == "nt" :
- runInShell = "true"
- resultText = Talker.runAntBuild(serverName,cmdName,runInShell)
- else :
- print "can't find the build.xml."
- @staticmethod
- def fetchResult(guid):
- resultText = BasicTalker.fetchResult(guid)
- lines = resultText.split("\n")
- VimUtil.writeToSzToolBuffer("JdeConsole",lines)
- class AutoImport(object):
- @staticmethod
- def getImportInsertLocation():
- vim_buffer = vim.current.buffer
- pkgLocation = 0
- impLocation = -1
- for index,line in enumerate(vim_buffer):
- if line.strip().startswith("import "):
- impLocation = index
- break
- if impLocation > -1 :
- return impLocation
- for index,line in enumerate(vim_buffer):
- if line.strip().startswith("package") :
- pkgLocation = index + 1
- break
- return pkgLocation
- @staticmethod
- def addImportDef(line):
- if line.strip() == "" : return
- vim_buffer_text ="\n".join(vim.current.buffer)
- tmpDefs = line[:-1].split(";")
- hadImported = False
- for tmpDef in tmpDefs :
- pat = r"import\s+%s\b|import\s+%s" % (tmpDef, tmpDef[0:tmpDef.rfind(".")]+"\.\*")
- if re.search(pat,vim_buffer_text) :
- hadImported = True
- break
- if not hadImported :
- location = AutoImport.getImportInsertLocation()
- if ( len(tmpDefs) == 1 ) :
- insertText = "import %s;" % tmpDefs[0]
- vim.command("call append(%s,'%s')" %(str(location),insertText))
- else :
- selectedIndex = VimUtil.inputOption(tmpDefs)
- if (selectedIndex) :
- insertText = "import %s;" % tmpDefs[int(selectedIndex)]
- vim.command("call append(%s,'%s')" %(str(location),insertText))
- @staticmethod
- def autoImportVar():
- AutoImport.removeUnusedImport()
- vim_buffer = vim.current.buffer
- current_file_name = vim_buffer.name
- classPathXml = ProjectManager.getClassPathXml(current_file_name)
- if not classPathXml : return
- currentPackage = Parser.getPackage()
- if not currentPackage :
- currentPackage = ""
- searchText = "\n".join(vim_buffer)
- #remove comments
- commentPat = re.compile(r"(\/\*.*?\*\/)|((\/\/.*?)(?=\n))", re.DOTALL)
- searchText = commentPat.sub("",searchText)
- strliteralPat = re.compile(r'"(\\"|[^"])*"')
- searchText = strliteralPat.sub("",searchText)
- dclClassNamePat = re.compile(r"(?<=class)\b\W\w+\b")
- dclClassNames =[item.strip() for item in dclClassNamePat.findall(searchText)]
- # upercase words except preceded by "."
- classNamePat = re.compile(r"\b(?<!\.)[A-Z]\w+\b")
- var_type_set=set(classNamePat.findall(searchText))
- for clsName in dclClassNames :
- var_type_set.discard(clsName)
- varNames=",".join(var_type_set)
- resultText = Talker.autoImport(classPathXml,varNames,currentPackage)
- lines = resultText.split("\n")
- for line in lines :
- AutoImport.addImportDef(line)
- location = AutoImport.getImportInsertLocation()
- if location > 0 and vim_buffer[location-1].strip() != "" :
- vim.command("call append(%s,'')" %(str(location)))
- @staticmethod
- def removeUnusedImport():
- vim_buffer = vim.current.buffer
- rowIndex = 0
- while True :
- line = vim_buffer[rowIndex].strip()
- if line.startswith("import") :
- lastName = line[7:-1].split(".")[-1]
- if lastName == "*" :
- del vim_buffer[rowIndex]
- continue
- groups = re.findall(r"\b%s\b" % lastName, "\n".join(vim_buffer))
- if len(groups) <= 1 :
- del vim_buffer[rowIndex]
- continue
- rowIndex += 1
- if rowIndex > len(vim_buffer) -1 :
- break
- class Parser(object):
- @staticmethod
- def parseClassEnd():
- vim_buffer = vim.current.buffer
- row_count = len(vim_buffer)
- end_line = 0
- for lineNum in range(row_count-1,-1,-1):
- line = vim_buffer[lineNum]
- if line.endswith("}") :
- end_line = lineNum
- break
- return end_line
- @staticmethod
- def parseCurrentMethod():
- (row,col) = vim.current.window.cursor
- vim_buffer = vim.current.buffer
- fullDeclLine = vim_buffer[row-1]
- methodPat = re.compile(r"(?P<rtntype>[\w<>,]+)\s+(?P<name>\w+)\s*\((?P<param>.*)\)")
- if "(" in fullDeclLine :
- startLine = row
- while True :
- if ")" in fullDeclLine :
- break
- fullDeclLine = fullDeclLine +vim_buffer[startLine].replace("\t"," ")
- startLine = startLine + 1
- result = methodPat.search(fullDeclLine)
- if result == None : return None
- name = result.group("name")
- rtntype = result.group("rtntype")
- param = result.group("param")
- return rtntype,name,param
- @staticmethod
- def parseCurrentMethodName():
- result = Parser.parseCurrentMethod()
- if result == None :
- return "", None
- rtntype,name,param = result
- return name,param
- @staticmethod
- def parseAllMemberInfo(lines):
- memberInfo = []
- scopeCount = 0
- methodPat = re.compile(r"(?P<rtntype>[\w<>\[\],]+)\s+(?P<name>\w+)\s*\((?P<param>.*)\)")
- assignPat = re.compile("(?P<rtntype>[\w<>\[\],]+)\s+(?P<name>\w+)\s*=")
- defPat = re.compile("(?P<rtntype>[\w<>\[\],]+)\s+(?P<name>\w+)\s*;")
- commentLine = False
- for lineNum,line in enumerate(lines) :
- line = line.strip()
- if (line.startswith("/*") and not line.endswith("*/") ) :
- commentLine = True
- continue
- if line.endswith("*/"):
- commentLine = False
- continue
- if commentLine == True or line.startswith("//"):
- continue
- if scopeCount == 1 :
- fullDeclLine = line
- if "=" in line :
- pat = assignPat
- mtype = "field"
- elif "(" in line :
- startLine = lineNum + 1
- while True :
- if ")" in fullDeclLine or startLine >= len(lines) :
- …
Large files files are truncated, but you can click here to view the full file