Advertisement
Abaduaber

SublimeText pascal functions autocomplete

Apr 13th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.64 KB | None | 0 0
  1. import sublime, sublime_plugin, os, re, string
  2. from os.path import basename
  3.  
  4. class pasfunc_GiveVerboseFunc(sublime_plugin.EventListener):
  5.     def on_post_save(self, view):
  6.         funcs = []
  7.         ids = []
  8.         open_folder_arr = view.window().views()
  9.         for pasfile in open_folder_arr:
  10.             tex = pasfile.file_name().lower()
  11.             if tex.find(".pas") != -1:
  12.                 file_lines = open(tex, "rU")
  13.                 isimp = False
  14.                 for line in file_lines:
  15.                     templine = line.lower()
  16.                     if not isimp:
  17.                         isimp = (templine.find("implementation") > -1)
  18.                         continue
  19.                     if (((templine.find("function") != -1) \
  20.                     or (templine.find("procedure") != -1) \
  21.                     or (templine.find("constructor") != -1) \
  22.                     or (templine.find("destructor") != -1)) \
  23.                     and ((templine.find("=") == -1) \
  24.                     and (templine.find("forward") == -1))):
  25.                         funcs.append(os.path.split(pasfile.file_name())[1] + ": " + line.strip())
  26.                     words = re.findall('[_A-Za-z][_a-zA-Z0-9]*', line)
  27.                     fn = os.path.split(pasfile.file_name())[1]
  28.                     for word in words:
  29.                         if (len(word) >= 3):
  30.                             if not word in funcs:
  31.                                 w = word.strip()
  32.                                 if not w in ids:
  33.                                     ids.append(w)
  34.                             #w = fn + ": " + word.strip()
  35.                             #if not w in funcs:
  36.                             #    if not w in ids:
  37.                             #        ids.append(w)
  38.                         #if (len(word) >= 3) and not (word in funcs) and not (word in ids):
  39.                             #ids.append(os.path.split(pasfile.file_name())[1] + ": " + word.strip())
  40.  
  41.         try:
  42.             from win32com.shell import shellcon, shell
  43.             homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
  44.         except ImportError: # quick semi-nasty fallback for non-windows/win32com case
  45.             homedir = os.path.expanduser("~")
  46.         sugfile = homedir + "\\AppData\\Roaming\\Sublime Text 3\\Packages\\User\\" + \
  47.         "Pascal.sublime-completions"
  48.         f = open(sugfile, "w")
  49.         f.writelines("{\n")
  50.         f.writelines('    "scope": "source.pascal",\n')
  51.         f.writelines('    "completions":\n    [\n')
  52.         for func in funcs:
  53.             f.writelines('        { "trigger": "')
  54.             index = 0
  55.             for word in func.split():
  56.                 if index == 1:
  57.                     f.writelines(word[0:1] + ' ')
  58.                 else:
  59.                     f.writelines(word + ' ')
  60.                 index = index + 1
  61.             f.writelines('", "contents": "')
  62.             if len(func.split('.')) > 2: #Если в списке больше двух частей
  63.                 f.writelines(func.split('.')[2][0:len(func)])
  64.             else:
  65.                 for i in range(2, 100):
  66.                     try:
  67.                         f.writelines(func.split()[i][0:1000] + ' ')
  68.                     except IndexError:
  69.                         break
  70.                # f.writelines(func.split()[2][0:len(func)])
  71.             f.writelines('" },\n')
  72.         for i in ids:
  73.             f.writelines('        { "trigger": "')
  74.             index = 0
  75.             f.writelines(i)
  76.             f.writelines('", "contents": "')
  77.             f.writelines(i)
  78.             f.writelines('" },\n')
  79.         f.writelines('    ]\n}')
  80.         f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement