File: clEditorConfig.h

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136,244 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (146 lines) | stat: -rw-r--r-- 4,565 bytes parent folder | download | duplicates (5)
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