Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Only format when project configured for formatting #2412

Open
jaraco opened this issue Feb 4, 2024 · 8 comments
Open

Feature: Only format when project configured for formatting #2412

jaraco opened this issue Feb 4, 2024 · 8 comments

Comments

@jaraco
Copy link

jaraco commented Feb 4, 2024

Is your feature request related to a problem? Please describe.

I've recently switched from python-black to lsp-ruff for formatting my Python code in Sublime Text.

I'd previously worked with the maintainers of python-black to enable "format on save" but only when the project appears to be configured to use black. They called this the "smart" setting for "format on save". See the Settings section of the docs.

Without this setting, if I configure "format on save" and then pick up a project that hasn't been configured for ruff, it will get formatted on save, causing unwanted changes.

Describe the solution you'd like

LSP to offer a similar feature - to only format a file if it's present in a project configured for ruff formatting (or whatever formatter is present).

Describe alternatives you've considered
A less convenient feature would be a way to suppress formatting for a given file (ephemerally).

It's also conceivable that the "format on save" option could be enabled in a per-project and per-editor configuration file, but that would be tedious and noisy.

@jaraco jaraco changed the title Feature: Smart format on save Feature: Only format when project configured for formatting Feb 4, 2024
@jwortmann
Copy link
Member

jwortmann commented Feb 4, 2024

It's also conceivable that the "format on save" option could be enabled in a per-project

This is not only conceivable, but already possible:

LSP/LSP.sublime-settings

Lines 14 to 16 in 8696a5d

// This option is also supported in syntax-specific settings and/or in the
// "settings" section of project files.
"lsp_format_on_save": false,

For this you need to use the Sublime Text project feature, i.e. save a *.sublime-project file (Project / Save Project As... from the menu) and edit the "settings" section in that project file.

A "smart" option for this setting might be a bit tricky to implement (but not impossible); it would first need to signalize that to the LSP-* helper plugin if applicable, which would then need to implement the logic to read from pyproject.toml whether the project is configured for ruff formatting, and pass the result back to LSP.

@jaraco
Copy link
Author

jaraco commented Mar 13, 2024

It's also conceivable that the "format on save" option could be enabled in a per-project

This is not only conceivable, but already possible:

Indeed, I've tested and I'm able to disable "format on save" in projects where I don't want it or enable it in projects where I do. I don't believe this solution is the right one, though, because it requires additional per-project configuration that's specific to my development workflow (using Sublime Text) and configuration (LSP plus lsp-ruff plus "format on save" enabled by default). As an example, I'm guessing the Python project wouldn't want me adding a cpython.sublime-settings to the project just to disable the behavior. And I'd like to avoid adding it to the 100+ projects I maintain where I want the setting enabled. I'm not complaining; just sharing my experience.

@rwols
Copy link
Member

rwols commented Mar 13, 2024

Can't this be a server setting like "ruff.smartAutoFormat" and let lsp-ruff (the server) figure it out?

@jaraco
Copy link
Author

jaraco commented Apr 21, 2024

For this you need to use the Sublime Text project feature, i.e. save a *.sublime-project file (Project / Save Project As... from the menu) and edit the "settings" section in that project file.

I've found it's not sufficient to create such a project file. It does work for that current session, but if one exits SublimeText and then re-opens it on that folder (e.g. subl .), the project file is not opened and the setting has no effect. One has to additionally change their workflow to open the project file when working on that project (i.e. subl *.sublime-project). That's rather inconvenient and clunky to have to rewire my brain for each affected project.

@rchl
Copy link
Member

rchl commented Apr 21, 2024

ctrl+shift+p is the default shortcut that opens a project selector that shows the list of projects that were saved. This is how you are really supposed to interact with the projects.

@jaraco
Copy link
Author

jaraco commented Apr 22, 2024

Today I encountered another case where having a .sublime-project is unhelpful. I was editing a specific file (e.g. subl ~/.local/bin/hg) and I was making some changes to it, but ruff's formatting was actually corrupting the intended syntax (replacing ''' with """ where ''' is needed). Again I needed a way to temporarily disable the save-on-format. Is there a way to configure a hot key or action that would disable format-on-save for the current session?

@jaraco
Copy link
Author

jaraco commented Apr 22, 2024

ctrl+shift+p is the default shortcut that opens a project selector that shows the list of projects that were saved. This is how you are really supposed to interact with the projects.

This didn't work for me, but I think on Mac, the hotkey is Ctrl+Cmd+p. I do get a project switcher there. This approach still doesn't suit the workflow very well as it requires having an open session and replacing the current session with a different one... and even I'm already working on that project and I realize the settings aren't loaded, using the quick project switcher causes the current tabs to be closed. It's the wrong workaround for the problem I'm facing.

@jwortmann
Copy link
Member

Is there a way to configure a hot key or action that would disable format-on-save for the current session?

Currently there is no dynamic toggle for that setting, but you could create a custom command to toggle it globally (and add a key binding for it):

import sublime
import sublime_plugin


class ToggleLspSettingCommand(sublime_plugin.ApplicationCommand):

    def run(self, setting_name='lsp_format_on_paste'):
        lsp_settings = sublime.load_settings('LSP.sublime-settings')
        value = lsp_settings.get(setting_name)
        if not isinstance(value, bool):
            raise ValueError(setting_name + 'is not boolean')
        lsp_settings.set(setting_name, not value)
        sublime.save_settings('LSP.sublime-settings')

If/when #2448 gets merged, it should work without the server(s) being restarted.

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

No branches or pull requests

4 participants