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