Debian Codegraph @

CodeFormatter/GenericFormatter.hpp

raw
1 #ifndef GENERICFORMATTER_HPP
2 #define GENERICFORMATTER_HPP
3
4 #include "SourceFormatterBase.hpp"
5 #include "clCodeLiteRemoteProcess.hpp"
6 #include "procutils.h"
7
8 #include <unordered_map>
9 #include <vector>
10 #include <wx/arrstr.h>
11
12 struct CommandMetadata {
13 wxString m_command;
14 wxString m_filepath;
15 wxEvtHandler* m_sink = nullptr;
16
17 CommandMetadata() {}
18 CommandMetadata(const wxString& command, const wxString& filepath, wxEvtHandler* sink)
19 : m_command(command)
20 , m_filepath(filepath)
21 , m_sink(sink)
22 {
23 }
24 };
25
26 /// A formatter based on a simple command execution
27 class GenericFormatter : public SourceFormatterBase
28 {
29 wxArrayString m_command;
30 wxString m_remote_command;
31 wxString m_workingDirectory;
32 std::unordered_map<long, CommandMetadata> m_pid_commands;
33
34 protected:
35 bool DoFormatFile(const wxString& filepath, wxEvtHandler* sink, wxString* output);
36 void async_format(const wxString& cmd, const wxString& wd, const wxString& filepath, bool inplace_formatter,
37 wxEvtHandler* sink);
38
39 void OnAsyncShellProcessTerminated(clShellProcessEvent& event);
40
41 public:
42 GenericFormatter();
43 virtual ~GenericFormatter();
44
45 void FromJSON(const JSONItem& json) override;
46 JSONItem ToJSON() const override;
47
48 bool FormatFile(const wxFileName& filepath, wxEvtHandler* sink) override;
49 bool FormatFile(const wxString& filepath, wxEvtHandler* sink) override;
50 bool FormatRemoteFile(const wxString& filepath, wxEvtHandler* sink) override;
51 bool FormatString(const wxString& content, const wxString& fullpath, wxString* output) override;
52
53 bool CanHandleRemoteFile() const { return !m_remote_command.empty(); }
54 void SetRemoteCommand(const wxString& cmd);
55
56 void SetCommand(const wxArrayString& command) { this->m_command = command; }
57 void SetCommand(const std::vector<wxString>& command);
58 void SetCommandFromString(const wxString& command);
59
60 const wxArrayString& GetCommand() const { return m_command; }
61 wxString GetCommandAsString() const;
62
63 const wxString& GetRemoteCommand() const { return m_remote_command; }
64
65 void SetWorkingDirectory(const wxString& workingDirectory) { this->m_workingDirectory = workingDirectory; }
66 const wxString& GetWorkingDirectory() const { return m_workingDirectory; }
67
68 wxString GetCommandWithComments() const;
69 };
70
71 #endif // GENERICFORMATTER_HPP