Debian Codegraph @

lib/autocomplete.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2007-2008 Lukáš Lalinský <[email protected]om>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 import re
21
22 # Based on my old commit code and http://tortoisesvn.tigris.org/svn/tortoisesvn/trunk/src/TortoiseSVNSetup/include/autolist.txt
23 autolist = [
24 (r'^\s*(?:class|typedef|struct|union|namespace)\s+([\w_]+)|\b([\w_]+)\s*\(',
25 ('.h', '.hh', '.hpp', '.hxx')),
26 (r'(?<=typedef)\s+(?:struct|union)\s+([\w_]+)',
27 ('.h', '.hh', '.hpp', '.hxx', '.c')),
28 (r'\b(([\w_]+)::([\w_]+))|\b([\w_]+)\s*\(',
29 ('.cpp', '.cc', '.c', '.cxx')),
30 (r'(?:(?:prototype\.|this\.)(\w+)\s*=\s*)?function\s*(?:(\w*)\s*)\(',
31 ('.js',)),
32 (r'(\w+)\s+=\s+(?:class|record|interface)|(?:procedure|function|property|constructor)\s+(\w+)',
33 ('.pas',)),
34 (r'^\s*(?:class|def)\s+(\w+)',
35 ('.py', '.pyw', '.rb')),
36 (r'(\w+)\s*=\s*',
37 ('.py', '.pyw')),
38 (r'^\s*sub\s+(\w+)|\s*(?:package|use)\s+([\w\:]+)',
39 ('.pl', '.pm')),
40 (r'^\s*class\s+(\w+)|^\s*(?:(?:public|private|)\s+function)\s+(\w+)|::(\w+)|->(\w+)',
41 ('.php',)),
42 (r'(?:class|function|sub)\s+(\w+)(?:\s*(?:[\(\']|$))',
43 ('.vb', '.vb6')),
44 (r'^\s*(?:(?:Public (?:Default )?|Private )?(?:Sub|Function|Property Get|Property Set|Property Let) ?)(\w+)\(|^Attribute VB_Name = "(\w+)"',
45 ('.bas', '.frm', '.cls')),
46 (r'(?:public|protected|private|internal)\s+(?:[\w\d_\.]+\s+)*([\w\d_\.]+)',
47 ('.cs', '.asp', '.aspx')),
48 (r'class\s+([\w_]+)(?:(?:\s+)?(?:extends|implements)(?:\s+)?([\w_]+)?)',
49 ('.java',)),
50 (r'(?:public|protected|private|internal)\s+(?:[\w\d_\.]+\s+)*([\w\d_\.]+)',
51 ('.java',)),
52 (r'^.{6} ([A-Z][A-Z0-9-]*)(?: SECTION)?\.',
53 ('.cbl', '.cpy')),
54 (r'Func\s+([\w_]+)|\$([\w_]+)',
55 ('.au3', '.auh')),
56 (r'^([\w]+) *',
57 ('.asm',)),
58 ]
59
60 _autolist_map = {}
61
62
63 class AutocompleteWordListBuilder(object):
64
65 def __init__(self):
66 self.regexes = []
67 self.compiled_regexes = None
68
69 def compile_regexes(self):
70 if self.compiled_regexes is None:
71 self.compiled_regexes = list(map(re.compile, self.regexes))
72
73 def iter_words(self, file):
74 self.compile_regexes()
75 for line in file:
76 for regex in self.compiled_regexes:
77 for result in regex.findall(line):
78 if not isinstance(result, tuple):
79 result = (result,)
80 for word in [_f for _f in result if _f]:
81 yield word
82
83
84 def get_wordlist_builder(ext):
85 if not _autolist_map:
86 for regex, extensions in autolist:
87 for extension in extensions:
88 if extension not in _autolist_map:
89 _autolist_map[extension] = AutocompleteWordListBuilder()
90 _autolist_map[extension].regexes.append(regex)
91 return _autolist_map.get(ext)