I found easy instructions on how to do what I need in pycharm here, for example:
How can I do the equivalent thing easily in komodo? I think the steps needed are to save the file, run autopep8 to modify the file, and then reload the file, right?
Hi, all you need to do is:
- Create a Python formatter via Edit > Preferences > Formatters.
- Click the “+” button, give it a name, select Python for the language, and make it a generic command-line formatter.
- Exit the preferences dialog.
- Enable format-on-save via Code > Format > On File Save using X, where X is the name of the formatted you created.
You know, I had tried that. I deleted all of the python formatters in preferences. Than I added a new one and chose
Generic command-line formatter
Executable to use: /usr/bin/autopep8
Command line arguments: --in-place --aggressive --aggressive
But when I run it with Code->Format document using->autopep8 or set Code->Format->On File Save using->autopep8 and then save the file, it’s not being reformatted.
I must be missing something. I tried Python and Python3 in the Formatter Configuration field- my test script is python 3 and is not modified when I run Code->Format document using->autopep8. It’s not changed at all. There are no error windows popping up or anything.
Are there any error messages displayed in Help > Troubleshooting > View Log File after running your formatter?
Yes, there is! I did not know about this log option! Here it is:
[2018-05-21 14:30:03,449] [ERROR] ko.formatters: Formatting failed
– EXCEPTION START –
[Exception… “Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [koIFormatter.format]” nsresult: “0x80004005 (NS_ERROR_FAILURE)” location: “JS frame :: chrome://komodo/content/formatters/formatters.js :: _formatViewWithFormatterAndContext :: line 135” data: no]
- toString (function) 3 lines
- message (string) ‘Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [koIFormatter.format]’
- result (number) 2147500037
- name (string) ‘NS_ERROR_FAILURE’
- filename (string) ‘chrome://komodo/content/formatters/formatters.js’
- lineNumber (number) 135
- columnNumber (number) 0
- inner (object) null
| + data (object) null
| + stack (string) ‘_formatViewWithFormatterAndContext@chrome://komodo/content/formatters/formatters.js:135:16
- location (object) JS frame :: chrome://komodo/content/formatters/formatters.js :: _formatViewWithFormatterAndContext :: line 135
| + QueryInterface (function) 3 lines
| + language (number) 2
| + filename (string) ‘chrome://komodo/content/formatters/formatters.js’
| + name (string) ‘_formatViewWithFormatterAndContext’
| + lineNumber (number) 135
| + columnNumber (number) 16
| + sourceLine (string) ‘’
| + caller (object) JS frame :: chrome://komodo/content/formatters/formatters.js :: formatViewUsingConfig :: line 870
| + formattedStack (string) ‘_formatViewWithFormatterAndContext@chrome://komodo/content/formatters/formatters.js:135:16
| + stack
– EXCEPTION END –
Hi, would you please post the entire contents of your error log? Despite the fact that there are “Exception start/end” markers, there’s usually some extra helpful information before or after (I forget which). As it stands, I know where the failure occurred, but I don’t know why. Thanks.
I will need to set up a test environment before I can post the entire error log. I see that there were some errors logged because the preferences window was still open after applying changes - could that have an effect on script execution?
I realize that using explicit paths rather than the environment variables used in the examples is less than ideal but did so as a means of troubleshooting. Below is a screenshot of the definition I created - does it look like it should work? I also tried it using - as the last input parameter instead of the --in-place option because it says the text will be passed using stdin.
How would YOU create a formatter to take a python file being edited in komodo and replace it with the same file after it has been reformatted by an external program - in this case: ‘autopep8 --aggressive --aggressive’? The autopep8 website has a nice example file for testing at https://github.com/hhatto/autopep8
autopep8 can be run either with a file argument or with - to read from stdin in the usual unix fashion. When run with a file argument, --in-place can be used to modify the file in place - otherwise, it writes the output to stdout.
Hi, can you run autopep8 manually from the command line? Make sure it’s not throwing any errors, or else Komodo will do the same.
Komodo passes the code to be formatted to the STDIN, so you should tell autopep8 to read the code from it. And Komodo waits for the formatted output at STDOUT, so my guess is you should omit
Yes, it works fine from the command line when run with:
cat bp-copy.py | autopep8 --aggressive --aggressive -
OK - that seems to work! Knowing that komodo uses the specified program as a filter is the key.
Changing the autopep8 Command-line Arguments to “–aggressive --aggressive -” has it working! The key being the hyphen at the end telling autopep8 to read input from stdin. As autopep8 sends it’s output to stdout, Komodo then replaces the contents of the buffer with the autopep8 reformatted contents. Just what we need!
Is there any way to create a keybinding to run this formatter? I checked in the preferences->keybindings but code->format didn’t seem to be one of the options. Does the fact that I am using the emacs keybindings matter?
Thanks for the help!