Notepad ++ Automatically detect Python tabs or spaces

I usually write tabbed code, but many python libraries use spaces. Is there a way for Notepad ++ to automatically determine how the file is formatted and does it automatically switch to using spaces when the file is already formatted in this way?

By the way, I know that there was already a SO question on how to change the format of the Notepad ++ tab. But it would be better if it automatically changed depending on the current file format.

+6
source share
3 answers

If you install the "Python Script" plugin for Notepad ++, you can write code to automatically switch between tabs and spaces.

Here's how:

  • From the menu: Plugins β†’ Python Script β†’ Configuration and set Initialization to ATSTARTUP. When Notepad ++ starts, startup.py Script starts.

  • Find startup.py and edit it. On my PC, its path is c:\Program Files\Notepad++\plugins\PythonScript\scripts\startup.py , add the following code to startup.py .

The buffer_active() function is called every time you switch the tab, and guess_tab() checks if the text uses the indent tab or not. You can show the Python console for debugging code.

 def guess_tab(text): count = 0 for line in text.split("\n"): indents = line[:len(line)-len(line.lstrip())] if "\t" in indents: count += 1 if count > 5: return True else: return False def buffer_active(arg): editor.setBackSpaceUnIndents(True) use_tab = guess_tab(editor.getText()) editor.setUseTabs(use_tab) sys.stderr.write( "setUseTabs %s\n" % use_tab ) notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED]) notepad.callback(buffer_active, [NOTIFICATION.BUFFERACTIVATED]) 

This is just an example, feel free to make guess_tab() better than yourself, perhaps use a global dict to cache the result and speed up the callback function.

+6
source

Here is an improved version based on HYRY's answer:

  • Works on the launch tab (when starting notepad ++ to open a file)
  • No minimum lines are required to trigger indentation detection. Indentation avoidance is based on the first indentation encountered.
  • Keeps indentation by default when indentation cannot be detected
  • Very effective, does not slow down Notepad ++ when opening large files (checked on 220 MB file, indentation detection takes only <300 ms)

Available for download here: https://gist.github.com/vincepare/8a204172d959defb2122

 import re import time def indent_guess_tab(text): for line in text.split("\n"): pattern = re.compile("^( {4,}|\t)") match = pattern.match(line) if (match): return True if ("\t" in match.group(1)) else False def indent_auto_detect(arg): start = time.clock() # Get text sample maxLen = 500000 len = editor.getTextLength() len = len if len < maxLen else maxLen sample = editor.getTextRange(0, len) # Indent set current_use_tab = editor.getUseTabs() use_tab = indent_guess_tab(sample) if (use_tab != None and use_tab != current_use_tab): console.write("Indent use tab switch (%s => %s)\n" % (current_use_tab, use_tab)) editor.setUseTabs(use_tab) end = time.clock() console.write("Indentation detection took %s ms\n" % (round((end-start)*1000, 3))) notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED, NOTIFICATION.READY]) notepad.callback(indent_auto_detect, [NOTIFICATION.BUFFERACTIVATED]) notepad.callback(indent_auto_detect, [NOTIFICATION.READY]) console.write("Automatic indentation detection started\n") indent_auto_detect(None) 
+2
source

Nope!

You can always just change them (into tabs, of course) to satisfy your needs with the "Replace All" (      , \t ) in advanced mode.

+1
source

Source: https://habr.com/ru/post/912279/


All Articles