flems icon indicating copy to clipboard operation
flems copied to clipboard

Feature request: option to re-indent code

Open foucist opened this issue 6 years ago • 7 comments

Would be nice to re-indent the whole page of code when things end up off from bad pastes and such.

foucist avatar Mar 04 '18 09:03 foucist

It would be easy to integrate this feature with Sparser and PrettyDiff or better yet Unibeautify which supports 37 languages and can digest JS Beautify, ES Lint, Prettier rulesets enabling users the ability to use their preferred tool.

panoply avatar Aug 11 '19 14:08 panoply

Yeah, I've been hesitant adding it because I don't want to increase bundle size too much, but I'm thinking it could be nice if it's loaded async once the feature is used, so I'm game 😊 PR welcome 😉

porsager avatar Aug 11 '19 15:08 porsager

I've been going over the Flems codebase (it's beautiful btw) but where best to apply formatting to the string content? Applying within the actions.js file would be how I'd initially tackle this, thoughts?

Edit:

Additionally, providing the option to auto-format would require some form of diffing, so on second thought, it's probably best creating a separate function, as it would be helpful for users to be able to toggle formatting and apply on an action basis (eg: not formatting on each change).

panoply avatar Aug 17 '19 17:08 panoply

Slightly unrelated, but I made this userscript a while back: https://github.com/fuzetsu/userscripts/blob/master/prettier-anything/prettier-anything.user.js

When you highlight some code and press Alt+Shift+I it loads prettier, formats the code and replaces the selection. If you also hold Ctrl it will instead copy the formatted code to your clipboard. Works well on flems (and most other websites).

Adding something like this to flems would probably work well enough.

fuzetsu avatar Aug 17 '19 17:08 fuzetsu

Very nice @fuzetsu – Will have a look (P.S, recently used mergerino on a project, very cool) – Currently I'm applying formatting using Sparser + PrettyDiff mostly because it's language aware and rather efficient. Prettier can be restrictive.

panoply avatar Aug 17 '19 18:08 panoply

Thanks @panoply! and fair enough, prettier is opinionated for sure. Not for everyone.

fuzetsu avatar Aug 17 '19 18:08 fuzetsu

Few hours later....

A multitude of formatters could be supported here, including Prettier. Formatting using a key-binding (hotkey), upon paste or manually invoked in the editor using a button (this would require UI changes) would be my suggestions. Formatting upon type (keystroke) is where things get a little more difficult, it's possible but you'd be continuously passing editor contents to a formatter parser, so maybe formatting in intervals could be a better solution to auto format.

could be nice if it's loaded async once the feature is used

Much like the compilers are applied, the formatters could be loaded in the same way, upon demand and asynchronously. If in the future flems was to support additional languages/variations like HTML (Pug) or Markdown (.md) providing support wouldn't be difficult.

What about defining rules?

Defining formatting rules could look something like below (just an example, hasn't been thought through) but here the iframe preview is replaced with a JSON code editor, where rulessets are applied based on the formatter, of course it be a lot more simple than this, I just like the aesthetic.

Screenshot 2019-08-18 at 02 12 19

Moving forward, I'll take a stab at this over the next couple of weeks.

panoply avatar Aug 18 '19 00:08 panoply