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

Implement "format region" feature for formatters with native support #150

Closed
lassik opened this issue Aug 11, 2021 · 5 comments
Closed

Implement "format region" feature for formatters with native support #150

lassik opened this issue Aug 11, 2021 · 5 comments

Comments

@lassik
Copy link
Owner

lassik commented Aug 11, 2021

Continued from #17

The following formatters already have this:

  • clang-format
  • prettier
  • swiftformat

We should add a format-all-region command to use the feature.

If you know of other formatters that also have official command line flags for formatting a region, please mention!

@drcxd
Copy link

drcxd commented Aug 11, 2021

In case you do not know about clang-format.el. I hope this could help you.

@lassik
Copy link
Owner Author

lassik commented Aug 11, 2021

Thanks.

For reference, from https://clang.llvm.org/docs/ClangFormat.html:

  --cursor=<uint>            - The position of the cursor when invoking
                               clang-format from an editor integration
  --offset=<uint>            - Format a range starting at this byte offset.
                               Multiple ranges can be formatted by specifying
                               several -offset and -length pairs.
                               Can only be used with one input file.
  --length=<uint>            - Format a range of this length (in bytes).
                               Multiple ranges can be formatted by specifying
                               several -offset and -length pairs.
                               When only a single -offset is specified without
                               -length, clang-format will format up to the end
                               of the file.
                               Can only be used with one input file.
  --lines=<string>           - <start line>:<end line> - format a range of
                               lines (both 1-based).
                               Multiple ranges can be formatted by specifying
                               several -lines arguments.
                               Can't be used with -offset and -length.
                               Can only be used with one input file.

@lassik
Copy link
Owner Author

lassik commented Aug 11, 2021

From prettier --help:

  --cursor-offset <int>    Print (to stderr) where a cursor at the given position would move to after
                           formatting.
                           This option cannot be used with --range-start and --range-end.
                           Defaults to -1.
  --range-start <int>      Format code starting at a given character offset.
                           The range will extend backwards to the start of the first line containing the
                           selected statement.
                           This option cannot be used with --cursor-offset.
                           Defaults to 0.
  --range-end <int>        Format code ending at a given character offset (exclusive).
                           The range will extend forwards to the end of the selected statement.
                           This option cannot be used with --cursor-offset.
                           Defaults to Infinity.

@lassik
Copy link
Owner Author

lassik commented Aug 11, 2021

From swiftformat --help:

--linerange        Range of lines to process within the input file (first, last)
--fragment         Input is part of a larger file: "true" or "false" (default)

lassik added a commit that referenced this issue Aug 12, 2021
Implement for clang-format, emacs-lisp, prettier, swiftformat.

A :features field is added into formatter definitions. Since most
formatters do not support formatting the region, (:features region)
indicates the ones who do.

Bumps the minimum Emacs version from 24.3 to 24.4.
@lassik
Copy link
Owner Author

lassik commented Aug 12, 2021

Done. If there are other formatters that also have region support, please send PRs updating their definitions!

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

No branches or pull requests

2 participants