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

List:       kde-commits
Subject:    KDE/kdevplatform/plugins/snippet
From:       Milian Wolff <mail () milianw ! de>
Date:       2009-09-13 18:54:24
Message-ID: 1252868064.620658.20272.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1023033 by mwolff:

introduce a new way to input snippet variables:
when the item gets completed, a SelectionJumper gets created that manages a list of \
smartranges for the variables. the first range is selected at the beginning. You can \
jump to the next/prev variable (and get its range selected) with ALT + Left and ALT + \
Right respectivly. Esc closes the SelectionJumper.

TODO: maybe it should be better visible that a selection jumper is still running. or \
delete the selection jumper when the user moves the cursor by hand. but that could \
lead to other issues.

 M  +1 -0      CMakeLists.txt  
 A             selectionjumper.cpp   [License: LGPL (v2+)]
 A             selectionjumper.h   [License: LGPL (v2+)]
 M  +45 -0     snippetcompletionitem.cpp  


--- trunk/KDE/kdevplatform/plugins/snippet/CMakeLists.txt #1023032:1023033
@@ -14,6 +14,7 @@
     snippetfilterproxymodel.cpp
     snippetcompletionmodel.cpp
     snippetcompletionitem.cpp
+    selectionjumper.cpp
     snippet.cpp
     moverepository.cpp
     editsnippet.cpp
--- trunk/KDE/kdevplatform/plugins/snippet/snippetcompletionitem.cpp #1023032:1023033
@@ -28,6 +28,7 @@
 #include <language/codecompletion/codecompletionmodel.h>
 
 #include "snippet.h"
+#include "selectionjumper.h"
 
 SnippetCompletionItem::SnippetCompletionItem( const QString& name, const QString& \
                snippet )
     : CompletionTreeItem(), m_name(name), m_snippet(snippet), m_expandingWidget(0)
@@ -81,5 +82,49 @@
 
 void SnippetCompletionItem::execute( KTextEditor::Document* document, const \
KTextEditor::Range& word )  {
+    // parse snippet for markers
+    QList<KTextEditor::Range> markers;
+    bool isEscaped = false;
+    int line = word.start().line();
+    int column = word.start().column();
+    KTextEditor::Cursor start = KTextEditor::Cursor::invalid();
+    QList<int> charsToRemove;
+    for ( int i = 0; i < m_snippet.size(); ++i ) {
+        if ( m_snippet[i] == '\\' ) {
+            ++column;
+            isEscaped = !isEscaped;
+        } else if ( m_snippet[i] == '\n' ) {
+            ++line;
+            column = 0;
+        } else if ( m_snippet[i] == '$' ) {
+            if ( isEscaped ) {
+                charsToRemove << i - 1;
+                isEscaped = false;
+            } else {
+                charsToRemove << i;
+                if ( start.isValid() ) {
+                    KTextEditor::Cursor end(line, column);
+                    markers << KTextEditor::Range(start, end);
+                    start = KTextEditor::Cursor::invalid();
+                } else {
+                    start = KTextEditor::Cursor(line, column);
+                }
+            }
+        } else {
+            isEscaped = false;
+            ++column;
+        }
+    }
+    // remove $'s
+    int deletedChars = 0;
+    foreach ( const int &pos, charsToRemove ) {
+        m_snippet.remove(pos - deletedChars, 1 );
+        ++deletedChars;
+    }
+
     document->replaceText( word, m_snippet );
+
+    if ( !markers.isEmpty() ) {
+        new SelectionJumper(markers, document);
+    }
 }


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

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