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

List:       kde-commits
Subject:    [kate] part/script/data: quick coding, we steal a bit ideas from zen coding aka emmet for other lang
From:       Christoph Cullmann <cullmann () kde ! org>
Date:       2012-10-31 20:07:03
Message-ID: 20121031200703.3B806A6078 () git ! kde ! org
[Download RAW message or body]

Git commit e3135d3abddb88b81e86b943a408cfe3de8d5092 by Christoph Cullmann.
Committed on 31/10/2012 at 21:07.
Pushed by cullmann into branch 'master'.

quick coding, we steal a bit ideas from zen coding aka emmet for other languages, but \
using the Underscore.js template language and short json style commands ;)

M  +4    -0    part/script/data/CMakeLists.txt
A  +147  -0    part/script/data/commands/quickcoding.js
A  +16   -0    part/script/data/files/quickcoding/cpp/c.template

http://commits.kde.org/kate/e3135d3abddb88b81e86b943a408cfe3de8d5092

diff --git a/part/script/data/CMakeLists.txt b/part/script/data/CMakeLists.txt
index eef769c..c192db6 100644
--- a/part/script/data/CMakeLists.txt
+++ b/part/script/data/CMakeLists.txt
@@ -9,3 +9,7 @@ install (DIRECTORY commands/ DESTINATION \
${DATA_INSTALL_DIR}/katepart/script/com  # install default indentation files
 install (DIRECTORY indentation/ DESTINATION \
${DATA_INSTALL_DIR}/katepart/script/indentation  FILES_MATCHING PATTERN "*.js")
+
+# install default files files
+# this is a place where scripts can store additional data
+install (DIRECTORY files/ DESTINATION ${DATA_INSTALL_DIR}/katepart/script/files)
diff --git a/part/script/data/commands/quickcoding.js \
b/part/script/data/commands/quickcoding.js new file mode 100644
index 0000000..0f28490
--- /dev/null
+++ b/part/script/data/commands/quickcoding.js
@@ -0,0 +1,147 @@
+/* kate-script
+ * author: Christoph Cullmann
+ * license: BSD
+ * revision: 1
+ * kate-version: 3.4
+ * functions: quickCodingExpand
+ *
+ * Like the "Zen Coding" for HTML, this aims to be a variant for C++ and other \
languages. + */
+
+// required katepart js libraries
+require ("range.js");
+require ("underscore.js");
+
+function action (cmd)
+{
+  var a = new Object();
+  a.icon = "";
+  a.category = i18n("Quick Coding");
+  a.interactive = false;
+  if ( cmd == 'quickCodingExpand' ) {
+    a.text = i18n('Expand Abbreviation');
+    a.shortcut = 'Ctrl+Alt+#';
+  }
+
+  return a;
+}
+
+function help (cmd)
+{
+  if (cmd == 'quickCodingExpand') {
+    return i18n('Expand Quick Coding Abbreviation');
+  }
+}
+
+/**
+ * cpp expansion command
+ */
+function quickCodingExpand ()
+{
+    /**
+     * some settings
+     * should be read from document/view...
+     */
+    var indentChars = "    ";
+    
+    /**
+     * get current cursor position as start
+     * get current line of text until cursor position
+     */
+    var start = view.cursorPosition();
+    var textInFronOfCursor = document.text (new Cursor(start.line, 0), start);
+    debug ("we look at: " + textInFronOfCursor);
+    
+    /**
+     * try to read out the abbreviation
+     */
+    var matches = textInFronOfCursor.match (/\w+#.*/);
+    if (!matches) {
+        debug ("found no abbreviation");
+        return;
+    }
+    
+    /**
+     * construct full abbreviation string and get its position
+     */
+    var abbreviation = matches[0];
+    var abbreviationIndex = textInFronOfCursor.indexOf (abbreviation);
+    var abbreviationStart = new Cursor (start.line, abbreviationIndex);
+    debug ("found at " + abbreviationIndex + " abbreviation: " + \
textInFronOfCursor); +    
+    /**
+     * now we have an abbreviation, something like "class#n:Test#p:Lala"
+     * split this now up into the parts, # separated
+     */
+    var abbreviationParts = abbreviation.split ("#");
+    if (abbreviationParts.length < 1) {
+        debug ("abbreviation not splitable in at least one ");
+        return;
+    }
+    
+    /**
+     * construct abbreviation object
+     * e.g. for c#n:Test#p:Parent => { "n" => "Test", "p" => "Parent" }
+     */
+    var abbreviationObject = {};
+    for (i = 1; i < abbreviationParts.length; ++i) {
+        var matches = abbreviationParts[i].match (/^(\w+):(.*)$/);
+        if (matches && matches[1] && matches[2])
+            abbreviationObject[matches[1]] = matches[2];
+    }
+    
+    /**
+     * get abbreviation template
+     * else nothing to do
+     */
+    var template = read("quickcoding/cpp/" + abbreviationParts[0] + ".template");
+    if (!template)
+        return;
+    
+    /**
+     * instanciate the template
+     */
+    var instanciatedTemplate = _.template (template) (abbreviationObject);
+    if (!instanciatedTemplate)
+        return;
+    
+    /**
+     * start editing transaction
+     */
+    document.editBegin ();
+    
+    /**
+     * remove abbreviation
+     */
+    document.removeText (abbreviationStart, start);
+    
+    /**
+     * insert instanciated template with right indentation
+     */
+    var text = instanciatedTemplate.split ("\n");
+    for (i = 0; i < text.length; ++i) {
+        /**
+         * now: for first line, we do nothing, for otheres we add the prefix of the \
first line +         */
+        if (i > 0)
+            document.insertText (new Cursor (start.line + i, 0), document.text (new \
Cursor (start.line, 0), start)); +                
+        /**
+         * create current textline, remove the 4 spaces place holder for one indent \
level +         */
+        var textLine = text[i];
+        if (i + 1 < text.length)
+            textLine += "\n";
+        textLine = textLine.replace ("    ", indentChars);
+        
+        /**
+         * insert the line itself
+         */
+        document.insertText (new Cursor (start.line + i, start.column), textLine);
+    }
+    
+    /**
+     * end editing transaction
+     */
+    document.editEnd ();
+}
diff --git a/part/script/data/files/quickcoding/cpp/c.template \
b/part/script/data/files/quickcoding/cpp/c.template new file mode 100644
index 0000000..45a71a0
--- /dev/null
+++ b/part/script/data/files/quickcoding/cpp/c.template
@@ -0,0 +1,16 @@
+/**
+ * Class <%= n %>
+ */
+class <%= n %><%= typeof(p)!== 'undefined' ?  " : public " + p : '' %>
+{
+    public:
+        /**
+         * Constructor of <%= n %>
+         */
+        <%= n %> ();
+        
+        /**
+         * Destructor of <%= n %>
+         */
+        virtual ~<%= n %> ();
+}
\ No newline at end of file


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

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