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

Try to use formatters for subfiletypes #380

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fenuks
Copy link
Contributor

@fenuks fenuks commented Oct 27, 2021

This adds support for multiple filetypes e.g. vimwiki.markdown. Currently neoformat only tries first filetype, which is problematic because as far I'm concerned, it's not possible to configure neoformat to use e.g. markdown prettier formatter for vimwiki. Not even :Neoformat prettier would work, because neoformat then searched prettier formatter definition for vimwiki which doesn't exist.

I only changed code for formatting, and didn't touch code for formatter autocompletion. I could search for all filetypes, but then there is problem to decide which formatter definition to use. We can assume that first definition within filetype should be used. So if I my &filetype=vimwiki.markdown, and I run :Neoformat prettier, code should try to find prettier for vimwiki, and then for markdown, and so on. For now, I left current behaviour, i.e. first filetype is used, in my example that would be vimwiki. and :Neoformat prettier would fail to find formatter.

I will mention this in docs when we will agree if that's fine. Also possibly this change should be configurable with global variable, I can add this if requested.

@fenuks
Copy link
Contributor Author

fenuks commented Nov 5, 2021

@sbdchd Can you please give your thoughts on this?

if using_user_passed_formatter
call neoformat#utils#msg('formatter definition for ' . a:user_input . ' not found')
for filetype_formatters in formatters
let filetype = filetype_formatters[0]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should enable multiple file formatters conditionally

My concern is that someone could have a filetype like typescript.jsx or something and the first one works but the second one fails

or maybe the two formatters are incompatible and there is fighting between them

I'm curious what typical multi-file filetypes are common in the ecosystem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to tell. Generally I would expect that if filetype is typescript.jsx then formatters for all filetypes should work correctly. In the same vein, it's uncertain whether all typescript formatters can handle jsx within file (or any other subfiletype that I might have invented), and yet currently NeoFormat will treat it as valid typescript file, which it might be not. I think it's generally a good assumption.

I think NeoFormat already has mechanisms to fulfil most common scenarios. User can specify per filetype whether one or more formatters should be used, and it is also possible to disable formatter. If I added option to disable search for subfiletype formatters with global or buffer level variable, combination of these three variables should be flexible enough to enable or disable this feature for specific filetypes. It could be disabled by default to maintain existing behaviour.
It's worth considering if NeoFormat should make additional checks like g:neoformat_enabled_typescript_jsx = ['prettier']. I think that is not necessary, it's very much possible to achieve that by setting b:neoformat_enabled_filetype in filetype plugin.

I wonder whether formatters should be de-duplicated. Let's stick to typescript.jsx, and suppose both filetypes have prettier definition. Should I run only first formatter, or run prettier twice? Perhaps prettier definition for jsx is different, e.g. different flags are passed to the command, or user has different settings for jsx prettier formatter, let say they wish different indentation width. This scenario could also be handled with proper setting enabled formatters in filetype plugin, though.

In conclusion, I think a variable to enable this behaviour should be sufficient. Disabling it by default will prevent users from stumbling upon weird combinations that wouldn't work without some manual tweaking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbdchd So, what do you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so I added option to enable using formatters for all subfiletypes. By default, Neoformat works as before, only first subfiletype is being considered.

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

Successfully merging this pull request may close these issues.

2 participants