bool CodeFormatter::AStyleBatchFOrmat(const std::vector<wxFileName>& files, const FormatOptions& options) { wxString fmtOptions = options.AstyleOptionsAsString(); wxProgressDialog dlg( _("Source Code Formatter"), _("Formatting files..."), (int)files.size(), m_mgr->GetTheApp()->GetTopWindow()); for(size_t i = 0; i < files.size(); ++i) { wxString content; if(!FileUtils::ReadFileContent(files.at(i), content)) { CL_WARNING("Failed to read file content. File: %s", files.at(i).GetFullPath()); continue; } wxString msg; msg << "[ " << i << " / " << files.size() << " ] " << files.at(i).GetFullName(); dlg.Update(i, msg); // determine indentation method and amount bool useTabs = m_mgr->GetEditorSettings()->GetIndentUsesTabs(); int tabWidth = m_mgr->GetEditorSettings()->GetTabWidth(); int indentWidth = m_mgr->GetEditorSettings()->GetIndentWidth(); fmtOptions << (useTabs && tabWidth == indentWidth ? wxT(" -t") : wxT(" -s")) << indentWidth; wxString output; AstyleFormat(content, fmtOptions, output); output << DoGetGlobalEOLString(); // Replace the content of the file if(!FileUtils::WriteFileContent(files.at(i), output)) { CL_WARNING("Failed to write file content. File: %s", files.at(i).GetFullPath()); } } return true; }
void CodeFormatter::OnFormatString(clSourceFormatEvent& e) { wxString str = e.GetInputString(); if(str.IsEmpty()) { e.SetFormattedString(str); return; } // execute the formatter FormatOptions fmtroptions; m_mgr->GetConfigTool()->ReadObject(wxT("FormatterOptions"), &fmtroptions); wxString output; if(FileExtManager::IsPHPFile(e.GetFileName())) { // use the built-in PHP formatter // Construct the formatting options PHPFormatterOptions phpOptions; phpOptions.flags = fmtroptions.GetPHPFormatterOptions(); if(m_mgr->GetEditorSettings()->GetIndentUsesTabs()) { phpOptions.flags |= kPFF_UseTabs; } phpOptions.indentSize = m_mgr->GetEditorSettings()->GetTabWidth(); phpOptions.eol = m_mgr->GetEditorSettings()->GetEOLAsString(); // Create the formatter buffer PHPFormatterBuffer buffer(e.GetInputString(), phpOptions); // Format the source buffer.format(); // set the output output = buffer.GetBuffer(); } else if(fmtroptions.GetEngine() == kFormatEngineAStyle) { wxString options = fmtroptions.AstyleOptionsAsString(); // determine indentation method and amount bool useTabs = m_mgr->GetEditorSettings()->GetIndentUsesTabs(); int tabWidth = m_mgr->GetEditorSettings()->GetTabWidth(); int indentWidth = m_mgr->GetEditorSettings()->GetIndentWidth(); options << (useTabs && tabWidth == indentWidth ? wxT(" -t") : wxT(" -s")) << indentWidth; AstyleFormat(str, options, output); output << DoGetGlobalEOLString(); } else if(fmtroptions.GetEngine() == kFormatEngineClangFormat) { ClangPreviewFormat(str, output, fmtroptions); } else { // ?? } e.SetFormattedString(output); }
void CodeFormatter::OnFormatString(clSourceFormatEvent& e) { wxString str = e.GetInputString(); if(str.IsEmpty()) { e.SetFormattedString(str); return; } // execute the formatter FormatOptions fmtroptions; m_mgr->GetConfigTool()->ReadObject(wxT("FormatterOptions"), &fmtroptions); wxString output; if(FileExtManager::IsPHPFile(e.GetFileName())) { if(fmtroptions.GetPhpEngine() == kPhpFormatEngineBuiltin) { // use the built-in PHP formatter // Construct the formatting options PHPFormatterOptions phpOptions; phpOptions.flags = fmtroptions.GetPHPFormatterOptions(); if(m_mgr->GetEditorSettings()->GetIndentUsesTabs()) { phpOptions.flags |= kPFF_UseTabs; } phpOptions.indentSize = m_mgr->GetEditorSettings()->GetTabWidth(); phpOptions.eol = m_mgr->GetEditorSettings()->GetEOLAsString(); // Create the formatter buffer PHPFormatterBuffer buffer(e.GetInputString(), phpOptions); // Format the source buffer.format(); // set the output output = buffer.GetBuffer(); } else { wxFileName php(fmtroptions.GetPhpExecutable()); if(!php.Exists()) { ::wxMessageBox(_("Can not format file using PHP-CS-Fixer: Missing PHP executable path"), "Code Formatter", wxICON_ERROR | wxOK | wxCENTER); return; } wxFileName phar(fmtroptions.GetPHPCSFixerPhar()); if(!phar.Exists()) { ::wxMessageBox(_("Can not format file using PHP-CS-Fixer: Missing PHAR file"), "Code Formatter", wxICON_ERROR | wxOK | wxCENTER); return; } // Run the command, PHP-CS-Fixer works directly on the file // output.Clear(); IProcess::Ptr_t phpFixer(::CreateSyncProcess(fmtroptions.GetPhpFixerCommand(), IProcessCreateDefault | IProcessCreateWithHiddenConsole)); CHECK_PTR_RET(phpFixer); phpFixer->WaitForTerminate(output); } } else if(fmtroptions.GetEngine() == kFormatEngineAStyle) { wxString options = fmtroptions.AstyleOptionsAsString(); // determine indentation method and amount bool useTabs = m_mgr->GetEditorSettings()->GetIndentUsesTabs(); int tabWidth = m_mgr->GetEditorSettings()->GetTabWidth(); int indentWidth = m_mgr->GetEditorSettings()->GetIndentWidth(); options << (useTabs && tabWidth == indentWidth ? wxT(" -t") : wxT(" -s")) << indentWidth; AstyleFormat(str, options, output); output << DoGetGlobalEOLString(); } else if(fmtroptions.GetEngine() == kFormatEngineClangFormat) { ClangPreviewFormat(str, output, fmtroptions); } else { // ?? } e.SetFormattedString(output); }