Debian Codegraph @

CodeFormatter/SourceFormatterBase.hpp

raw
1 #ifndef SOURCEFORMATTERBASE_HPP
2 #define SOURCEFORMATTERBASE_HPP
3
4 #include "JSON.h"
5 #include "cl_command_event.h"
6 #include "fileextmanager.h"
7 #include "macros.h"
8 #include "wxStringHash.h"
9
10 #include <vector>
11 #include <wx/event.h>
12 #include <wx/filename.h>
13 #include <wx/string.h>
14
15 /// Defines the formatter interface
16 enum class FormatterFlags {
17 // Is this formatter enabled?
18 ENABLED = (1 << 0),
19
20 // The formatter support file config (e.g. `.clang-format`)
21 HAS_FILE_CONFIG = (1 << 1),
22
23 // Indicate that this formatter fixes the file inline
24 // and reload should take place after a successful formatting
25 INPLACE_EDIT = (1 << 2),
26
27 // trigger a formatting after saving the file
28 FORMAT_ON_SAVE = (1 << 3),
29 };
30
31 #define __HAS_FLAG(flags, bit) (flags & (size_t)bit)
32 #define __SET_FLAG(flags, bit) (flags |= (size_t)bit)
33 #define __UNSET_FLAG(flags, bit) (flags &= ~(size_t)bit)
34
35 wxDECLARE_EVENT(wxEVT_FORMAT_COMPELTED, clSourceFormatEvent);
36 wxDECLARE_EVENT(wxEVT_FORMAT_INPLACE_COMPELTED, clSourceFormatEvent);
37
38 class SourceFormatterBase : public wxEvtHandler
39 {
40 protected:
41 wxArrayString m_languages;
42 size_t m_flags = (size_t)FormatterFlags::ENABLED;
43 wxString m_configFile;
44 wxString m_name;
45 wxString m_description;
46 wxString m_shortDescription;
47
48 protected:
49 void SetFlag(FormatterFlags flag, bool set)
50 {
51 if(set) {
52 __SET_FLAG(m_flags, flag);
53 } else {
54 __UNSET_FLAG(m_flags, flag);
55 }
56 }
57
58 bool HasFlag(FormatterFlags flag) const { return __HAS_FLAG(m_flags, flag); }
59
60 public:
61 SourceFormatterBase();
62 virtual ~SourceFormatterBase();
63
64 virtual void FromJSON(const JSONItem& json);
65 virtual JSONItem ToJSON() const;
66
67 /**
68 * @brief can this formatter handle `file_type`?
69 */
70 bool CanHandle(FileExtManager::FileType file_type) const;
71 bool IsConfigFileSuppoerted() const { return HasFlag(FormatterFlags::HAS_FILE_CONFIG); }
72
73 bool IsEnabled() const { return HasFlag(FormatterFlags::ENABLED); }
74 void SetEnabled(bool b) { SetFlag(FormatterFlags::ENABLED, b); }
75
76 bool IsFormatOnSave() const { return HasFlag(FormatterFlags::FORMAT_ON_SAVE); }
77 void SetFormatOnSave(bool b) { SetFlag(FormatterFlags::FORMAT_ON_SAVE, b); }
78
79 void SetInplaceFormatter(bool b) { SetFlag(FormatterFlags::INPLACE_EDIT, b); }
80 bool IsInplaceFormatter() const { return HasFlag(FormatterFlags::INPLACE_EDIT); }
81
82 virtual bool FormatFile(const wxFileName& file_path, wxEvtHandler* sink) = 0;
83 virtual bool FormatFile(const wxString& file_path, wxEvtHandler* sink) = 0;
84 virtual bool FormatRemoteFile(const wxString& file_path, wxEvtHandler* sink) = 0;
85 virtual bool FormatString(const wxString& content, const wxString& fullpath, wxString* output) = 0;
86
87 void SetFileTypes(const std::vector<FileExtManager::FileType>& types);
88 void SetLanguages(const wxArrayString& langs) { m_languages = langs; }
89 const wxArrayString& GetLanguages() const { return m_languages; }
90
91 void SetConfigFilepath(const wxString& filepath);
92 const wxString& GetConfigFilepath() const { return m_configFile; }
93
94 void SetName(const wxString& name) { this->m_name = name; }
95 const wxString& GetName() const { return m_name; }
96
97 void SetDescription(const wxString& description) { this->m_description = description; }
98 const wxString& GetDescription() const { return m_description; }
99
100 void SetShortDescription(const wxString& shortDescription) { this->m_shortDescription = shortDescription; }
101 const wxString& GetShortDescription() const { return m_shortDescription; }
102 };
103
104 #endif // SOURCEFORMATTERBASE_HPP