| 1 |
#include "CodeFormatterManager.hpp" |
| 2 |
|
| 3 |
#include "JSON.h" |
| 4 |
#include "cl_standard_paths.h" |
| 5 |
#include "file_logger.h" |
| 6 |
#include "fmtBlack.hpp" |
| 7 |
#include "fmtCMakeForamt.hpp" |
| 8 |
#include "fmtClangFormat.hpp" |
| 9 |
#include "fmtJQ.hpp" |
| 10 |
#include "fmtPHPCBF.hpp" |
| 11 |
#include "fmtPHPCSFixer.hpp" |
| 12 |
#include "fmtRustfmt.hpp" |
| 13 |
#include "fmtXmlLint.hpp" |
| 14 |
#include "fmtYQ.hpp" |
| 15 |
|
| 16 |
#include <wx/filename.h> |
| 17 |
|
| 18 |
CodeFormatterManager::CodeFormatterManager() {} |
| 19 |
|
| 20 |
CodeFormatterManager::~CodeFormatterManager() { clear(); } |
| 21 |
|
| 22 |
std::shared_ptr<GenericFormatter> CodeFormatterManager::GetFormatter(const wxString& filepath) const |
| 23 |
{ |
| 24 |
auto type = FileExtManager::GetType(filepath); |
| 25 |
for(auto f : m_formatters) { |
| 26 |
if(f->IsEnabled() && f->CanHandle(type)) { |
| 27 |
return f; |
| 28 |
} |
| 29 |
} |
| 30 |
return nullptr; |
| 31 |
} |
| 32 |
|
| 33 |
void CodeFormatterManager::clear() { m_formatters.clear(); } |
| 34 |
|
| 35 |
void CodeFormatterManager::initialize_defaults() |
| 36 |
{ |
| 37 |
clear(); |
| 38 |
push_back(new fmtClangFormat); |
| 39 |
push_back(new fmtPHPCBF); |
| 40 |
push_back(new fmtPHPCSFixer); |
| 41 |
push_back(new fmtJQ); |
| 42 |
push_back(new fmtXmlLint); |
| 43 |
push_back(new fmtRustfmt); |
| 44 |
push_back(new fmtBlack); |
| 45 |
push_back(new fmtYQ); |
| 46 |
push_back(new fmtCMakeForamt); |
| 47 |
} |
| 48 |
|
| 49 |
void CodeFormatterManager::push_back(GenericFormatter* formatter) |
| 50 |
{ |
| 51 |
std::shared_ptr<GenericFormatter> ptr{ formatter }; |
| 52 |
m_formatters.push_back(ptr); |
| 53 |
} |
| 54 |
|
| 55 |
size_t CodeFormatterManager::GetAllNames(wxArrayString* names) const |
| 56 |
{ |
| 57 |
if(!names) { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
names->reserve(m_formatters.size()); |
| 62 |
for(auto f : m_formatters) { |
| 63 |
names->Add(f->GetName()); |
| 64 |
} |
| 65 |
return names->size(); |
| 66 |
} |
| 67 |
|
| 68 |
void CodeFormatterManager::Load() |
| 69 |
{ |
| 70 |
wxFileName config_file{ clStandardPaths::Get().GetUserDataDir(), "code-formatters.json" }; |
| 71 |
config_file.AppendDir("config"); |
| 72 |
JSON root(config_file); |
| 73 |
if(!root.isOk() || !root.toElement().isArray()) { |
| 74 |
initialize_defaults(); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
clear(); |
| 79 |
auto arr = root.toElement(); |
| 80 |
int count = arr.arraySize(); |
| 81 |
for(int i = 0; i < count; ++i) { |
| 82 |
GenericFormatter* fmtr = new GenericFormatter(); |
| 83 |
fmtr->FromJSON(arr[i]); |
| 84 |
push_back(fmtr); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void CodeFormatterManager::Save() |
| 89 |
{ |
| 90 |
wxFileName config_file{ clStandardPaths::Get().GetUserDataDir(), "code-formatters.json" }; |
| 91 |
config_file.AppendDir("config"); |
| 92 |
JSON root{ cJSON_Array }; |
| 93 |
auto arr = root.toElement(); |
| 94 |
for(auto fmtr : m_formatters) { |
| 95 |
arr.arrayAppend(fmtr->ToJSON()); |
| 96 |
} |
| 97 |
root.save(config_file); |
| 98 |
} |
| 99 |
|
| 100 |
std::shared_ptr<GenericFormatter> CodeFormatterManager::GetFormatterByName(const wxString& name) const |
| 101 |
{ |
| 102 |
for(auto f : m_formatters) { |
| 103 |
if(f->GetName() == name) { |
| 104 |
return f; |
| 105 |
} |
| 106 |
} |
| 107 |
return nullptr; |
| 108 |
} |
| 109 |
|
| 110 |
bool CodeFormatterManager::CanFormat(const wxString& filepath) const |
| 111 |
{ |
| 112 |
auto file_type = FileExtManager::GetType(filepath); |
| 113 |
for(auto f : m_formatters) { |
| 114 |
if(f->IsEnabled() && f->CanHandle(file_type)) { |
| 115 |
return true; |
| 116 |
} |
| 117 |
} |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
bool CodeFormatterManager::CanFormatByContent(const wxString& content) const |
| 122 |
{ |
| 123 |
FileExtManager::FileType file_type; |
| 124 |
if(!FileExtManager::GetContentType(content, file_type)) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
for(auto f : m_formatters) { |
| 129 |
if(f->IsEnabled() && f->CanHandle(file_type)) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
void CodeFormatterManager::RestoreDefaults() |
| 137 |
{ |
| 138 |
clear(); |
| 139 |
initialize_defaults(); |
| 140 |
} |
| 141 |
|
| 142 |
void CodeFormatterManager::ClearRemoteCommands() |
| 143 |
{ |
| 144 |
for(auto f : m_formatters) { |
| 145 |
f->SetRemoteCommand(wxEmptyString); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
std::shared_ptr<GenericFormatter> CodeFormatterManager::GetFormatterByContent(const wxString& content) const |
| 150 |
{ |
| 151 |
FileExtManager::FileType type; |
| 152 |
if(!FileExtManager::GetContentType(content, type)) { |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
for(auto f : m_formatters) { |
| 157 |
if(f->IsEnabled() && f->CanHandle(type)) { |
| 158 |
return f; |
| 159 |
} |
| 160 |
} |
| 161 |
return nullptr; |
| 162 |
} |