1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at* 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0* 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FORMATTING_SETTINGS_H 17 #define FORMATTING_SETTINGS_H 18 19 #include <string> 20 21 namespace ark::es2panda::lsp { 22 23 enum class IndentStyle { NONE, BLOCK, SMART }; 24 25 enum class SemicolonPreference { IGNORE, INSERT, REMOVE }; 26 27 struct EditorSettings { 28 public: 29 explicit EditorSettings() = default; 30 GetBaseIndentSizeEditorSettings31 size_t GetBaseIndentSize() const 32 { 33 return baseIndentSize_; 34 } 35 GetIndentSizeEditorSettings36 size_t GetIndentSize() const 37 { 38 return indentSize_; 39 } 40 GetTabSizeEditorSettings41 size_t GetTabSize() const 42 { 43 return tabSize_; 44 } 45 GetNewLineCharacterEditorSettings46 std::string GetNewLineCharacter() const 47 { 48 return newLineCharacter_; 49 } 50 GetConvertTabsToSpacesEditorSettings51 bool GetConvertTabsToSpaces() const 52 { 53 return convertTabsToSpaces_; 54 } 55 GetIndentStyleEditorSettings56 IndentStyle GetIndentStyle() const 57 { 58 return indentStyle_; 59 } 60 GetTrimTrailingWhitespaceEditorSettings61 bool GetTrimTrailingWhitespace() const 62 { 63 return trimTrailingWhitespace_; 64 } 65 SetBaseIndentSizeEditorSettings66 void SetBaseIndentSize(size_t value) 67 { 68 baseIndentSize_ = value; 69 } 70 SetIndentSizeEditorSettings71 void SetIndentSize(size_t value) 72 { 73 indentSize_ = value; 74 } 75 SetTabSizeEditorSettings76 void SetTabSize(size_t value) 77 { 78 tabSize_ = value; 79 } 80 SetNewLineCharacterEditorSettings81 void SetNewLineCharacter(const std::string &value) 82 { 83 newLineCharacter_ = value; 84 } 85 SetConvertTabsToSpacesEditorSettings86 void SetConvertTabsToSpaces(bool value) 87 { 88 convertTabsToSpaces_ = value; 89 } 90 SetIndentStyleEditorSettings91 void SetIndentStyle(IndentStyle value) 92 { 93 indentStyle_ = value; 94 } 95 SetTrimTrailingWhitespaceEditorSettings96 void SetTrimTrailingWhitespace(bool value) 97 { 98 trimTrailingWhitespace_ = value; 99 } 100 101 private: 102 size_t baseIndentSize_ = 4; 103 size_t indentSize_ = 4; 104 size_t tabSize_ = 4; 105 std::string newLineCharacter_ = "\n"; 106 bool convertTabsToSpaces_ = true; 107 IndentStyle indentStyle_ = IndentStyle::SMART; 108 bool trimTrailingWhitespace_ = true; 109 }; 110 111 struct FormatCodeSettings : public EditorSettings { 112 public: 113 explicit FormatCodeSettings() = default; 114 GetInsertSpaceAfterCommaDelimiterFormatCodeSettings115 bool GetInsertSpaceAfterCommaDelimiter() const 116 { 117 return insertSpaceAfterCommaDelimiter_; 118 } 119 GetInsertSpaceAfterSemicolonInForStatementsFormatCodeSettings120 bool GetInsertSpaceAfterSemicolonInForStatements() const 121 { 122 return insertSpaceAfterSemicolonInForStatements_; 123 } 124 GetInsertSpaceBeforeAndAfterBinaryOperatorsFormatCodeSettings125 bool GetInsertSpaceBeforeAndAfterBinaryOperators() const 126 { 127 return insertSpaceBeforeAndAfterBinaryOperators_; 128 } 129 GetInsertSpaceAfterConstructorFormatCodeSettings130 bool GetInsertSpaceAfterConstructor() const 131 { 132 return insertSpaceAfterConstructor_; 133 } 134 GetInsertSpaceAfterKeywordsInControlFlowStatementsFormatCodeSettings135 bool GetInsertSpaceAfterKeywordsInControlFlowStatements() const 136 { 137 return insertSpaceAfterKeywordsInControlFlowStatements_; 138 } 139 GetInsertSpaceAfterFunctionKeywordForAnonymousFunctionsFormatCodeSettings140 bool GetInsertSpaceAfterFunctionKeywordForAnonymousFunctions() const 141 { 142 return insertSpaceAfterFunctionKeywordForAnonymousFunctions_; 143 } 144 GetInsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisFormatCodeSettings145 bool GetInsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis() const 146 { 147 return insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis_; 148 } 149 GetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsFormatCodeSettings150 bool GetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets() const 151 { 152 return insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets_; 153 } 154 GetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBracesFormatCodeSettings155 bool GetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces() const 156 { 157 return insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces_; 158 } 159 GetInsertSpaceAfterOpeningAndBeforeClosingEmptyBracesFormatCodeSettings160 bool GetInsertSpaceAfterOpeningAndBeforeClosingEmptyBraces() const 161 { 162 return insertSpaceAfterOpeningAndBeforeClosingEmptyBraces_; 163 } 164 GetInsertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesFormatCodeSettings165 bool GetInsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces() const 166 { 167 return insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces_; 168 } 169 GetInsertSpaceAfterTypeAssertionFormatCodeSettings170 bool GetInsertSpaceAfterTypeAssertion() const 171 { 172 return insertSpaceAfterTypeAssertion_; 173 } 174 GetInsertSpaceBeforeFunctionParenthesisFormatCodeSettings175 bool GetInsertSpaceBeforeFunctionParenthesis() const 176 { 177 return insertSpaceBeforeFunctionParenthesis_; 178 } 179 GetPlaceOpenBraceOnNewLineForFunctionsFormatCodeSettings180 bool GetPlaceOpenBraceOnNewLineForFunctions() const 181 { 182 return placeOpenBraceOnNewLineForFunctions_; 183 } 184 GetPlaceOpenBraceOnNewLineForControlBlocksFormatCodeSettings185 bool GetPlaceOpenBraceOnNewLineForControlBlocks() const 186 { 187 return placeOpenBraceOnNewLineForControlBlocks_; 188 } 189 GetInsertSpaceBeforeTypeAnnotationFormatCodeSettings190 bool GetInsertSpaceBeforeTypeAnnotation() const 191 { 192 return insertSpaceBeforeTypeAnnotation_; 193 } 194 GetIndentMultiLineObjectLiteralBeginningOnBlankLineFormatCodeSettings195 bool GetIndentMultiLineObjectLiteralBeginningOnBlankLine() const 196 { 197 return indentMultiLineObjectLiteralBeginningOnBlankLine_; 198 } 199 GetSemicolonsFormatCodeSettings200 SemicolonPreference GetSemicolons() const 201 { 202 return semicolons_; 203 } 204 SetInsertSpaceAfterCommaDelimiterFormatCodeSettings205 void SetInsertSpaceAfterCommaDelimiter(bool value) 206 { 207 insertSpaceAfterCommaDelimiter_ = value; 208 } 209 SetInsertSpaceAfterSemicolonInForStatementsFormatCodeSettings210 void SetInsertSpaceAfterSemicolonInForStatements(bool value) 211 { 212 insertSpaceAfterSemicolonInForStatements_ = value; 213 } 214 SetInsertSpaceBeforeAndAfterBinaryOperatorsFormatCodeSettings215 void SetInsertSpaceBeforeAndAfterBinaryOperators(bool value) 216 { 217 insertSpaceBeforeAndAfterBinaryOperators_ = value; 218 } 219 SetInsertSpaceAfterConstructorFormatCodeSettings220 void SetInsertSpaceAfterConstructor(bool value) 221 { 222 insertSpaceAfterConstructor_ = value; 223 } 224 SetInsertSpaceAfterKeywordsInControlFlowStatementsFormatCodeSettings225 void SetInsertSpaceAfterKeywordsInControlFlowStatements(bool value) 226 { 227 insertSpaceAfterKeywordsInControlFlowStatements_ = value; 228 } 229 SetInsertSpaceAfterFunctionKeywordForAnonymousFunctionsFormatCodeSettings230 void SetInsertSpaceAfterFunctionKeywordForAnonymousFunctions(bool value) 231 { 232 insertSpaceAfterFunctionKeywordForAnonymousFunctions_ = value; 233 } 234 SetInsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisFormatCodeSettings235 void SetInsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis(bool value) 236 { 237 insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis_ = value; 238 } 239 SetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsFormatCodeSettings240 void SetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets(bool value) 241 { 242 insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets_ = value; 243 } 244 SetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBracesFormatCodeSettings245 void SetInsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces(bool value) 246 { 247 insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces_ = value; 248 } 249 SetInsertSpaceAfterOpeningAndBeforeClosingEmptyBracesFormatCodeSettings250 void SetInsertSpaceAfterOpeningAndBeforeClosingEmptyBraces(bool value) 251 { 252 insertSpaceAfterOpeningAndBeforeClosingEmptyBraces_ = value; 253 } 254 SetInsertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesFormatCodeSettings255 void SetInsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces(bool value) 256 { 257 insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces_ = value; 258 } 259 SetInsertSpaceAfterTypeAssertionFormatCodeSettings260 void SetInsertSpaceAfterTypeAssertion(bool value) 261 { 262 insertSpaceAfterTypeAssertion_ = value; 263 } 264 SetInsertSpaceBeforeFunctionParenthesisFormatCodeSettings265 void SetInsertSpaceBeforeFunctionParenthesis(bool value) 266 { 267 insertSpaceBeforeFunctionParenthesis_ = value; 268 } 269 SetPlaceOpenBraceOnNewLineForFunctionsFormatCodeSettings270 void SetPlaceOpenBraceOnNewLineForFunctions(bool value) 271 { 272 placeOpenBraceOnNewLineForFunctions_ = value; 273 } 274 SetPlaceOpenBraceOnNewLineForControlBlocksFormatCodeSettings275 void SetPlaceOpenBraceOnNewLineForControlBlocks(bool value) 276 { 277 placeOpenBraceOnNewLineForControlBlocks_ = value; 278 } 279 SetInsertSpaceBeforeTypeAnnotationFormatCodeSettings280 void SetInsertSpaceBeforeTypeAnnotation(bool value) 281 { 282 insertSpaceBeforeTypeAnnotation_ = value; 283 } 284 SetIndentMultiLineObjectLiteralBeginningOnBlankLineFormatCodeSettings285 void SetIndentMultiLineObjectLiteralBeginningOnBlankLine(bool value) 286 { 287 indentMultiLineObjectLiteralBeginningOnBlankLine_ = value; 288 } 289 SetSemicolonsFormatCodeSettings290 void SetSemicolons(SemicolonPreference value) 291 { 292 semicolons_ = value; 293 } 294 295 private: 296 bool insertSpaceAfterCommaDelimiter_ = true; 297 bool insertSpaceAfterSemicolonInForStatements_ = true; 298 bool insertSpaceBeforeAndAfterBinaryOperators_ = true; 299 bool insertSpaceAfterConstructor_ = false; 300 bool insertSpaceAfterKeywordsInControlFlowStatements_ = true; 301 bool insertSpaceAfterFunctionKeywordForAnonymousFunctions_ = false; 302 bool insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis_ = false; 303 bool insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets_ = false; 304 bool insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces_ = true; 305 bool insertSpaceAfterOpeningAndBeforeClosingEmptyBraces_ = false; 306 bool insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces_ = false; 307 bool insertSpaceAfterTypeAssertion_ = false; 308 bool insertSpaceBeforeFunctionParenthesis_ = false; 309 bool placeOpenBraceOnNewLineForFunctions_ = false; 310 bool placeOpenBraceOnNewLineForControlBlocks_ = false; 311 bool insertSpaceBeforeTypeAnnotation_ = false; 312 bool indentMultiLineObjectLiteralBeginningOnBlankLine_ = false; 313 SemicolonPreference semicolons_ = SemicolonPreference::IGNORE; 314 }; 315 316 FormatCodeSettings GetDefaultFormatCodeSettings(const std::string &newLineCharacter); 317 318 } // namespace ark::es2panda::lsp 319 320 #endif // FORMATTING_CODE_SETTINGS_H