Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: AutoComplete.py
"""AutoComplete.py - An IDLE extension for automatically completing names.
[0] Fix | Delete
[1] Fix | Delete
This extension can complete either attribute names or file names. It can pop
[2] Fix | Delete
a window with all available names, for the user to select from.
[3] Fix | Delete
"""
[4] Fix | Delete
import os
[5] Fix | Delete
import sys
[6] Fix | Delete
import string
[7] Fix | Delete
[8] Fix | Delete
from idlelib.configHandler import idleConf
[9] Fix | Delete
[10] Fix | Delete
# This string includes all chars that may be in a file name (without a path
[11] Fix | Delete
# separator)
[12] Fix | Delete
FILENAME_CHARS = string.ascii_letters + string.digits + os.curdir + "._~#$:-"
[13] Fix | Delete
# This string includes all chars that may be in an identifier
[14] Fix | Delete
ID_CHARS = string.ascii_letters + string.digits + "_"
[15] Fix | Delete
[16] Fix | Delete
# These constants represent the two different types of completions
[17] Fix | Delete
COMPLETE_ATTRIBUTES, COMPLETE_FILES = range(1, 2+1)
[18] Fix | Delete
[19] Fix | Delete
from idlelib import AutoCompleteWindow
[20] Fix | Delete
from idlelib.HyperParser import HyperParser
[21] Fix | Delete
[22] Fix | Delete
import __main__
[23] Fix | Delete
[24] Fix | Delete
SEPS = os.sep
[25] Fix | Delete
if os.altsep: # e.g. '/' on Windows...
[26] Fix | Delete
SEPS += os.altsep
[27] Fix | Delete
[28] Fix | Delete
class AutoComplete:
[29] Fix | Delete
[30] Fix | Delete
menudefs = [
[31] Fix | Delete
('edit', [
[32] Fix | Delete
("Show Completions", "<<force-open-completions>>"),
[33] Fix | Delete
])
[34] Fix | Delete
]
[35] Fix | Delete
[36] Fix | Delete
popupwait = idleConf.GetOption("extensions", "AutoComplete",
[37] Fix | Delete
"popupwait", type="int", default=0)
[38] Fix | Delete
[39] Fix | Delete
def __init__(self, editwin=None):
[40] Fix | Delete
self.editwin = editwin
[41] Fix | Delete
if editwin is None: # subprocess and test
[42] Fix | Delete
return
[43] Fix | Delete
self.text = editwin.text
[44] Fix | Delete
self.autocompletewindow = None
[45] Fix | Delete
[46] Fix | Delete
# id of delayed call, and the index of the text insert when the delayed
[47] Fix | Delete
# call was issued. If _delayed_completion_id is None, there is no
[48] Fix | Delete
# delayed call.
[49] Fix | Delete
self._delayed_completion_id = None
[50] Fix | Delete
self._delayed_completion_index = None
[51] Fix | Delete
[52] Fix | Delete
def _make_autocomplete_window(self):
[53] Fix | Delete
return AutoCompleteWindow.AutoCompleteWindow(self.text)
[54] Fix | Delete
[55] Fix | Delete
def _remove_autocomplete_window(self, event=None):
[56] Fix | Delete
if self.autocompletewindow:
[57] Fix | Delete
self.autocompletewindow.hide_window()
[58] Fix | Delete
self.autocompletewindow = None
[59] Fix | Delete
[60] Fix | Delete
def force_open_completions_event(self, event):
[61] Fix | Delete
"""Happens when the user really wants to open a completion list, even
[62] Fix | Delete
if a function call is needed.
[63] Fix | Delete
"""
[64] Fix | Delete
self.open_completions(True, False, True)
[65] Fix | Delete
[66] Fix | Delete
def try_open_completions_event(self, event):
[67] Fix | Delete
"""Happens when it would be nice to open a completion list, but not
[68] Fix | Delete
really necessary, for example after a dot, so function
[69] Fix | Delete
calls won't be made.
[70] Fix | Delete
"""
[71] Fix | Delete
lastchar = self.text.get("insert-1c")
[72] Fix | Delete
if lastchar == ".":
[73] Fix | Delete
self._open_completions_later(False, False, False,
[74] Fix | Delete
COMPLETE_ATTRIBUTES)
[75] Fix | Delete
elif lastchar in SEPS:
[76] Fix | Delete
self._open_completions_later(False, False, False,
[77] Fix | Delete
COMPLETE_FILES)
[78] Fix | Delete
[79] Fix | Delete
def autocomplete_event(self, event):
[80] Fix | Delete
"""Happens when the user wants to complete his word, and if necessary,
[81] Fix | Delete
open a completion list after that (if there is more than one
[82] Fix | Delete
completion)
[83] Fix | Delete
"""
[84] Fix | Delete
if hasattr(event, "mc_state") and event.mc_state:
[85] Fix | Delete
# A modifier was pressed along with the tab, continue as usual.
[86] Fix | Delete
return
[87] Fix | Delete
if self.autocompletewindow and self.autocompletewindow.is_active():
[88] Fix | Delete
self.autocompletewindow.complete()
[89] Fix | Delete
return "break"
[90] Fix | Delete
else:
[91] Fix | Delete
opened = self.open_completions(False, True, True)
[92] Fix | Delete
if opened:
[93] Fix | Delete
return "break"
[94] Fix | Delete
[95] Fix | Delete
def _open_completions_later(self, *args):
[96] Fix | Delete
self._delayed_completion_index = self.text.index("insert")
[97] Fix | Delete
if self._delayed_completion_id is not None:
[98] Fix | Delete
self.text.after_cancel(self._delayed_completion_id)
[99] Fix | Delete
self._delayed_completion_id = \
[100] Fix | Delete
self.text.after(self.popupwait, self._delayed_open_completions,
[101] Fix | Delete
*args)
[102] Fix | Delete
[103] Fix | Delete
def _delayed_open_completions(self, *args):
[104] Fix | Delete
self._delayed_completion_id = None
[105] Fix | Delete
if self.text.index("insert") != self._delayed_completion_index:
[106] Fix | Delete
return
[107] Fix | Delete
self.open_completions(*args)
[108] Fix | Delete
[109] Fix | Delete
def open_completions(self, evalfuncs, complete, userWantsWin, mode=None):
[110] Fix | Delete
"""Find the completions and create the AutoCompleteWindow.
[111] Fix | Delete
Return True if successful (no syntax error or so found).
[112] Fix | Delete
if complete is True, then if there's nothing to complete and no
[113] Fix | Delete
start of completion, won't open completions and return False.
[114] Fix | Delete
If mode is given, will open a completion list only in this mode.
[115] Fix | Delete
"""
[116] Fix | Delete
# Cancel another delayed call, if it exists.
[117] Fix | Delete
if self._delayed_completion_id is not None:
[118] Fix | Delete
self.text.after_cancel(self._delayed_completion_id)
[119] Fix | Delete
self._delayed_completion_id = None
[120] Fix | Delete
[121] Fix | Delete
hp = HyperParser(self.editwin, "insert")
[122] Fix | Delete
curline = self.text.get("insert linestart", "insert")
[123] Fix | Delete
i = j = len(curline)
[124] Fix | Delete
if hp.is_in_string() and (not mode or mode==COMPLETE_FILES):
[125] Fix | Delete
self._remove_autocomplete_window()
[126] Fix | Delete
mode = COMPLETE_FILES
[127] Fix | Delete
while i and curline[i-1] in FILENAME_CHARS:
[128] Fix | Delete
i -= 1
[129] Fix | Delete
comp_start = curline[i:j]
[130] Fix | Delete
j = i
[131] Fix | Delete
while i and curline[i-1] in FILENAME_CHARS + SEPS:
[132] Fix | Delete
i -= 1
[133] Fix | Delete
comp_what = curline[i:j]
[134] Fix | Delete
elif hp.is_in_code() and (not mode or mode==COMPLETE_ATTRIBUTES):
[135] Fix | Delete
self._remove_autocomplete_window()
[136] Fix | Delete
mode = COMPLETE_ATTRIBUTES
[137] Fix | Delete
while i and curline[i-1] in ID_CHARS:
[138] Fix | Delete
i -= 1
[139] Fix | Delete
comp_start = curline[i:j]
[140] Fix | Delete
if i and curline[i-1] == '.':
[141] Fix | Delete
hp.set_index("insert-%dc" % (len(curline)-(i-1)))
[142] Fix | Delete
comp_what = hp.get_expression()
[143] Fix | Delete
if not comp_what or \
[144] Fix | Delete
(not evalfuncs and comp_what.find('(') != -1):
[145] Fix | Delete
return
[146] Fix | Delete
else:
[147] Fix | Delete
comp_what = ""
[148] Fix | Delete
else:
[149] Fix | Delete
return
[150] Fix | Delete
[151] Fix | Delete
if complete and not comp_what and not comp_start:
[152] Fix | Delete
return
[153] Fix | Delete
comp_lists = self.fetch_completions(comp_what, mode)
[154] Fix | Delete
if not comp_lists[0]:
[155] Fix | Delete
return
[156] Fix | Delete
self.autocompletewindow = self._make_autocomplete_window()
[157] Fix | Delete
return not self.autocompletewindow.show_window(
[158] Fix | Delete
comp_lists, "insert-%dc" % len(comp_start),
[159] Fix | Delete
complete, mode, userWantsWin)
[160] Fix | Delete
[161] Fix | Delete
def fetch_completions(self, what, mode):
[162] Fix | Delete
"""Return a pair of lists of completions for something. The first list
[163] Fix | Delete
is a sublist of the second. Both are sorted.
[164] Fix | Delete
[165] Fix | Delete
If there is a Python subprocess, get the comp. list there. Otherwise,
[166] Fix | Delete
either fetch_completions() is running in the subprocess itself or it
[167] Fix | Delete
was called in an IDLE EditorWindow before any script had been run.
[168] Fix | Delete
[169] Fix | Delete
The subprocess environment is that of the most recently run script. If
[170] Fix | Delete
two unrelated modules are being edited some calltips in the current
[171] Fix | Delete
module may be inoperative if the module was not the last to run.
[172] Fix | Delete
"""
[173] Fix | Delete
try:
[174] Fix | Delete
rpcclt = self.editwin.flist.pyshell.interp.rpcclt
[175] Fix | Delete
except:
[176] Fix | Delete
rpcclt = None
[177] Fix | Delete
if rpcclt:
[178] Fix | Delete
return rpcclt.remotecall("exec", "get_the_completion_list",
[179] Fix | Delete
(what, mode), {})
[180] Fix | Delete
else:
[181] Fix | Delete
if mode == COMPLETE_ATTRIBUTES:
[182] Fix | Delete
if what == "":
[183] Fix | Delete
namespace = __main__.__dict__.copy()
[184] Fix | Delete
namespace.update(__main__.__builtins__.__dict__)
[185] Fix | Delete
bigl = eval("dir()", namespace)
[186] Fix | Delete
bigl.sort()
[187] Fix | Delete
if "__all__" in bigl:
[188] Fix | Delete
smalll = sorted(eval("__all__", namespace))
[189] Fix | Delete
else:
[190] Fix | Delete
smalll = [s for s in bigl if s[:1] != '_']
[191] Fix | Delete
else:
[192] Fix | Delete
try:
[193] Fix | Delete
entity = self.get_entity(what)
[194] Fix | Delete
bigl = dir(entity)
[195] Fix | Delete
bigl.sort()
[196] Fix | Delete
if "__all__" in bigl:
[197] Fix | Delete
smalll = sorted(entity.__all__)
[198] Fix | Delete
else:
[199] Fix | Delete
smalll = [s for s in bigl if s[:1] != '_']
[200] Fix | Delete
except:
[201] Fix | Delete
return [], []
[202] Fix | Delete
[203] Fix | Delete
elif mode == COMPLETE_FILES:
[204] Fix | Delete
if what == "":
[205] Fix | Delete
what = "."
[206] Fix | Delete
try:
[207] Fix | Delete
expandedpath = os.path.expanduser(what)
[208] Fix | Delete
bigl = os.listdir(expandedpath)
[209] Fix | Delete
bigl.sort()
[210] Fix | Delete
smalll = [s for s in bigl if s[:1] != '.']
[211] Fix | Delete
except OSError:
[212] Fix | Delete
return [], []
[213] Fix | Delete
[214] Fix | Delete
if not smalll:
[215] Fix | Delete
smalll = bigl
[216] Fix | Delete
return smalll, bigl
[217] Fix | Delete
[218] Fix | Delete
def get_entity(self, name):
[219] Fix | Delete
"""Lookup name in a namespace spanning sys.modules and __main.dict__"""
[220] Fix | Delete
namespace = sys.modules.copy()
[221] Fix | Delete
namespace.update(__main__.__dict__)
[222] Fix | Delete
return eval(name, namespace)
[223] Fix | Delete
[224] Fix | Delete
[225] Fix | Delete
if __name__ == '__main__':
[226] Fix | Delete
from unittest import main
[227] Fix | Delete
main('idlelib.idle_test.test_autocomplete', verbosity=2)
[228] Fix | Delete
[229] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function