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?
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)
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.
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.
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.
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.
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.