[kdevplatform/1.3] /: Push the work-around for kate tab-indentation into DynamicCodeRepresentation.

David nolden david.nolden.kde at art-master.de
Sun Feb 12 14:12:38 UTC 2012


Git commit b9bafa9f8d5768810d095e38b039927e20cbf10d by David nolden.
Committed on 12/02/2012 at 15:09.
Pushed by zwabel into branch '1.3'.

Push the work-around for kate tab-indentation into DynamicCodeRepresentation.

This way the work-around is applied when reforatting as well as when inserting new declarations etc.

CCMAIL: kdevelop-devel at barney.cs.uni-potsdam.de

M  +46   -4    language/codegen/coderepresentation.cpp
M  +2    -1    language/codegen/coderepresentation.h
M  +20   -17   shell/sourceformattercontroller.cpp

http://commits.kde.org/kdevplatform/b9bafa9f8d5768810d095e38b039927e20cbf10d

diff --git a/language/codegen/coderepresentation.cpp b/language/codegen/coderepresentation.cpp
index 704c000..fee1f4e 100644
--- a/language/codegen/coderepresentation.cpp
+++ b/language/codegen/coderepresentation.cpp
@@ -24,6 +24,8 @@
 #include <interfaces/icore.h>
 #include <editor/modificationrevision.h>
 #include <ktexteditor/movinginterface.h>
+#include <ktexteditor/configinterface.h>
+#include <tuple>
 
 namespace KDevelop {
     
@@ -67,12 +69,43 @@ static void grepLine(const QString& identifier, const QString& lineText, int lin
             ret << SimpleRange(lineNumber, start, lineNumber, end);
         }
     }
-    
 }
 
+//NOTE: this is ugly, but otherwise kate might remove tabs again :-/
+// see also: https://bugs.kde.org/show_bug.cgi?id=291074
+struct EditorDisableReplaceTabs {
+  EditorDisableReplaceTabs(KTextEditor::Document* document) : m_iface(qobject_cast<KTextEditor::ConfigInterface*>(document)), m_count(0) {
+  }
+
+  void start() {
+    ++m_count;
+    if( m_count > 1 )
+      return;
+    if ( m_iface ) {
+      m_oldReplaceTabs = m_iface->configValue( "replace-tabs" );
+      m_iface->setConfigValue( "replace-tabs", false );
+    }
+  }
+
+  void end() {
+    --m_count;
+    if( m_count > 0 )
+      return;
+    
+    Q_ASSERT( m_count == 0 );
+    
+    if (m_iface)
+      m_iface->setConfigValue("replace-tabs", m_oldReplaceTabs);
+  }
+
+  KTextEditor::ConfigInterface* m_iface;
+  int m_count;
+  QVariant m_oldReplaceTabs;
+};
+
 class EditorCodeRepresentation : public DynamicCodeRepresentation {
   public:
-  EditorCodeRepresentation(KTextEditor::Document* document) : m_document(document) {
+  EditorCodeRepresentation(KTextEditor::Document* document) : m_document(document), m_replaceTabs(document) {
       m_url = IndexedString(m_document->url());
   }
   
@@ -103,7 +136,10 @@ class EditorCodeRepresentation : public DynamicCodeRepresentation {
   }
   
   bool setText(const QString& text) {
+    
+    startEdit();
     bool ret = m_document->setText(text);
+    endEdit();
     ModificationRevision::clearModificationCache(m_url);
     return ret;
   }
@@ -114,10 +150,12 @@ class EditorCodeRepresentation : public DynamicCodeRepresentation {
   
   void startEdit() {
       m_document->startEditing();
+      m_replaceTabs.start();
   }
   
   void endEdit() {
       m_document->endEditing();
+      m_replaceTabs.end();
   }
   
   bool replace(const KTextEditor::Range& range, const QString& oldText,
@@ -126,10 +164,13 @@ class EditorCodeRepresentation : public DynamicCodeRepresentation {
       if(oldText != old && !ignoreOldText) {
           return false;
       }
-      
+
+      startEdit();
       bool ret = m_document->replaceText(range, newText);
+      endEdit();
+
       ModificationRevision::clearModificationCache(m_url);
-      
+
       return ret;
   }
   
@@ -140,6 +181,7 @@ class EditorCodeRepresentation : public DynamicCodeRepresentation {
   private:
     KTextEditor::Document* m_document;
     IndexedString m_url;
+    EditorDisableReplaceTabs m_replaceTabs;
 };
 
 class FileCodeRepresentation : public CodeRepresentation {
diff --git a/language/codegen/coderepresentation.h b/language/codegen/coderepresentation.h
index 57e229d..41ffcb1 100644
--- a/language/codegen/coderepresentation.h
+++ b/language/codegen/coderepresentation.h
@@ -81,7 +81,8 @@ class KDEVPLATFORMLANGUAGE_EXPORT CodeRepresentation : public QSharedData {
 
 class KDEVPLATFORMLANGUAGE_EXPORT DynamicCodeRepresentation : public CodeRepresentation {
   public:
-      /** Used to group edit-history together. Call this before a bunch of replace(), and endEdit in the end. */
+      /** Used to group edit-history together. Call this optionally before a bunch
+       *  of replace() calls, and then call endEdit in the end, to group them together. */
       virtual void startEdit() = 0;
       virtual bool replace(const KTextEditor::Range& range, const QString& oldText,
                            const QString& newText, bool ignoreOldText = false) = 0;
diff --git a/shell/sourceformattercontroller.cpp b/shell/sourceformattercontroller.cpp
index 369f533..24749cc 100644
--- a/shell/sourceformattercontroller.cpp
+++ b/shell/sourceformattercontroller.cpp
@@ -30,6 +30,7 @@ Boston, MA 02110-1301, USA.
 #include <interfaces/iplugincontroller.h>
 #include <interfaces/ilanguagecontroller.h>
 #include <language/interfaces/ilanguagesupport.h>
+#include <language/codegen/coderepresentation.h>
 #include <interfaces/isourceformatter.h>
 #include "core.h"
 #include <ktexteditor/view.h>
@@ -304,15 +305,6 @@ void SourceFormatterController::beautifySource()
 	if (view && view->selection())
 		has_selection = true;
 
-	//NOTE: this is ugly, but otherwise kate might remove tabs again :-/
-	// see also: https://bugs.kde.org/show_bug.cgi?id=291074
-	KTextEditor::ConfigInterface* iface = qobject_cast<KTextEditor::ConfigInterface*>(doc->textDocument());
-	QVariant oldReplaceTabs;
-	if (iface) {
-		oldReplaceTabs = iface->configValue("replace-tabs");
-		iface->setConfigValue("replace-tabs", false);
-	}
-
 	if (has_selection) {
 		QString original = view->selectionText();
 
@@ -324,13 +316,15 @@ void SourceFormatterController::beautifySource()
 		if (!original.endsWith('\n')  && output.endsWith('\n'))
 			output.resize(output.length() - 1);
 		//there was a selection, so only change the part of the text related to it
-		doc->textDocument()->replaceText(view->selectionRange(), output);
+			
+		// We don't use KTextEditor::Document directly, because CodeRepresentation transparently works
+		// around a possible tab-replacement incompatibility between kate and kdevelop
+		DynamicCodeRepresentation::Ptr code = DynamicCodeRepresentation::Ptr::dynamicCast( KDevelop::createCodeRepresentation( IndexedString( doc->url() ) ) );
+		Q_ASSERT( code );
+		code->replace( view->selectionRange(), original, output );
 	} else {
 		formatDocument(doc, formatter, mime);
 	}
-	if (iface) {
-		iface->setConfigValue("replace-tabs", oldReplaceTabs);
-	}
 }
 
 void SourceFormatterController::beautifyLine()
@@ -356,19 +350,28 @@ void SourceFormatterController::beautifyLine()
 	const QString post = "\n" + tDoc->text(KTextEditor::Range(KTextEditor::Cursor(cursor.line() + 1, 0), tDoc->documentEnd()));
 	
 	const QString formatted = formatter->formatSource(line, doc->url(), mime, prev, post);
-	tDoc->replaceText(KTextEditor::Range(cursor.line(), 0, cursor.line(), line.length()), formatted);
+	
+	// We don't use KTextEditor::Document directly, because CodeRepresentation transparently works
+	// around a possible tab-replacement incompatibility between kate and kdevelop
+	DynamicCodeRepresentation::Ptr code = DynamicCodeRepresentation::Ptr::dynamicCast( KDevelop::createCodeRepresentation( IndexedString( doc->url() ) ) );
+	Q_ASSERT( code );
+	code->replace( KTextEditor::Range(cursor.line(), 0, cursor.line(), line.length()), line, formatted );
+	
 	// advance cursor one line
 	tDoc->activeView()->setCursorPosition(KTextEditor::Cursor(cursor.line() + 1, 0));
 }
 
 void SourceFormatterController::formatDocument(KDevelop::IDocument *doc, ISourceFormatter *formatter, const KMimeType::Ptr &mime)
 {
-	KTextEditor::Document *textDoc = doc->textDocument();
+	// We don't use KTextEditor::Document directly, because CodeRepresentation transparently works
+	// around a possible tab-replacement incompatibility between kate and kdevelop
+	CodeRepresentation::Ptr code = KDevelop::createCodeRepresentation( IndexedString( doc->url() ) );
 
 	KTextEditor::Cursor cursor = doc->cursorPosition();
-	QString text = formatter->formatSource(textDoc->text(), doc->url(), mime);
+	QString text = formatter->formatSource(code->text(), doc->url(), mime);
 	text = addModelineForCurrentLang(text, doc->url(), mime);
-	textDoc->setText(text);
+	code->setText(text);
+	
 	doc->setCursorPosition(cursor);
 }
 





More information about the KDevelop-devel mailing list