diff options
author | wolfbeast <mcwerewolf@wolfbeast.com> | 2019-01-02 11:05:16 +0100 |
---|---|---|
committer | wolfbeast <mcwerewolf@wolfbeast.com> | 2019-01-02 11:05:16 +0100 |
commit | eec3d00bb3e9ae428ee762b32bd0d1cceca44d19 (patch) | |
tree | 738bfe912c40775bdd995e9ad7d55a5b076cc8f9 | |
parent | 517f502980cd5238e1aad66bd2f98a106db624a7 (diff) | |
download | UXP-eec3d00bb3e9ae428ee762b32bd0d1cceca44d19.tar UXP-eec3d00bb3e9ae428ee762b32bd0d1cceca44d19.tar.gz UXP-eec3d00bb3e9ae428ee762b32bd0d1cceca44d19.tar.lz UXP-eec3d00bb3e9ae428ee762b32bd0d1cceca44d19.tar.xz UXP-eec3d00bb3e9ae428ee762b32bd0d1cceca44d19.zip |
Update UXP Coding Style guideline document (WIP)
-rw-r--r-- | docs/UXP Coding Style.md | 134 |
1 files changed, 119 insertions, 15 deletions
diff --git a/docs/UXP Coding Style.md b/docs/UXP Coding Style.md index 72655678f..1d4b132d5 100644 --- a/docs/UXP Coding Style.md +++ b/docs/UXP Coding Style.md @@ -1,4 +1,4 @@ -# UXP Coding Style Guide +# UXP Coding Style Guide While our source tree is currently in a number of different Coding Styles and the general rule applies to adhere to style of surrounding code when you are making changes, it is our goal to unify the style of our source tree and making it adhere to a single style. This document describes the preferred style for new source code files and the preferred style to convert existing files to. @@ -10,11 +10,11 @@ The following formatting rules apply to all code: - Always use spaces for indentation, never use tabs! - Put a space between a keyword and parenthesis, e.g. `if (`. - Put a space between variables and operators, e.g. `a == b`. -- Put a space after a comma in variable lists, e.g. `function (a, b, c)`. +- Put a space after a comma or semicolon in variable lists, e.g. `function (a, b, c)` or `for (i = 1; i < 10; i++)`. - Indentation of scopes is 2 spaces. - Indentation of long lines is variable-aligned or expression-aligned (see "long line wrapping") - Conditional defines are always placed on column 1. This is also true for nested defines. -- Maximum line length is 120 characters. This departs from the often-used 80-character limit which is rooted in the archaic use of 80-column text terminals and should no longer apply in this day. +- Maximum line length is 120 characters. This departs from the often-used 80-character limit which is rooted in the archaic use of 80-column text terminals and should no longer apply in this day. Github, our chief web VCS, has no issues dealing with 120 character lines either. - Variables passed to functions are passed on one line unless the line would exceed the maximum length, in which case variables will be passed 1-per-line, expression-aligned (see "long line wrapping"). - Avoid trailing commas. - Comment blocks are line-quoted if they appear within functions. @@ -22,12 +22,51 @@ The following formatting rules apply to all code: ## C and C++ Applies to `*.c`, `*.cc`, `*.cpp` and `*.h`. Potentially also `*.mm` for Mac code. +### General formatting guidelines +- Place function return types, including modifiers like `static` on the same line as the function signature. + ```C++ + static bool somefunction(int var1, char* var2) { + ... + } + ``` +- When using templates, do not add spaces between the template name and its object. + ```C++ + nsCOMPtr<nsISomeInterfaceType> + ``` +- Use braces, even for single-command lines. Avoid placing executed code on the expression line or using single-line indentation to indicate the scope. Always brace executed code. + + WRONG: + ```C++ + if (a == b) do_something(); + if (a == b) + do_something(); + if (a == b) { do_something(); } + ``` + CORRECT: + ```C++ + if (a == b) { + do_something(); + } + ``` +- Pointer types: When declaring pointer types, place the `*` with the type. When referencing pointer types, place `*` or `&` with the variable. Do not use `*` by itself with whitespace. + + WRONG: + ```C++ + int * myvar; + ``` + CORRECT: + ```C++ + char* someString; + function a(char* someString) [...] + myString = *someString; + myPointer = &myString; + ``` ### Flow control Flow control expressions should follow the following guidelines: - Scopes have their opening braces on the expression line - Scopes have their closing braces on a separate line, indent-aligned with the flow control expression - Any alternative flow control paths are generally started with an expression on the closing brace line -- Case statements are indent-aligned with the case expression content directly following the case. +- Case statements are indented by 2 on a new line with the case expression on its own line. - Flow control default scopes are always placed at the bottom. #### if..else `if..else` statements example: @@ -56,17 +95,77 @@ while (something == true) { `switch..case` flow control example: ```C++ switch (variable) { - case value1: code_for_1; - code_for_1; - break; - case value2: code_for_2; - code_for_2; - // fallthrough - case value3: code_for_3; - break; - default: code_for_default; - code_for_default; -} + case value1: + // Comment describing 1 + code_for_1; + code_for_1; + break; + case value2: + code_for_2; + code_for_2; + // fallthrough + case value3: + case value4: + code_for_3_and_4; + break; + default: + code_for_default; + code_for_default; +} +``` +### Classes +Classes have some special formatting rules applied to them: +- Classes follow the following sequence: CTOR, DTOR, methods, variables +- Class CTOR variable initializers are listed one-per-line with a leading comma (or colon for the first). +- `public`, `private` and `protected` keywords are not indented and are at the same level as the class identifier statement. + +Example class definition: +```C++ +class FocusClassExample : public Runnable { +public: + FocusClassExample(nsISupports* aTarget, + EventMessage aEventMessage, + nsPresContext* aContext, + nsPIDOMWindowOuter* aOriginalFocusedWindow, + nsIContent* aOriginalFocusedContent, + EventTarget* aRelatedTarget) + : mTarget(aTarget) + , mContext(aContext) + , mEventMessage(aEventMessage) + , mOriginalFocusedWindow(aOriginalFocusedWindow) + , mOriginalFocusedContent(aOriginalFocusedContent) + , mRelatedTarget(aRelatedTarget) { + CTOR_initializer_code here + } + + ~FocusClassExample { + DTOR_code here + } + + NS_IMETHOD Run() override { + nsCOMPtr<nsIContent> originalWindowFocus = mOriginalFocusedWindow ? + mOriginalFocusedWindow->GetFocusedNode() : + nullptr; + // Blink does not check that focus is the same after blur, but WebKit does. + // Opt to follow Blink's behavior (see bug 687787). + if (mEventMessage == eFocusOut || originalWindowFocus == mOriginalFocusedContent) { + InternalFocusEvent event(true, mEventMessage); + event.mFlags.mBubbles = true; + event.mFlags.mCancelable = false; + event.mRelatedTarget = mRelatedTarget; + return EventDispatcher::Dispatch(mTarget, mContext, &event); + } + return NS_OK; + } + + nsCOMPtr<nsISupports> mTarget; + RefPtr<nsPresContext> mContext; +private: + EventMessage mEventMessage; + nsCOMPtr<nsPIDOMWindowOuter> mOriginalFocusedWindow; + nsCOMPtr<nsIContent> mOriginalFocusedContent; + nsCOMPtr<EventTarget> mRelatedTarget; +}; ``` ## JavaScript Applies to `*.js` and `*.jsm`. @@ -75,3 +174,8 @@ Applies to `*.xul`, `*.html`, `*.xhtml`. ## IDL Applies to `*.idl`, `*.xpidl` and `*.webidl`. +<!--stackedit_data: +eyJoaXN0b3J5IjpbODA5MjEzNTEyLC01Mzg0MjM4MDAsMzgyNj +I3NDYzLDIwODYwMjIwODUsLTE1MjU5MjE2MjIsLTY1OTMzMTA0 +MCwtNzQwOTE5MDQ1LDE4Njc1NTQxNDJdfQ== +-->
\ No newline at end of file |