diff --git a/src/eepp/ui/doc/syntaxdefinitionmanager.cpp b/src/eepp/ui/doc/syntaxdefinitionmanager.cpp index 84912c3b2..89cc1016a 100644 --- a/src/eepp/ui/doc/syntaxdefinitionmanager.cpp +++ b/src/eepp/ui/doc/syntaxdefinitionmanager.cpp @@ -150,7 +150,7 @@ SyntaxDefinitionManager::SyntaxDefinitionManager() { // JavaScript add( { "Javascript", - { "%.js$", "%.json$", "%.cson$", "%.ts$" }, + { "%.js$", "%.json$", "%.cson$", "%.ts$", "%.tsx$" }, { { { "//.-\n" }, "comment" }, { { "/%*", "%*/" }, "comment" }, diff --git a/src/eepp/ui/doc/syntaxhighlighter.cpp b/src/eepp/ui/doc/syntaxhighlighter.cpp index b06737c79..d3dab381b 100644 --- a/src/eepp/ui/doc/syntaxhighlighter.cpp +++ b/src/eepp/ui/doc/syntaxhighlighter.cpp @@ -67,7 +67,7 @@ bool SyntaxHighlighter::updateDirty( int visibleLinesCount ) { mMaxWantedLine = 0; } else { bool changed = false; - Int64 max = eemin( mFirstInvalidLine + visibleLinesCount, mMaxWantedLine ); + Int64 max = eemax( 0LL, eemin( mFirstInvalidLine + visibleLinesCount, mMaxWantedLine ) ); for ( Int64 index = mFirstInvalidLine; index <= max; index++ ) { int state = SYNTAX_TOKENIZER_STATE_NONE; diff --git a/src/tools/codeeditor/appconfig.cpp b/src/tools/codeeditor/appconfig.cpp index 99f0f3c5a..390a0236f 100644 --- a/src/tools/codeeditor/appconfig.cpp +++ b/src/tools/codeeditor/appconfig.cpp @@ -73,13 +73,22 @@ void AppConfig::load( std::string& confPath, std::string& keybindingsPath, editor.formatter = ini.getValueB( "editor", "formatter", true ); editor.showDocInfo = ini.getValueB( "editor", "show_doc_info", true ); editor.hideTabBarOnSingleTab = ini.getValueB( "editor", "hide_tab_bar_on_single_tab", true ); - editor.singleClickTreeNavigation = ini.getValueB( "editor", "single_click_tree_navigation", false ); + editor.singleClickTreeNavigation = + ini.getValueB( "editor", "single_click_tree_navigation", false ); + iniInfo = FileInfo( ini.path() ); } void AppConfig::save( const std::vector& recentFiles, const std::vector& recentFolders, const std::string& panelPartition, EE::Window::Window* win, const std::string& colorSchemeName ) { + + FileInfo configInfo( ini.path() ); + if ( iniInfo.getModificationTime() != 0 && + iniInfo.getModificationTime() != configInfo.getModificationTime() ) { + ini.loadFromFile( ini.path() ); + } + editor.colorScheme = colorSchemeName; window.size = win->getLastWindowedSize(); window.maximized = win->isMaximized(); diff --git a/src/tools/codeeditor/appconfig.hpp b/src/tools/codeeditor/appconfig.hpp index 6c447188d..7e795b30e 100644 --- a/src/tools/codeeditor/appconfig.hpp +++ b/src/tools/codeeditor/appconfig.hpp @@ -66,6 +66,7 @@ struct AppConfig { UIConfig ui; IniFile ini; IniFile iniState; + FileInfo iniInfo; void load( std::string& confPath, std::string& keybindingsPath, std::string& initColorScheme, std::vector& recentFiles, std::vector& recentFolders, diff --git a/src/tools/codeeditor/codeeditor.cpp b/src/tools/codeeditor/codeeditor.cpp index f4b41da23..bd85e2e69 100644 --- a/src/tools/codeeditor/codeeditor.cpp +++ b/src/tools/codeeditor/codeeditor.cpp @@ -373,16 +373,16 @@ void App::onTextDropped( String text ) { App::App() : mThreadPool( ThreadPool::createShared( eemax( 2, Sys::getCPUCount() ) ) ) {} App::~App() { + if ( mFileWatcher ) + delete mFileWatcher; + if ( mFileSystemListener ) + delete mFileSystemListener; saveConfig(); eeSAFE_DELETE( mEditorSplitter ); eeSAFE_DELETE( mAutoCompleteModule ); eeSAFE_DELETE( mLinterModule ); eeSAFE_DELETE( mFormatterModule ); eeSAFE_DELETE( mConsole ); - if ( mFileWatcher ) - delete mFileWatcher; - if ( mFileSystemListener ) - delete mFileSystemListener; } void App::updateRecentFiles() { diff --git a/src/tools/codeeditor/formattermodule.cpp b/src/tools/codeeditor/formattermodule.cpp index f57e0e0c7..a3a183dfb 100644 --- a/src/tools/codeeditor/formattermodule.cpp +++ b/src/tools/codeeditor/formattermodule.cpp @@ -2,6 +2,8 @@ #include "thirdparty/json.hpp" #include "thirdparty/subprocess.h" #include +#include +#include using json = nlohmann::json; @@ -78,17 +80,47 @@ void FormatterModule::load( const std::string& formatterPath ) { Log::error( "Parsing formatter failed:\n%s", e.what() ); } } +static std::string randString( size_t len ) { + std::string str( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ); + std::random_device rd; + std::mt19937 generator( rd() ); + std::shuffle( str.begin(), str.end(), generator ); + return str.substr( 0, len ); +} void FormatterModule::formatDoc( UICodeEditor* editor ) { if ( !mReady ) return; - Clock clock; std::shared_ptr doc = editor->getDocumentRef(); auto formatter = supportsFormatter( doc ); if ( formatter.command.empty() && doc->getFilePath().empty() ) return; + IOStreamString fileString; + if ( doc->isDirty() || !doc->hasFilepath() ) { + std::string tmpPath; + if ( !doc->hasFilepath() ) { + tmpPath = Sys::getTempPath() + ".ecode-" + doc->getFilename() + "." + randString( 8 ); + } else { + std::string fileDir( FileSystem::fileRemoveFileName( doc->getFilePath() ) ); + FileSystem::dirAddSlashAtEnd( fileDir ); + tmpPath = fileDir + "." + randString( 8 ) + "." + doc->getFilename(); + } + + doc->save( fileString, true ); + FileSystem::fileWrite( tmpPath, (Uint8*)fileString.getStreamPointer(), + fileString.getSize() ); + runFormatter( editor, formatter, tmpPath ); + FileSystem::fileRemove( tmpPath ); + } else { + runFormatter( editor, formatter, doc->getFilePath() ); + } +} + +void FormatterModule::runFormatter( UICodeEditor* editor, const Formatter& formatter, + const std::string& path ) { + Clock clock; + std::string cmd( formatter.command ); - std::string path( doc->getFilePath() ); String::replaceAll( cmd, "$FILENAME", path ); std::vector cmdArr = String::split( cmd, ' ' ); std::vector strings; diff --git a/src/tools/codeeditor/formattermodule.hpp b/src/tools/codeeditor/formattermodule.hpp index 9683df0be..fbeb63188 100644 --- a/src/tools/codeeditor/formattermodule.hpp +++ b/src/tools/codeeditor/formattermodule.hpp @@ -40,6 +40,8 @@ class FormatterModule : public UICodeEditorModule { void formatDoc( UICodeEditor* editor ); + void runFormatter( UICodeEditor* editor, const Formatter& formatter, const std::string& path ); + FormatterModule::Formatter supportsFormatter( std::shared_ptr doc ); };