/
mergeLines.js
41 lines (35 loc) · 1.11 KB
/
mergeLines.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Merges selected lines or lines between XHTML tag pairs
* @param {Function} require
* @param {Underscore} _
*/
emmet.exec(function(require, _) {
require('actions').add('merge_lines', function(editor) {
var matcher = require('htmlMatcher');
var utils = require('utils');
var editorUtils = require('editorUtils');
var info = editorUtils.outputInfo(editor);
/** @type Range */
var selection = require('range').create(editor.getSelectionRange());
if (!selection.length()) {
// find matching tag
var pair = matcher.find(info.content, editor.getCaretPos());
if (pair) {
selection = pair.outerRange;
}
}
if (selection.length()) {
// got range, merge lines
var text = selection.substring(info.content);
var lines = utils.splitByLines(text);
for (var i = 1; i < lines.length; i++) {
lines[i] = lines[i].replace(/^\s+/, '');
}
text = lines.join('').replace(/\s{2,}/, ' ');
editor.replaceContent(text, selection.start, selection.end);
editor.createSelection(selection.start, selection.start + text.length);
return true;
}
return false;
});
});