-
Notifications
You must be signed in to change notification settings - Fork 69
Make fprime-util format work for libraries #331
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
Open
Sammy-Dabbas
wants to merge
3
commits into
nasa:devel
Choose a base branch
from
Sammy-Dabbas:issue-4926-format-libraries
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+74
−10
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,7 +20,10 @@ | |
| import importlib.metadata | ||
|
|
||
|
|
||
| from fprime.common.error import FprimeException | ||
| from fprime.fbuild.builder import Build, InvalidBuildCacheException | ||
| from fprime.fbuild.settings import IniSettings | ||
| from fprime.fbuild.types import UnableToDetectProjectException | ||
| from fprime.util.code_formatter import ClangFormatter | ||
| from .versioning import VersionException, FPRIME_PIP_PACKAGES | ||
| from fprime.util.cookiecutter_wrapper import ( | ||
|
|
@@ -154,6 +157,51 @@ def run_new( | |
| ) | ||
|
|
||
|
|
||
| def locate_clang_format_file(parsed: argparse.Namespace) -> Path: | ||
| """Locate the .clang-format style file to use for formatting. | ||
|
|
||
| The `format` command must work both inside an F´ project (which declares a | ||
| `framework_path` in settings.ini) and inside a standalone F´ library (which | ||
| has neither a settings.ini nor a project root). Discovery proceeds as: | ||
|
|
||
| 1. If a parent F´ project can be detected, use the `.clang-format` at the | ||
| root of the framework it points to (the historical behavior). | ||
| 2. Otherwise, walk up the directory tree from the working path looking for | ||
| a `.clang-format` file. This mirrors clang-format's own discovery rules | ||
| and lets libraries supply their own style file. | ||
|
|
||
| Args: | ||
| parsed: parsed input arguments | ||
|
|
||
| Returns: | ||
| Path to the .clang-format file to use (may not exist; the caller | ||
| reports a clear error in that case). | ||
| """ | ||
| # Try to resolve the framework's .clang-format via project settings | ||
| try: | ||
| cmake_root = ( | ||
| Path(parsed.root) | ||
| if parsed.root is not None | ||
| else Build.find_nearest_parent_project(Path.cwd()) | ||
| ) | ||
| settings = IniSettings.load(cmake_root / "settings.ini") | ||
| framework_path = settings.get("framework_path") | ||
| if framework_path is not None: | ||
| return Path(framework_path) / ".clang-format" | ||
| except (UnableToDetectProjectException, FprimeException): | ||
| pass # Not in a project (e.g. a standalone library); fall back to search | ||
|
|
||
| # Library fallback: walk up from the working path searching for a .clang-format | ||
| start = parsed.path if parsed.path is not None else Path.cwd() | ||
| for directory in [Path(start).resolve(), *Path(start).resolve().parents]: | ||
| candidate = directory / ".clang-format" | ||
| if candidate.is_file(): | ||
| return candidate | ||
| # Nothing found: return the working-directory candidate so the error message | ||
| # points at a sensible location | ||
| return Path(start).resolve() / ".clang-format" | ||
|
|
||
|
|
||
| def run_code_format( | ||
| build: Build, | ||
| parsed: argparse.Namespace, | ||
|
|
@@ -164,7 +212,7 @@ def run_code_format( | |
| """Runs code formatting using clang-format | ||
|
|
||
| Args: | ||
| build: used to retrieve .clang-format file | ||
| build: unused; format runs without a build cache (may be None) | ||
| parsed: parsed input arguments | ||
| __: unused cmake_args | ||
| ___: unused make_args | ||
|
|
@@ -179,7 +227,7 @@ def run_code_format( | |
| } | ||
| clang_formatter = ClangFormatter( | ||
| "clang-format", | ||
| build.settings.get("framework_path", Path(".")) / ".clang-format", | ||
| locate_clang_format_file(parsed), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be useful to print out the found location in the case that |
||
| options, | ||
| ) | ||
| if not clang_formatter.is_supported(): | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the default
clang-formatbehavior so I think it'd be better to just delegate to it, and not rewrite the logic ourselves.