This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditPoints.js
118 lines (104 loc) · 2.89 KB
/
editPoints.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Move between next/prev edit points. 'Edit points' are places between tags
* and quotes of empty attributes in html
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
/**
* Search for new caret insertion point
* @param {IEmmetEditor} editor Editor instance
* @param {Number} inc Search increment: -1 — search left, 1 — search right
* @param {Number} offset Initial offset relative to current caret position
* @return {Number} Returns -1 if insertion point wasn't found
*/
function findNewEditPoint(editor, inc, offset) {
inc = inc || 1;
offset = offset || 0;
var curPoint = editor.getCaretPos() + offset;
var content = String(editor.getContent());
var maxLen = content.length;
var nextPoint = -1;
var reEmptyLine = /^\s+$/;
function getLine(ix) {
var start = ix;
while (start >= 0) {
var c = content.charAt(start);
if (c == '\n' || c == '\r')
break;
start--;
}
return content.substring(start, ix);
}
while (curPoint <= maxLen && curPoint >= 0) {
curPoint += inc;
var curChar = content.charAt(curPoint);
var nextChar = content.charAt(curPoint + 1);
var prevChar = content.charAt(curPoint - 1);
switch (curChar) {
case '"':
case '\'':
if (nextChar == curChar && prevChar == '=') {
// empty attribute
nextPoint = curPoint + 1;
}
break;
case '>':
if (nextChar == '<') {
// between tags
nextPoint = curPoint + 1;
}
break;
case '\n':
case '\r':
// empty line
if (reEmptyLine.test(getLine(curPoint - 1))) {
nextPoint = curPoint;
}
break;
}
if (nextPoint != -1)
break;
}
return nextPoint;
}
return {
/**
* Move to previous edit point
* @param {IEmmetEditor} editor Editor instance
* @param {String} syntax Current document syntax
* @param {String} profile Output profile name
* @return {Boolean}
*/
previousEditPointAction: function(editor, syntax, profile) {
var curPos = editor.getCaretPos();
var newPoint = findNewEditPoint(editor, -1);
if (newPoint == curPos)
// we're still in the same point, try searching from the other place
newPoint = findNewEditPoint(editor, -1, -2);
if (newPoint != -1) {
editor.setCaretPos(newPoint);
return true;
}
return false;
},
/**
* Move to next edit point
* @param {IEmmetEditor} editor Editor instance
* @param {String} syntax Current document syntax
* @param {String} profile Output profile name
* @return {Boolean}
*/
nextEditPointAction: function(editor, syntax, profile) {
var newPoint = findNewEditPoint(editor, 1);
if (newPoint != -1) {
editor.setCaretPos(newPoint);
return true;
}
return false;
}
};
});