diff --git a/include/eepp/ui/doc/textdocument.hpp b/include/eepp/ui/doc/textdocument.hpp index c84396e49..38dcfdbc1 100644 --- a/include/eepp/ui/doc/textdocument.hpp +++ b/include/eepp/ui/doc/textdocument.hpp @@ -435,6 +435,8 @@ class EE_API TextDocument { void setNonWordChars( const String& nonWordChars ); + const SyntaxDefinition& guessSyntax() const; + void resetSyntax(); bool getAutoDetectIndentType() const; diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index 64ce43270..d6b014b6f 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -375,6 +375,11 @@ bool TextDocument::hasSyntaxDefinition() const { return !mSyntaxDefinition.getPatterns().empty(); } +const SyntaxDefinition& TextDocument::guessSyntax() const { + String header( getText( { { 0, 0 }, positionOffset( { 0, 0 }, 128 ) } ) ); + return SyntaxDefinitionManager::instance()->find( mFilePath, header, mHAsCpp ); +} + void TextDocument::resetSyntax() { String header( getText( { { 0, 0 }, positionOffset( { 0, 0 }, 128 ) } ) ); std::string oldDef = mSyntaxDefinition.getLSPName(); diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index e3b999212..80618ece9 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -2592,8 +2592,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { refreshFolderView(); } if ( !editor->getDocument().hasSyntaxDefinition() ) { - editor->getDocument().resetSyntax(); - editor->setSyntaxDefinition( editor->getDocument().getSyntaxDefinition() ); + editor->setSyntaxDefinition( editor->getDocument().guessSyntax() ); } } ); @@ -2642,8 +2641,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { UICodeEditor* editor = event->getNode()->asType(); editor->runOnMainThread( [this, editor] { updateEditorTabTitle( editor ); - editor->getDocument().resetSyntax(); - editor->setSyntaxDefinition( editor->getDocument().getSyntaxDefinition() ); + editor->setSyntaxDefinition( editor->getDocument().guessSyntax() ); } ); } );