Skip to content

Commit

Permalink
haiku-format based on clang-format 10.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca committed Aug 10, 2021
1 parent ef32c61 commit bcc5a44
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
24 changes: 24 additions & 0 deletions _haiku-format
@@ -0,0 +1,24 @@
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignEscapedNewlines: DontAlign
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AlwaysBreakAfterReturnType: TopLevelDefinitions
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterExternBlock: true
AfterFunction: true
BeforeCatch: true
IndentCaseLabels: true
IndentWidth: 4
MaxEmptyLinesToKeep: 2
PointerAlignment: Left
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: false
TabWidth: 4
UseTab: Always
5 changes: 4 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -758,6 +758,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
Penalty += Style.PenaltyBreakFirstLessLess;

State.Column = getNewLineColumn(State);
if (State.Line->startsWith(tok::kw_for) && PreviousNonComment &&
PreviousNonComment->is(tok::semi))
State.Column -= State.Column % 4;

// Indent nested blocks relative to this column, unless in a very specific
// JavaScript special case where:
Expand Down Expand Up @@ -1129,7 +1132,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
// ^ line up here.
State.Stack.back().Indent =
State.Column +
(Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma
(Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon
? 0
: 2);
State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Format/Format.cpp
Expand Up @@ -2534,7 +2534,7 @@ const char *StyleOptionHelpDescription =
"Coding style, currently supports:\n"
" LLVM, Google, Chromium, Mozilla, WebKit.\n"
"Use -style=file to load style configuration from\n"
".clang-format file located in one of the parent\n"
".haiku-format file located in one of the parent\n"
"directories of the source file (or current\n"
"directory for stdin).\n"
"Use -style=\"{key: value, ...}\" to set specific\n"
Expand Down Expand Up @@ -2619,8 +2619,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
return make_string_error(EC.message());

llvm::SmallVector<std::string, 2> FilesToLookFor;
FilesToLookFor.push_back(".clang-format");
FilesToLookFor.push_back("_clang-format");
FilesToLookFor.push_back(".haiku-format");
FilesToLookFor.push_back("_haiku-format");

for (StringRef Directory = Path; !Directory.empty();
Directory = llvm::sys::path::parent_path(Directory)) {
Expand Down
23 changes: 23 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -2073,6 +2073,7 @@ class ExpressionParser {

void TokenAnnotator::setCommentLineLevels(
SmallVectorImpl<AnnotatedLine *> &Lines) {
AnnotatedLine *NextCommentLine = nullptr;
const AnnotatedLine *NextNonCommentLine = nullptr;
for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
E = Lines.rend();
Expand All @@ -2085,6 +2086,16 @@ void TokenAnnotator::setCommentLineLevels(
}
}

if (CommentLine) {
NextCommentLine = *I;
} else {
if (NextCommentLine && NextCommentLine->First->NewlinesBefore == 1 &&
NextCommentLine->First->OriginalColumn ==
(*I)->First->OriginalColumn + 4)
NextCommentLine->Level = (*I)->Level + 1;
NextCommentLine = nullptr;
}

// If the comment is currently aligned with the line immediately following
// it, that's probably intentional and we should keep it.
if (NextNonCommentLine && CommentLine &&
Expand Down Expand Up @@ -2146,6 +2157,15 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {

Line.First->SpacesRequiredBefore = 1;
Line.First->CanBreakBefore = Line.First->MustBreakBefore;

static bool Comment = false;
if (Line.First->is(tok::comment))
Comment = true;
else if (Comment)
Comment = false;
else if (Line.Level == 0 && !Line.First->IsFirst &&
Line.MightBeFunctionDecl && Line.mightBeFunctionDefinition())
Line.First->NewlinesBefore = 3;
}

// This function heuristically determines whether 'Current' starts the name of a
Expand Down Expand Up @@ -3224,6 +3244,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
Right.Previous->MatchingParen->NestingLevel == 0 &&
Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes)
return true;
if (Left.NestingLevel == 0 &&
Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma))
return true;
if (Right.is(TT_CtorInitializerComma) &&
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
!Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
Expand Down
2 changes: 1 addition & 1 deletion clang/tools/clang-format/ClangFormat.cpp
Expand Up @@ -441,7 +441,7 @@ static bool format(StringRef FileName) {
} // namespace clang

static void PrintVersion(raw_ostream &OS) {
OS << clang::getClangToolFullVersion("clang-format") << '\n';
OS << clang::getClangToolFullVersion("haiku-format") << '\n';
}

// Dump the configuration.
Expand Down
4 changes: 4 additions & 0 deletions install.sh
@@ -0,0 +1,4 @@
#!/bin/bash -e

cp -uv _haiku-format ~/.haiku-format
cp -uv build/bin/clang-format ~/config/non-packaged/bin/haiku-format

0 comments on commit bcc5a44

Please sign in to comment.