Skip to content

Commit

Permalink
Change surround to tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukaii committed Apr 26, 2020
1 parent 2a28a9d commit 8ece8a1
Showing 1 changed file with 81 additions and 12 deletions.
93 changes: 81 additions & 12 deletions keymap/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2852,12 +2852,20 @@
closeC = character
}
}
return [openC, closeC]

if (!openC || !closeC) {
return null
} else {
return [openC, closeC]
}
}

function replaceSurround (cm, searchCharacter, replaceCharacter) {
function replaceSurround (cm, searchCharacter, replacePair, addSpace = false) {
var searchPair = transformCharacterPair(searchCharacter)
var replacePair = transformCharacterPair(replaceCharacter)

if (!replacePair) {
return
}

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

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

var addSpace = openCs.includes(replaceCharacter)

var openPos = { ch: openIndex, line: cursor.line }
var closePos = { ch: cursor.ch + closeIndex + 1, line: cursor.line }

Expand All @@ -2896,17 +2902,80 @@
var tmp = selectCompanionObject(cm, cursor, searchCharacter, true)
const replacePair = transformCharacterPair(replaceCharacter)

if (!replacePair) {
return
}

replaceCharacterAt(cm, replacePair[0], tmp.start)
replaceCharacterAt(cm, replacePair[1], { ch: tmp.end.ch - 1, line: tmp.end.line })
}

if (mirroredPairs[actionArgs.search]) {
replaceSurround(cm, actionArgs.search, character)
} else if (multilinePairs[actionArgs.search]) {
replaceMultilineSurround(cm, actionArgs.search, character)
}
if (character === '<') {
// editing tags object
showPrompt(cm, {
onCloseDialog: function (inputRef) {
var input = inputRef.getElementsByTagName("input")[0].value
// Give the prompt some time to close so that if processCommand shows
// an error, the elements don't overlap.
vimGlobalState.exCommandHistoryController.pushInput(input + '>');
vimGlobalState.exCommandHistoryController.reset();

input = '<' + input + '>';
var openTagRegex = /<([A-Za-z][A-Za-z0-9]*)\b[^>]*>/
var match = input.match(openTagRegex)
if (!match) {
return
}

cm.setCursor(cursor)
replaceSurround(cm, actionArgs.search, [
input,
'</' + match[1] + '>'
])
},
prefix: '<',
onKeyDown: function (e, input, close) {
var keyName = CodeMirror.keyName(e), up, offset;
if (keyName == 'Esc' || keyName == 'Ctrl-C' || keyName == 'Ctrl-[' ||
(keyName == 'Backspace' && input == '')) {
vimGlobalState.exCommandHistoryController.pushInput(input);
vimGlobalState.exCommandHistoryController.reset();
CodeMirror.e_stop(e);
clearInputState(cm);
close();
cm.focus();
}
if (e.key === '>') {
close(input)
CodeMirror.e_stop(e);
close()
} else if (keyName == 'Up' || keyName == 'Down') {
CodeMirror.e_stop(e);
up = keyName == 'Up' ? true : false;
offset = e.target ? e.target.selectionEnd : 0;
input = vimGlobalState.exCommandHistoryController.nextMatch(input, up) || '';
close(input);
if (offset && e.target) e.target.selectionEnd = e.target.selectionStart = Math.min(offset, e.target.value.length);
} else if (keyName == 'Ctrl-U') {
// Ctrl-U clears input.
CodeMirror.e_stop(e);
close('');
} else {
if ( keyName != 'Left' && keyName != 'Right' && keyName != 'Ctrl' && keyName != 'Alt' && keyName != 'Shift')
vimGlobalState.exCommandHistoryController.reset();
}
}
});
} else {
if (mirroredPairs[actionArgs.search]) {
var replacePair = transformCharacterPair(character)
var addSpace = openCs.includes(character)
replaceSurround(cm, actionArgs.search, replacePair, addSpace)
} else if (multilinePairs[actionArgs.search]) {
replaceMultilineSurround(cm, actionArgs.search, character)
}

cm.setCursor(cursor)
}
}
};

Expand Down Expand Up @@ -4070,7 +4139,7 @@
if (cm.openDialog) {
cm.openDialog(template, onClose, { bottom: true, value: options.value,
onKeyDown: options.onKeyDown, onKeyUp: options.onKeyUp,
selectValueOnOpen: false});
selectValueOnOpen: false, onClose: options.onCloseDialog});
}
else {
onClose(prompt(shortText, ''));
Expand Down

0 comments on commit 8ece8a1

Please sign in to comment.