Python Indent plugin not working correctly?



  • To All,

    So I downloaded the code for the Python Indent plugin, looked at it, did some testing and this is what I found:

    • Plain vanilla Notepad++ does not do any Python indenting. It just sets the tab to 4 spaces as required by Python and follows the previous indent level.
    • The Python Indent Plugin just does very basic Python indenting for if statements, for loops def statements and such by indenting the next line 4 spaces.

    Here is some snippets from the Python Indent plugin’s code:

    char* clauseHeaders[] = {"if", "elif", "else",
                         "for", "while",
                         "try", "except", "finally",
                         "with",
                         "def", "class",
                         nullptr};
    

    and

        /* Compare the first word in the code against the headers. Auto-indent only if
           one of the headers is found. */
        for(int j = 0; clauseHeaders[j] != nullptr; j++)
        {
            if(strncmp(&line[i], clauseHeaders[j], clauseHeadersLen[j]) == 0 && !isVarName(line[i+clauseHeadersLen[j]]))
            {
                ret = true;
                break;
            }
        }
    

    It would be more work to write Python Indent to follow PEP8 as each line would have to be parsed for parentheses, square brackets and such and their positions recorded. Python Indent was written in C++ and compiled with Microsoft Visual Studio which I have installed. I do not know C++ but I do know C and to my knowledge (which may be wrong) the line parsing part should be no different between C++ and C so I may look into doing this.

    On the Python Indent plugin developer’s webpage https://bitbucket.org/Kered13/python-indent-for-notepad , there is no contact information for him other than to log an issue so I may do this to let him know that I may be tweaking his code.

    Steve



  • @Steven-Haymes

    do you know yapf?
    Maybe useful for your project.



  • @Ekopalypse

    One step at a time. I was able to open the solution is Visual Studio. Next I need to understand the code.

    S



  • FWIW, I do a LOT of Python programming using Notepad++. I’m not sure the time/effort saved from having it do that kind of tabbing for me would be all that noticeable. It certainly wouldn’t do out-tabbing (unless it could read my mind), so there’s that. And for working with existing code I have a nice little Pythonscript tabber/untabber that does the current line no matter where the caret is on that line, which I feel is a bigger time saver than this “Python indenter” thing sounds like. To each his own, though, which you may know better as YMMV. Cheers.



  • do you know yapf?

    I use yapf to format my Python code in Notepad++. I have an NppExec script tied to “Style Format” and based on the current file extension, it makes the current file “pretty”:

    .c/.cpp = astyle
    .html/.xml = tidy5
    .pl/.pm = perltidy
    .py = yapf

    Cheers.



  • Michael,

    Can you please post your NppExec script? I am curios how you are implementing yapf.

    Are you are running the NppExec yapf script on the open file after a save and it corrects the errors then or does it correct the errors as you type? I assume that it is run after a save just like I would run Pylint or Flake8 to detect errors but with yapf, it both detects and corrects the errors.

    What the Python Indent plugin does is provide proper (but limited) Python indenting as you type.

    Thanks,

    Steve



  • I should have been more specific - I have an NppExec script calling a Windows batch script that does all the heavy lifting. I have all sorts of compile, run, help, etc. features enabled by file extension and a simple config file to change parameters was easier than hacking lots of NppExec scripts.

    However, it’d be easy enough to do in NppExec alone. Basically, my script does:

    NPP_MENUCOMMAND Edit\Blank Operations\Trim Trailing Space
    IF "$(NAME_PART)"=="Makefile" GOTO CONTINUE
    IF "$(EXT_PART)"==".mak" GOTO CONTINUE
    IF "$(EXT_PART)"==".mk" GOTO CONTINUE
    // Only convert tab to space if not a Makefile, so above bypass for Makefiles
    NPP_MENUCOMMAND Edit\Blank Operations\TAB to Space
    
    :CONTINUE
    NPP_SAVE
    cd "$(CURRENT_DIRECTORY)"
    "$(NPP_DIRECTORY)\plugins\nppExtTasks.bat" style "$(FULL_CURRENT_PATH)"
    NPP_OPEN "$(FULL_CURRENT_PATH)"
    

    But you could replace the call to my "nppExtTasks.bat with:

    IF "$(EXT_PART)"==".c" GOTO CCPP
    IF "$(EXT_PART)"==".cpp" GOTO CCPP
    IF "$(EXT_PART)"==".py" GOTO PYTHON
    IF "$(EXT_PART)"==".pl" GOTO PERL
    IF "$(EXT_PART)"==".pm" GOTO PERL
    IF "$(EXT_PART)"==".html" GOTO HTML
    IF "$(EXT_PART)"==".xml" GOTO XML
    GOTO END
    
    :CCPP
    cmd /c astyle "$(FULL_CURRENT_PATH)"
    GOTO END
    
    :PYTHON
     cmd /c yapf -i "$(FULL_CURRENT_PATH)"
    GOTO END
    
    :PERL
     cmd /c perltidy "$(FULL_CURRENT_PATH)"
    GOTO END
    
    :HTML
    cmd /c tidy5 -m "$(FULL_CURRENT_PATH)"
    GOTO END
    
    :XML
    cmd /c tidy5 -m -config "$(PLUGINS_CONFIG_DIR)\tidy5.xml.conf" "$(FULL_CURRENT_PATH)"
    :END
    

    I have a .perltidyrc, and an .astylerc in my home directory. And of course, all the executables (i.e., perltidy, tidy5, astyle, etc.) are in directories in my PATH.

    Hope that helps.

    Cheers.



  • @Michael-Vincent

    I got it on the batch file doing the heavy lifting. Your suggestion for a NppExec script to run yapf:

    cmd /c yapf -i “$(FULL_CURRENT_PATH)”

    is similar to my my NppExec scripts to run Pylint and Flake8:

    C:\Python37\Scripts\pylint.exe “$(FULL_CURRENT_PATH)”
    C:\Python37\Scripts\flake8.exe “$(FULL_CURRENT_PATH)”

    Pylint and Flake8 only read my current Python file in NPP. They does not modify it. Does your NppExec example script that runs yapf actually modify the current Python file in NPP?

    Thanks,

    Steve



  • Yes, that’s what the “-i” argument to yapf (in-place) and subsequent NPP_OPEN - to reopen the file and force a N++ refresh do.

    PS VinsWorldcom@C:\Users\VinsWorldcom> yapf -h  | grep "in-place"
      -i, --in-place        make changes to files in place
    


  • Hey, this is old but I just now stumbled upon it. I am the author of this plugin.

    As some of the posters above have figured out, the plugin does not attempt to follow PEP8. This is not because I have anything against PEP8, but simply I didn’t want to deal with that amount of parsing. I originally wrote this plugin to deal with the simple annoyance of having to tab every time I opened a new block. It applies a very simple heuristic of checking the start of the line for one of the keywords that open a new block in Python.

    It can actually be tricked into doing the wrong thing in a number of other situations (fairly unlikely situations though), such as having a multi-line statement to open a block, or using one of those keywords at the start of a line in a multi-line string. These situations are sufficiently rare that I have never attempted to fix them. In the much more common case of line continuations (which OP is talking about), I simply manually align them with space.

    If you want to get in touch with me about the plugin the best way is to open an issue on the Bitbucket page. I do not check these forums very often. I am also open to any code contributions if someone would like to tackle this.


Log in to reply