Skip to content

Commit 8ece8a1

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

File tree

1 file changed

+81
-12
lines changed

1 file changed

+81
-12
lines changed

keymap/vim.js

+81-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,80 @@
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+
CodeMirror.e_stop(e);
2950+
close()
2951+
} else if (keyName == 'Up' || keyName == 'Down') {
2952+
CodeMirror.e_stop(e);
2953+
up = keyName == 'Up' ? true : false;
2954+
offset = e.target ? e.target.selectionEnd : 0;
2955+
input = vimGlobalState.exCommandHistoryController.nextMatch(input, up) || '';
2956+
close(input);
2957+
if (offset && e.target) e.target.selectionEnd = e.target.selectionStart = Math.min(offset, e.target.value.length);
2958+
} else if (keyName == 'Ctrl-U') {
2959+
// Ctrl-U clears input.
2960+
CodeMirror.e_stop(e);
2961+
close('');
2962+
} else {
2963+
if ( keyName != 'Left' && keyName != 'Right' && keyName != 'Ctrl' && keyName != 'Alt' && keyName != 'Shift')
2964+
vimGlobalState.exCommandHistoryController.reset();
2965+
}
2966+
}
2967+
});
2968+
} else {
2969+
if (mirroredPairs[actionArgs.search]) {
2970+
var replacePair = transformCharacterPair(character)
2971+
var addSpace = openCs.includes(character)
2972+
replaceSurround(cm, actionArgs.search, replacePair, addSpace)
2973+
} else if (multilinePairs[actionArgs.search]) {
2974+
replaceMultilineSurround(cm, actionArgs.search, character)
2975+
}
2976+
2977+
cm.setCursor(cursor)
2978+
}
29102979
}
29112980
};
29122981

@@ -4070,7 +4139,7 @@
40704139
if (cm.openDialog) {
40714140
cm.openDialog(template, onClose, { bottom: true, value: options.value,
40724141
onKeyDown: options.onKeyDown, onKeyUp: options.onKeyUp,
4073-
selectValueOnOpen: false});
4142+
selectValueOnOpen: false, onClose: options.onCloseDialog});
40744143
}
40754144
else {
40764145
onClose(prompt(shortText, ''));

0 commit comments

Comments
 (0)