Mon, 05 May 2025 10:17:49 +0200
Moved the configuration of including global environments in the pip packages widget from the pip configuration page to that widget (see issue 586).
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
3 | # Copyright (c) 2007 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
10199
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
7 | Package implementing typing completers for the various supported programming languages. |
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
10199
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
10 | import collections |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
11 | import contextlib |
9493
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
12 | import importlib |
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
13 | |
10199
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
14 | # Typing Completer Registry Item |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
15 | # Each item contains two function references. |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
16 | # createCompleter: A function that must accept two arguments, a reference to the editor |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
17 | # and a reference to the parent object. It must return an instantiated |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
18 | # typing completer object. |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
19 | # createConfigPage: A function that must return a fully populated configuration widget |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
20 | # to be added to the Editor / Typing configuration page. This widget |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
21 | # must have a method "save" to save the entered values. |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
22 | CompleterRegistryItem = collections.namedtuple( |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
23 | "CompleterRegistryItem", ["createCompleter", "createConfigPage"] |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
24 | ) |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
25 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
26 | # The Typing Completer Registry |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
27 | # Dictionary with the language name as key. Each entry contains a reference to a |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
28 | # 'CompleterRegistryItem' object. |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
29 | CompleterRegistry = {} |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
30 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
31 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
32 | def registerCompleter(language, createCompleterFunction, createConfigPageFunction): |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
33 | """ |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
34 | Function to register a typing completer for a lexer language. |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
35 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
36 | @param language lexer language of the typing completer |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
37 | @type str |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
38 | @param createCompleterFunction reference to a function to instantiate a |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
39 | typing completer object |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
40 | @type function |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
41 | @param createConfigPageFunction reference to a function returning a ready |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
42 | populated configuration widget |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
43 | @type function |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
44 | @exception KeyError raised when the given name is already in use |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
45 | """ |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
46 | global CompleterRegistry |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
47 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
48 | if language in CompleterRegistry: |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
49 | raise KeyError('Typing completer "{0}" already registered.'.format(language)) |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
50 | else: |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
51 | CompleterRegistry[language] = CompleterRegistryItem( |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
52 | createCompleter=createCompleterFunction, |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
53 | createConfigPage=createConfigPageFunction, |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
54 | ) |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
55 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
56 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
57 | def unregisterTypingCompleter(language): |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
58 | """ |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
59 | Function to unregister a previously registered typing completer. |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
60 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
61 | @param language lexer language of the typing completer |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
62 | @type str |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
63 | """ |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
64 | global CompleterRegistry |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
65 | |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
66 | with contextlib.suppress(KeyError): |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
67 | del CompleterRegistry[language] |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
68 | |
945
8cd4d08fa9f6
Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
802
diff
changeset
|
69 | |
8cd4d08fa9f6
Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
802
diff
changeset
|
70 | def getCompleter(language, editor, parent=None): |
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71 | """ |
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | Module function to instantiate a lexer object for a given language. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | |
10199
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
74 | @param language language of the lexer |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
75 | @type str |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
76 | @param editor reference to the editor object |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
77 | @type QScintilla.Editor |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
78 | @param parent reference to the parent object (defaults to None) |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
79 | @type QObject (optional) |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
80 | @return reference to the instantiated typing completer object |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
81 | @rtype CompleterBase |
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | """ |
9493
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
83 | languageCompleterMapping = { |
9497
8beca4047c53
Modified most of the importlib.import_module() calls to use relative imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9493
diff
changeset
|
84 | "Python": ".CompleterPython", |
8beca4047c53
Modified most of the importlib.import_module() calls to use relative imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9493
diff
changeset
|
85 | "Python3": ".CompleterPython", |
8beca4047c53
Modified most of the importlib.import_module() calls to use relative imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9493
diff
changeset
|
86 | "MicroPython": ".CompleterPython", |
8beca4047c53
Modified most of the importlib.import_module() calls to use relative imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9493
diff
changeset
|
87 | "Cython": ".CompleterPython", |
8beca4047c53
Modified most of the importlib.import_module() calls to use relative imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9493
diff
changeset
|
88 | "Ruby": ".CompleterRuby", |
8beca4047c53
Modified most of the importlib.import_module() calls to use relative imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9493
diff
changeset
|
89 | "YAML": ".CompleterYaml", |
10201 | 90 | "Pygments|TOML": ".CompleterToml", |
9493
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
91 | } |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
92 | |
9493
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
93 | if language in languageCompleterMapping: |
9497
8beca4047c53
Modified most of the importlib.import_module() calls to use relative imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9493
diff
changeset
|
94 | mod = importlib.import_module(languageCompleterMapping[language], __package__) |
9493
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
95 | if mod: |
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
96 | return mod.createCompleter(editor, parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
97 | |
10199
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
98 | elif language in CompleterRegistry: |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
99 | return CompleterRegistry[language].createCompleter(editor, parent) |
2cd5ff8e0e0f
Extended the Typing Completers infrastructure to allow the addition of completers via plugins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
100 | |
9493
aeaa6bf49831
Changed typing completer imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
101 | return None |