question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoking tool from command line for VSCode Extension

See original GitHub issue

Hello,

I’m in the process of writing a VSCode extension that adds support for reorder-python-imports. As part of this process, I need to be able to provide a file that may not yet have been saved to your tool, so that it can sort its imports. Ideally, I would like to provide the text of the file via stdin, and get the file output via stdout. However, to get this to work, I have the following questions:

  1. Is reorder-python-imports /dev/stdin still the recommended method of reading from stdin? ~It might be better if this was reorder-python-imports -- instead to indicate that the file to be read will be from stdin, as that is a more platform agnostic way of doing things.~
  2. --print-only is marked as deprecated, but I don’t see another way of getting the output of the file that is easy to parse for a command line tool (writing a regex to parse the difflib output seems tedious). Is there a better way that I’m not aware of to solve this, or is there any chance that option could be un-deprecated?
  3. My understanding is that reorder-python-imports uses static analysis to determine which imports are third party and which are first party. Will providing the file via stdin to this tool mess that up? What impact does the current working directory have on this static analysis?

Looking forward to getting this tool better supported in my favorite IDE! I strongly prefer your approach over isort 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
TheButlahcommented, May 7, 2020

By the way, the first working version is available at this location: https://github.com/TheButlah/vscode-reorder-python-imports

There’s still a few issues to iron out, namely figuring out how to avoid isort from microsoft’s python extension overwriting reorder-python-imports changes on filesaves, but there ya go.

1reaction
asottilecommented, May 5, 2020

for (1.) and (2.) reorder-python-imports - does exactly what you want (reads from stdin, writes to stdout)

for 3. the filename itself shouldn’t matter unless the script is importing from itself (but that seems super rare???). the working directory is pretty sensitive as that’s used to find the local modules – there’s also the --application-directories option which is commonly used when using a src layout (for example)

hope that helps! some other people were looking for a vs code extension in #117 so maybe once it’s completed you can resolve that one as well!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Command Line Interface (CLI) - Visual Studio Code
Visual Studio Code has a powerful command-line interface built-in that lets you control how you launch the editor. You can open files, install...
Read more >
Managing Extensions in Visual Studio Code
You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the...
Read more >
Integrated Terminal in Visual Studio Code
From the Command Palette (Ctrl+Shift+P), use the View: Toggle Terminal command. The integrated terminal can run commands such as mkdir and git just...
Read more >
Commands | Visual Studio Code Extension API
The vscode.commands.executeCommand API programmatically executes a command. This lets you use VS Code's built-in functionality, and build on extensions such as ...
Read more >
Terminal Shell Integration in Visual Studio Code
Visual Studio Code has the ability to integrate with common shells, allowing the terminal to understand more about what's actually happening inside the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found