File indexing completed on 2024-05-05 04:39:00
0001 /* 0002 SPDX-FileCopyrightText: 2008 CÃ ©dric Pasteur <cedric.pasteur@free.fr> 0003 SPDX-FileCopyrightText: 2001 Matthias Hölzer-Klüpfel <mhk@caldera.de> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #ifndef ASTYLEFORMATTER_H 0009 #define ASTYLEFORMATTER_H 0010 0011 #include <QVariant> 0012 #include <QString> 0013 0014 #include "astyle.h" 0015 0016 namespace AStyleOptionKey { 0017 QString forceTabs(); 0018 QString tabSpaceConversion(); 0019 QString fillEmptyLines(); 0020 QString bracesAdd(); 0021 } 0022 0023 class AStyleFormatter 0024 { 0025 public: 0026 /** Creates an empty AStyleFormatter with C style by default. 0027 */ 0028 AStyleFormatter(); 0029 0030 QString formatSource(const QString& text, const QString& leftContext = QString(), const QString& rightContext = QString()); 0031 0032 QVariant option(const QString &name) const; 0033 0034 bool predefinedStyle(const QString &name); 0035 void loadStyle(const QString &content); 0036 QString saveStyle() const; 0037 0038 // The following 3 setters specify the programming language of the code that is being formatted. 0039 // A source file's MIME type should be used to autodetect the language. Knowing the programming language 0040 // allows the formatter to identify language-specific syntax, such as classes, templates and keywords. 0041 void setCStyle(); 0042 void setJavaStyle(); 0043 void setSharpStyle(); 0044 0045 // fill 0046 /// Indents using one tab per indentation and disables tab-space conversion. 0047 void setTabIndentation(int length, bool forceTabs); 0048 /// Indents using {@p length per indentation} spaces and sets tab-space conversion to @p tabSpaceConversion. 0049 void setSpaceIndentationAndTabSpaceConversion(int length, bool tabSpaceConversion); 0050 /// Indents using {@p length per indentation} spaces and disables tab-space conversion. 0051 void setSpaceIndentationNoConversion(int length); 0052 void setEmptyLineFill(bool on); 0053 // indent+continuation 0054 void setBlockIndent(bool on); 0055 void setBracketIndent(bool on); 0056 void setCaseIndent(bool on); 0057 void setClassIndent(bool on); 0058 void setLabelIndent(bool on); 0059 void setNamespaceIndent(bool on); 0060 void setPreprocessorIndent(bool on); 0061 void setSwitchIndent(bool on); 0062 void setMaxInStatementIndentLength(int max); 0063 void setMinConditionalIndentLength(int min); 0064 void setAfterParens(bool on); 0065 void setContinuation(int n); 0066 //brackets 0067 void setBracketFormatMode(astyle::BraceMode mode); 0068 void setBreakClosingHeaderBracketsMode(bool state); 0069 void setAddBracesMode(bool state); 0070 //blocks 0071 void setBreakBlocksMode(bool state); 0072 void setBreakElseIfsMode(bool state); 0073 void setBreakClosingHeaderBlocksMode(bool state); 0074 //padding 0075 void setOperatorPaddingMode(bool mode); 0076 void setParensOutsidePaddingMode(bool mode); 0077 void setParensInsidePaddingMode(bool mode); 0078 void setParensHeaderPaddingMode(bool mode); 0079 void setParensUnPaddingMode(bool state); 0080 //oneliners 0081 void setBreakOneLineBlocksMode(bool state); 0082 void setBreakOneLineStatementsMode(bool state); 0083 //pointer 0084 void setPointerAlignment(astyle::PointerAlign alignment); 0085 0086 private: 0087 class Engine : public astyle::ASFormatter 0088 { 0089 public: 0090 // ASBeautifier::setBlockIndent() and ASBeautifier::setBraceIndent() became protected in 2011. 0091 // The upstream commit https://sourceforge.net/p/astyle/code/285 "removes" them 0092 // ("Remove indent-brackets and indent-blocks options.") with the following suggestions: 0093 // --indent-brackets: THIS IS NO LONGER A VALID OPTION. Instead use style=whitesmith or style=banner. 0094 // --indent-blocks: THIS IS NO LONGER A VALID OPTION. Instead use style=gnu. 0095 // Still, the now-protected functions work just fine. They are called internally by ASFormatter 0096 // for STYLE_WHITESMITH and STYLE_GNU. In order to make our predefined styles match the 0097 // upstream built-in styles, we need to call these protected functions, and so inherit 0098 // this class Engine from ASFormatter to make them public. 0099 using astyle::ASFormatter::setBlockIndent; 0100 using astyle::ASFormatter::setBraceIndent; 0101 }; 0102 0103 void updateFormatter(); 0104 void resetStyle(); 0105 0106 QVariantMap m_options; 0107 Engine m_engine; 0108 }; 0109 0110 #endif // ASTYLEFORMATTER_H