RefactoringRope/CodeAssist.py

Sun, 22 Mar 2015 17:33:44 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 22 Mar 2015 17:33:44 +0100
changeset 118
d242ba11a04c
parent 106
b2b2107b8047
child 119
a03f2be1997b
permissions
-rw-r--r--

Fixed a few code style issues.

100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2008 - 2015 Detlev Offenbach <detlev@die-offenbachs.de>
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the autocompletion interface to rope.
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
101
5098ad8960ed Fixed issues connecting/disconnecting editors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 100
diff changeset
10 from __future__ import unicode_literals
5098ad8960ed Fixed issues connecting/disconnecting editors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 100
diff changeset
11
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import sys
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 sys.path.insert(0, os.path.dirname(__file__))
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 if sys.version_info[0] >= 3:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 path = os.path.join(os.path.dirname(__file__), 'rope_py3')
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 else:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 path = os.path.join(os.path.dirname(__file__), 'rope_py2')
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 str = unicode # __IGNORE_WARNING__
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 sys.path.insert(0, path)
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 import rope.base.libutils
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 import rope.contrib.codeassist
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 from PyQt5.QtCore import QObject
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
28 import Globals
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
29
118
d242ba11a04c Fixed a few code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 106
diff changeset
30
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 class CodeAssist(QObject):
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 Class implementing the autocompletion interface to rope.
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 """
118
d242ba11a04c Fixed a few code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 106
diff changeset
35 def __init__(self, plugin, parent=None):
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 Constructor
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 @param plugin reference to the plugin object
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 @param parent parent (QObject)
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 """
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 QObject.__init__(self, parent)
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.__plugin = plugin
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
45
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
46 self.__project = rope.base.project.Project(Globals.getConfigDir())
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
47 self.__project.validate(self.__project.root)
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
49 def getCompletions(self, editor):
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 """
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 Public method to calculate the possible completions.
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
53 @param editor reference to the editor object, that called this method
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
54 QScintilla.Editor)
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 @return list of proposals (QStringList)
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 """
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
57 filename = editor.getFileName()
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 if filename:
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
59 resource = rope.base.libutils.path_to_resource(
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
60 self.__project, filename)
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 else:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 resource = None
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
63 line, index = editor.getCursorPosition()
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
64 source = editor.text()
103
2d77b3afc98f Changed the way the offset into the text gets calculated because rope handles unicode characters as one character (i.e. string) and QScintilla as multiple (i.e. bytes).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 101
diff changeset
65 offset = len("".join(source.splitlines(True)[:line])) + index
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 maxfixes = self.__plugin.getPreferences("MaxFixes")
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 try:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 proposals = rope.contrib.codeassist.code_assist(
118
d242ba11a04c Fixed a few code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 106
diff changeset
69 self.__project, source, offset, resource, maxfixes=maxfixes)
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 proposals = rope.contrib.codeassist.sorted_proposals(proposals)
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 names = [proposal.name for proposal in proposals]
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 return names
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 except Exception:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 return []
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
76 def getCallTips(self, pos, editor):
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 """
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 Public method to calculate calltips.
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
118
d242ba11a04c Fixed a few code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 106
diff changeset
80 @param pos position in the text for the calltip (integer)
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
81 @param editor reference to the editor object, that called this method
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
82 QScintilla.Editor)
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 @return list of possible calltips (list of strings)
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 """
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
85 filename = editor.getFileName()
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 if filename:
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
87 resource = rope.base.libutils.path_to_resource(
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
88 self.__project, filename)
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 else:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 resource = None
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
91 source = editor.text()
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes")
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 try:
105
45872a13d197 Refined the offset calculation for call tips.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 104
diff changeset
94 line, index = editor.lineIndexFromPosition(pos)
45872a13d197 Refined the offset calculation for call tips.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 104
diff changeset
95 offset = len("".join(source.splitlines(True)[:line])) + index
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 cts = rope.contrib.codeassist.get_calltip(
118
d242ba11a04c Fixed a few code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 106
diff changeset
97 self.__project, source, offset, resource, maxfixes=maxfixes,
d242ba11a04c Fixed a few code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 106
diff changeset
98 remove_self=True)
100
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 if cts is not None:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 cts = [cts]
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 else:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 cts = []
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 except Exception:
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 cts = []
2bfe9e3fad8d Ported the code completion and calltips support of the eric4 variant of this plug-in.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 return cts
104
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
106
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
107 def reportChanged(self, filename, oldSource):
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
108 """
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
109 Public slot to report some changed sources.
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
110
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
111 @param filename file name of the changed source (string)
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
112 @param oldSource source code before the change (string)
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
113 """
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
114 try:
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
115 rope.base.libutils.report_change(
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
116 self.__project, filename, oldSource)
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
117 except RuntimeError:
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
118 # this could come from trying to do PyQt4/PyQt5 mixed stuff
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
119 # simply ignore it
f6049d39f83d Made the code completion work in case no project is open.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 103
diff changeset
120 pass

eric ide

mercurial