Skip to content

Commit 1272574

Browse files
committed
Change surround to tag
1 parent 2a28a9d commit 1272574

File tree

1 file changed

+80
-12
lines changed

1 file changed

+80
-12
lines changed

keymap/vim.js

+80-12
Original file line numberDiff line numberDiff line change
@@ -2852,12 +2852,20 @@
28522852
closeC = character
28532853
}
28542854
}
2855-
return [openC, closeC]
2855+
2856+
if (!openC || !closeC) {
2857+
return null
2858+
} else {
2859+
return [openC, closeC]
2860+
}
28562861
}
28572862

2858-
function replaceSurround (cm, searchCharacter, replaceCharacter) {
2863+
function replaceSurround (cm, searchCharacter, replacePair, addSpace = false) {
28592864
var searchPair = transformCharacterPair(searchCharacter)
2860-
var replacePair = transformCharacterPair(replaceCharacter)
2865+
2866+
if (!replacePair) {
2867+
return
2868+
}
28612869

28622870
var openIndex, closeIndex, lineContent = cm.getLine(cursor.line)
28632871
openIndex = lineContent.slice(0, cursor.ch).lastIndexOf(searchPair[0])
@@ -2869,8 +2877,6 @@
28692877

28702878
var inner = lineContent.slice(openIndex + 1, closeIndex + cursor.ch)
28712879

2872-
var addSpace = openCs.includes(replaceCharacter)
2873-
28742880
var openPos = { ch: openIndex, line: cursor.line }
28752881
var closePos = { ch: cursor.ch + closeIndex + 1, line: cursor.line }
28762882

@@ -2896,17 +2902,79 @@
28962902
var tmp = selectCompanionObject(cm, cursor, searchCharacter, true)
28972903
const replacePair = transformCharacterPair(replaceCharacter)
28982904

2905+
if (!replacePair) {
2906+
return
2907+
}
2908+
28992909
replaceCharacterAt(cm, replacePair[0], tmp.start)
29002910
replaceCharacterAt(cm, replacePair[1], { ch: tmp.end.ch - 1, line: tmp.end.line })
29012911
}
29022912

2903-
if (mirroredPairs[actionArgs.search]) {
2904-
replaceSurround(cm, actionArgs.search, character)
2905-
} else if (multilinePairs[actionArgs.search]) {
2906-
replaceMultilineSurround(cm, actionArgs.search, character)
2907-
}
2913+
if (character === '<') {
2914+
// editing tags object
2915+
showPrompt(cm, {
2916+
onCloseDialog: function (inputRef) {
2917+
var input = inputRef.getElementsByTagName("input")[0].value
2918+
// Give the prompt some time to close so that if processCommand shows
2919+
// an error, the elements don't overlap.
2920+
vimGlobalState.exCommandHistoryController.pushInput(input + '>');
2921+
vimGlobalState.exCommandHistoryController.reset();
2922+
2923+
input = '<' + input + '>';
2924+
var openTagRegex = /<([A-Za-z][A-Za-z0-9]*)\b[^>]*>/
2925+
var match = input.match(openTagRegex)
2926+
if (!match) {
2927+
return
2928+
}
29082929

2909-
cm.setCursor(cursor)
2930+
replaceSurround(cm, actionArgs.search, [
2931+
input,
2932+
'</' + match[1] + '>'
2933+
])
2934+
},
2935+
prefix: '<',
2936+
onKeyDown: function (e, input, close) {
2937+
var keyName = CodeMirror.keyName(e), up, offset;
2938+
if (keyName == 'Esc' || keyName == 'Ctrl-C' || keyName == 'Ctrl-[' ||
2939+
(keyName == 'Backspace' && input == '')) {
2940+
vimGlobalState.exCommandHistoryController.pushInput(input);
2941+
vimGlobalState.exCommandHistoryController.reset();
2942+
CodeMirror.e_stop(e);
2943+
clearInputState(cm);
2944+
close();
2945+
cm.focus();
2946+
}
2947+
if (e.key === '>') {
2948+
close(input)
2949+
close()
2950+
} else if (keyName == 'Up' || keyName == 'Down') {
2951+
CodeMirror.e_stop(e);
2952+
up = keyName == 'Up' ? true : false;
2953+
offset = e.target ? e.target.selectionEnd : 0;
2954+
input = vimGlobalState.exCommandHistoryController.nextMatch(input, up) || '';
2955+
close(input);
2956+
if (offset && e.target) e.target.selectionEnd = e.target.selectionStart = Math.min(offset, e.target.value.length);
2957+
} else if (keyName == 'Ctrl-U') {
2958+
// Ctrl-U clears input.
2959+
CodeMirror.e_stop(e);
2960+
close('');
2961+
} else {
2962+
if ( keyName != 'Left' && keyName != 'Right' && keyName != 'Ctrl' && keyName != 'Alt' && keyName != 'Shift')
2963+
vimGlobalState.exCommandHistoryController.reset();
2964+
}
2965+
}
2966+
});
2967+
} else {
2968+
if (mirroredPairs[actionArgs.search]) {
2969+
var replacePair = transformCharacterPair(character)
2970+
var addSpace = openCs.includes(character)
2971+
replaceSurround(cm, actionArgs.search, replacePair, addSpace)
2972+
} else if (multilinePairs[actionArgs.search]) {
2973+
replaceMultilineSurround(cm, actionArgs.search, character)
2974+
}
2975+
2976+
cm.setCursor(cursor)
2977+
}
29102978
}
29112979
};
29122980

@@ -4070,7 +4138,7 @@
40704138
if (cm.openDialog) {
40714139
cm.openDialog(template, onClose, { bottom: true, value: options.value,
40724140
onKeyDown: options.onKeyDown, onKeyUp: options.onKeyUp,
4073-
selectValueOnOpen: false});
4141+
selectValueOnOpen: false, onClose: options.onCloseDialog});
40744142
}
40754143
else {
40764144
onClose(prompt(shortText, ''));

0 commit comments

Comments
 (0)