| 1 |
#ifndef CLEDITOR_CONFIG_H |
| 2 |
#define CLEDITOR_CONFIG_H |
| 3 |
|
| 4 |
#include "codelite_exports.h" |
| 5 |
#include <wx/string.h> |
| 6 |
#include <wx/filename.h> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
class WXDLLIMPEXP_CL clEditorConfigSection |
| 10 |
{ |
| 11 |
enum { |
| 12 |
kIndentStyleSet = (1 << 0), |
| 13 |
kIndentSizeSet = (1 << 1), |
| 14 |
kTabWidthSet = (1 << 2), |
| 15 |
kCharsetSet = (1 << 3), |
| 16 |
kTrimTrailingWhitespaceSet = (1 << 4), |
| 17 |
kInsertEOLSet = (1 << 5), |
| 18 |
kEOLStyleSet = (1 << 6), |
| 19 |
}; |
| 20 |
|
| 21 |
public: |
| 22 |
wxArrayString patterns; |
| 23 |
|
| 24 |
size_t flags; |
| 25 |
|
| 26 |
wxString indent_style; |
| 27 |
size_t indent_size; |
| 28 |
size_t tab_width; |
| 29 |
wxString charset; |
| 30 |
bool trim_trailing_whitespace; |
| 31 |
bool insert_final_newline; |
| 32 |
wxString end_of_line; |
| 33 |
wxFileName filename; // the path to the .editorconfig file |
| 34 |
typedef std::vector<clEditorConfigSection> Vec_t; |
| 35 |
|
| 36 |
clEditorConfigSection() |
| 37 |
: flags(0) |
| 38 |
, indent_style("space") |
| 39 |
, indent_size(4) |
| 40 |
, tab_width(4) |
| 41 |
, charset("utf-8") |
| 42 |
, trim_trailing_whitespace(false) |
| 43 |
, insert_final_newline(false) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @brief print this section properties to the log file |
| 49 |
*/ |
| 50 |
void PrintToLog(); |
| 51 |
|
| 52 |
void SetFilename(const wxFileName& filename) {this->filename = filename;} |
| 53 |
const wxFileName& GetFilename() const {return filename;} |
| 54 |
|
| 55 |
void SetCharset(const wxString& charset) |
| 56 |
{ |
| 57 |
this->charset = charset; |
| 58 |
this->flags |= kCharsetSet; |
| 59 |
} |
| 60 |
bool IsCharsetSet() const { return this->flags & kCharsetSet; } |
| 61 |
|
| 62 |
void SetIndentSize(size_t indent_size) |
| 63 |
{ |
| 64 |
this->indent_size = indent_size; |
| 65 |
this->flags |= kIndentSizeSet; |
| 66 |
} |
| 67 |
bool IsIndentSizeSet() const { return (this->flags & kTabWidthSet) || (this->flags & kIndentSizeSet); } |
| 68 |
|
| 69 |
void SetIndentStyle(const wxString& indent_style) |
| 70 |
{ |
| 71 |
this->indent_style = indent_style; |
| 72 |
this->flags |= kIndentStyleSet; |
| 73 |
} |
| 74 |
bool IsIndentStyleSet() const { return this->flags & kIndentStyleSet; } |
| 75 |
|
| 76 |
void SetInsertFinalNewline(bool insert_final_newline) |
| 77 |
{ |
| 78 |
this->insert_final_newline = insert_final_newline; |
| 79 |
this->flags |= kInsertEOLSet; |
| 80 |
} |
| 81 |
bool IsInsertFinalNewlineSet() const { return this->flags & kInsertEOLSet; } |
| 82 |
|
| 83 |
void SetTabWidth(size_t tab_width) |
| 84 |
{ |
| 85 |
this->tab_width = tab_width; |
| 86 |
this->flags |= kTabWidthSet; |
| 87 |
} |
| 88 |
bool IsTabWidthSet() const { return (this->flags & kTabWidthSet) || (this->flags & kIndentSizeSet); } |
| 89 |
|
| 90 |
void SetTrimTrailingWhitespace(bool trim_trailing_whitespace) |
| 91 |
{ |
| 92 |
this->trim_trailing_whitespace = trim_trailing_whitespace; |
| 93 |
this->flags |= kTrimTrailingWhitespaceSet; |
| 94 |
} |
| 95 |
bool IsTrimTrailingWhitespaceSet() const { return this->flags & kTrimTrailingWhitespaceSet; } |
| 96 |
|
| 97 |
void SetEndOfLine(const wxString& end_of_line) |
| 98 |
{ |
| 99 |
this->end_of_line = end_of_line; |
| 100 |
this->flags |= kEOLStyleSet; |
| 101 |
} |
| 102 |
bool IsSetEndOfLineSet() const { return this->flags & kEOLStyleSet; } |
| 103 |
|
| 104 |
const wxString& GetCharset() const { return charset; } |
| 105 |
// According to the docs: |
| 106 |
// When set to tab, the value of tab_width (if specified) will be used |
| 107 |
size_t GetIndentSize() const |
| 108 |
{ |
| 109 |
return (GetIndentStyle() == "tab" && (this->flags & kTabWidthSet)) ? tab_width : indent_size; |
| 110 |
} |
| 111 |
|
| 112 |
// If user set the tab_width, return its value, otherwise return the size of the indent size |
| 113 |
size_t GetTabWidth() const { return (this->flags & kTabWidthSet) ? tab_width : indent_size; } |
| 114 |
const wxString& GetIndentStyle() const { return indent_style; } |
| 115 |
bool IsInsertFinalNewline() const { return insert_final_newline; } |
| 116 |
bool IsTrimTrailingWhitespace() const { return trim_trailing_whitespace; } |
| 117 |
const wxString& GetEndOfLine() const { return end_of_line; } |
| 118 |
}; |
| 119 |
|
| 120 |
class WXDLLIMPEXP_CL clEditorConfig |
| 121 |
{ |
| 122 |
clEditorConfigSection::Vec_t m_sections; |
| 123 |
bool m_rootFileFound; |
| 124 |
|
| 125 |
private: |
| 126 |
wxArrayString ProcessSection(wxString& strLine); |
| 127 |
void ProcessDirective(wxString& strLine); |
| 128 |
bool ReadUntil(wxChar delim, wxString& strLine, wxString& output); |
| 129 |
|
| 130 |
public: |
| 131 |
clEditorConfig(); |
| 132 |
~clEditorConfig(); |
| 133 |
|
| 134 |
/** |
| 135 |
* @brief try and load a .editorconfig settings for 'filename'. We start looking from the current file location |
| 136 |
* and going up until we hit the root folder |
| 137 |
*/ |
| 138 |
bool LoadForFile(const wxFileName& filename, wxFileName& editorConfigFile); |
| 139 |
|
| 140 |
/** |
| 141 |
* @brief find the best section for a file |
| 142 |
*/ |
| 143 |
bool GetSectionForFile(const wxFileName& filename, clEditorConfigSection& section); |
| 144 |
}; |
| 145 |
|
| 146 |
#endif // CLEDITOR_CONFIG_H |