Python Indent plugin not working correctly?



  • To All,

    I installed the Python Indent plugin and enabled it. The plugin appears to not be following PEP8 indent standards when splitting lines. For example:

    PEP8 (correct):
    80 column
    ____print(“a very very…”)
    _________"… long line")
    Python indent (not correct):
    ____print(“a very very…”)
    ____("…long line")

    I could give other examples when splitting lists, function arguments, etc. across lines. Has anyone else seen this problem and is there a fix other than just manually indenting correctly?

    Hopefully my use of “_” will format this correctly when being viewed.

    Thanks,

    Steve



  • @Steven-Haymes said:

    plugin appears to not be following PEP8 indent standards

    I don’t recall any mentions of that plugin in the forum, so I don’t know if there are any experts or users of that plugin who can help confirm. But that could be a bug in the plugin…

    I don’t know if the user @Derek-Brown is the same Derek Brown as credited for being the author in the Plugins Admin, and he hasn’t logged in here for 5 months, if he is the right Derek Brown. But Plugins Admin does list https://bitbucket.org/Kered13/python-indent-for-notepad as the project page, where there’s an (empty) issues page… so if no one else is able to step in here and help with the issue, you might be able to file a bug report at the plugin repository.



  • Hi Peter,

    Thanks for the reply. I will give this forum a week or so before I contact Derek Brown. Also, I was bad when I made my first post. It should read:

    The plugin appears to not be following PEP8 indent standards when splitting lines. For example:

    PEP8 (correct):
    _______________________80 column
    ____print(“a very very…”)
    _________("… long line")

    Python indent (not correct):
    ____print(“a very very…”)
    ____("…long line")

    Steve



  • I think I understand, but it sure would have been clearer to show some real spacing instead of a bunch of ___________.

    You could have shown real code spacing by leading off each line with four blank spaces before the real content.



  • Further to @Alan-Kilborn suggestion, this is what it looks like in the forum if you indent your code by 4 spaces and replace every _ with a space:

    PEP8 (correct):
                           80 column
        print(“a very very...”)
             (“... long line”)
    
    Python indent (not correct):
        print(“a very very...”)
        ("...long line")


  • Is PythonIndent advertised as following PEP8?



  • Ross,

    The Plugin says “Python auto-indent plugin”. The developer’s website https://bitbucket.org/Kered13/python-indent-for-notepad/src/default/ says nothing. PEP8 is pretty basic to Python indenting. How does one insert a picture so that I can give a real code example, using some Python that I wrote for NPP?

    Steve



  • @Steven-Haymes said:

    How does one insert a picture

    upload image to imgur (or similar). embed images with the syntax ![](http://i.imgur.com/QTHZysa.png). please use imgur’s “direct link” with i.imgur.com as the hostname and the appropriate .png or .gif extension, rather than the “image” link, which really links to the HTML-wrapper, and will not embed in the forum

    You can also embed raw text (like code) between ~~~ lines, like

    ~~~
    raw text goes here
       it will include spaces
       and _not_ use Markdown
    ~~~
    

    ----
    FYI:

    This forum is formatted using Markdown, with a help link buried on the little grey ? in the COMPOSE window/pane when writing your post. For more about how to use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic, and my updates near the end. It is very important that you use these formatting tips – using single backtick marks around small snippets, and using code-quoting for pasting multiple lines from your example data files – because otherwise, the forum will change normal quotes ("") to curly “smart” quotes (“”), will change hyphens to dashes, will sometimes hide asterisks (or if your text is c:\folder\*.txt, it will show up as c:\folder*.txt, missing the backslash). If you want to clearly communicate your text data to us, you need to properly format it.



  • @Steven-Haymes said:

    insert a picture so that I can give a real code example, using some Python that I wrote

    Why do you want it to be a “picture”? Make it real code text instead. Assuming your code is in Notepad++: Select the code you want to post. Press Tab. Press Ctrl+c. Press Shift+Tab. Change to browser where you are composing for this forum. Press Ctrl+v. Check the preview window on the right to make sure it looks like code. Submit when ready.



  • Alan,

    Thanks on how to insert code from NPP to my web browser which is Firefox. Here is a snippet of the Python code showing proper PEP8 indentation that the Python Indent plugin is not doing so I have to do it manually:

    # get local Hunspell directory path and user dictionary file
    app_data = os.getenv("APPDATA")
    loc_hunspell_dir = os.path.join(app_data,
                                    "Notepad++\\plugins\\config\\Hunspell")
    loc_hunspell_file = os.path.join(loc_hunspell_dir, "en_US.usr")
    

    Note the 2nd and 3rd lines that show the proper PEP8 indentation when splitting a function argument list over 2 lines.

    A similar Python indent plugin that I have for my other programming editor ViM works properly for PEP8 so I am assuming that the Python Indent plugin for Notepad++ is not properly handling PEP8 indent rules.

    This Python program synchronizes my DspellCheck plugin’s user defined dictionary files between my desktop and laptop outputting a file of merged and sorted unique words that replaces the original files.

    Steve



  • My two cents,
    first I never used that plugin so I’m unaware of its functionality and
    second, pep8 is a proposal not a law.
    I assume the plugin author didn’t like this proposal.



  • NPP already has the ability to set tabs to 4 spaces and to follow tab levels built into it, so it seems to me that a plugin named “Python Indent” would therefore follow PEP8 as NPP is already doing the rest when it comes to tabs and Python. At this point, I do not see what the Python Indent plugin is actually doing.



  • @Steven-Haymes

    I do not see what the Python Indent plugin is actually doing.

    I think you’ve tapped out the depths of anyone that has any knowledge about that plugin here. Barring the author replying or attempting to contact the author more directly, I don’t think you’re going to get any further nuggets on this topic. Of course you can always examine the source code of the plugin itself to get your answers about what it endeavors to do.



  • 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.


Log in to reply