Permalink
Browse files

Merge pull request #44 from pvanek/master

issue #15 XML formatter should indent with user's preference
  • Loading branch information...
2 parents 46166c1 + e557943 commit b6262a5c12b9f7323ceefb7b5c993d40b8df6853 @Mezomish committed Oct 11, 2012
Showing with 49 additions and 1 deletion.
  1. +2 −1 plugins/keybindings/KeysPage.cpp
  2. +39 −0 plugins/xmlformat/xmlformat.cpp
  3. +8 −0 plugins/xmlformat/xmlformat.h
@@ -37,7 +37,8 @@ void KeysPage::init() {
QAction* a = storage_->action(id);
if ( NULL != a ) {
QStringList list;
- list << ""<< a->text() << a->shortcut().toString();
+ // let's remove "&" from action's text to fix #6: Shortcut manager shows extra "&"
+ list << ""<< a->text().replace("&", "") << a->shortcut().toString();
QTreeWidgetItem* item = new QTreeWidgetItem(list);
item->setIcon(0, a->icon());
item->setData(3, Qt::UserRole + 1, id);
@@ -103,6 +103,10 @@ void XmlformatPlugin::formatDocument(Juff::Document *doc)
if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{
QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
+
+ if (EditorSettings::get(EditorSettings::UseTabs))
+ newContent = changeSpacesToTabs(newContent);
+
doc->setText(newContent);
}
else
@@ -130,6 +134,10 @@ void XmlformatPlugin::formatSelection(Juff::Document *doc)
if (dom.setContent(content, false, &errmsg, &errline, &errcolumn))
{
QString newContent = dom.toString(EditorSettings::get(EditorSettings::TabWidth));
+
+ if (EditorSettings::get(EditorSettings::UseTabs))
+ newContent = changeSpacesToTabs(newContent);
+
doc->replaceSelectedText(newContent);
}
else
@@ -141,6 +149,37 @@ void XmlformatPlugin::formatSelection(Juff::Document *doc)
}
}
+QString XmlformatPlugin::changeSpacesToTabs(const QString &text)
+{
+ /* hack and slash implementation of <TAB> leading lines.
+ * Qt itself do not handle <TAB>s in QDomDocument::toString internally
+ * so output is always <SPACE> indented.
+ * Here, if is Juffed setup to use <TAB> for indentation, are leading
+ * spaces converted to tabs.
+ * Currently it's implemented line by line searching for 1st non-whitespace
+ * character in the line. Then are spaces replaced by tabs. One tab per "indent"
+ * spaces.
+ * Finally the new QString is returned.
+ */
+ QString tab('\t');
+ QStringList ret;
+ int indent = EditorSettings::get(EditorSettings::TabWidth);
+
+ foreach (QString line, text.split('\n'))
+ {
+ int ix = line.indexOf(QRegExp("\\S"));
+ if (ix < 1)
+ ret << line;
+ else
+ {
+ line = line.replace(0, ix, tab.repeated(ix / indent));
+ ret << line;
+ }
+ }
+
+ return ret.join("\n");
+}
+
#include "xmlformat.moc"
@@ -56,6 +56,14 @@ public slots:
void formatDocument(Juff::Document *doc);
void formatSelection(Juff::Document *doc);
+
+ /*! QDomDocument::toString uses spaces for indent.
+ * We have to change spaces to tabs manually to handle text by current
+ * editor settings. See implementation notes in the method.
+ * \param text a QString to be handled
+ * \retval QString with leading TABs
+ */
+ QString changeSpacesToTabs(const QString &text);
};
#endif

0 comments on commit b6262a5

Please sign in to comment.