Minor fix in the SyntaxHighlighter.

ecode: Improved the FormatterModule. Some minor fixes.
This commit is contained in:
Martín Lucas Golini
2021-09-25 22:31:20 -03:00
parent 4cf65c66bc
commit 21e8ae53af
7 changed files with 53 additions and 9 deletions

View File

@@ -150,7 +150,7 @@ SyntaxDefinitionManager::SyntaxDefinitionManager() {
// JavaScript
add( { "Javascript",
{ "%.js$", "%.json$", "%.cson$", "%.ts$" },
{ "%.js$", "%.json$", "%.cson$", "%.ts$", "%.tsx$" },
{
{ { "//.-\n" }, "comment" },
{ { "/%*", "%*/" }, "comment" },

View File

@@ -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;

View File

@@ -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<std::string>& recentFiles,
const std::vector<std::string>& 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();

View File

@@ -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<std::string>& recentFiles, std::vector<std::string>& recentFolders,

View File

@@ -373,16 +373,16 @@ void App::onTextDropped( String text ) {
App::App() : mThreadPool( ThreadPool::createShared( eemax<int>( 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() {

View File

@@ -2,6 +2,8 @@
#include "thirdparty/json.hpp"
#include "thirdparty/subprocess.h"
#include <eepp/system/filesystem.hpp>
#include <eepp/system/iostreamstring.hpp>
#include <random>
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<TextDocument> 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<std::string> cmdArr = String::split( cmd, ' ' );
std::vector<const char*> strings;

View File

@@ -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<TextDocument> doc );
};