From e085ac761ae5fb22961d3c599a2c0e057e1ccaa2 Mon Sep 17 00:00:00 2001 From: as3boyan Date: Mon, 24 Mar 2014 18:49:37 +0200 Subject: [PATCH 1/2] CodeMirror externsion: addon/hint/Code snippets Stop jumping between variables on ${cursor} --- .../addon/hint/templates-hint.js | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/codemirror-extension/addon/hint/templates-hint.js b/codemirror-extension/addon/hint/templates-hint.js index 25a8bd4..345070c 100644 --- a/codemirror-extension/addon/hint/templates-hint.js +++ b/codemirror-extension/addon/hint/templates-hint.js @@ -17,7 +17,8 @@ var ourMap = { Tab : selectNextVariable, - Esc : uninstall + Esc : uninstall, + Enter : uninstall, } function TemplateState() { @@ -75,8 +76,10 @@ } var marker = state.selectableMarkers[state.varIndex]; var pos = marker.find(); + var templateVar = marker._templateVar; + cm.setSelection(pos.from, pos.to); - var templateVar = marker._templateVar; + for ( var i = 0; i < state.marked.length; i++) { var m = state.marked[i]; if (m == marker) { @@ -96,6 +99,12 @@ } } cm.refresh(); + + if (templateVar == "cursor") + { + cm.setSelection(pos.from); + uninstall(cm); + } } } @@ -193,6 +202,22 @@ }); variables[token.variable] = false; } + else { + content += " "; + var from = Pos(data.from.line + line, data.from.ch + token.x); + var to = Pos(data.from.line + line, data.from.ch + token.x + + 1); + var selectable = variables[token.variable] != false; + markers.push({ + from : from, + to : to, + variable : token.variable, + selectable : true + }); + + variables[token.variable] = false; + + } } else { content += token; if (token == "\n") { From 8a25f24226e6e72f0c648184c6720752de7b75c2 Mon Sep 17 00:00:00 2001 From: as3boyan Date: Sun, 4 May 2014 11:56:00 +0300 Subject: [PATCH 2/2] Snippets indentation correction --- .../addon/hint/templates-hint.js | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/codemirror-extension/addon/hint/templates-hint.js b/codemirror-extension/addon/hint/templates-hint.js index 345070c..78d3efc 100644 --- a/codemirror-extension/addon/hint/templates-hint.js +++ b/codemirror-extension/addon/hint/templates-hint.js @@ -102,6 +102,7 @@ if (templateVar == "cursor") { + cm.replaceRange("", pos.from, {line: pos.from.line, ch: pos.from.ch + 2}) cm.setSelection(pos.from); uninstall(cm); } @@ -190,8 +191,8 @@ if (token.variable) { if (!isSpecialVar(token.variable)) { content += token.variable; - var from = Pos(data.from.line + line, data.from.ch + token.x); - var to = Pos(data.from.line + line, data.from.ch + token.x + var from = Pos(data.from.line + line, token.x); + var to = Pos(data.from.line + line, token.x + token.variable.length); var selectable = variables[token.variable] != false; markers.push({ @@ -203,10 +204,10 @@ variables[token.variable] = false; } else { - content += " "; - var from = Pos(data.from.line + line, data.from.ch + token.x); - var to = Pos(data.from.line + line, data.from.ch + token.x - + 1); + content += "//"; + var from = Pos(data.from.line + line, token.x); + var to = Pos(data.from.line + line, token.x + + 2); var selectable = variables[token.variable] != false; markers.push({ from : from, @@ -229,7 +230,27 @@ var from = data.from; var to = data.to; cm.replaceRange(content, from, to); - + + var lines = content.split("\n"); + + for (x = 0; x < lines.length; x++) + { + var targetLine = from.line + x; + + cm.indentLine(targetLine); + var line = cm.getLine(targetLine); + var deltaIndent = line.length - lines[x].length; + + for (y = 0; y < markers.length; y++) + { + if (markers[y].from.line == targetLine) + { + markers[y].from.ch += deltaIndent; + markers[y].to.ch += deltaIndent; + } + } + } + for ( var i = 0; i < markers.length; i++) { var marker = markers[i], from = marker.from, to = marker.to; var markText = cm.markText(from, to, {