Skip to content

Commit bce2eb4

Browse files
bpo-37765: Add keywords to IDLE tab completions (pythonGH-15138)
Keywords are present in the main module tab completion lists generated by rlcompleter, which is used by REPLs on *nix. Add all keywords to IDLE's main module name list except those already added from builtins (True, False, and None) . This list may also be used by Show Completions on the Edit menu, and its hot key. Rewrite Completions doc. Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
1 parent 1ee5dc1 commit bce2eb4

File tree

6 files changed

+110
-83
lines changed

6 files changed

+110
-83
lines changed

Doc/library/idle.rst

+48-42
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Go to Line
147147
Clear any selection and update the line and column status.
148148

149149
Show Completions
150-
Open a scrollable list allowing selection of keywords and attributes. See
150+
Open a scrollable list allowing selection of existing names. See
151151
:ref:`Completions <completions>` in the Editing and navigation section below.
152152

153153
Expand Word
@@ -469,52 +469,58 @@ are restricted to four spaces due to Tcl/Tk limitations.
469469
See also the indent/dedent region commands on the
470470
:ref:`Format menu <format-menu>`.
471471

472-
473472
.. _completions:
474473

475474
Completions
476475
^^^^^^^^^^^
477476

478-
Completions are supplied for functions, classes, and attributes of classes,
479-
both built-in and user-defined. Completions are also provided for
480-
filenames.
481-
482-
The AutoCompleteWindow (ACW) will open after a predefined delay (default is
483-
two seconds) after a '.' or (in a string) an os.sep is typed. If after one
484-
of those characters (plus zero or more other characters) a tab is typed
485-
the ACW will open immediately if a possible continuation is found.
486-
487-
If there is only one possible completion for the characters entered, a
488-
:kbd:`Tab` will supply that completion without opening the ACW.
489-
490-
'Show Completions' will force open a completions window, by default the
491-
:kbd:`C-space` will open a completions window. In an empty
492-
string, this will contain the files in the current directory. On a
493-
blank line, it will contain the built-in and user-defined functions and
494-
classes in the current namespaces, plus any modules imported. If some
495-
characters have been entered, the ACW will attempt to be more specific.
496-
497-
If a string of characters is typed, the ACW selection will jump to the
498-
entry most closely matching those characters. Entering a :kbd:`tab` will
499-
cause the longest non-ambiguous match to be entered in the Editor window or
500-
Shell. Two :kbd:`tab` in a row will supply the current ACW selection, as
501-
will return or a double click. Cursor keys, Page Up/Down, mouse selection,
502-
and the scroll wheel all operate on the ACW.
503-
504-
"Hidden" attributes can be accessed by typing the beginning of hidden
505-
name after a '.', e.g. '_'. This allows access to modules with
506-
``__all__`` set, or to class-private attributes.
507-
508-
Completions and the 'Expand Word' facility can save a lot of typing!
509-
510-
Completions are currently limited to those in the namespaces. Names in
511-
an Editor window which are not via ``__main__`` and :data:`sys.modules` will
512-
not be found. Run the module once with your imports to correct this situation.
513-
Note that IDLE itself places quite a few modules in sys.modules, so
514-
much can be found by default, e.g. the re module.
515-
516-
If you don't like the ACW popping up unbidden, simply make the delay
517-
longer or disable the extension.
477+
Completions are supplied, when requested and available, for module
478+
names, attributes of classes or functions, or filenames. Each request
479+
method displays a completion box with existing names. (See tab
480+
completions below for an exception.) For any box, change the name
481+
being completed and the item highlighted in the box by
482+
typing and deleting characters; by hitting :kbd:`Up`, :kbd:`Down`,
483+
:kbd:`PageUp`, :kbd:`PageDown`, :kbd:`Home`, and :kbd:`End` keys;
484+
and by a single click within the box. Close the box with :kbd:`Escape`,
485+
:kbd:`Enter`, and double :kbd:`Tab` keys or clicks outside the box.
486+
A double click within the box selects and closes.
487+
488+
One way to open a box is to type a key character and wait for a
489+
predefined interval. This defaults to 2 seconds; customize it
490+
in the settings dialog. (To prevent auto popups, set the delay to a
491+
large number of milliseconds, such as 100000000.) For imported module
492+
names or class or function attributes, type '.'.
493+
For filenames in the root directory, type :data:`os.sep` or
494+
data:`os.altsep` immediately after an opening quote. (On Windows,
495+
one can specify a drive first.) Move into subdirectories by typing a
496+
directory name and a separator.
497+
498+
Instead of waiting, or after a box is closed, open a completion box
499+
immediately with Show Completions on the Edit menu. The default hot
500+
key is :kbd:`C-space`. If one types a prefix for the desired name
501+
before opening the box, the first match or near miss is made visible.
502+
The result is the same as if one enters a prefix
503+
after the box is displayed. Show Completions after a quote completes
504+
filenames in the current directory instead of a root directory.
505+
506+
Hitting :kbd:`Tab` after a prefix usually has the same effect as Show
507+
Completions. (With no prefix, it indents.) However, if there is only
508+
one match to the prefix, that match is immediately added to the editor
509+
text without opening a box.
510+
511+
Invoking 'Show Completions', or hitting :kbd:`Tab` after a prefix,
512+
outside of a string and without a preceding '.' opens a box with
513+
keywords, builtin names, and available module-level names.
514+
515+
When editing code in an editor (as oppose to Shell), increase the
516+
available module-level names by running your code
517+
and not restarting the Shell thereafter. This is especially useful
518+
after adding imports at the top of a file. This also increases
519+
possible attribute completions.
520+
521+
Completion boxes intially exclude names beginning with '_' or, for
522+
modules, not included in '__all__'. The hidden names can be accessed
523+
by typing '_' after '.', either before or after the box is opened.
518524

519525
.. _calltips:
520526

Lib/idlelib/NEWS.txt

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Released on 2020-10-05?
33
======================================
44

55

6+
bpo-37765: Add keywords to module name completion list. Rewrite
7+
Completions section of IDLE doc.
8+
69
bpo-41152: The encoding of ``stdin``, ``stdout`` and ``stderr`` in IDLE
710
is now always UTF-8.
811

Lib/idlelib/autocomplete.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
pop up a list of candidates.
55
"""
66
import __main__
7+
import keyword
78
import os
89
import string
910
import sys
@@ -171,10 +172,13 @@ def fetch_completions(self, what, mode):
171172
(what, mode), {})
172173
else:
173174
if mode == ATTRS:
174-
if what == "":
175+
if what == "": # Main module names.
175176
namespace = {**__main__.__builtins__.__dict__,
176177
**__main__.__dict__}
177178
bigl = eval("dir()", namespace)
179+
kwds = (s for s in keyword.kwlist
180+
if s not in {'True', 'False', 'None'})
181+
bigl.extend(kwds)
178182
bigl.sort()
179183
if "__all__" in bigl:
180184
smalll = sorted(eval("__all__", namespace))

Lib/idlelib/help.html

+47-38
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<html xmlns="http://www.w3.org/1999/xhtml">
55
<head>
66
<meta charset="utf-8" />
7-
<title>IDLE &#8212; Python 3.9.0a4 documentation</title>
7+
<title>IDLE &#8212; Python 3.10.0a0 documentation</title>
88
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
99
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
1010

@@ -17,7 +17,7 @@
1717
<script type="text/javascript" src="../_static/sidebar.js"></script>
1818

1919
<link rel="search" type="application/opensearchdescription+xml"
20-
title="Search within Python 3.9.0a4 documentation"
20+
title="Search within Python 3.10.0a0 documentation"
2121
href="../_static/opensearch.xml"/>
2222
<link rel="author" title="About these documents" href="../about.html" />
2323
<link rel="index" title="Index" href="../genindex.html" />
@@ -71,7 +71,7 @@ <h3>Navigation</h3>
7171

7272

7373
<li>
74-
<a href="../index.html">3.9.0a4 Documentation</a> &#187;
74+
<a href="../index.html">3.10.0a0 Documentation</a> &#187;
7575
</li>
7676

7777
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
@@ -201,7 +201,7 @@ <h3>Edit menu (Shell and Editor)<a class="headerlink" href="#edit-menu-shell-and
201201
line visible. A request past the end of the file goes to the end.
202202
Clear any selection and update the line and column status.</p>
203203
</dd>
204-
<dt>Show Completions</dt><dd><p>Open a scrollable list allowing selection of keywords and attributes. See
204+
<dt>Show Completions</dt><dd><p>Open a scrollable list allowing selection of existing names. See
205205
<a class="reference internal" href="#completions"><span class="std std-ref">Completions</span></a> in the Editing and navigation section below.</p>
206206
</dd>
207207
<dt>Expand Word</dt><dd><p>Expand a prefix you have typed to match a full word in the same window;
@@ -465,38 +465,47 @@ <h3>Automatic indentation<a class="headerlink" href="#automatic-indentation" tit
465465
</div>
466466
<div class="section" id="completions">
467467
<span id="id3"></span><h3>Completions<a class="headerlink" href="#completions" title="Permalink to this headline"></a></h3>
468-
<p>Completions are supplied for functions, classes, and attributes of classes,
469-
both built-in and user-defined. Completions are also provided for
470-
filenames.</p>
471-
<p>The AutoCompleteWindow (ACW) will open after a predefined delay (default is
472-
two seconds) after a ‘.’ or (in a string) an os.sep is typed. If after one
473-
of those characters (plus zero or more other characters) a tab is typed
474-
the ACW will open immediately if a possible continuation is found.</p>
475-
<p>If there is only one possible completion for the characters entered, a
476-
<kbd class="kbd docutils literal notranslate">Tab</kbd> will supply that completion without opening the ACW.</p>
477-
<p>‘Show Completions’ will force open a completions window, by default the
478-
<kbd class="kbd docutils literal notranslate">C-space</kbd> will open a completions window. In an empty
479-
string, this will contain the files in the current directory. On a
480-
blank line, it will contain the built-in and user-defined functions and
481-
classes in the current namespaces, plus any modules imported. If some
482-
characters have been entered, the ACW will attempt to be more specific.</p>
483-
<p>If a string of characters is typed, the ACW selection will jump to the
484-
entry most closely matching those characters. Entering a <kbd class="kbd docutils literal notranslate">tab</kbd> will
485-
cause the longest non-ambiguous match to be entered in the Editor window or
486-
Shell. Two <kbd class="kbd docutils literal notranslate">tab</kbd> in a row will supply the current ACW selection, as
487-
will return or a double click. Cursor keys, Page Up/Down, mouse selection,
488-
and the scroll wheel all operate on the ACW.</p>
489-
<p>“Hidden” attributes can be accessed by typing the beginning of hidden
490-
name after a ‘.’, e.g. ‘_’. This allows access to modules with
491-
<code class="docutils literal notranslate"><span class="pre">__all__</span></code> set, or to class-private attributes.</p>
492-
<p>Completions and the ‘Expand Word’ facility can save a lot of typing!</p>
493-
<p>Completions are currently limited to those in the namespaces. Names in
494-
an Editor window which are not via <code class="docutils literal notranslate"><span class="pre">__main__</span></code> and <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> will
495-
not be found. Run the module once with your imports to correct this situation.
496-
Note that IDLE itself places quite a few modules in sys.modules, so
497-
much can be found by default, e.g. the re module.</p>
498-
<p>If you don’t like the ACW popping up unbidden, simply make the delay
499-
longer or disable the extension.</p>
468+
<p>Completions are supplied, when requested and available, for module
469+
names, attributes of classes or functions, or filenames. Each request
470+
method displays a completion box with existing names. (See tab
471+
completions below for an exception.) For any box, change the name
472+
being completed and the item highlighted in the box by
473+
typing and deleting characters; by hitting <kbd class="kbd docutils literal notranslate">Up</kbd>, <kbd class="kbd docutils literal notranslate">Down</kbd>,
474+
<kbd class="kbd docutils literal notranslate">PageUp</kbd>, <kbd class="kbd docutils literal notranslate">PageDown</kbd>, <kbd class="kbd docutils literal notranslate">Home</kbd>, and <kbd class="kbd docutils literal notranslate">End</kbd> keys;
475+
and by a single click within the box. Close the box with <kbd class="kbd docutils literal notranslate">Escape</kbd>,
476+
<kbd class="kbd docutils literal notranslate">Enter</kbd>, and double <kbd class="kbd docutils literal notranslate">Tab</kbd> keys or clicks outside the box.
477+
A double click within the box selects and closes.</p>
478+
<p>One way to open a box is to type a key character and wait for a
479+
predefined interval. This defaults to 2 seconds; customize it
480+
in the settings dialog. (To prevent auto popups, set the delay to a
481+
large number of milliseconds, such as 100000000.) For imported module
482+
names or class or function attributes, type ‘.’.
483+
For filenames in the root directory, type <a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.sep</span></code></a> or
484+
data:<cite>os.altsep</cite> immediately after an opening quote. (On Windows,
485+
one can specify a drive first.) Move into subdirectories by typing a
486+
directory name and a separator.</p>
487+
<p>Instead of waiting, or after a box is closed. open a completion box
488+
immediately with Show Completions on the Edit menu. The default hot
489+
key is <kbd class="kbd docutils literal notranslate">C-space</kbd>. If one types a prefix for the desired name
490+
before opening the box, the first match is displayed.
491+
The result is the same as if one enters a prefix
492+
after the box is displayed. Show Completions after a quote completes
493+
filenames in the current directory instead of a root directory.</p>
494+
<p>Hitting <kbd class="kbd docutils literal notranslate">Tab</kbd> after a prefix usually has the same effect as Show
495+
Completions. (With no prefix, it indents.) However, if there is only
496+
one match to the prefix, that match is immediately added to the editor
497+
text without opening a box.</p>
498+
<p>Invoking ‘Show Completions’, or hitting <kbd class="kbd docutils literal notranslate">Tab</kbd> after a prefix,
499+
outside of a string and without a preceding ‘.’ opens a box with
500+
keywords, builtin names, and available module-level names.</p>
501+
<p>When editing code in an editor (as oppose to Shell), increase the
502+
available module-level names by running your code
503+
and not restarting the Shell thereafter. This is especially useful
504+
after adding imports at the top of a file. This also increases
505+
possible attribute completions.</p>
506+
<p>Completion boxes intially exclude names beginning with ‘_’ or, for
507+
modules, not included in ‘__all__’. The hidden names can be accessed
508+
by typing ‘_’ after ‘.’, either before or after the box is opened.</p>
500509
</div>
501510
<div class="section" id="calltips">
502511
<span id="id4"></span><h3>Calltips<a class="headerlink" href="#calltips" title="Permalink to this headline"></a></h3>
@@ -935,7 +944,7 @@ <h3>Navigation</h3>
935944

936945

937946
<li>
938-
<a href="../index.html">3.9.0a4 Documentation</a> &#187;
947+
<a href="../index.html">3.10.0a0 Documentation</a> &#187;
939948
</li>
940949

941950
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
@@ -966,7 +975,7 @@ <h3>Navigation</h3>
966975
<br />
967976
<br />
968977

969-
Last updated on Mar 07, 2020.
978+
Last updated on Jul 08, 2020.
970979
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
971980
<br />
972981

Lib/idlelib/idle_test/test_autocomplete.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,11 @@ def test_fetch_completions(self):
240240
with patch.dict('__main__.__dict__', {'__all__': ['a', 'b']}):
241241
s, b = acp.fetch_completions('', ac.ATTRS)
242242
self.assertEqual(s, ['a', 'b'])
243-
self.assertIn('__name__', b) # From __main__.__dict__
244-
self.assertIn('sum', b) # From __main__.__builtins__.__dict__
243+
self.assertIn('__name__', b) # From __main__.__dict__.
244+
self.assertIn('sum', b) # From __main__.__builtins__.__dict__.
245+
self.assertIn('nonlocal', b) # From keyword.kwlist.
246+
pos = b.index('False') # Test False not included twice.
247+
self.assertNotEqual(b[pos+1], 'False')
245248

246249
# Test attributes with name entity.
247250
mock = Mock()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add keywords to module name completion list. Rewrite Completions
2+
section of IDLE doc.

0 commit comments

Comments
 (0)