Skip to content

Commit

Permalink
Create workflow for code formatting (#17)
Browse files Browse the repository at this point in the history
* Create workflow for code formatting

* add .styleguide file

* Create .clang-format

* License for default style guide

* change to different formatter workflow

* test Google format

* Auto fix

* clang auto fix

* Fix code style issues with clang_format

---------

Co-authored-by: having11 <[email protected]>
Co-authored-by: 5690 <[email protected]>
Co-authored-by: Lint Action <[email protected]>
  • Loading branch information
4 people authored Feb 8, 2023
1 parent bf89a8f commit a149104
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 114 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BasedOnStyle: Google
IndentWidth: 4
Language: Cpp
30 changes: 30 additions & 0 deletions .github/workflows/wpiformat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Lint and Format

on:
push:
branches:
- 'main'
pull_request:

permissions:
checks: write
contents: write

jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Install ClangFormat
run: sudo apt-get install -y clang-format

- name: Run linters
uses: wearerequired/lint-action@v2
with:
clang_format: true
auto_fix: true
clang_format_auto_fix: true
30 changes: 13 additions & 17 deletions src/main/cpp/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ void Robot::RobotInit() {}
* <p> This runs after the mode specific periodic functions, but before
* LiveWindow and SmartDashboard integrated updating.
*/
void Robot::RobotPeriodic() {
frc2::CommandScheduler::GetInstance().Run();
}
void Robot::RobotPeriodic() { frc2::CommandScheduler::GetInstance().Run(); }

/**
* This function is called once each time the robot enters Disabled mode. You
Expand All @@ -34,23 +32,23 @@ void Robot::DisabledPeriodic() {}
* RobotContainer} class.
*/
void Robot::AutonomousInit() {
m_autonomousCommand = m_container.GetAutonomousCommand();
m_autonomousCommand = m_container.GetAutonomousCommand();

if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
if (m_autonomousCommand) {
m_autonomousCommand->Schedule();
}
}

void Robot::AutonomousPeriodic() {}

void Robot::TeleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
if (m_autonomousCommand) {
m_autonomousCommand->Cancel();
}
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
if (m_autonomousCommand) {
m_autonomousCommand->Cancel();
}
}

/**
Expand All @@ -74,7 +72,5 @@ void Robot::SimulationInit() {}
void Robot::SimulationPeriodic() {}

#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
int main() { return frc::StartRobot<Robot>(); }
#endif
26 changes: 13 additions & 13 deletions src/main/cpp/RobotContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
#include "commands/ExampleCommand.h"

RobotContainer::RobotContainer() {
// Initialize all of your commands and subsystems here
// Initialize all of your commands and subsystems here

// Configure the button bindings
ConfigureBindings();
// Configure the button bindings
ConfigureBindings();
}

void RobotContainer::ConfigureBindings() {
// Configure your trigger bindings here
// Configure your trigger bindings here

// Schedule `ExampleCommand` when `exampleCondition` changes to `true`
frc2::Trigger([this] {
return m_subsystem.ExampleCondition();
}).OnTrue(ExampleCommand(&m_subsystem).ToPtr());
// Schedule `ExampleCommand` when `exampleCondition` changes to `true`
frc2::Trigger([this] {
return m_subsystem.ExampleCondition();
}).OnTrue(ExampleCommand(&m_subsystem).ToPtr());

// Schedule `ExampleMethodCommand` when the Xbox controller's B button is
// pressed, cancelling on release.
m_driverController.B().WhileTrue(m_subsystem.ExampleMethodCommand());
// Schedule `ExampleMethodCommand` when the Xbox controller's B button is
// pressed, cancelling on release.
m_driverController.B().WhileTrue(m_subsystem.ExampleMethodCommand());
}

frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// An example command will be run in autonomous
return autos::ExampleAuto(&m_subsystem);
// An example command will be run in autonomous
return autos::ExampleAuto(&m_subsystem);
}
4 changes: 2 additions & 2 deletions src/main/cpp/commands/Autos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
#include "commands/ExampleCommand.h"

frc2::CommandPtr autos::ExampleAuto(ExampleSubsystem* subsystem) {
return frc2::cmd::Sequence(subsystem->ExampleMethodCommand(),
ExampleCommand(subsystem).ToPtr());
return frc2::cmd::Sequence(subsystem->ExampleMethodCommand(),
ExampleCommand(subsystem).ToPtr());
}
4 changes: 2 additions & 2 deletions src/main/cpp/commands/ExampleCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

ExampleCommand::ExampleCommand(ExampleSubsystem* subsystem)
: m_subsystem{subsystem} {
// Register that this command requires the subsystem.
AddRequirements(m_subsystem);
// Register that this command requires the subsystem.
AddRequirements(m_subsystem);
}
16 changes: 8 additions & 8 deletions src/main/cpp/subsystems/ExampleSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
#include "subsystems/ExampleSubsystem.h"

ExampleSubsystem::ExampleSubsystem() {
// Implementation of subsystem constructor goes here.
// Implementation of subsystem constructor goes here.
}

frc2::CommandPtr ExampleSubsystem::ExampleMethodCommand() {
// Inline construction of command goes here.
// Subsystem::RunOnce implicitly requires `this` subsystem.
return RunOnce([/* this */] { /* one-time action goes here */ });
// Inline construction of command goes here.
// Subsystem::RunOnce implicitly requires `this` subsystem.
return RunOnce([/* this */] { /* one-time action goes here */ });
}

bool ExampleSubsystem::ExampleCondition() {
// Query some boolean state, such as a digital sensor.
return false;
// Query some boolean state, such as a digital sensor.
return false;
}

void ExampleSubsystem::Periodic() {
// Implementation of subsystem periodic method goes here.
// Implementation of subsystem periodic method goes here.
}

void ExampleSubsystem::SimulationPeriodic() {
// Implementation of subsystem simulation periodic method goes here.
// Implementation of subsystem simulation periodic method goes here.
}
38 changes: 19 additions & 19 deletions src/main/include/Robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@

#pragma once

#include <optional>

#include <frc/TimedRobot.h>
#include <frc2/command/CommandPtr.h>

#include <optional>

#include "RobotContainer.h"

class Robot : public frc::TimedRobot {
public:
void RobotInit() override;
void RobotPeriodic() override;
void DisabledInit() override;
void DisabledPeriodic() override;
void AutonomousInit() override;
void AutonomousPeriodic() override;
void TeleopInit() override;
void TeleopPeriodic() override;
void TestPeriodic() override;
void SimulationInit() override;
void SimulationPeriodic() override;
public:
void RobotInit() override;
void RobotPeriodic() override;
void DisabledInit() override;
void DisabledPeriodic() override;
void AutonomousInit() override;
void AutonomousPeriodic() override;
void TeleopInit() override;
void TeleopPeriodic() override;
void TestPeriodic() override;
void SimulationInit() override;
void SimulationPeriodic() override;

private:
// Have it empty by default so that if testing teleop it
// doesn't have undefined behavior and potentially crash.
std::optional<frc2::CommandPtr> m_autonomousCommand;
private:
// Have it empty by default so that if testing teleop it
// doesn't have undefined behavior and potentially crash.
std::optional<frc2::CommandPtr> m_autonomousCommand;

RobotContainer m_container;
RobotContainer m_container;
};
20 changes: 10 additions & 10 deletions src/main/include/RobotContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
* commands, and trigger mappings) should be declared here.
*/
class RobotContainer {
public:
RobotContainer();
public:
RobotContainer();

frc2::CommandPtr GetAutonomousCommand();
frc2::CommandPtr GetAutonomousCommand();

private:
// Replace with CommandPS4Controller or CommandJoystick if needed
frc2::CommandXboxController m_driverController{
OperatorConstants::kDriverControllerPort};
private:
// Replace with CommandPS4Controller or CommandJoystick if needed
frc2::CommandXboxController m_driverController{
OperatorConstants::kDriverControllerPort};

// The robot's subsystems are defined here...
ExampleSubsystem m_subsystem;
// The robot's subsystems are defined here...
ExampleSubsystem m_subsystem;

void ConfigureBindings();
void ConfigureBindings();
};
18 changes: 9 additions & 9 deletions src/main/include/commands/ExampleCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
class ExampleCommand
: public frc2::CommandHelper<frc2::CommandBase, ExampleCommand> {
public:
/**
* Creates a new ExampleCommand.
*
* @param subsystem The subsystem used by this command.
*/
explicit ExampleCommand(ExampleSubsystem* subsystem);
public:
/**
* Creates a new ExampleCommand.
*
* @param subsystem The subsystem used by this command.
*/
explicit ExampleCommand(ExampleSubsystem* subsystem);

private:
ExampleSubsystem* m_subsystem;
private:
ExampleSubsystem* m_subsystem;
};
60 changes: 30 additions & 30 deletions src/main/include/subsystems/ExampleSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@
#include <frc2/command/SubsystemBase.h>

class ExampleSubsystem : public frc2::SubsystemBase {
public:
ExampleSubsystem();

/**
* Example command factory method.
*/
frc2::CommandPtr ExampleMethodCommand();

/**
* An example method querying a boolean state of the subsystem (for example, a
* digital sensor).
*
* @return value of some boolean subsystem state, such as a digital sensor.
*/
bool ExampleCondition();

/**
* Will be called periodically whenever the CommandScheduler runs.
*/
void Periodic() override;

/**
* Will be called periodically whenever the CommandScheduler runs during
* simulation.
*/
void SimulationPeriodic() override;

private:
// Components (e.g. motor controllers and sensors) should generally be
// declared private and exposed only through public methods.
public:
ExampleSubsystem();

/**
* Example command factory method.
*/
frc2::CommandPtr ExampleMethodCommand();

/**
* An example method querying a boolean state of the subsystem (for example,
* a digital sensor).
*
* @return value of some boolean subsystem state, such as a digital sensor.
*/
bool ExampleCondition();

/**
* Will be called periodically whenever the CommandScheduler runs.
*/
void Periodic() override;

/**
* Will be called periodically whenever the CommandScheduler runs during
* simulation.
*/
void SimulationPeriodic() override;

private:
// Components (e.g. motor controllers and sensors) should generally be
// declared private and exposed only through public methods.
};
8 changes: 4 additions & 4 deletions src/test/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "gtest/gtest.h"

int main(int argc, char** argv) {
HAL_Initialize(500, 0);
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
HAL_Initialize(500, 0);
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
}

0 comments on commit a149104

Please sign in to comment.