Debian Codegraph @

CodeLite/clEditorConfig.cpp

raw
1 #include "clEditorConfig.h"
2
3 #include "file_logger.h"
4 #include "fileutils.h"
5
6 #include <algorithm>
7 #include <wx/tokenzr.h>
8
9 clEditorConfig::clEditorConfig()
10 : m_rootFileFound(false)
11 {
12 }
13
14 clEditorConfig::~clEditorConfig() {}
15
16 bool clEditorConfig::LoadForFile(const wxFileName& filename, wxFileName& editorConfigFile)
17 {
18 editorConfigFile = wxFileName(filename.GetPath(), ".editorconfig");
19
20 bool foundFile = false;
21 while(editorConfigFile.GetDirCount()) {
22 if(editorConfigFile.FileExists()) {
23 foundFile = true;
24 break;
25 }
26 editorConfigFile.RemoveLastDir();
27 }
28
29 if(!foundFile)
30 return false;
31
32 wxString content;
33 if(!FileUtils::ReadFileContent(editorConfigFile, content)) {
34 clDEBUG() << "Failed to read file:" << editorConfigFile << clEndl;
35 return false;
36 }
37
38 clEditorConfigSection section;
39 m_sections.push_back(section);
40 clEditorConfigSection* cursection = &(m_sections.back());
41 wxUnusedVar(cursection); // for debug purposes
42 wxArrayString lines = ::wxStringTokenize(content, "\n", wxTOKEN_STRTOK);
43 for(size_t i = 0; i < lines.size(); ++i) {
44 // Remove comments
45 wxString strLine = lines.Item(i);
46 strLine = strLine.BeforeFirst('#');
47 strLine = strLine.BeforeFirst(';');
48
49 strLine.Trim().Trim(false);
50
51 if(strLine.IsEmpty())
52 continue;
53
54 // Process the line
55 if(strLine.StartsWith("[") && strLine.EndsWith("]")) {
56 strLine.RemoveLast().Remove(0, 1); // remove the []
57 clEditorConfigSection section;
58 section.patterns = ProcessSection(strLine);
59 m_sections.push_back(section);
60 cursection = &(m_sections.back());
61
62 } else {
63 ProcessDirective(strLine);
64 }
65 }
66 return true;
67 }
68
69 struct clEditorConfigTreeNode {
70 wxString data;
71 typedef std::vector<clEditorConfigTreeNode*> Vec_t;
72 clEditorConfigTreeNode::Vec_t children;
73
74 clEditorConfigTreeNode* AddChild(const wxString& data)
75 {
76 clEditorConfigTreeNode* child = new clEditorConfigTreeNode;
77 child->data = data;
78 children.push_back(child);
79 return child;
80 }
81
82 public:
83 clEditorConfigTreeNode() {}
84 ~clEditorConfigTreeNode()
85 {
86 std::for_each(children.begin(), children.end(), [&](clEditorConfigTreeNode* child) { delete child; });
87 children.clear();
88 }
89
90 public:
91 void GetPatterns(wxArrayString& patterns) { DoGetPatterns(this, patterns, data); }
92 void Add(const wxString& pattern)
93 {
94 wxArrayString arr;
95 arr.Add(pattern);
96 Add(arr);
97 }
98
99 void Add(const wxArrayString& patterns)
100 {
101 clEditorConfigTreeNode::Vec_t leaves;
102 DoGetLeaves(this, leaves);
103
104 std::for_each(leaves.begin(), leaves.end(), [&](clEditorConfigTreeNode* leaf) {
105 for(size_t i = 0; i < patterns.size(); ++i) {
106 leaf->AddChild(patterns.Item(i));
107 }
108 });
109 }
110
111 bool IsEmpty() const { return children.empty(); }
112
113 private:
114 void DoGetLeaves(clEditorConfigTreeNode* node, clEditorConfigTreeNode::Vec_t& leaves)
115 {
116 if(node->children.empty()) {
117 // leaf node
118 leaves.push_back(node);
119 } else {
120 for(size_t i = 0; i < node->children.size(); ++i) {
121 DoGetLeaves(node->children.at(i), leaves);
122 }
123 }
124 }
125
126 /**
127 * @brief collect the patterns from the leaf nodes
128 */
129 void DoGetPatterns(clEditorConfigTreeNode* node, wxArrayString& patterns, const wxString& curpattern)
130 {
131 if(node->children.empty()) {
132 // leaf node
133 patterns.Add(curpattern + node->data);
134 } else {
135 for(size_t i = 0; i < node->children.size(); ++i) {
136 DoGetPatterns(node->children.at(i), patterns, curpattern + node->data);
137 }
138 }
139 }
140
141 /**
142 * @brief collect the patterns from the leaf nodes
143 */
144 void DoAddToAllLeaves(clEditorConfigTreeNode* node, const wxString& pattern)
145 {
146 if(node->children.empty()) {
147 // leaf node
148 node->data << pattern;
149 } else {
150 for(size_t i = 0; i < node->children.size(); ++i) {
151 DoAddToAllLeaves(node->children.at(i), pattern);
152 }
153 }
154 }
155 };
156
157 enum eEditorConfigState {
158 kEC_STATE_NORMAL,
159 kEC_STATE_IN_CURLYGRP,
160 kEC_STATE_IN_SQUAREGRP,
161 };
162
163 wxArrayString clEditorConfig::ProcessSection(wxString& strLine)
164 {
165 eEditorConfigState state = kEC_STATE_NORMAL;
166
167 clEditorConfigTreeNode* tree = new clEditorConfigTreeNode;
168 std::vector<clEditorConfigTreeNode*> trees;
169 trees.push_back(tree);
170
171 wxString curpattern;
172 while(!strLine.IsEmpty()) {
173 switch(state) {
174 case kEC_STATE_NORMAL: {
175 wxChar ch = strLine.at(0);
176 strLine.Remove(0, 1);
177 switch(ch) {
178 case '{':
179 state = kEC_STATE_IN_CURLYGRP;
180 break;
181 case '[':
182 state = kEC_STATE_IN_SQUAREGRP;
183 break;
184 case ',':
185 // new pattern
186 if(!curpattern.IsEmpty()) {
187 tree->Add(curpattern);
188
189 tree = new clEditorConfigTreeNode;
190 trees.push_back(tree);
191 curpattern.clear();
192 }
193 break;
194 default:
195 curpattern << ch;
196 break;
197 }
198 break;
199 }
200 case kEC_STATE_IN_CURLYGRP: {
201 // if we got something so far, add it before we continue
202 if(!curpattern.IsEmpty()) {
203 tree->Add(curpattern);
204 curpattern.clear();
205 }
206
207 // read the buffer until we hit the closing brace
208 wxString buffer;
209 if(!ReadUntil('}', strLine, buffer)) {
210 return wxArrayString();
211 }
212 state = kEC_STATE_NORMAL;
213 wxArrayString groupPatterns = ProcessSection(buffer);
214 tree->Add(groupPatterns);
215 break;
216 }
217 case kEC_STATE_IN_SQUAREGRP: {
218 wxString buffer;
219 if(!ReadUntil(']', strLine, buffer)) {
220 return wxArrayString();
221 }
222 state = kEC_STATE_NORMAL;
223 break;
224 }
225 }
226 }
227
228 // Remainder
229 if(!curpattern.IsEmpty()) {
230 tree->Add(curpattern);
231 }
232
233 wxArrayString res;
234 for(size_t i = 0; i < trees.size(); ++i) {
235 wxArrayString patterns;
236 trees.at(i)->GetPatterns(patterns);
237 res.insert(res.end(), patterns.begin(), patterns.end());
238 delete trees.at(i);
239 }
240
241 // Loop over the array and change "**" => "*"
242 for(size_t i = 0; i < res.size(); ++i) {
243 res.Item(i).Replace("**", "*");
244 }
245 return res;
246 }
247
248 #define IS_TRUE(value) ((value.CmpNoCase("true") == 0) || (value.CmpNoCase("yes") == 0) || (value.CmpNoCase("1") == 0))
249 void clEditorConfig::ProcessDirective(wxString& strLine)
250 {
251 clEditorConfigSection* cursection = &(m_sections.back());
252 wxString key = strLine.BeforeFirst('=');
253 wxString value = strLine.AfterFirst('=');
254 key.Trim().Trim(false);
255 value.Trim().Trim(false);
256
257 if(key == "indent_style") {
258 cursection->SetIndentStyle(value.Lower());
259
260 } else if(key == "indent_size") {
261 long lv = 4;
262 value.ToCLong(&lv);
263 cursection->SetIndentSize(lv);
264
265 } else if(key == "tab_width") {
266 long lv = 4;
267 value.ToCLong(&lv);
268 cursection->SetTabWidth(lv);
269
270 } else if(key == "charset") {
271 cursection->SetCharset(value.Lower());
272
273 } else if(key == "trim_trailing_whitespace") {
274 cursection->SetTrimTrailingWhitespace(IS_TRUE(value));
275
276 } else if(key == "insert_final_newline") {
277 cursection->SetInsertFinalNewline(IS_TRUE(value));
278
279 } else if(key == "end_of_line") {
280 cursection->SetEndOfLine(value.Lower());
281
282 } else if(key == "root") {
283 m_rootFileFound = IS_TRUE(value);
284 }
285 }
286
287 bool clEditorConfig::ReadUntil(wxChar delim, wxString& strLine, wxString& output)
288 {
289 while(!strLine.IsEmpty()) {
290 wxChar ch = strLine.at(0);
291 strLine.Remove(0, 1);
292 if(ch == delim) {
293 return true;
294 } else {
295 output << ch;
296 }
297 }
298 return false;
299 }
300
301 bool clEditorConfig::GetSectionForFile(const wxFileName& filename, clEditorConfigSection& section)
302 {
303 wxFileName editorConfigFile;
304 if(!LoadForFile(filename, editorConfigFile))
305 return false;
306 section = clEditorConfigSection();
307 section.filename = editorConfigFile;
308 bool match_found = false;
309 std::for_each(m_sections.begin(), m_sections.end(), [&](const clEditorConfigSection& sec) {
310 for(size_t i = 0; i < sec.patterns.size(); ++i) {
311 const wxString& pattern = sec.patterns.Item(i);
312 bool is_wild = pattern.Contains("*");
313
314 wxString fullpath = filename.GetFullPath(wxPATH_UNIX);
315 wxString fullname = filename.GetFullName();
316
317 if((is_wild && ::wxMatchWild(pattern, fullpath, false)) || (!is_wild && fullname == pattern)) {
318 match_found = true;
319 if(sec.IsCharsetSet()) {
320 section.SetCharset(sec.GetCharset());
321 }
322 if(sec.IsIndentSizeSet()) {
323 section.SetIndentSize(sec.GetIndentSize());
324 }
325 if(sec.IsIndentStyleSet()) {
326 section.SetIndentStyle(sec.GetIndentStyle());
327 }
328 if(sec.IsInsertFinalNewlineSet()) {
329 section.SetInsertFinalNewline(sec.IsInsertFinalNewline());
330 }
331 if(sec.IsSetEndOfLineSet()) {
332 section.SetEndOfLine(sec.GetEndOfLine());
333 }
334 if(sec.IsTabWidthSet()) {
335 section.SetTabWidth(sec.GetTabWidth());
336 }
337 if(sec.IsTrimTrailingWhitespaceSet()) {
338 section.SetTrimTrailingWhitespace(sec.IsTrimTrailingWhitespace());
339 }
340 break;
341 }
342 }
343 });
344
345 // Print the match to the log file
346 if(match_found) {
347 section.PrintToLog();
348 }
349 return match_found;
350 }
351
352 void clEditorConfigSection::PrintToLog()
353 {
354 LOG_IF_TRACE
355 {
356 clDEBUG1() << ".editorconfig (" << filename << ") :" << clEndl;
357 if(IsCharsetSet()) {
358 clDEBUG1() << "charset:" << GetCharset() << clEndl;
359 }
360 if(IsIndentSizeSet()) {
361 clDEBUG1() << "indent_size:" << GetIndentSize() << clEndl;
362 }
363 if(IsIndentStyleSet()) {
364 clDEBUG1() << "indent_style:" << GetIndentStyle() << clEndl;
365 }
366 if(IsInsertFinalNewlineSet()) {
367 clDEBUG1() << "insert_final_newline:" << IsInsertFinalNewline() << clEndl;
368 }
369 if(IsSetEndOfLineSet()) {
370 clDEBUG1() << "end_of_line:" << GetEndOfLine() << clEndl;
371 }
372 if(IsTabWidthSet()) {
373 clDEBUG1() << "tab_width:" << GetTabWidth() << clEndl;
374 }
375 if(IsTrimTrailingWhitespaceSet()) {
376 clDEBUG1() << "trim_trailing_whitespace:" << IsTrimTrailingWhitespace() << clEndl;
377 }
378 }
379 }