[geany/geany-plugins] 58bdb8: Merge pull request #683 from sagarchalise/prettyprint-usage

Frank Lanitz git-noreply at xxxxx
Tue Jan 30 18:19:44 UTC 2018


Branch:      refs/heads/master
Author:      Frank Lanitz <frank at frank.uvena.de>
Committer:   GitHub <noreply at github.com>
Date:        Tue, 30 Jan 2018 18:19:44 UTC
Commit:      58bdb808d28122d7b124679509bbb2b5f4bd75e5
             https://github.com/geany/geany-plugins/commit/58bdb808d28122d7b124679509bbb2b5f4bd75e5

Log Message:
-----------
Merge pull request #683 from sagarchalise/prettyprint-usage

Prettyprint usage


Modified Paths:
--------------
    pretty-printer/src/PluginEntry.c

Modified: pretty-printer/src/PluginEntry.c
23 lines changed, 17 insertions(+), 6 deletions(-)
===================================================================
@@ -185,6 +185,7 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
     GeanyEditor* editor;
     ScintillaObject* sco;
     int input_length;
+    gboolean has_selection;
     const gchar* input_buffer;
     int output_length;
     gchar* output_buffer;
@@ -197,20 +198,21 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
 
     editor = doc->editor;
     sco = editor->sci;
-
+    
     /* default printing options */
     if (prettyPrintingOptions == NULL) { prettyPrintingOptions = createDefaultPrettyPrintingOptions(); }
 
+    has_selection = sci_has_selection(sco);
     /* retrieves the text */
-    input_length = sci_get_length(sco);
-    input_buffer = (gchar *) scintilla_send_message(sco, SCI_GETCHARACTERPOINTER, 0, 0);
+    input_buffer = (has_selection)?sci_get_selection_contents(sco):sci_get_contents(sco, -1);
 
     /* checks if the data is an XML format */
     parsedDocument = xmlParseDoc((const unsigned char*)input_buffer);
 
     /* this is not a valid xml => exit with an error message */
     if(parsedDocument == NULL)
     {
+        g_free(input_buffer);
         dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to parse the content as XML."));
         return;
     }
@@ -219,23 +221,32 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
     xmlFreeDoc(parsedDocument);
 
     /* process pretty-printing */
+    input_length = (has_selection)?sci_get_selected_text_length(sco):sci_get_length(sco);
     result = processXMLPrettyPrinting(input_buffer, input_length, &output_buffer, &output_length, prettyPrintingOptions);
     if (result != PRETTY_PRINTING_SUCCESS)
     {
+        g_free(input_buffer);
         dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to process PrettyPrinting on the specified XML because some features are not supported.\n\nSee Help > Debug messages for more details..."));
         return;
     }
 
     /* updates the document */
-    sci_set_text(sco, output_buffer);
+     if(has_selection){
+        sci_replace_sel(sco, output_buffer);
+    }
+    else{
+        sci_set_text(sco, output_buffer);
+    }
 
     /* set the line */
     xOffset = scintilla_send_message(sco, SCI_GETXOFFSET, 0, 0);
     scintilla_send_message(sco, SCI_LINESCROLL, -xOffset, 0); /* TODO update with the right function-call for geany-0.19 */
 
     /* sets the type */
-    fileType = filetypes_index(GEANY_FILETYPES_XML);
-    document_set_filetype(doc, fileType);
+    if(!has_selection && (doc->file_type->id != GEANY_FILETYPES_HTML)){
+        fileType = filetypes_index(GEANY_FILETYPES_XML);
+        document_set_filetype(doc, fileType);
+    }
 
     g_free(output_buffer);
 }



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Plugins-Commits mailing list