Skip to content
This repository has been archived by the owner on Dec 24, 2020. It is now read-only.

How to enable formatting #63

Open
p5a0u9l opened this issue Jul 17, 2019 · 14 comments
Open

How to enable formatting #63

p5a0u9l opened this issue Jul 17, 2019 · 14 comments

Comments

@p5a0u9l
Copy link

p5a0u9l commented Jul 17, 2019

Unable to find from documentation how I should enable formatting which is "off" by default. It seems like there is no provision for python.formatting.enabled setting.

@dofinn
Copy link

dofinn commented Jul 26, 2019

bump

@chemzqm
Copy link
Member

chemzqm commented Jul 26, 2019

python.formatting.provider

@chemzqm
Copy link
Member

chemzqm commented Jul 28, 2019

Use "coc.preferences.formatOnType": true to enable format on type feature.
Use "coc.preferences.formatOnSaveFiletypes" to include filetypes you want format on save.
Use :call CocAction('format') to format currrent document.
Use

xmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)

to format selected range.

@danjenson
Copy link

Hm, this doesn't seem to be working; my coc-settings.json has:

{
...
"python.formatting.provider": "yapf",
"coc.preferences.formatOnSaveFiletypes": ["py"]
...
}

And I still have to manually call :Format for it to work.

@fannheyward
Copy link
Member

Try "coc.preferences.formatOnSaveFiletypes": ["python"]

@lanox
Copy link

lanox commented Nov 18, 2019

I am having same issue here as well, I cant seem to get formatting to work.

  "python.formatting.provider": "black",
  "python.formatting.blackPat": "/Users/lp/.local/share/virtualenvs/akamaictrl-BweOeLIM/bin/black",
  "coc.preferences.formatOnSaveFiletypes": [
    "python",
    "json"
  ],

when i run black i get this

black akamaictrl.py
error: cannot format akamaictrl.py: Cannot parse: 14:0:  def run(region, file):
Oh no! 💥 💔 💥
1 file failed to reformat.

I have indented def run on purpose

@AdrienLemaire
Copy link

AdrienLemaire commented Jan 21, 2020

Adding the python.formatting.blackPath fixed the problem for me. It probably broke because I had set python.pythonPath to my virtualenv python, and coc was having trouble to find the path for black.

@maxcountryman
Copy link

Is there a trick to getting formatting to format as you type?

@gabriel-viviani
Copy link

Is there a trick to getting formatting to format as you type?

As far as I know even if you do this trick it will decrease significantly your typing experience. It's much better formatting on save ;D

@ngocbh
Copy link

ngocbh commented Jul 27, 2020

Code formatting is useful in general. But sometimes, I just do not want to use it. For example, when I want to append the library to sys in the first line. Code formatting ( I use autopep8 ) forces the import of the library above sys.append, and I have an error.

Could I save file without autopep8 in some situations?

For example, :w for write with autopep8 and :wwp for write without autopep8

@DarrienG
Copy link

DarrienG commented Aug 4, 2020

Could someone post a full config that makes it work?

@ccaprani
Copy link

ccaprani commented Aug 9, 2020

Agree, it would be good to have a working example. I've been at this for a few days now on and off and can't get black to format on save. Have tried above ideas. Using nvim on Windows (for my sins). Here is my coc-settings file:

{
    "suggest.enablePreview": true,
    "python.jediEnabled": true,
    "coc.preferences.formatOnType": true,
    "coc.preferences.formatOnSaveFiletypes": ["python"],
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "C:\\Users\\ccapr\\Anaconda3\\Scripts\\black.exe",
    "python.linting.enabled": true,
    "python.linting.pylintUseMinimalCheckers": false,
    "python.linting.pylintPath": "pylint",
    "python.condaPath": "conda"
}

@DarrienG
Copy link

DarrienG commented Aug 9, 2020

This is super handy, for anyone on Linux like myself, this is the minimal config you need:

{                                                                                                                                                                            
  "python.formatting.provider": "black",
  "coc.preferences.formatOnType": true,
  "coc.preferences.formatOnSaveFiletypes": ["python"]
}

If you have python installed, CoC will prompt you to install black and you're done. Apologies I can't help with the Windows config.

@zstreeter
Copy link

I am having trouble with formatting with black. Here is my pertinent setup:

{
     "python.jediEnabled": false,
     "python.pythonPath": "~/opt/miniconda3/envs/INTELenv/bin/python",
     "python.pipenvPath": "~/opt/miniconda3/envs/INTELenv/bin/pip",
     "python.condaPath": "~/opt/miniconda3/bin/conda",
     "python.linting.enabled": true,
     "python.linting.pylintUseMinimalCheckers": false,
     "python.linting.pylintEnabled": true,
     "python.linting.pylintPath":"~/opt/miniconda3/envs/INTELenv/bin/pylint",
     "python.linting.flake8Args": ["--max-line-length=80"],
     "python.formatting.provider": "black",
     "python.formatting.blackArgs": ["--line-length","80"],
     "python.formatting.blackPath": "~/opt/miniconda3/envs/INTELenv/bin/black",
     "coc.preferences.formatOnSaveFiletypes": [
            "python",
            "javascript",
            "html",
            "markdown",
            "css",
            "dart"
    ],
}

When I save a file, it says "Formatted with Black" but nothing happens. I checked that black was working by using it in the terminal in my INTELenv and it worked. Though it wouldn't force 80 lines with the following command:

black --line-length=80 program.py

Could there be a plugin or other setting screwing things up?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests