diff --git a/bin/assets/plugins/formatters.json b/bin/assets/plugins/formatters.json index f63bf367a..582e61f9b 100644 --- a/bin/assets/plugins/formatters.json +++ b/bin/assets/plugins/formatters.json @@ -25,5 +25,10 @@ "file_patterns": ["%.xml", "%.html?$"], "command": "xml", "type": "native" + }, + { + "file_patterns": ["%.css"], + "command": "css", + "type": "native" } ] diff --git a/include/eepp/ui/css/mediaquery.hpp b/include/eepp/ui/css/mediaquery.hpp index 280842117..6ae863375 100644 --- a/include/eepp/ui/css/mediaquery.hpp +++ b/include/eepp/ui/css/mediaquery.hpp @@ -161,10 +161,13 @@ class EE_API MediaQueryList { void setMarker( const Uint32& marker ); + const std::string& getQueryString() const { return mQueryStr; } + private: Uint32 mMarker{ 0 }; MediaQuery::vector mQueries; bool mUsed; + std::string mQueryStr; }; }}} // namespace EE::UI::CSS diff --git a/include/eepp/ui/css/stylesheet.hpp b/include/eepp/ui/css/stylesheet.hpp index 6196d3ba3..2cab50fa6 100644 --- a/include/eepp/ui/css/stylesheet.hpp +++ b/include/eepp/ui/css/stylesheet.hpp @@ -18,7 +18,7 @@ class EE_API StyleSheet { bool isEmpty() const; - void print(); + std::string print(); void combineStyleSheet( const StyleSheet& styleSheet ); diff --git a/include/eepp/ui/css/stylesheetstyle.hpp b/include/eepp/ui/css/stylesheetstyle.hpp index c05636723..fba13e314 100644 --- a/include/eepp/ui/css/stylesheetstyle.hpp +++ b/include/eepp/ui/css/stylesheetstyle.hpp @@ -19,7 +19,7 @@ class EE_API StyleSheetStyle { const StyleSheetVariables& variables, MediaQueryList::ptr mediaQueryList ); - std::string build(); + std::string build( bool emmitMediaQueryStart = true, bool emmitMediaQueryEnd = true ); const StyleSheetSelector& getSelector() const; diff --git a/src/eepp/ui/css/mediaquery.cpp b/src/eepp/ui/css/mediaquery.cpp index ed9c6889f..dff7d4182 100644 --- a/src/eepp/ui/css/mediaquery.cpp +++ b/src/eepp/ui/css/mediaquery.cpp @@ -159,6 +159,8 @@ MediaQueryList::ptr MediaQueryList::parse( const std::string& str ) { list = 0; } + list->mQueryStr = str; + return list; } diff --git a/src/eepp/ui/css/stylesheet.cpp b/src/eepp/ui/css/stylesheet.cpp index 1fdefeae0..e5e517ec5 100644 --- a/src/eepp/ui/css/stylesheet.cpp +++ b/src/eepp/ui/css/stylesheet.cpp @@ -134,10 +134,25 @@ bool StyleSheet::isEmpty() const { return mNodes.empty(); } -void StyleSheet::print() { - for ( auto& style : mNodes ) { - std::cout << style->build(); +std::string StyleSheet::print() { + std::string str; + std::map> byMQ; + + for ( size_t i = 0; i < mNodes.size(); ++i ) { + auto node = mNodes[i]; + byMQ[node->getMediaQueryList()].emplace_back( node.get() ); } + + for ( const auto& mq : byMQ ) { + bool first = true; + for ( size_t q = 0; q < mq.second.size(); ++q ) { + const auto& style = mq.second[q]; + str += style->build( first, q == mq.second.size() - 1 ); + first = false; + } + } + + return str; } void StyleSheet::combineStyleSheet( const StyleSheet& styleSheet ) { diff --git a/src/eepp/ui/css/stylesheetstyle.cpp b/src/eepp/ui/css/stylesheetstyle.cpp index 272c6ddfd..cf4db6a5a 100644 --- a/src/eepp/ui/css/stylesheetstyle.cpp +++ b/src/eepp/ui/css/stylesheetstyle.cpp @@ -22,10 +22,16 @@ StyleSheetStyle::StyleSheetStyle( const std::string& selector, } } -std::string StyleSheetStyle::build() { +std::string StyleSheetStyle::build( bool emmitMediaQueryStart, bool emmitMediaQueryEnd ) { std::string css; - css += mSelector.getName() + " {"; + if ( emmitMediaQueryStart && mMediaQueryList && !mMediaQueryList->getQueryString().empty() ) + css += mMediaQueryList->getQueryString() + " {\n\n"; + + css += mSelector.getName() + " {\n"; + + for ( auto& it : mVariables ) + css += "\t" + it.second.getName() + ": " + it.second.getValue() + ";\n"; for ( auto& it : mProperties ) { StyleSheetProperty& prop = it.second; @@ -33,7 +39,10 @@ std::string StyleSheetStyle::build() { css += "\t" + prop.getName() + ": " + prop.getValue() + ";\n"; } - css += "}\n"; + css += "}\n\n"; + + if ( emmitMediaQueryEnd && mMediaQueryList && !mMediaQueryList->getQueryString().empty() ) + css += "}\n\n"; return css; } diff --git a/src/tools/ecode/plugins/formatter/formatterplugin.cpp b/src/tools/ecode/plugins/formatter/formatterplugin.cpp index e70992f1d..b510f1f78 100644 --- a/src/tools/ecode/plugins/formatter/formatterplugin.cpp +++ b/src/tools/ecode/plugins/formatter/formatterplugin.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #define PUGIXML_HEADER_ONLY #include @@ -286,12 +288,22 @@ void FormatterPlugin::registerNativeFormatters() { if ( result ) { xml_string_writer str; doc.save( str ); - return { true, str.result }; + return { true, str.result, "" }; } else { - Log::error( "Couldn't load: %s", file.c_str() ); - Log::error( "Error description: %s", result.description() ); - Log::error( "Error offset: %d", result.offset ); - return { false, "" }; + std::string err( + String::format( "Couldn't load: %s\nError description: %s\nError offset: %td\n", + file.c_str(), result.description(), result.offset ) ); + Log::error( err ); + return { false, "", err }; + } + }; + + mNativeFormatters["css"] = []( const std::string& file ) -> NativeFormatterResult { + CSS::StyleSheetParser parser; + if ( parser.loadFromFile( file ) ) { + return { true, parser.getStyleSheet().print(), "" }; + } else { + return { false, "", "Couldn't parse CSS file." }; } }; } diff --git a/src/tools/ecode/plugins/formatter/formatterplugin.hpp b/src/tools/ecode/plugins/formatter/formatterplugin.hpp index 11dc613b6..3a04aa7e7 100644 --- a/src/tools/ecode/plugins/formatter/formatterplugin.hpp +++ b/src/tools/ecode/plugins/formatter/formatterplugin.hpp @@ -17,6 +17,7 @@ class FormatterPlugin : public UICodeEditorPlugin { struct NativeFormatterResult { bool success; std::string result; + std::string err; }; FormatterPlugin( const std::string& formatterPath, std::shared_ptr pool );