Revision: 2103 http://geany-plugins.svn.sourceforge.net/geany-plugins/?rev=2103&view=re... Author: ctabin Date: 2011-07-22 09:57:55 +0000 (Fri, 22 Jul 2011)
Log Message: ----------- XML Pretty-Printer: Configuration panel added and new features for comments and text node formatting
Modified Paths: -------------- trunk/geany-plugins/pretty-printer/src/ConfigUI.c trunk/geany-plugins/pretty-printer/src/PluginEntry.c trunk/geany-plugins/pretty-printer/src/PluginEntry.h trunk/geany-plugins/pretty-printer/src/PrettyPrinter.c trunk/geany-plugins/pretty-printer/src/PrettyPrinter.h
Modified: trunk/geany-plugins/pretty-printer/src/ConfigUI.c =================================================================== --- trunk/geany-plugins/pretty-printer/src/ConfigUI.c 2011-06-26 22:03:46 UTC (rev 2102) +++ trunk/geany-plugins/pretty-printer/src/ConfigUI.c 2011-07-22 09:57:55 UTC (rev 2103) @@ -18,65 +18,203 @@
#include "ConfigUI.h"
-//redefine the variable declared as extern +//======================= FUNCTIONS ==================================================================== + +static GtkWidget* createTwoOptionsBox(const char* label, const char* checkBox1, const char* checkBox2, gboolean cb1Active, gboolean cb2Active, GtkWidget** option1, GtkWidget** option2); +static GtkWidget* createEmptyTextOptions(gboolean emptyNodeStripping, gboolean emptyNodeStrippingSpace, gboolean forceEmptyNodeSplit); +static GtkWidget* createIndentationOptions(char indentation, int count); +static GtkWidget* createLineReturnOptions(const char* lineReturn); + +//============================================ PRIVATE PROPERTIES ====================================== + +static GtkWidget* commentOneLine; +static GtkWidget* commentInline; +static GtkWidget* textOneLine; +static GtkWidget* textInline; +static GtkWidget* cdataOneLine; +static GtkWidget* cdataInline; +static GtkWidget* emptyNodeStripping; +static GtkWidget* emptyNodeStrippingSpace; +static GtkWidget* emptyNodeSplit; +static GtkWidget* indentationChar; +static GtkWidget* indentationCount; +static GtkWidget* lineBreak; + +//============================================= PUBLIC FUNCTIONS ======================================== + +//redeclaration of extern variable PrettyPrintingOptions* prettyPrintingOptions;
-GtkWidget* createPrettyPrinterConfigUI(GtkDialog* dialog) +//Will never be used, just here for example +GtkWidget* createPrettyPrinterConfigUI(GtkDialog * dialog) { //default printing options if (prettyPrintingOptions == NULL) { prettyPrintingOptions = createDefaultPrettyPrintingOptions(); } + PrettyPrintingOptions* ppo = prettyPrintingOptions; + + GtkWidget* container = gtk_hbox_new(FALSE, 10);
- //TODO create configuration widget + GtkWidget* leftBox = gtk_vbox_new(TRUE, 6); + GtkWidget* commentOptions = createTwoOptionsBox("Comments", "Put on one line", "Inline if possible", ppo->oneLineComment, ppo->inlineComment, &commentOneLine, &commentInline); + GtkWidget* textOptions = createTwoOptionsBox("Text nodes", "Put on one line", "Inline if possible", ppo->oneLineText, ppo->inlineText, &textOneLine, &textInline); + GtkWidget* cdataOptions = createTwoOptionsBox("CDATA", "Put on one line", "Inline if possible", ppo->oneLineCdata, ppo->inlineCdata, &cdataOneLine, &cdataInline); + GtkWidget* emptyOptions = createEmptyTextOptions(ppo->emptyNodeStripping, ppo->emptyNodeStrippingSpace, ppo->forceEmptyNodeSplit); + GtkWidget* indentationOptions = createIndentationOptions(ppo->indentChar, ppo->indentLength); + GtkWidget* lineReturnOptions = createLineReturnOptions(ppo->newLineChars); + + gtk_box_pack_start(GTK_BOX(leftBox), commentOptions, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(leftBox), textOptions, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(leftBox), cdataOptions, FALSE, FALSE, 3);
- return NULL; + GtkWidget* rightBox = gtk_vbox_new(FALSE, 6); + gtk_box_pack_start(GTK_BOX(rightBox), emptyOptions, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), indentationOptions, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), lineReturnOptions, FALSE, FALSE, 3); + + gtk_box_pack_start(GTK_BOX(container), leftBox, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(container), rightBox, FALSE, FALSE, 3); + + gtk_widget_show_all(container); + return container; }
void saveSettings() { - //TODO save settings into a file + PrettyPrintingOptions* ppo = prettyPrintingOptions; + + ppo->oneLineComment = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(commentOneLine)); + ppo->inlineComment = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(commentInline)); + ppo->oneLineText = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(textOneLine)); + ppo->inlineText = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(textInline)); + ppo->oneLineCdata = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cdataOneLine)); + ppo->inlineCdata = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cdataInline)); + + ppo->emptyNodeStripping = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(emptyNodeStripping)); + ppo->emptyNodeStrippingSpace = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(emptyNodeStrippingSpace)); + ppo->forceEmptyNodeSplit = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(emptyNodeSplit)); + + ppo->indentLength = gtk_spin_button_get_value(GTK_SPIN_BUTTON(indentationCount)); + ppo->indentChar = gtk_combo_box_get_active(GTK_COMBO_BOX(indentationChar))==0 ? '\t' : ' '; + + int breakStyle = gtk_combo_box_get_active(GTK_COMBO_BOX(lineBreak)); + if (breakStyle == 0) ppo->newLineChars = "\r"; + else if (breakStyle == 1) ppo->newLineChars = "\n"; + else ppo->newLineChars = "\r\n"; }
+//============================================= PRIVATE FUNCTIONS =======================================
-//Will never be used, just here for example -/*GtkWidget* plugin_configure(GtkDialog * dialog) +GtkWidget* createTwoOptionsBox(const char* label, const char* checkBox1, const char* checkBox2, gboolean cb1Active, gboolean cb2Active, GtkWidget** option1, GtkWidget** option2) { - //default printing options - if (prettyPrintingOptions == NULL) { prettyPrintingOptions = createDefaultPrettyPrintingOptions(); } + GtkWidget* container = gtk_hbox_new(TRUE, 2); + GtkWidget* rightBox = gtk_vbox_new(FALSE, 6); + GtkWidget* leftBox = gtk_vbox_new(FALSE, 6);
- GtkWidget* globalBox = gtk_hbox_new(TRUE, 4); + GtkWidget* lbl = gtk_label_new(label); + GtkWidget* chb1 = gtk_check_button_new_with_label(checkBox1); + GtkWidget* chb2 = gtk_check_button_new_with_label(checkBox2); + + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chb1), cb1Active); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chb2), cb2Active); + + gtk_box_pack_start(GTK_BOX(container), leftBox, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(container), rightBox, FALSE, FALSE, 3); + + gtk_box_pack_start(GTK_BOX(leftBox), lbl, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), chb1, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), chb2, FALSE, FALSE, 3); + + *option1 = chb1; + *option2 = chb2; + + return container; +} + +GtkWidget* createEmptyTextOptions(gboolean optEmptyNodeStripping, gboolean optEmptyNodeStrippingSpace, gboolean optForceEmptyNodeSplit) +{ + GtkWidget* container = gtk_hbox_new(FALSE, 2); GtkWidget* rightBox = gtk_vbox_new(FALSE, 6); - GtkWidget* centerBox = gtk_vbox_new(FALSE, 6); GtkWidget* leftBox = gtk_vbox_new(FALSE, 6);
- GtkWidget* textLabel = gtk_label_new("Text nodes"); - GtkWidget* textOneLine = gtk_check_button_new_with_label("One line"); - GtkWidget* textInline = gtk_check_button_new_with_label("Inline"); + GtkWidget* lbl = gtk_label_new("Empty nodes"); + GtkWidget* chb1 = gtk_check_button_new_with_label("Concatenation (<x></x> to <x/>)"); + GtkWidget* chb2 = gtk_check_button_new_with_label("Spacing (<x/> to <x />)"); + GtkWidget* chb3 = gtk_check_button_new_with_label("Expansion (<x/> to <x></x>)");
- GtkWidget* cdataLabel = gtk_label_new("CDATA nodes"); - GtkWidget* cdataOneLine = gtk_check_button_new_with_label("One line"); - GtkWidget* cdataInline = gtk_check_button_new_with_label("Inline"); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chb1), optEmptyNodeStripping); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chb2), optEmptyNodeStrippingSpace); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chb3), optForceEmptyNodeSplit);
- GtkWidget* commentLabel = gtk_label_new("Comments"); - GtkWidget* commentOneLine = gtk_check_button_new_with_label("One line"); - GtkWidget* commentInline = gtk_check_button_new_with_label("Inline"); + gtk_box_pack_start(GTK_BOX(container), leftBox, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(container), rightBox, FALSE, FALSE, 3);
- gtk_box_pack_start(GTK_BOX(globalBox), leftBox, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(globalBox), centerBox, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(globalBox), rightBox, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(leftBox), lbl, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), chb1, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), chb2, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), chb3, FALSE, FALSE, 3); + + emptyNodeStripping = chb1; + emptyNodeStrippingSpace = chb2; + emptyNodeSplit = chb3; + + return container; +}
- gtk_box_pack_start(GTK_BOX(leftBox), textLabel, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(centerBox), textOneLine, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(rightBox), textInline, FALSE, FALSE, 3); +GtkWidget* createIndentationOptions(char indentation, int count) +{ + GtkWidget* container = gtk_hbox_new(FALSE, 20); + GtkWidget* rightBox = gtk_hbox_new(FALSE, 6); + GtkWidget* leftBox = gtk_vbox_new(FALSE, 6); + + GtkWidget* lbl = gtk_label_new("Indentation"); + GtkWidget* comboChar = gtk_combo_box_text_new(); + GtkWidget* spinIndent = gtk_spin_button_new_with_range(0, 100, 1); + + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboChar), "Tab"); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboChar), "Space"); + gtk_combo_box_set_active(GTK_COMBO_BOX(comboChar), (indentation == ' ') ? 1 : 0); + + gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinIndent), count); + + gtk_box_pack_start(GTK_BOX(leftBox), lbl, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), comboChar, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), spinIndent, FALSE, FALSE, 3); + + gtk_box_pack_start(GTK_BOX(container), leftBox, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(container), rightBox, FALSE, FALSE, 3); + + indentationChar = comboChar; + indentationCount = spinIndent; + + return container; +}
- gtk_box_pack_start(GTK_BOX(leftBox), cdataLabel, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(centerBox), cdataOneLine, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(rightBox), cdataInline, FALSE, FALSE, 3); +static GtkWidget* createLineReturnOptions(const char* lineReturn) +{ + GtkWidget* container = gtk_hbox_new(FALSE, 25); + GtkWidget* rightBox = gtk_hbox_new(FALSE, 6); + GtkWidget* leftBox = gtk_vbox_new(FALSE, 6);
- gtk_box_pack_start(GTK_BOX(leftBox), commentLabel, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(centerBox), commentOneLine, FALSE, FALSE, 3); - gtk_box_pack_start(GTK_BOX(rightBox), commentInline, FALSE, FALSE, 3); - - gtk_widget_show_all(globalBox); - return globalBox; + GtkWidget* lbl = gtk_label_new("Line break"); + GtkWidget* comboChar = gtk_combo_box_text_new(); + + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboChar), "\r"); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboChar), "\n"); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(comboChar), "\r\n"); + + int active = 0; + if (strlen(lineReturn) == 2) active = 2; + else if (lineReturn[0] == '\n') active = 1; + + gtk_combo_box_set_active(GTK_COMBO_BOX(comboChar), active); + + gtk_box_pack_start(GTK_BOX(leftBox), lbl, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(rightBox), comboChar, FALSE, FALSE, 3); + + gtk_box_pack_start(GTK_BOX(container), leftBox, FALSE, FALSE, 3); + gtk_box_pack_start(GTK_BOX(container), rightBox, FALSE, FALSE, 3); + + lineBreak = comboChar; + + return container; } -*/
Modified: trunk/geany-plugins/pretty-printer/src/PluginEntry.c =================================================================== --- trunk/geany-plugins/pretty-printer/src/PluginEntry.c 2011-06-26 22:03:46 UTC (rev 2102) +++ trunk/geany-plugins/pretty-printer/src/PluginEntry.c 2011-07-22 09:57:55 UTC (rev 2103) @@ -41,8 +41,11 @@ //declaration of the functions static void xml_format(GtkMenuItem *menuitem, gpointer gdata); static void kb_run_xml_pretty_print(G_GNUC_UNUSED guint key_id); +static void config_closed(GtkWidget* configWidget, gint response, gpointer data); + void plugin_init(GeanyData *data); void plugin_cleanup(void); +GtkWidget* plugin_configure(GtkDialog * dialog);
//========================================== FUNCTIONS ===================================================================
@@ -73,29 +76,25 @@ gtk_widget_destroy(main_menu_item); }
-#ifdef PLUGIN_XMLPP_GUI - static void config_closed(GtkWidget* configWidget, gint response, gpointer data); +GtkWidget* plugin_configure(GtkDialog * dialog) +{ + //creates the configuration widget + GtkWidget* widget = createPrettyPrinterConfigUI(dialog); + g_signal_connect(dialog, "response", G_CALLBACK(config_closed), NULL); + return widget; +}
- GtkWidget* plugin_configure(GtkDialog * dialog) - { - //creates the configuration widget - GtkWidget* widget = createPrettyPrinterConfigUI(dialog); - g_signal_connect(dialog, "response", G_CALLBACK(config_closed), NULL); - return widget; - } - //========================================== LISTENERS ===================================================================
- void config_closed(GtkWidget* configWidget, gint response, gpointer gdata) +void config_closed(GtkWidget* configWidget, gint response, gpointer gdata) +{ + //if the user clicked OK or APPLY, then save the settings + if (response == GTK_RESPONSE_OK || + response == GTK_RESPONSE_APPLY) { - //if the user clicked OK or APPLY, then save the settings - if (response == GTK_RESPONSE_OK || - response == GTK_RESPONSE_APPLY) - { - saveSettings(); - } + saveSettings(); } -#endif +}
static void kb_run_xml_pretty_print(G_GNUC_UNUSED guint key_id) { @@ -124,15 +123,18 @@ sci_get_text(sco, length, buffer);
//checks if the data is an XML format - xmlDoc* xmlDoc = xmlParseDoc((unsigned char*)buffer); + xmlDoc* parsedDocument = xmlParseDoc((unsigned char*)buffer);
//this is not a valid xml => exit with an error message - if(xmlDoc == NULL) + if(parsedDocument == NULL) { dialogs_show_msgbox(GTK_MESSAGE_ERROR, "Unable to parse the content as XML."); return; }
+ //free all + xmlFreeDoc(parsedDocument); + //process pretty-printing int result = processXMLPrettyPrinting(&buffer, &length, prettyPrintingOptions); if (result != PRETTY_PRINTING_SUCCESS) @@ -151,7 +153,4 @@ //sets the type GeanyFiletype* fileType = filetypes_index(GEANY_FILETYPES_XML); document_set_filetype(doc, fileType); - - //free all - xmlFreeDoc(xmlDoc); }
Modified: trunk/geany-plugins/pretty-printer/src/PluginEntry.h =================================================================== --- trunk/geany-plugins/pretty-printer/src/PluginEntry.h 2011-06-26 22:03:46 UTC (rev 2102) +++ trunk/geany-plugins/pretty-printer/src/PluginEntry.h 2011-07-22 09:57:55 UTC (rev 2103) @@ -19,9 +19,6 @@ #ifndef PLUGIN_ENTRY_H #define PLUGIN_ENTRY_H
-// This constants will be enabled once the preferences GUI will be finished :) -//#define PLUGIN_XMLPP_GUI - //========================================== INCLUDES ==========================================================
#include <stdlib.h>
Modified: trunk/geany-plugins/pretty-printer/src/PrettyPrinter.c =================================================================== --- trunk/geany-plugins/pretty-printer/src/PrettyPrinter.c 2011-06-26 22:03:46 UTC (rev 2102) +++ trunk/geany-plugins/pretty-printer/src/PrettyPrinter.c 2011-07-22 09:57:55 UTC (rev 2103) @@ -24,7 +24,7 @@ static void putCharInBuffer(char charToAdd); //put a char into the new char buffer static void putCharsInBuffer(char* charsToAdd); //put the chars into the new char buffer static void putNextCharsInBuffer(int nbChars); //put the next nbChars of the input buffer into the new buffer -static int readWhites(); //read the next whites into the input buffer +static int readWhites(gboolean considerLineBreakAsWhite); //read the next whites into the input buffer static char readNextChar(); //read the next char into the input buffer; static char getNextChar(); //returns the next char but do not increase the input buffer index (use readNextChar for that) static char getPreviousInsertedChar(); //returns the last inserted char into the new buffer @@ -33,6 +33,7 @@ static gboolean isQuote(char c); //check if the specified char is a quote (simple or double) static int putNewLine(); //put a new line into the new char buffer with the correct number of whites (indentation) static gboolean isInlineNodeAllowed(); //check if it is possible to have an inline node +static gboolean isOnSingleLine(int skip, char stop1, char stop2);//check if the current node data is on one line (for inlining) static void resetBackwardIndentation(gboolean resetLineBreak); //reset the indentation for the current depth (just reset the index in fact)
//specific parsing functions @@ -98,7 +99,7 @@ if (xmlPrettyPrinted == NULL) { g_error("Allocation error"); }
//go to the first char - readWhites(); + readWhites(TRUE);
//process the pretty-printing processElements(); @@ -148,17 +149,18 @@ options->newLineChars = "\r\n"; options->indentChar = ' '; options->indentLength = 2; - options->oneLineText = TRUE; + options->oneLineText = FALSE; options->inlineText = TRUE; - options->oneLineComment = TRUE; + options->oneLineComment = FALSE; options->inlineComment = TRUE; - options->oneLineCdata = TRUE; + options->oneLineCdata = FALSE; options->inlineCdata = TRUE; options->emptyNodeStripping = TRUE; options->emptyNodeStrippingSpace = TRUE; options->forceEmptyNodeSplit = FALSE; options->trimLeadingWhites = TRUE; options->trimTrailingWhites = TRUE; + options->commentAlign = TRUE;
return options; } @@ -227,10 +229,12 @@ return inputBuffer[inputBufferIndex++]; }
-int readWhites() +int readWhites(gboolean considerLineBreakAsWhite) { int counter = 0; - while(isWhite(inputBuffer[inputBufferIndex])) + while(isWhite(inputBuffer[inputBufferIndex]) && + (!isLineBreak(inputBuffer[inputBufferIndex]) || + considerLineBreakAsWhite)) { ++counter; ++inputBufferIndex; @@ -327,6 +331,48 @@ return FALSE; }
+gboolean isOnSingleLine(int skip, char stop1, char stop2) +{ + int currentIndex = inputBufferIndex+skip; //skip the n first chars (in comment <!--) + gboolean isOnSingleLine = TRUE; + + char oldChar = inputBuffer[currentIndex]; + char currentChar = inputBuffer[currentIndex+1]; + while(isOnSingleLine && oldChar != stop1 && currentChar != stop2) + { + isOnSingleLine = !isLineBreak(oldChar); + + ++currentIndex; + oldChar = currentChar; + currentChar = inputBuffer[currentIndex+1]; + + /** + * A line break inside the node has been reached. But we should check + * if there is something before the end of the node (otherwise, there + * are only spaces and it may be wanted to be considered as a single + * line). //TODO externalize an option for that ? + */ + if (!isOnSingleLine) + { + while(oldChar != stop1 && currentChar != stop2) + { + //okay there is something else => this is not on one line + if (!isWhite(oldChar)) return FALSE; + + ++currentIndex; + oldChar = currentChar; + currentChar = inputBuffer[currentIndex+1]; + } + + //the end of the node has been reached with only whites. Then + //the node can be considered being one single line + return TRUE; + } + } + + return isOnSingleLine; +} + void resetBackwardIndentation(gboolean resetLineBreak) { xmlPrettyPrintedIndex -= (currentDepth*options->indentLength); @@ -355,7 +401,7 @@ while (loop && result == PRETTY_PRINTING_SUCCESS) { //strip unused whites - readWhites(); + readWhites(TRUE);
char nextChar = getNextChar(); if (nextChar == '\0') { return 0; } //no more data to read @@ -459,7 +505,7 @@ gboolean loop = TRUE; while (loop) { - readWhites(); //strip the whites + readWhites(TRUE); //strip the whites
char next = getNextChar(); //don't read the last char (processed afterwards) if (next == '/') { loop = FALSE; } /* end of node */ @@ -492,7 +538,7 @@
while(!isWhite(getNextChar())) { putNextCharsInBuffer(1); }
- readWhites(); + readWhites(TRUE); processElementAttributes();
//puts the '?' and '>' chars into the new buffer @@ -537,7 +583,7 @@ lastNodeOpen = TRUE;
//process the attributes - readWhites(); + readWhites(TRUE); processElementAttributes();
//process the end of the tag @@ -642,6 +688,7 @@ { gboolean inlineAllowed = FALSE; if (options->inlineComment) { inlineAllowed = isInlineNodeAllowed(); } + if (inlineAllowed && !options->oneLineComment) { inlineAllowed = isOnSingleLine(4, '-', '-'); } if (inlineAllowed) { resetBackwardIndentation(TRUE); }
putNextCharsInBuffer(4); //add the chars '<!--' @@ -660,18 +707,45 @@ { putCharInBuffer(nextChar); oldChar = nextChar; + + if (!loop && options->commentAlign) //end of comment + { + //ensures the chars preceding the first '-' are all spaces + gboolean rewind = xmlPrettyPrinted[xmlPrettyPrintedIndex-3] == ' ' && + xmlPrettyPrinted[xmlPrettyPrintedIndex-4] == ' ' && + xmlPrettyPrinted[xmlPrettyPrintedIndex-5] == ' ' && + xmlPrettyPrinted[xmlPrettyPrintedIndex-6] == ' ' && + xmlPrettyPrinted[xmlPrettyPrintedIndex-7] == ' '; + + //if all the preceding chars are white, then go for replacement + if (rewind) + { + xmlPrettyPrintedIndex -= 7; //remove indentation spaces + putCharsInBuffer("--"); //reset the first chars of '-->' + } + } } - else if (!options->oneLineComment) //oh ! there is a line break + else if (!options->oneLineComment && !inlineAllowed) //oh ! there is a line break { - readWhites(); //strip the whites and new line - putNewLine(); //put a new indentation line + int read = readWhites(FALSE); //strip the whites and new line + if (nextChar == '\r' && read == 0 && getNextChar() == '\n') //handles the \r\n return line + { + readNextChar(); + readWhites(FALSE); + } + + //if the comments need to be aligned, just add 5 spaces + if (options->commentAlign) + { + putNewLine(); //put a new indentation line + putCharsInBuffer(" "); + } + oldChar = ' '; //and update the last char - - //TODO manage relative spacing } else //the comments must be inlined { - readWhites(); //strip the whites and add a space if needed + readWhites(TRUE); //strip the whites and add a space if needed if (getPreviousInsertedChar() != ' ') { putCharInBuffer(' '); @@ -699,6 +773,7 @@ //checks if inline is allowed gboolean inlineTextAllowed = FALSE; if (options->inlineText) { inlineTextAllowed = isInlineNodeAllowed(); } + if (inlineTextAllowed && !options->oneLineText) { inlineTextAllowed = isOnSingleLine(0, '<', '/'); } if (inlineTextAllowed) { resetBackwardIndentation(TRUE); } //remove previous indentation
//the leading whites are automatically stripped. So we re-add it @@ -730,9 +805,10 @@ char nextChar = readNextChar(); if (isLineBreak(nextChar)) { - readWhites(); if (options->oneLineText) { + readWhites(TRUE); + //as we can put text on one line, remove the line break //and replace it by a space but only if the previous //char wasn't a space @@ -743,6 +819,13 @@ } else { + int read = readWhites(FALSE); + if (nextChar == '\r' && read == 0 && getNextChar() == '\n') //handles the '\r\n' + { + nextChar = readNextChar(); + readWhites(FALSE); + } + //put a new line only if the closing tag is not reached if (getNextChar() != '<') { @@ -796,7 +879,7 @@ } else if (!options->oneLineCdata) { - readWhites(); //strip the whites and new line + readWhites(TRUE); //strip the whites and new line putNewLine(); //put a new indentation line oldChar = ' '; //and update the last char
@@ -804,7 +887,7 @@ } else //cdata are inlined { - readWhites(); //strip the whites and add a space if necessary + readWhites(TRUE); //strip the whites and add a space if necessary if(getPreviousInsertedChar() != ' ') { putCharInBuffer(' '); } } } @@ -832,7 +915,7 @@ gboolean loop = TRUE; while(loop) { - readWhites(); + readWhites(TRUE); putCharInBuffer(' '); //only one space for the attributes
int nextChar = readNextChar(); @@ -892,7 +975,7 @@ result = PRETTY_PRINTING_NOT_SUPPORTED_YET; }
-void printError(char *msg, ...) +void printError(char *msg, ...) { va_list va; va_start(va, msg); @@ -902,7 +985,7 @@ //TODO also do a fprintf on stderr ?
printDebugStatus(); -} +}
void printDebugStatus() {
Modified: trunk/geany-plugins/pretty-printer/src/PrettyPrinter.h =================================================================== --- trunk/geany-plugins/pretty-printer/src/PrettyPrinter.h 2011-06-26 22:03:46 UTC (rev 2102) +++ trunk/geany-plugins/pretty-printer/src/PrettyPrinter.h 2011-07-22 09:57:55 UTC (rev 2103) @@ -57,7 +57,8 @@ gboolean forceEmptyNodeSplit; //force an empty node to be splitted : <node /> becomes <node></node> (only if emptyNodeStripping = false) gboolean trimLeadingWhites; //trim the leading whites in a text node gboolean trimTrailingWhites; //trim the trailing whites in a text node -} + gboolean commentAlign; //align the comments: add 5 spaces in indentation (only if oneLineComment = false) +} PrettyPrintingOptions;
//========================================== FUNCTIONS =========================================================
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.