FormatterPlugin: CSS native formatter.

This commit is contained in:
Martín Lucas Golini
2022-09-07 19:47:54 -03:00
parent 31847b7918
commit 41dd9bcc6e
9 changed files with 60 additions and 13 deletions

View File

@@ -25,5 +25,10 @@
"file_patterns": ["%.xml", "%.html?$"],
"command": "xml",
"type": "native"
},
{
"file_patterns": ["%.css"],
"command": "css",
"type": "native"
}
]

View File

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

View File

@@ -18,7 +18,7 @@ class EE_API StyleSheet {
bool isEmpty() const;
void print();
std::string print();
void combineStyleSheet( const StyleSheet& styleSheet );

View File

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

View File

@@ -159,6 +159,8 @@ MediaQueryList::ptr MediaQueryList::parse( const std::string& str ) {
list = 0;
}
list->mQueryStr = str;
return list;
}

View File

@@ -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<MediaQueryList::ptr, std::vector<StyleSheetStyle*>> 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 ) {

View File

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

View File

@@ -6,6 +6,8 @@
#include <eepp/system/iostreamstring.hpp>
#include <eepp/system/lock.hpp>
#include <eepp/system/luapattern.hpp>
#include <eepp/ui/css/stylesheet.hpp>
#include <eepp/ui/css/stylesheetparser.hpp>
#include <random>
#define PUGIXML_HEADER_ONLY
#include <pugixml/pugixml.hpp>
@@ -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." };
}
};
}

View File

@@ -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<ThreadPool> pool );