Skip to content

Commit

Permalink
add clang-format script
Browse files Browse the repository at this point in the history
  • Loading branch information
gretacb committed Aug 4, 2017
1 parent edab78c commit dbef468
Show file tree
Hide file tree
Showing 12 changed files with 718 additions and 469 deletions.
88 changes: 88 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
# Mapbox.Variant C/C+ style
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
34 changes: 34 additions & 0 deletions scripts/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

set -eu
set -o pipefail

: '
Runs clang-format on the code in src/
Return `1` if there are files to be formatted, and automatically formats them.
Returns `0` if everything looks properly formatted.
'
# Set up the environment by installing mason and clang++
# See https://github.com/mapbox/node-cpp-skel/blob/master/docs/extended-tour.md#configuration-files
./scripts/setup.sh --config local.env
source local.env

# Add clang-format as a dep
mason install clang-format ${MASON_LLVM_RELEASE}
mason link clang-format ${MASON_LLVM_RELEASE}
find src/ -type f -name '*.hpp' -o -name '*.cpp' \
| xargs -I{} clang-format -i -style=file {}

dirty=$(git ls-files --modified)

if [[ $dirty ]]; then
echo "The following files have been modified:"
echo $dirty
exit 1
else
exit 0
fi
30 changes: 17 additions & 13 deletions src/module.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
#include <nan.h>
#include "object_async/hello_async.hpp"
#include "object_sync/hello.hpp"
#include "standalone/hello.hpp"
#include "standalone_async/hello_async.hpp"
#include "object_sync/hello.hpp"
#include "object_async/hello_async.hpp"
#include <nan.h>
// #include "your_code.hpp"

// "target" is a magic var that nodejs passes into a module's scope.
// When you write things to target, they become available to call from Javascript world.
static void init(v8::Local<v8::Object> target) {
// When you write things to target, they become available to call from
// Javascript world.
static void init(v8::Local<v8::Object> target)
{

// expose hello method
Nan::SetMethod(target, "hello", standalone::hello);

// expose helloAsync method
Nan::SetMethod(target, "helloAsync", standalone_async::helloAsync);

// expose HelloObject class
object_sync::HelloObject::Init(target);

// expose HelloObjectAsync class
object_async::HelloObjectAsync::Init(target);

/**
* You may have noticed there are multiple "hello" functions as part of this module.
* They are exposed a bit differently.
* 1) standalone::hello // exposed above
* 2) HelloObject.hello // exposed in object_sync/hello.cpp as part of HelloObject
*/
/**
* You may have noticed there are multiple "hello" functions as part of this
* module.
* They are exposed a bit differently.
* 1) standalone::hello // exposed above
* 2) HelloObject.hello // exposed in object_sync/hello.cpp as part of
* HelloObject
*/

// add more methods/classes below that youd like to use in Javascript world
// then create a directory in /src with a .cpp and a .hpp file.
Expand Down
42 changes: 23 additions & 19 deletions src/module_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@

namespace utils {

/*
* This is an internal function used to return callback error messages instead of
* throwing errors.
* Usage:
*
* v8::Local<v8::Function> callback;
* return CallbackError("error message", callback); // "return" is important to prevent duplicate callbacks from being fired!
*
*
* "inline" is important here as well. See for more contex:
* - https://github.com/mapbox/node-cpp-skel/pull/52#discussion_r126847394 for context
* - https://github.com/mapbox/cpp/pull/29 ...eventually point this to glossary when it merges
*
*/
inline void CallbackError(std::string message, v8::Local<v8::Function> callback) {
v8::Local<v8::Value> argv[1] = { Nan::Error(message.c_str()) };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, 1, argv);
}

/*
* This is an internal function used to return callback error messages instead of
* throwing errors.
* Usage:
*
* v8::Local<v8::Function> callback;
* return CallbackError("error message", callback); // "return" is important to
* prevent duplicate callbacks from being fired!
*
*
* "inline" is important here as well. See for more contex:
* - https://github.com/mapbox/node-cpp-skel/pull/52#discussion_r126847394 for
* context
* - https://github.com/mapbox/cpp/pull/29 ...eventually point this to glossary
* when it merges
*
*/
inline void CallbackError(std::string message,
v8::Local<v8::Function> callback)
{
v8::Local<v8::Value> argv[1] = {Nan::Error(message.c_str())};
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, 1, argv);
}
}
Loading

0 comments on commit dbef468

Please sign in to comment.