vysker / vscode-php-formatter

Visual Studio Code extension. A wrapper for the Sensiolabs PHP CS Fixer. Analyzes some PHP source code and tries to fix coding standards issues (PSR-1 and PSR-2 compatible).
MIT License
93 stars 12 forks source link

formatting on save #4

Closed dxkite closed 7 years ago

dxkite commented 8 years ago

when set onSave is true, i save some file, the file formatted, but file status is "change" i need to save again,Then Formatting Again And Again.... screengif

vysker commented 8 years ago

Thanks for taking the time to open this issue.

Unfortunately there is no fix for this as of now. Microsoft is working on a preSave event for VSCode, which would prevent this issue from cropping up. It is currently in the backlog of the September milestone.

For now, you can work around this issue by creating a custom command that triggers this extension's fix functionality.

In order to avoid future confusion, I will add this to the extension's docs and readme.

dxkite commented 8 years ago

You can add Format Code to Right click menu like Native Command Ctrl+F qq 20160919174336

vysker commented 8 years ago

That's a great suggestion. Do you happen to know how the when condition should be setup to allow file extensions other than php?

I'm trying something like this:

"contributes": {
    "commands": [
      {
        "command": "phpformatter.fix",
        "title": "Format PHP",
        "icon": {
          "light": "./images/fix-icon-light.svg",
          "dark": "./images/fix-icon-dark.svg"
        }
      }
    ],
    "menus": {
      "editor/context": [
        {
          "when": "resourceLangId == php || vscode.workspace.getConfiguration('phpformatter').get('additionalExtensions', []).indexOf(resourceLangId) > -1",
          "command": "phpformatter.fix"
        }
      ]
    }
}
dxkite commented 8 years ago

htm* or tpl ?

vysker commented 8 years ago

Let me elaborate by saying that I'm trying to use a user setting in the when condition.

So, if I configured my extension like this:

"phpformatter.additionalExtensions": [
  "tpl", "inc"
]

Then the context menu option should only be visible when editing either of those file types. Otherwise the context menu option would be visible on every file type, which would just clutter the UI.

dxkite commented 8 years ago

You Can add a item like Format As PHP when The Extension may has Php code,Or Just Use in php file, another file needn't Format such as in html or template : <title><?php echo $title ; ?></title>

vysker commented 8 years ago

Yes, that would indeed be unnecessary. However, what I meant is that it would be even less necessary to have the context menu item at all, it the current file is not a PHP file.

So I'm looking for a way to check whether the current file is a PHP file. If that is the case, the context menu item should be displayed. If it is not a PHP file, it should check the user setting called phpformatter.additionalExtensions. If the current file type matches any of those, it should also display the context menu item.

dxkite commented 8 years ago

Last Night,I use PHP Function token_get_all to Compile template file ( Copy from Laravel ) ,I guess Maybe Can use something like this?

jrieken commented 8 years ago

@Dickurt Since version 1.6 VS Code supports the onWillSaveTextDocument-event. Also there is a new editor.formatOnSave-setting that will invoke registered formatters on save, see #5

vysker commented 7 years ago

Resolved with conversion to TypeScript in latest milestone.