[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdevplatform
From:       Cédric Pasteur <cedric.pasteur () free ! fr>
Date:       2008-09-10 20:14:15
Message-ID: 1221077655.238843.24626.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 859617 by ccpasteur:

A little refactoring + one bugfix

 M  +35 -16    plugins/sourceformatter/sourceformatterplugin.cpp  
 M  +7 -1      plugins/sourceformatter/sourceformatterplugin.h  
 M  +3 -4      util/sourceformattermanager.cpp  


--- trunk/KDE/kdevplatform/plugins/sourceformatter/sourceformatterplugin.cpp \
#859616:859617 @@ -35,6 +35,7 @@
 #include <kparts/partmanager.h>
 #include <ktexteditor/document.h>
 #include <ktexteditor/view.h>
+#include <ktexteditor/variableinterface.h>
 #include <KApplication>
 #include <KConfig>
 #include <KFileDialog>
@@ -88,10 +89,37 @@
 	SourceFormatterManager::self()->deleteLater();
 }
 
+void SourceFormatterPlugin::formatDocument(KDevelop::IDocument *doc, \
ISourceFormatter *formatter, const KMimeType::Ptr &mime) +{
+	KTextEditor::Document *textDoc = doc->textDocument();
+// 	QString oldSpaceIndent;
+// 	QString indentWithTabs;
+// 	if(formatter->indentationType() == ISourceFormatter::IndentWithTabs)
+// 		indentWithTabs = "on";
+// 	else
+// 		indentWithTabs = "off";
+
+// 	KTextEditor::VariableInterface *iface =
+// 		qobject_cast<KTextEditor::VariableInterface*>(textDoc);
+// 	if(iface) {
+// 		oldSpaceIndent = iface->variable("space-indent");
+// 		emit iface->variableChanged(textDoc, "space-indent", indentWithTabs);
+// 	}
+	
+	KTextEditor::Cursor cursor = doc->cursorPosition();
+	QString text = formatter->formatSource(textDoc->text(), mime);
+	text = SourceFormatterManager::self()->addModelineForCurrentLang(text, mime);
+	textDoc->setText(text);
+	doc->setCursorPosition(cursor);
+	
+// 	if(iface)
+// 		iface->variableChanged(textDoc, "space-indent", oldSpaceIndent);
+}
+
 void SourceFormatterPlugin::beautifySource()
 {
-	KTextEditor::Document *doc =
-	    core()->documentController()->activeDocument()->textDocument();
+	KDevelop::IDocumentController *docController = \
KDevelop::ICore::self()->documentController(); +	KDevelop::IDocument *doc = \
docController->activeDocument();  if (!doc)
 		return;
 	// load the appropriate formatter
@@ -101,7 +129,7 @@
 	ISourceFormatter *formatter = manager->formatterForMimeType(mime);
 
 	bool has_selection = false;
-	KTextEditor::View *view = doc->views().first();
+	KTextEditor::View *view = doc->textDocument()->views().first();
 	if (view && view->selection())
 		has_selection = true;
 
@@ -131,14 +159,9 @@
 		if (!original.endsWith('\n'))
 			output.resize(output.length() - 1);
 		//there was a selection, so only change the part of the text related to it
-		doc->replaceText(view->selectionRange(), output);
-	} else {
-		KTextEditor::Cursor cursor = view->cursorPosition();
-		QString text = formatter->formatSource(doc->text(), mime);
-		text = manager->addModelineForCurrentLang(text, mime);
-		doc->setText(text);
-		view->setCursorPosition(cursor);
-	}
+		doc->textDocument()->replaceText(view->selectionRange(), output);
+	} else 
+		formatDocument(doc, formatter, mime);
 }
 
 QString SourceFormatterPlugin::replaceSpacesWithTab(const QString &input, \
ISourceFormatter *formatter) @@ -269,11 +292,7 @@
 		KDevelop::IDocument *doc = docController->documentForUrl(list[fileCount]);
 		if (doc) {
 			kDebug() << "Processing file " << list[fileCount].pathOrUrl() << "opened in \
                editor" << endl;
-			KTextEditor::Cursor cursor = doc->cursorPosition();
-			QString text = formatter->formatSource(doc->textDocument()->text(), mime);
-			text = manager->addModelineForCurrentLang(text, mime);
-			doc->textDocument()->setText(text);
-			doc->setCursorPosition(cursor);
+			formatDocument(doc, formatter, mime);
 			return;
 		}
 
--- trunk/KDE/kdevplatform/plugins/sourceformatter/sourceformatterplugin.h \
#859616:859617 @@ -30,6 +30,10 @@
 {
 	class Part;
 }
+namespace KTextEditor
+{
+	class Document;
+}
 
 #include <interfaces/iplugin.h>
 
@@ -37,7 +41,7 @@
 namespace KDevelop
 {
 	class IPlugin;
-	class Context;
+	class IDocument;
 	class ContextMenuExtension;
 	class ProjectBaseItem;
 }
@@ -60,6 +64,8 @@
 		void formatItem();
 
 	protected:
+		void formatDocument(KDevelop::IDocument *doc, ISourceFormatter *formatter,
+							 const KMimeType::Ptr &mime);
 		QString replaceSpacesWithTab(const QString &input, ISourceFormatter *formatter);
 		QString addIndentation(QString input, const QString indentWith);
 
--- trunk/KDE/kdevplatform/util/sourceformattermanager.cpp #859616:859617
@@ -339,15 +339,14 @@
 	QTextStream os(&output, QIODevice::WriteOnly);
 	QTextStream is(&input, QIODevice::ReadOnly);
 
-        QString modeline;
-// kate: indent-mode cstyle; space-indent off; tab-width 4;  ");
+    QString modeline("// kate: ");
 	QString length = QString::number(m_currentPlugins[m_currentLang]->indentationLength());
  // add indentation style
 	modeline.append("indent-mode ").append(indentationMode(mime)).append("; ");
 
 	ISourceFormatter::IndentationType type = \
m_currentPlugins[m_currentLang]->indentationType();  if (type == \
                ISourceFormatter::IndentWithTabs) {
-		modeline.append("space-indent off; ");
+		modeline.append("replace-tabs off; ");
 		modeline.append("tab-width ").append(length).append("; ");
 	} else {
 		modeline.append("space-indent on; ");
@@ -359,7 +358,7 @@
 	kDebug() << "created modeline: " << modeline << endl;
 
 	bool modelinefound = false;
-	QRegExp kateModeline("//\\s*kate:(.*)$");
+	QRegExp kateModeline("^\\s*//\\s*kate:(.*)$");
 	QRegExp knownOptions("\\s*(indent-width|space-indent|tab-width|indent-mode)");
 	while (!is.atEnd()) {
 		QString line = is.readLine();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic