Skip to content

Commit

Permalink
Add Editor support.md (#1135)
Browse files Browse the repository at this point in the history
Add instructions for common editors.

Closes #1124 and #1125
  • Loading branch information
Spitfire1900 authored Sep 18, 2023
1 parent 77c29ef commit 16ad355
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
82 changes: 82 additions & 0 deletions EDITOR SUPPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Using YAPF with your editor

YAPF is supported by multiple editors via community extensions or plugins.

- [IntelliJ/PyCharm](#intellijpycharm)
- [IPython](#ipython)
- [VSCode](#vscode)

## IntelliJ/PyCharm

Use the `File Watchers` plugin to run YAPF against a file when you perform a save.

1. Install the [File Watchers](https://www.jetbrains.com/help/idea/using-file-watchers.html) Plugin
1. Add the following `.idea/watcherTasks.xml` to your project. If you already have this file just add the `TaskOptions` section from below. This example uses Windows and a virtual environment, modify the `program` option as appropriate.
```xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<TaskOptions isEnabled="true">
<option name="arguments" value="-i $FilePathRelativeToProjectRoot$" />
<option name="checkSyntaxErrors" value="true" />
<option name="description" />
<option name="exitCodeBehavior" value="ERROR" />
<option name="fileExtension" value="py" />
<option name="immediateSync" value="true" />
<option name="name" value="yapf" />
<option name="output" value="" />
<option name="outputFilters">
<array />
</option>
<option name="outputFromStdout" value="false" />
<option name="program" value="$PROJECT_DIR$/.venv/Scripts/yapf.exe" />
<option name="runOnExternalChanges" value="true" />
<option name="scopeName" value="Project Files" />
<option name="trackOnlyRoot" value="false" />
<option name="workingDir" value="$Projectpath$" />
<envs />
</TaskOptions>
</component>
</project>
```

## IPython

IPython supports formatting lines automatically when you press the `<Enter>` button to submit the current code block.

Make sure that the YAPF module is available to the IPython runtime:

```shell
pip install ipython yapf
```

pipx example:

```shell
pipx install ipython
pipx inject ipython yapf
```

Add following to `~/.ipython/profile_default/ipython_config.py`:

```python
c.TerminalInteractiveShell.autoformatter = 'yapf'
```

## VSCode

VSCode has deprecated support for YAPF in its official Python extension [in favor of dedicated formatter extensions](https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions).

1. Install EeyoreLee's [yapf](https://marketplace.visualstudio.com/items?itemName=eeyore.yapf) extension.
1. Install the yapf package from pip.
```
pip install yapf
```
1. Add the following to VSCode's `settings.json`:
```jsonc
"[python]": {
"editor.formatOnSaveMode": "file",
"editor.formatOnSave": true,
"editor.defaultFormatter": "eeyore.yapf" # choose this extension
},
```
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ optional arguments:
-vv, --verbose print out file names while processing
```

### Using YAPF within your favorite editor
YAPF is supported by multiple editors via community extensions or plugins. See [Editor Support](EDITOR%20SUPPORT.md) for more info.

### Return Codes

Normally YAPF returns zero on successful program termination and non-zero
Expand Down

0 comments on commit 16ad355

Please sign in to comment.