oni icon indicating copy to clipboard operation
oni copied to clipboard

Feature Request: Format Selection with Prettier

Open parkerault opened this issue 7 years ago • 4 comments

Is it possible to set up the integrated prettier plugin to format a selection in the buffer without saving the file? I often run a dev server in watch mode and I would like to be able to preview the changes before I save the file. @Akin909 the plugin is fantastic, thanks for adding it! Is this something that can be easily added?

parkerault avatar Apr 20 '18 16:04 parkerault

@parkerault I think the prettier module comes with a format selection method and I briefly considered adding it on the first pass but with all the things that still needed implementation at that point it seemed overly ambitious at the time.

I should be able to add that without too much trouble the main thing to figure out is how oni getting and setting a visual selection works (haven't worked with that part of oni's api yet), but have seen bits of it so should be fine 😆.

akinsho avatar Apr 21 '18 16:04 akinsho

Little bit offtop maybe, but I would like to see the ability to format not-yet-saved-and-named file (when you just open a new instance of oni). My motivation is that I'm copying pieces of json from the logs of VM, pasting it in oni and want to format the output right away, without save a named file

saitonakamura avatar Dec 20 '18 09:12 saitonakamura

Yeah, ideally it would operate on buffers, not files, so you could format an arbitrary amount of text without saving to disk.

parkerault avatar Dec 20 '18 16:12 parkerault

Its probably worth making an issue for that (ie formatting the buffer without saving).

I think the reason currently that isn't possible is due to the attempted grabbing of the custom prettierrc

https://github.com/onivim/oni/blob/eefa28326c872bbf3673282014f45252ccb8b385/extensions/oni-plugin-prettier/index.js#L102

Since an unsaved file does not have have a filePath it fails, due to

https://github.com/onivim/oni/blob/eefa28326c872bbf3673282014f45252ccb8b385/extensions/oni-plugin-prettier/index.js#L54-L63

If the logic here could be updated then it should work fine. Can the check just be swapped to instead of throwing an error we return some known empty value? The next step after that is comparing if the default or file specific config should be used, so the worst case is that the default config is applied.

(This can all be tested with the command in the Ctrl-P menu).

As for the actual issue, as mentioned it doesn't seem to hard to get prettier itself to work, its more the getting the correct region in. I think we can use something like the following:

Oni.editors.activeEditor.neovim.eval(`getpos("v")`)

Which returns [BufferNum, Line, Col, Offset] of the start of the visual region, and then we can just use Oni.editors.activeEditor.activeBuffer.cursor to get the current cursor position to work out the end of the cursor. That gives the start and end of the visual region.

It can probably be assumed we only care about the line numbers, since I can't think of a time someone would only want to format from a given column to another?

CrossR avatar Dec 20 '18 17:12 CrossR