Форматтер кода Scala (стиль Scalafmt)

РазработчикТекст
Реклама · УДАЛИТЬ?
Реклама · УДАЛИТЬ?

Гид

Scala Code Formatter (Scalafmt Style)

Форматтер кода Scala (стиль Scalafmt)

Paste raw Scala source and get back code formatted in the Scalafmt house style — 2-space indentation, balanced braces, normalized operator spacing, sorted imports, and consistent trailing commas. Output is deterministic, so the same input always produces the same result, which means cleaner diffs in code review and no more bikeshedding over whitespace.

Как использовать

  1. Paste your Scala code into the input box, or load the sample to see how it works.
  2. Pick an indent style (2 spaces is the Scalafmt default) and a maximum column width (80, 100, 120, or 160).
  3. Toggle the options you want: trailing commas in multi-line lists, sorted/grouped imports, operator spacing, comment preservation, and blank-line collapsing.
  4. Нажмите Формат. Copy the result with the copy button or download it as a .scala файл.

Возможности

  • Scalafmt default style – Mirrors the conventions used by the official scalafmt tool, including indentation, brace handling, and binary operator spacing.
  • Настраиваемое форматирование отступов – Choose 2 spaces, 4 spaces, or tabs to match your project’s style guide.
  • Max column width – Pick 80, 100, 120, or 160 to suit your team’s wrap policy.
  • Sorted & grouped imports – Reorders imports alphabetically and groups them into stdlib, third-party, and local buckets with blank lines between groups.
  • Завершающие запятые – Adds trailing commas to multi-line argument and parameter lists for cleaner version-control diffs.
  • Сохранение комментариев – Keeps line comments, block comments, and Scaladoc intact while still tidying the surrounding code.
  • String & interpolation safe – Reformatting never touches the contents of strings, character literals, triple-quoted strings, or s"..." / f"..." interpolations.
  • Scala 3 friendly – Understands modern Scala 3 keywords like given, using, then, enumи derives.
  • Работает полностью в браузере – No upload, no server round-trip, no telemetry. Your code never leaves your machine.

Распространенные случаи использования

  • Tidy up a quick code snippet before pasting it into a Slack thread, Stack Overflow answer, or technical blog post.
  • Normalize formatting in legacy projects that don’t yet have a scalafmt config checked in.
  • Pre-format generated code (macro output, schema-derived case classes, etc.) so it reads like hand-written Scala.
  • Standardize style across mixed-author files where each contributor used a slightly different IDE setting.
  • Sanity-check a small refactor without spinning up sbt or the Scala build pipeline.

Часто задаваемые вопросы

  1. What is Scalafmt and why is consistent Scala formatting important?

    Scalafmt is the de facto code formatter for the Scala ecosystem. It enforces a single, deterministic style across an entire codebase, which removes formatting from code-review discussions, reduces noisy diffs caused by whitespace changes, and helps new contributors blend in with the rest of the project. Consistent formatting also makes it easier to scan unfamiliar code because indentation and spacing always carry the same meaning.

  2. Why does Scalafmt default to a maximum column width of 80?

    The 80-column convention dates back to early terminals and punched cards, but it survives today because narrower lines are easier to read in side-by-side diffs, code review tools, and split editor panes. An 80-column limit also encourages shorter identifiers, less deeply nested expressions, and more frequent extraction of helper methods. Many teams relax this to 100 or 120 columns, which is why modern formatters let you configure it.

  3. What are trailing commas and why are they useful?

    A trailing comma is a comma after the last element of a multi-line list, argument list, or parameter list. They became common in formatters because they make line-based diffs cleaner: adding a new element only adds one line instead of also modifying the previous line to add a comma. Trailing commas also make it easier to reorder lines, since every element ends the same way. Scala has supported trailing commas in argument and parameter lists since version 2.12.2.

  4. Why are imports typically grouped into stdlib, third-party, and local?

    Grouping imports by origin is a long-standing convention borrowed from languages like Go and Python (with isort). It makes dependency provenance visible at a glance: standard-library imports come first, followed by third-party libraries, followed by code from the same project. The blank lines between groups act as visual anchors so reviewers can quickly spot when a change introduces a new external dependency, which is often a signal worth discussing.

  5. What is the difference between syntactic and semantic code formatting?

    A syntactic formatter rearranges whitespace, line breaks, and indentation based purely on the source text — it doesn't need to understand types, resolve symbols, or run the compiler. A semantic formatter, by contrast, can rename identifiers, reorder methods based on usage, or remove unused imports because it has access to the compiler's view of the program. Scalafmt is primarily syntactic, which makes it fast and safe to run on incomplete or non-compiling code.

Хотите убрать рекламу? Откажитесь от рекламы сегодня

Установите наши расширения

Добавьте инструменты ввода-вывода в свой любимый браузер для мгновенного доступа и более быстрого поиска

в Расширение Chrome в Расширение края в Расширение Firefox в Расширение Opera

Табло результатов прибыло!

Табло результатов — это интересный способ следить за вашими играми, все данные хранятся в вашем браузере. Скоро появятся новые функции!

Реклама · УДАЛИТЬ?
Реклама · УДАЛИТЬ?
Реклама · УДАЛИТЬ?

новости с техническими моментами

Примите участие

Помогите нам продолжать предоставлять ценные бесплатные инструменты

Купи мне кофе
Реклама · УДАЛИТЬ?