Dtag Color

Embed Size (px)

DESCRIPTION

some sort of text thing

Citation preview

#! /usr/bin/env pythonfrom sys import argvimport popen2import xml.etree.ElementTree as etreeimport vimimport globimport osimport randomCLASSCOLORS = ["#11dd44 gui=bold"]STRUCTCOLORS = ["#dd1144 gui=bold"]VARIABLECOLORS = ['#F7977A', '#F9AD81', '#FDC68A', '#FFF79A', '#C4DF9B', '#A2D39C', '#82CA9D', '#7BCDC8', '#6ECFF6', '#7EA7D8', '#8493CA', '#8882BE', '#A187BE', '#BC8DBF', '#F49AC2', '#F6989D', '#F26C4F', '#F68E55', '#FBAF5C', '#FFF467', '#ACD372', '#7CC576', '#3BB878', '#1ABBB4', '#00BFF3', '#438CCA', '#5574B9', '#605CA8', '#855FA8', '#A763A8', '#F06EA9', '#F26D7D']FUNCTIONCOLORS = ['#9E0B0F', '#A0410D', '#A36209', '#ABA000', '#598527', '#1A7B30', '#007236', '#00746B', '#0076A3', '#004B80', '#003471', '#1B1464', '#440E62', '#630460', '#9E005D', '#9E0039', '#790000', '#7B2E00', '#7D4900', '#827B00', '#406618', '#005E20', '#005826', '#005952', '#005B7F', '#003663', '#002157', '#0D004C', '#32004B', '#4B0049', '#7B0046', '#7A0026']MODULEDIRS = [".", "./src/", "/opt/dmd2/src/phobos/", "/opt/dmd2/src/druntime/import/"] + glob.glob(os.environ["HOME"] + "/.dub/packages/*")#LOCALDIRS = [".", "./src/"]#MODULEDIRS = glob.glob(os.environ["HOME"] + "/.dub/packages/*/source/")#shuffleif False: random.shuffle(VARIABLECOLORS) random.shuffle(FUNCTIONCOLORS)def findstructs(xmltree): out = set() c = xmltree.findall(".//structDeclaration/name") for i in c: out.add(i.text) return list(out)def findclasses(xmltree): out = set() c = xmltree.findall(".//classDeclaration/name") c += xmltree.findall(".//interfaceDeclaration/name") for i in c: out.add(i.text) return list(out)def findvariables(xmltree): out = set() c = xmltree.findall(".//variableDeclaration/declarator/name") c += xmltree.findall(".//autoDeclaration/item/name") for i in c: out.add(i.text) return list(out)def findfunctions(xmltree): out = set() #members = set(xmltree.findall(".//classDeclaration//functionDeclaration/name")) #funcs = set(xmltree.findall(".//functionDeclaration/name")) c = set(xmltree.findall(".//functionDeclaration/name")) #c = funcs - members for i in c: out.add(i.text) return list(out)def findmodules(xmltree): out = set() c = xmltree.findall(".//identifierChain") for i in c: parts = i.getchildren() mod = [] for p in parts: mod.append(p.text) out.add('/'.join(mod)) return list(out)def findmembers(xmltree): out = [] z = xmltree.findall(".//classDeclaration") for a in z: cname = a.findall("name")[0].text for b in a.findall("structBody"): for c in b.findall("declaration"): for d in c.findall("functionDeclaration"): for e in d.findall("name"): out.append([cname, e.text]) return outdef findmethodcalls(xmltree): out = [] a = xmltree.findall(".//functionCallExpression") for b in a: var = "" method = "" for c in b.findall("unaryExpression"): for d in c.findall("unaryExpression"): for e in d.findall("primaryExpression"): for f in e.findall("identifierOrTemplateInstance"): for g in f.findall("identifier"): var = g.text for d in c.findall("identifierOrTemplateInstance"): for e in d.findall("identifier"): method = e.text if var != '' and method != '': out.append([var, method]) return outdef searchmodules(xmltree): modules = findmodules(xmltree); modfiles = set() for i in modules: for p in MODULEDIRS: path = "%s/%s.d" % (p, i) if (os.path.exists(path)): modfiles.add(path) for i in modfiles: tagfile(i, True)def getcolor(colors, name): value = len(name) for c in name: i = ord(c) if i % 7 == 0: value += ord(c) if i % 7 == 1: value -= ord(c) if i % 7 == 2: value *= ord(c) if i % 7 == 3: value /= ord(c) if i % 7 == 4: value += ord(c)**2 if i % 7 == 5: value **= 2 return colors[value%len(colors)]def dovimstuff(prefix, tokens, colors): for index, var in enumerate(tokens): #vim.command("hi clear %s_%s" % (prefix, var)) #vim.command("syn clear %s_%s" % (prefix, var)) vim.command("syn keyword %s_%s %s"% (prefix, var, var)) #vim.command("hi %s_%s guifg=%s" % (prefix, var, colors[index%len(colors)])) vim.command("hi %s_%s guifg=%s" % (prefix, var, getcolor(colors, var)))def tagfile(path, inmodule = False): xmldata = popen2.popen2("dscanner --ast " + path)[0].read() tag(xmldata, inmodule)def tagdata(data, inmodule = False): #xmldata = popen2.popen2("dscanner --ast " + path)[0].read() #print path #print dir(path) ast = popen2.popen2("dscanner --ast") ast[1].write(data) ast[1].close() xmldata = ast[0].read() tag(xmldata)def tag(xmldata, inmodule = False): #print xmldata #try: xmltree = etree.fromstring(xmldata) #except etree.ParseError, e: # return #a = findmembers(xmltree) #for c, func in a: # pass #a = findmethodcalls(xmltree) #for var, func in a: # pass #return #if not inmodule: # searchmodules(xmltree) #vim.command("syn off | syn on") vim.command("syn reset") #vim.command("colorscheme slate") dovimstuff("class", findclasses(xmltree), CLASSCOLORS) dovimstuff("struct", findstructs(xmltree), STRUCTCOLORS) dovimstuff("var", findvariables(xmltree), VARIABLECOLORS) dovimstuff("func", findfunctions(xmltree), FUNCTIONCOLORS)def dtagcolor(): tagdata('\n'.join(vim.current.buffer[:])) #tagfile(argv[1])dtagcolor()