| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : PHPFormatterBuffer.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef PHPFORMATTERBUFFER_H |
| 27 |
#define PHPFORMATTERBUFFER_H |
| 28 |
|
| 29 |
#include "PhpLexerAPI.h" |
| 30 |
|
| 31 |
#include <stack> |
| 32 |
#include <wx/string.h> |
| 33 |
|
| 34 |
enum ePHPFormatterFlags { |
| 35 |
kPFF_UseTabs = (1 << 1), |
| 36 |
kPFF_BreakBeforeFunction = (1 << 2), |
| 37 |
kPFF_BreakBeforeWhile = (1 << 3), |
| 38 |
kPFF_BreakBeforeIf = (1 << 4), |
| 39 |
kPFF_BreakBeforeForeach = (1 << 5), |
| 40 |
kPFF_ElseOnSameLineAsClosingCurlyBrace = (1 << 6), |
| 41 |
kPFF_BreakAfterHeredoc = (1 << 7), |
| 42 |
kPFF_BreakBeforeClass = (1 << 8), |
| 43 |
kPFF_VerticalArrays = (1 << 9), |
| 44 |
kPFF_BreakAfterStringConcatentation = (1 << 10), |
| 45 |
kPFF_Defaults = kPFF_BreakBeforeFunction | kPFF_BreakBeforeWhile | kPFF_BreakBeforeIf | kPFF_BreakBeforeForeach | |
| 46 |
kPFF_ElseOnSameLineAsClosingCurlyBrace | kPFF_VerticalArrays | kPFF_BreakAfterStringConcatentation | |
| 47 |
kPFF_BreakBeforeClass, |
| 48 |
}; |
| 49 |
|
| 50 |
struct PHPFormatterOptions { |
| 51 |
wxString eol; // default: \n |
| 52 |
size_t indentSize; // default: 4 |
| 53 |
size_t flags; |
| 54 |
PHPFormatterOptions() |
| 55 |
: eol("\n") |
| 56 |
, indentSize(4) |
| 57 |
, flags(kPFF_Defaults) |
| 58 |
{ |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
/** |
| 63 |
* @class PHPFormatterBuffer |
| 64 |
* A simple PHP formatter based on a PHP scanner |
| 65 |
*/ |
| 66 |
class PHPFormatterBuffer |
| 67 |
{ |
| 68 |
enum eDepthCommand { |
| 69 |
kDepthNone, |
| 70 |
kDepthInc, |
| 71 |
kDepthDec, |
| 72 |
kDepthIncTemporarily, |
| 73 |
}; |
| 74 |
|
| 75 |
protected: |
| 76 |
PHPScanner_t m_scanner; |
| 77 |
PHPFormatterOptions m_options; |
| 78 |
wxString m_buffer; |
| 79 |
phpLexerToken m_lastToken; |
| 80 |
wxString m_indentString; |
| 81 |
bool m_openTagWithEcho; |
| 82 |
std::stack<phpLexerToken::Vet_t> m_stack; |
| 83 |
phpLexerToken::Vet_t* m_sequence; |
| 84 |
phpLexerToken::Vet_t m_tokensBuffer; |
| 85 |
|
| 86 |
// For statement |
| 87 |
int m_forDepth; |
| 88 |
bool m_insideForStatement; |
| 89 |
|
| 90 |
// Heredoc |
| 91 |
bool m_insideHereDoc; |
| 92 |
|
| 93 |
// Depth management |
| 94 |
int m_depth; |
| 95 |
|
| 96 |
// Is the last seen function has a comment associated with it? |
| 97 |
int m_lastCommentLine; |
| 98 |
|
| 99 |
// Paren depth "(" |
| 100 |
int m_parenDepth; |
| 101 |
|
| 102 |
protected: |
| 103 |
/** |
| 104 |
* @brief indent using tabs? |
| 105 |
*/ |
| 106 |
bool IsUseTabs() const { return m_options.flags & kPFF_UseTabs; } |
| 107 |
|
| 108 |
/** |
| 109 |
* @brief insert a new line before the current statement |
| 110 |
*/ |
| 111 |
void InsertSeparatorLine(); |
| 112 |
|
| 113 |
/** |
| 114 |
* @brief remove everything from the buffer until we find |
| 115 |
* delim |
| 116 |
*/ |
| 117 |
void ReverseClearUntilFind(const wxString& delim); |
| 118 |
|
| 119 |
/** |
| 120 |
* @brief format doxygen comment (C-style comment) |
| 121 |
*/ |
| 122 |
wxString FormatDoxyComment(const wxString& comment); |
| 123 |
|
| 124 |
void HandleOpenCurlyBrace(); |
| 125 |
/** |
| 126 |
* @brief return an indent string based on the current settings |
| 127 |
*/ |
| 128 |
wxString& GetIndent(); |
| 129 |
|
| 130 |
/** |
| 131 |
* @brief remove one indent size from the current buffer |
| 132 |
*/ |
| 133 |
void UnIndent(); |
| 134 |
|
| 135 |
/** |
| 136 |
* @brief remove last space character from the buffer |
| 137 |
*/ |
| 138 |
void RemoveLastSpace(); |
| 139 |
|
| 140 |
void AppendEOL(eDepthCommand depth = kDepthNone); |
| 141 |
|
| 142 |
PHPFormatterBuffer& ProcessToken(const phpLexerToken& token); |
| 143 |
|
| 144 |
/** |
| 145 |
* @brief return the next token. This function wraps the standard ::phpLexerNext |
| 146 |
* but it also takes into considertation our internal buffer |
| 147 |
*/ |
| 148 |
bool NextToken(phpLexerToken& token); |
| 149 |
|
| 150 |
/** |
| 151 |
* @brief return the next token but place it back in the m_buffer so next call to NextToken() will |
| 152 |
* re-use it |
| 153 |
*/ |
| 154 |
bool PeekToken(phpLexerToken& token); |
| 155 |
|
| 156 |
/** |
| 157 |
* @brief process an array. We can assume that the token at this point is |
| 158 |
* "(" and the token previously found was "array" |
| 159 |
*/ |
| 160 |
void ProcessArray(int openParen, int closingChar); |
| 161 |
|
| 162 |
/** |
| 163 |
* @brief reverse find ch in the buffer and return a whitepace representing |
| 164 |
* the distance from the line start position and ch index |
| 165 |
* If 'ch' is not found, return the current line indentation string |
| 166 |
*/ |
| 167 |
wxString GetIndentationToLast(wxChar ch); |
| 168 |
|
| 169 |
public: |
| 170 |
PHPFormatterBuffer(const wxString& buffer, const PHPFormatterOptions& options); |
| 171 |
virtual ~PHPFormatterBuffer(); |
| 172 |
|
| 173 |
/** |
| 174 |
* @brief format the buffer (provided in the constructor) |
| 175 |
*/ |
| 176 |
void format(); |
| 177 |
|
| 178 |
const wxString& GetBuffer() const { return m_buffer; } |
| 179 |
}; |
| 180 |
|
| 181 |
#endif // PHPFORMATTERBUFFER_H |