Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

xmlformat improvements (selection) as discussed on jabber #2

Merged
merged 1 commit into from

2 participants

@pvanek
Collaborator

No description provided.

@Mezomish Mezomish merged commit b89be5d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 3, 2012
  1. @pvanek
This page is out of date. Refresh to see the latest.
Showing with 54 additions and 8 deletions.
  1. +48 −7 plugins/xmlformat/xmlformat.cpp
  2. +6 −1 plugins/xmlformat/xmlformat.h
View
55 plugins/xmlformat/xmlformat.cpp
@@ -25,14 +25,13 @@
#include <QtGui>
#include <QDomDocument>
-#include "Document.h"
-
+#include "EditorSettings.h"
XmlformatPlugin::XmlformatPlugin() : QObject(), JuffPlugin()
{
actDoc = new QAction(QIcon(":xmlwrap"), tr("Format XML Document"), this);
- connect(actDoc, SIGNAL(triggered()), this, SLOT(formatDocument()));
+ connect(actDoc, SIGNAL(triggered()), this, SLOT(format()));
}
void XmlformatPlugin::init() {
@@ -71,7 +70,7 @@ Juff::ActionList XmlformatPlugin::mainMenuActions(Juff::MenuID id) const
return list;
}
-void XmlformatPlugin::formatDocument()
+void XmlformatPlugin::format()
{
Juff::Document* doc = api()->currentDocument();
// api()->currentDocument() never returns NULL, it returns
@@ -79,22 +78,64 @@ void XmlformatPlugin::formatDocument()
if ( doc->isNull() )
return;
+ // if there is a selection - try to format a selection.
+ // In the other case format all document
+ if (doc->hasSelectedText())
+ {
+ formatSelection(doc);
+ }
+ else
+ {
+ formatDocument(doc);
+ }
+}
+
+void XmlformatPlugin::formatDocument(Juff::Document *doc)
+{
QString content;
+ QDomDocument dom;
+ QString errmsg;
+ int errline, errcolumn;
+
if (! doc->getText(content))
return;
+ if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
+ {
+ QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
+ doc->setText(newContent);
+ }
+ else
+ {
+ QMessageBox::information(0, tr("XML format error"),
+ tr("Cannot format XML due error (line: %1, column: %2)").arg(errline).arg(errcolumn)
+ + "<br/>"
+ + errmsg);
+ }
+}
+
+void XmlformatPlugin::formatSelection(Juff::Document *doc)
+{
+ QString content;
QDomDocument dom;
QString errmsg;
int errline, errcolumn;
+
+ if (! doc->getSelectedText(content))
+ return;
+
+ int line, column, tmp1, tmp2;
+ doc->getSelection(line, column, tmp1, tmp2);
+
if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{
- QString newContent = dom.toString(4);
- doc->setText(newContent);
+ QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
+ doc->replaceSelectedText(newContent);
}
else
{
QMessageBox::information(0, tr("XML format error"),
- tr("Cannot format XML due error (line: %1, column: %2)").arg(errline).arg(errcolumn)
+ tr("Cannot format XML due error (line: %1, column: %2)").arg(errline+line).arg(errline==1 ? errcolumn+column : errcolumn)
+ "<br/>"
+ errmsg);
}
View
7 plugins/xmlformat/xmlformat.h
@@ -25,6 +25,8 @@
#include <QtCore/QObject>
#include "JuffPlugin.h"
+#include "Document.h"
+
/*! Try to format content of the current dcument as a XML or
similar markup language (HTML, SGML,...)
@@ -47,10 +49,13 @@ class XmlformatPlugin : public QObject, public JuffPlugin {
Juff::ActionList mainMenuActions(Juff::MenuID) const;
public slots:
- void formatDocument();
+ void format();
private:
QAction * actDoc;
+
+ void formatDocument(Juff::Document *doc);
+ void formatSelection(Juff::Document *doc);
};
#endif
Something went wrong with that request. Please try again.