From bd0c576bff9a9d289aebfef86b4d59bcfa3c8096 Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 08:10:37 -0800 Subject: [PATCH 1/7] Use length caching in JS `for` snippet The previous version of this snippet referenced the object's length on each iteration of the loop. This adds unnecessary overhead, causing the loop to be slower. While this will not make much of a difference in many cases, if the for loop was to be used on a large object or in a tight loop, it would degrade performance. Perhaps more importantly, if you were to not cache the length of a live query, such as DOM queries, you would see significant performance degradation. If nothing else, this adjustment to the snippet sets things up for success in more scenarios. --- UltiSnips/javascript.snippets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index bc530e9..d458750 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -39,8 +39,8 @@ for (var ${2:i} = ${1:Things}.length - 1; $2 >= 0; $2--) { endsnippet snippet for "for (...) {...}" -for (var ${2:i}=0; $2 < ${1:Things}.length; $2++) { - ${3:$1[$2]}${VISUAL}$0 +for (var ${1:i} = 0, ${2:len} = ${3:Things}.length; $1 < $2; $1++) { + ${4:${3}[$1]}${VISUAL}$0 } endsnippet From 98f88de9768a6f6a32180827ec9938ff3eaff983 Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 08:22:54 -0800 Subject: [PATCH 2/7] Increase scope of JS `for` snippet placeholder The `Things.length` part of the JS `for` snippet previously did not include the `.length` portion. Using some regex replacement, we can include this bit in the placeholder while still only using the `Things` bit inside the `for` loop. This should make the snippet slightly more pleasant to use. --- UltiSnips/javascript.snippets | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index d458750..b866903 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -33,14 +33,14 @@ ${1:class_name}.prototype.${2:method_name} = function(${3:first_argument}) { endsnippet snippet for "for (...) {...} (faster)" -for (var ${2:i} = ${1:Things}.length - 1; $2 >= 0; $2--) { - ${3:$1[$2]}${VISUAL}$0 +for (var ${2:i} = ${1:Things.length} - 1; $2 >= 0; $2--) { + ${3:${1/([^.]+).*/$1/}[$2]}${VISUAL}$0 } endsnippet snippet for "for (...) {...}" -for (var ${1:i} = 0, ${2:len} = ${3:Things}.length; $1 < $2; $1++) { - ${4:${3}[$1]}${VISUAL}$0 +for (var ${1:i} = 0, ${2:len} = ${3:Things.length}; $1 < $2; $1++) { + ${4:${3/([^.]+).*/$1/}[$1]}${VISUAL}$0 } endsnippet From 6fa064468542ee5f3a86e32bc19d2108f7d820cd Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 08:18:33 -0800 Subject: [PATCH 3/7] Improve descriptions of JS `for` snippets More specifically than "faster" and not faster, these snippets count down and count up, respectively. Adding this information to the descriptions will help people make the correct decision. --- UltiSnips/javascript.snippets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index b866903..ffc9e36 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -32,13 +32,13 @@ ${1:class_name}.prototype.${2:method_name} = function(${3:first_argument}) { endsnippet -snippet for "for (...) {...} (faster)" +snippet for "for (...) {...} (counting down, faster)" for (var ${2:i} = ${1:Things.length} - 1; $2 >= 0; $2--) { ${3:${1/([^.]+).*/$1/}[$2]}${VISUAL}$0 } endsnippet -snippet for "for (...) {...}" +snippet for "for (...) {...} (counting up)" for (var ${1:i} = 0, ${2:len} = ${3:Things.length}; $1 < $2; $1++) { ${4:${3/([^.]+).*/$1/}[$1]}${VISUAL}$0 } From 5c36a8945cd6c83c814b8ea45ab834f3a25391df Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 22:14:00 -0800 Subject: [PATCH 4/7] Change JS `for` counting down trigger from for to ford As suggested by @SirVer[1], this commit changes the trigger of the JavaScript `for` snippet that counts down from "for" to "ford". Since "for" is used pretty often, this will prevent people from needing to select which one they want from the menu each time. [1]: https://github.com/SirVer/ultisnips/pull/123/files#r8686514 --- UltiSnips/javascript.snippets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index ffc9e36..1dff6b1 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -32,7 +32,7 @@ ${1:class_name}.prototype.${2:method_name} = function(${3:first_argument}) { endsnippet -snippet for "for (...) {...} (counting down, faster)" +snippet ford "for (...) {...} (counting down, faster)" for (var ${2:i} = ${1:Things.length} - 1; $2 >= 0; $2--) { ${3:${1/([^.]+).*/$1/}[$2]}${VISUAL}$0 } From 983d494752839e7cbd9c75a42da4bfd392047037 Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 22:16:05 -0800 Subject: [PATCH 5/7] Add "b" options to JS `for` and `ford` snippets `for` loops usually appear at the beginning of the line. This commit makes these snippets more context-aware by adding the "b" option that prevents them from being triggered if they are preceded with something other than whitespace, as suggested by @SirVer[1]. [1]: https://github.com/SirVer/ultisnips/pull/123/files#r8686514 --- UltiSnips/javascript.snippets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index 1dff6b1..8389156 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -32,13 +32,13 @@ ${1:class_name}.prototype.${2:method_name} = function(${3:first_argument}) { endsnippet -snippet ford "for (...) {...} (counting down, faster)" +snippet ford "for (...) {...} (counting down, faster)" b for (var ${2:i} = ${1:Things.length} - 1; $2 >= 0; $2--) { ${3:${1/([^.]+).*/$1/}[$2]}${VISUAL}$0 } endsnippet -snippet for "for (...) {...} (counting up)" +snippet for "for (...) {...} (counting up)" b for (var ${1:i} = 0, ${2:len} = ${3:Things.length}; $1 < $2; $1++) { ${4:${3/([^.]+).*/$1/}[$1]}${VISUAL}$0 } From 23d7ded7546d0621d05c0ef6c727b43352471368 Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 22:19:25 -0800 Subject: [PATCH 6/7] Simplify JS `for` snippet bodies As suggested by @SirVer[1], having `Things[i]` be pre-populated in the body of the `for` loop snippets is not very useful given the merits of autocompletion. This commit simplifies these bodies by simply using `${VISUAL}$0`. [1]: https://github.com/SirVer/ultisnips/pull/123/files#r8686533 --- UltiSnips/javascript.snippets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index 8389156..b36ef71 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -34,13 +34,13 @@ endsnippet snippet ford "for (...) {...} (counting down, faster)" b for (var ${2:i} = ${1:Things.length} - 1; $2 >= 0; $2--) { - ${3:${1/([^.]+).*/$1/}[$2]}${VISUAL}$0 + ${VISUAL}$0 } endsnippet snippet for "for (...) {...} (counting up)" b for (var ${1:i} = 0, ${2:len} = ${3:Things.length}; $1 < $2; $1++) { - ${4:${3/([^.]+).*/$1/}[$1]}${VISUAL}$0 + ${VISUAL}$0 } endsnippet From 2f66a3fc2bc076f46d23a0cd87b5a1bf139b36bb Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 22:21:50 -0800 Subject: [PATCH 7/7] Re-order JS `for` snippets Now that we have separated the two `for` loop snippet triggers by changing the one that counts down from "for" to "ford", it feels more and more like "ford" is a variant of "for" so I am moving it after it in the file. Also, alphabetical. --- UltiSnips/javascript.snippets | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index b36ef71..95f96c8 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -32,14 +32,14 @@ ${1:class_name}.prototype.${2:method_name} = function(${3:first_argument}) { endsnippet -snippet ford "for (...) {...} (counting down, faster)" b -for (var ${2:i} = ${1:Things.length} - 1; $2 >= 0; $2--) { +snippet for "for (...) {...} (counting up)" b +for (var ${1:i} = 0, ${2:len} = ${3:Things.length}; $1 < $2; $1++) { ${VISUAL}$0 } endsnippet -snippet for "for (...) {...} (counting up)" b -for (var ${1:i} = 0, ${2:len} = ${3:Things.length}; $1 < $2; $1++) { +snippet ford "for (...) {...} (counting down, faster)" b +for (var ${2:i} = ${1:Things.length} - 1; $2 >= 0; $2--) { ${VISUAL}$0 } endsnippet