Kate Snippet Adds Indent on Every Line Break - Why?

My newly created snippet, in the Kate editor, to insert a docstring template for Python functions, keeps adding an indent for each line break in the snippet. Why? Is there a way to prevent this?

My Snippet Text:

""" Description: Provides 
PARAM:
PARAM:
PARAM:
PARAM:
USAGE:
NOTE:
TODO: 
"""

Result in Code When I Insert the Snippet:

    def _display_editHeader(self)
    """ Description: Provides 
    PARAM:
        PARAM:
            PARAM:
                PARAM:
                    USAGE:
                        NOTE:
                            TODO: 
                                """
            print('')
            print(f"{''.ljust(self.headerColWidth, '=')}")
            print('Count-In Track will be added based on the following song Characteristics.')
            print('ENTER C to continue, Q to quit and exit, or the number of a parameter to modify:')
            self.display_songInfo(ctIn, song, blankLines=0, forConfirm=True)
1 Like

I think the problem here is not with the snippets plugin, but with the KTextEditor’s auto-indent feature for Python.
Looks like it doesn’t really takes account that we are in a multi-line string, and after each colon increases the indentation. You can disable this by “set-indent-mode none”, but this probably is not what you want, moreover this no longer keeps the base indentation.

The closest thing I was able to come up with in order to work around this issue is the following:

Snippet:

""" Description= Provides
PARAM= 
PARAM= 
PARAM= 
USAGE= 
NOTE= 
TODO= 
    """
${set_indent_mode("none")}${replace_before(8)}${set_indent_mode("python")}

Scripts:

function set_indent_mode(mode) { view.executeCommand("set-indent-mode", mode); return ""; }
function replace_before(before){
    let cur = view.cursorPosition();
    view.setSelection(new Range(cur.line - before, 1, cur.line, 4);
    let res = view.selectedText().replace(/=/g, ":");
    view.removeSelectedText();
    return res;
}

As you can see initially I use “=” instead of colon, so the Python indentation algorithm doesn’t get confused. After that I call the set_indent_mode function in order to disable the indentation, call the replace_before() function to replace the equal sign, then re-enable the indentation mode for Python. The constant in the replace_before() is the number of lines before the function call to process.

The question may rise why I do not disable the indentation at the beginning of the snippiet? Well, the plugin works in a way that first it inserts the snippet, then resolves the macros, therefore at the time the first function is called the output is already messed up.

First, let me say that I really like your avatar Måté. :slightly_smiling_face:

Second - you’ve taught me two things - One - that it is the colons that are driving the cascading indention. Now that I know that I can consider just use an equal sign; or maybe I’ll use a blank space. And more importantly now I know this for future snippets I may create; which I have avoided so far due to this issue.

Two - the thought of using some scripting. I kinda vaguely know you could do that with Kate, but I never really had that in my head. So that’s another possible tool I may be able to make good use of.

Thanks for the help Måté!

Say Måté - where do I put the script? Meaning where do I put the function replace_before(before)?

I found the page Scripting with JavaScript which says Kate scripts are in the location: XDG_DATA_HOME/katepart5/script/indentation. However, on my fedora-40 the environment variable XDG_DATA_HOME is not set. Further, ~/.local has nothing related to KDE in it. And ~.local/share only has /kate, under which is /kuserfeedback, /sessions, and /stash.

I cannot find any katepart5 or katepart6 or katepart anything directory in my .local tree.

In the Edit snippet dialog there is a scripts tab. I used that one.

Ok. I gave that a try, and it does look like it is supposed to work; but for me it generated an error message that I can’t figure out. But it’s just a ‘nice to have’ for me and I don’t want, or need, to devote any time to fussing around with it. So I’m going to let it go.

Thanks for your help on this topic tho!