issue #15 XML formatter should indent with user's preference #44

Merged
merged 2 commits into from Oct 11, 2012
Jump to file or symbol
Failed to load files and symbols.
+49 −1
Split
@@ -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