GithubHelp home page GithubHelp logo

yaegassy / coc-php-cs-fixer Goto Github PK

View Code? Open in Web Editor NEW
31.0 2.0 2.0 184 KB

PHP CS Fixer (PHP Coding Standards Fixer) and Laravel Pint extension for coc.nvim

Home Page: https://www.npmjs.com/package/coc-php-cs-fixer

License: MIT License

JavaScript 4.70% TypeScript 95.30%
vim neovim php coc coc-nvim php-cs-fixer coc-extensions pint laravel

coc-php-cs-fixer's Introduction

coc-php-cs-fixer

PHP CS Fixer (PHP Coding Standards Fixer) and Laravel Pint extension for coc.nvim

Install

:CocInstall coc-php-cs-fixer

Features

php-cs-fixer and laravel/pint are supported.

  • Formatter
  • Command
  • Code Action
  • Status Bar
  • pint.json Auto Completion and JSON validation
  • Downloader

Note

The formatter tool used is php-cs-fixer by default. If you want to use laravel/pint, change the php-cs-fixer.activateTool setting in coc-settings.json.

{
  "php-cs-fixer.activateTool": "pint"
}

Detects the php-cs-fixer or pint tool. They are prioritized in order from the top.

  1. php-cs-fixer.toolPath or php-cs-fixer.pint.toolPath
  2. vendor/bin/php-cs-fixer or vendor/bin/pint
  3. php-cs-fixer or pint retrieved by the download feature (:CocCommand php-cs-fixer.download or php-cs-fixer.pintDownload)
    • php-cs-fixer:
      • Mac/Linux: ~/.config/coc/extensions/coc-php-cs-fixer-data/php-cs-fixer
      • Windows: ~/AppData/Local/coc/extensions/coc-php-cs-fixer-data/php-cs-fixer
    • pint:
      • Mac/Linux: ~/.config/coc/extensions/coc-php-cs-fixer-data/pint
      • Windows: ~/AppData/Local/coc/extensions/coc-php-cs-fixer-data/pint

If "1" and "2" above are not detected, the download feature will be executed (The prompt will be displayed)

Usage

coc-php-cs-fixer can be executed in multiple ways.

Auto run when saving a file

Add the settings to coc-settings.json.

{
  "[php]": {
    "coc.preferences.formatOnSave": true
  },
}

If the file size is large or the tool (php-cs-fixer or pint) is slow in your environment, formatting may not work properly when saving. In coc.nvim, time-consuming formatting on save is canceled.

In that case, please perform the formatting in another way.

Run from CocCommand

  • If the php-cs-fixer.activateTool setting is php-cs-fixer
    • :CocCommand php-cs-fixer.fix
  • If the php-cs-fixer.activateTool setting is pint
    • :CocCommand php-cs-fixer.pintFix

Run formatting from call function

  • :call CocAction('format')

Run codeAction from call function

  • :call CocAction('codeAction')
    • If the php-cs-fixer.activateTool setting is php-cs-fixer
      • Choose action: "Run: php-cs-fixer.fix"
    • If the php-cs-fixer.activateTool setting is pint
      • Choose action: "Run: php-cs-fixer.pintFix"

Precedence of "php-cs-fixer" and "laravel/pint" configuration files and options

php-cs-fixer

  1. php-cs-fixer.config setting for this extension.
  2. .php-cs-fixer.php or .php-cs-fixer.dist.php config file in the workspace (project) root.
  3. options-reated settings for this extension. e.g. php-cs-fixer.rules and more.

pint

  1. php-cs-fixer.pint.config setting for this extension.
  2. pint.json config file in the workspace (project) root.
  3. options-reated settings for this extension. php-cs-fixer.pint.preset.

Configuration options

  • php-cs-fixer.enable: Enable coc-php-cs-fixer extension, default: true
  • php-cs-fixer.activateTool: Formatter tool to be used, valid option ["php-cs-fixer", "pint"], default: "php-cs-fixer"
  • php-cs-fixer.toolPath: The path to the php-cs-fixer tool, default: ""
  • php-cs-fixer.config: Path to php-cs-fixer config file (--config), default: ""
  • php-cs-fixer.useCache: Use a cache file when fixing files (--using-cache), default: false
  • php-cs-fixer.allowRisky: Determines whether risky rules are allowed (--allow-risky), default: false
  • php-cs-fixer.rules: Rules to use when fixing files (--rules), e.g. "@PSR12,@Symfony", default: "@PSR12"
  • php-cs-fixer.enableIgnoreEnv: Add the environment variable PHP_CS_FIXER_IGNORE_ENV=1 and run php-cs-fixer, default: false
  • php-cs-fixer.pint.toolPath: The path to the pint tool, default: ""
  • php-cs-fixer.pint.config: Path to pint.json config file (--config), default: ""
  • php-cs-fixer.pint.preset: Presets define a set of rules that can be used to fix code style issues in your code (--preset), valid option ["laravel", "psr12", "symfony"], default: "laravel"
  • php-cs-fixer.downloadCheckOnStartup: If php-cs-fixer or pint is not present at startup, run the built-in download. The tool to be downloaded will follow the php-cs-fixer.activateTool configuration, default: true
  • php-cs-fixer.downloadMajorVersion: Specify the major version of php-cs-fixer to download for the extension, valid option [2, 3], default: 3
  • php-cs-fixer.enableFormatProvider: Enable format provider, default: true
  • php-cs-fixer.enableActionProvider: Enable codeAction provider, default: true
  • php-cs-fixer.terminal.enableSplitRight: Use vertical belowright for dryRunDiff and pintTest terminal window, default: false

Commands

  • php-cs-fixer.fix: Run php-cs-fixer fix
  • php-cs-fixer.dryRunDiff: Run php-cs-fixer fix with --dry-run and --diff in a terminal window | DEMO
  • php-cs-fixer.pintFix: Run pint
  • php-cs-fixer.pintTest: Run pint with --test in a terminal window | DEMO
  • php-cs-fixer.download: Download php-cs-fixer
    • By default, the "v3" series will be downloaded. If you want to download "v2" series, please change the php-cs-fixer.downloadMajorVersion setting.
  • php-cs-fixer.pintDownload: Download pint
  • php-cs-fixer.showOutput: Show php-cs-fixer output channel

Code Actions

  • Run: php-cs-fixer.fix
  • Run: php-cs-fixer.pintFix

Thanks

License

MIT


This extension is built with create-coc-extension

coc-php-cs-fixer's People

Contributors

github-actions[bot] avatar glspdotnet avatar yaegassy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

josecl glspdotnet

coc-php-cs-fixer's Issues

Nothing happend after running :CocCommand php-cs-fixer.download

The following command works under my project root:

./vendor/bin/php-cs-fixer fix --ansi

But nothing happend after running :CocCommand php-cs-fixer.download in vim. Files that can be formatted by the plain command cannot be formatted by this CocCommand.

Format on save does (sometimes) not work with Pint

Hi,

I have a very strange issue with the plugin using Pint.

It does not work with format on save but when I run the :Format (from the base coc-nvim config) it does the job..

In the showOutput it shows that Pint ran and when I debugged the plugin in the class PintFixFormattingEditProvider the function _provideEdits var code has the formatted document.

Been trying to figure out why this is happening but cant solve it.

Also strange is that when I create a new PHP file in my project is does work.

What i've tried:

  • Disabling all plugins except coc-nvim
  • Revert my config and use base coc-nvim from their readme
  • Pint from project and downloaded Pint by plugin

My coc-settings.json:

{
  "php-cs-fixer.activateTool": "pint",
  "coc.preferences.formatOnSaveFiletypes": [
    "php"
  ]
}

How does this work with coc-format-selected

I was trying to figure out how this works with coc-format-selected.

In the recommended settings they have the following key mappings:

" Formatting selected code.
xmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)

When I use that key mapping the code is formatted but I'm not sure it's using this extension.

Does coc-format-selected use this extension under the hood? And if not, is there a way to tell it to?

Using wrong configuration

Simply

A/
 .php-cs-fixer.php
  B/
   .php-cs-fixer.php
   src/
     files..

go inside of B directory and run 'nvim'. After open a php file and save file (so format on save). This plugin uses configuration that inside of A directory, not B. Both are git repository.

How can I make the plugin autosave the changes?

I'm having an issue at the moment where the plugin fixes the file, but the fixes are not saved automatically.

I've tried to add an auto command but it's still saving before the formatting. So I have to save twice to have the formatting changes saved properly.

autocmd BufWritePre *.php silent! call CocCommand php-cs-fixer.fix

I've managed to make it work by adding a sleep but I don't think that's ideal.

function! PhpFixFile()
    CocCommand php-cs-fixer.fix
    sleep 700m
endfunction

autocmd BufWritePre *.php call PhpFixFile()

PHP 8.1 gives an error because php-cs-fixer is not yet supported

It seems that it is not yet supported at this time. Therefore, it outputs an error and cannot be executed.

The workaround at the moment is to use the environment variable PHP_CS_FIXER_IGNORE_ENV. However, this may not result in the expected correct formatting.

bashrc, etc...:

export PHP_CS_FIXER_IGNORE_ENV=1

How to fix current file only?

Hi,

I was wondering if the action php-cs-fixer.fix only fixing the current file or the whole directory? I have it configured to fix automatically upon saving a file, and sometimes I see weird behaviour of my file getting updated as I type, so I have a feeling that it is fixing the whole directory.

If so, is there a way to only fix current buffer/file?

I'm using this to apply the fixes automatically:

autocmd BufWritePost *.php silent! CocCommand php-cs-fixer.fix

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.