This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Idle: highlite tabs
Type: enhancement Stage: test needed
Components: IDLE Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: Christian.Kleineidam, ezio.melotti, markroseman, terry.reedy
Priority: normal Keywords:

Created on 2014-09-07 13:17 by Christian.Kleineidam, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg226535 - (view) Author: Christian Kleineidam (Christian.Kleineidam) Date: 2014-09-07 13:17
Python accepts both tabs and spaces. Code that mixes tab and spaces can lead to problematic issues. Especially beginners who are new to python can be confused if they copy some code and it doesn't work as they expected because of issues of invisible whitespace.

Beginners are also more likely to use the editor that comes with the IDLE instead of using a more specialised editor. 

If the IDLE would highlite the fact that tabs are used instead of spaces, it would be easier to spot the issue. I therefore suggest that the IDLE highlites tabs both in the shell mode and the editor mode.

Possible ways to highlite is to have a light grey: 
<--> 
» (at the beginning of the tab)
PyCharm style error underlining
msg226846 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-09-12 21:41
I agree that this is an issue, and I believe others have made similar comments or requests, but I cannot find an existing issue for this.

By experiment, it is possible to tag a tab and change the background color for the spaces a tab is visually converted to.  

import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.insert('insert', 'a\tb')
text.tag_add('TAB', 1.1, 1.2)
text.tag_config('TAB', background='#ffd')  # light yellow, or
text.tag_config('TAB', background='#eee')  # light gray

http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/text-methods.html
The other tag configuration options that refer to text, such as overstrike and underline, have no visual effect. Neither does non-zero borderwidth.

I am reluctant to add visible characters. The need to delete them would complicate converting tabs to spaces and saving files.  Since Idle normally converts tabs to spaces on input, they are not common in edited files.  The main issue, as you mention, is code imported from elsewhere.

Some issue remain.

1. Tagging tabs: I presume this is no problem, but will not know until there is a patch.

2. The priority of the TAB tag relative to others. The importance of this depends on the next question.

3. Should tab space in comments and strings be shaded?  I think so.  If so, should the shading match the comment/string foreground color?
msg230491 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-11-02 15:33
> Possible ways to highlite is to have a light grey: 
> <--> 
> » (at the beginning of the tab)

FWIW the latter (») is what I usually see used, with a lighter color to distinguish it from regular text.

> 3. Should tab space in comments and strings be shaded?  I think so.
> If so, should the shading match the comment/string foreground color?

Tabs in comments and strings should generally be avoided (\t can be used in strings), and at least for Python source I would assume they don't occur too frequently, so I wouldn't worry too much about this issue.
msg230519 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-11-02 19:55
I looked more into tab handling in tk and Idle. Tk uses literal tabs in text to position following text according to the tab stops configured with the Text tabs option.  Tabs stops are ultimately set to the pixel (not character), although one may enter distances in other units.  The following text may be left, right, center, or number justified with respect to the tab stop, with 'left' the default.  I am rather sure that there is no way to tell tk to display a special char at the beginning of any extra space it adds. In the general case, there may not be enough extra to do so.

Tab handling in Idle is more confused.  They are normally converted to spaces in Editor windows, but in left as is and used for auto-indents in multi-line statements in Shell.  (The latter is a major nuisance, which I hope to remedy.)  There are more oddities within the editor, some probably not necessary now.  One is that turning tabs on and off changes the tab to space replacement to 8 instead of what it was (the default now being 4).  Another is that Idle was written when mixed space-and-tab indents were legal.  So a) Idle allows one to write illegal-in-3.x indents, and b) the code to handle tab, backspace, enter, indent, and dedent is probably more complicated that it would have to be if illegal indents were never allowed.  (The tab handling code is in EditorWindow.py, in 3.4.2 mostly around lines 240-260 and 1200-1300.)

Given the above, I would consider tagging tabs at least when a file is read and probably during editing.  Since indents are the major concern, I would not worry much about what happens elsewhere.
---

Successive tabs could be differentiated by having two tab tags with different background colors and alternate their use.  The following does this with a sample line.

import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()

text.tag_config('TAB0', background='#fed')  # light yellow
text.tag_config('TAB1', background='#eef')  # light blue
ttags = ('TAB0', 'TAB1')
tdex = 0

line = 'a\t\t    \t b'
text.insert('insert', line)
for i, c in enumerate(line):
    if c == '\t':
        text.tag_add(ttags[tdex], '1.%d'%i, '1.%d'%(i+1))
        tdex ^=1
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66550
2020-06-08 00:37:47terry.reedysetversions: + Python 3.10, - Python 3.6, Python 3.7
2017-06-30 00:23:01terry.reedysetassignee: terry.reedy
versions: + Python 3.6, Python 3.7, - Python 2.7, Python 3.4, Python 3.5
2015-09-18 16:43:32markrosemansetnosy: + markroseman
2014-11-02 19:55:09terry.reedysetmessages: + msg230519
2014-11-02 15:33:30ezio.melottisetnosy: + ezio.melotti
messages: + msg230491
2014-09-12 21:41:10terry.reedysetnosy: + terry.reedy
title: Highlite tabs in the IDLE -> Idle: highlite tabs
messages: + msg226846

versions: - Python 3.1, Python 3.2, Python 3.3
stage: test needed
2014-09-07 13:17:02Christian.Kleineidamcreate