forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…13814) Add the `scalafmt` tool for `fmt` and `lint` goals. Pants will choose the nearest ancestor `.scalafmt.conf` file as the configuration file to use for a source file. This differs from the logic used by `scalafmt` which only chooses between a `.scalafmt.conf` in the same directory as a file or one in the root of the repository. This should provide a better experience for multi-team monorepos where it could be useful to have a `.scalafmt.conf` that applies to a subtree of the repository. Closes pantsbuild#11943.
- Loading branch information
Tom Dyas
authored
Dec 7, 2021
1 parent
59abe54
commit 1c47a19
Showing
16 changed files
with
2,452 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/python/pants/backend/experimental/scala/lint/scalafmt/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources() |
15 changes: 15 additions & 0 deletions
15
src/python/pants/backend/experimental/scala/lint/scalafmt/register.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
from pants.backend.experimental.scala.register import rules as all_scala_rules | ||
from pants.backend.scala.lint import scala_lang_fmt | ||
from pants.backend.scala.lint.scalafmt import rules as scalafmt_rules | ||
from pants.backend.scala.lint.scalafmt import skip_field | ||
|
||
|
||
def rules(): | ||
return [ | ||
*all_scala_rules(), | ||
*scala_lang_fmt.rules(), | ||
*scalafmt_rules.rules(), | ||
*skip_field.rules(), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
from dataclasses import dataclass | ||
from typing import Iterable | ||
|
||
from pants.backend.scala.target_types import ScalaSourceField | ||
from pants.core.goals.fmt import FmtResult, LanguageFmtResults, LanguageFmtTargets | ||
from pants.core.goals.style_request import StyleRequest | ||
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest | ||
from pants.engine.fs import Digest, Snapshot | ||
from pants.engine.internals.selectors import Get | ||
from pants.engine.rules import collect_rules, rule | ||
from pants.engine.unions import UnionMembership, UnionRule, union | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ScalaLangFmtTargets(LanguageFmtTargets): | ||
required_fields = (ScalaSourceField,) | ||
|
||
|
||
@union | ||
class ScalaLangFmtRequest(StyleRequest): | ||
pass | ||
|
||
|
||
@rule | ||
async def format_scala_target( | ||
scala_fmt_targets: ScalaLangFmtTargets, union_membership: UnionMembership | ||
) -> LanguageFmtResults: | ||
original_sources = await Get( | ||
SourceFiles, | ||
SourceFilesRequest(target[ScalaSourceField] for target in scala_fmt_targets.targets), | ||
) | ||
prior_formatter_result = original_sources.snapshot | ||
|
||
results = [] | ||
fmt_request_types: Iterable[type[StyleRequest]] = union_membership[ScalaLangFmtRequest] | ||
for fmt_request_type in fmt_request_types: | ||
request = fmt_request_type( | ||
( | ||
fmt_request_type.field_set_type.create(target) | ||
for target in scala_fmt_targets.targets | ||
if fmt_request_type.field_set_type.is_applicable(target) | ||
), | ||
prior_formatter_result=prior_formatter_result, | ||
) | ||
if not request.field_sets: | ||
continue | ||
result = await Get(FmtResult, ScalaLangFmtRequest, request) | ||
results.append(result) | ||
if result.did_change: | ||
prior_formatter_result = await Get(Snapshot, Digest, result.output) | ||
return LanguageFmtResults( | ||
tuple(results), | ||
input=original_sources.snapshot.digest, | ||
output=prior_formatter_result.digest, | ||
) | ||
|
||
|
||
def rules(): | ||
return [*collect_rules(), UnionRule(LanguageFmtTargets, ScalaLangFmtTargets)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources(dependencies=[":lockfile"]) | ||
|
||
python_tests(name="tests", timeout=360) | ||
|
||
resource(name="lockfile", source="scalafmt.default.lockfile.txt") |
Empty file.
Oops, something went wrong.