This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathinteractive.js
215 lines (188 loc) · 5.54 KB
/
interactive.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Definition of interactive functions: the function
* that require additional dialog prompt and update
* editor content when user types data in prompt
*/
var utils = require('emmet/lib/utils/common');
var editorUtils = require('emmet/lib/utils/editor');
var actionUtils = require('emmet/lib/utils/action');
var range = require('emmet/lib/assets/range');
var htmlMatcher = require('emmet/lib/assets/htmlMatcher');
var parser = require('emmet/lib/parser/abbreviation');
var updateTag = require('emmet/lib/action/updateTag');
var atom = require('atom');
var PromptView = require('./prompt');
var Point = atom.Point;
var Range = atom.Range;
var prompt = new PromptView();
/**
* Caches wrapping context for current selection in editor
* @param {IEmmetEditor} editor
* @param {Object} info Current editor info (content, syntax, etc.)
* @return {Object}
*/
function selectionContext(editor, info) {
info = info || editorUtils.outputInfo(editor);
return editor.selectionList().map(function(sel, i) {
var r = range(sel);
var tag = htmlMatcher.tag(info.content, r.start);
if (!r.length() && tag) {
// no selection, use tag pair
r = utils.narrowToNonSpace(info.content, tag.range);
}
var out = {
selection: r,
tag: tag,
caret: r.start,
syntax: info.syntax,
profile: info.profile || null,
counter: i + 1,
contextNode: actionUtils.captureContext(editor, r.start)
};
if (r.length()) {
var pasted = utils.escapeText(r.substring(info.content));
out.pastedContent = editorUtils.unindent(editor, pasted);
}
return out;
});
}
function updateFinalCarets(selCtx, fromIndex, delta) {
if (!delta) {
return;
}
var offset = new Point(delta, 0);
for (var i = fromIndex + 1, il = selCtx.length; i < il; i++) {
selCtx[i].finalCaret = selCtx[i].finalCaret.translate(offset);
}
}
/**
* Returns current caret position for given editor
* @param {Editor} editor Atom editor instance
* @return {Number} Character index in editor
*/
function getCaret(editor) {
// we can’t use default `getCursor()` method because it returns
// the most recent (e.g. the latest) caret, but we need the first one
return editor.getSelectedBufferRanges()[0].start;
}
function lineDelta(prev, cur) {
return utils.splitByLines(cur).length - utils.splitByLines(prev).length;
}
function setFinalCarets(selCtx, editor) {
if (selCtx && selCtx.length > 1) {
editor.setSelectedBufferRanges(selCtx.map(function(ctx) {
return new Range(ctx.finalCaret, ctx.finalCaret);
}));
}
}
module.exports = {
run: function(cmd, editor) {
if (cmd === 'wrap_with_abbreviation') {
return this.wrapWithAbbreviation(editor);
}
if (cmd === 'update_tag') {
return this.updateTag(editor);
}
if (cmd === 'interactive_expand_abbreviation') {
return this.expandAbbreviation(editor);
}
},
expandAbbreviation: function(editor) {
var info = editorUtils.outputInfo(editor);
var selCtx = editor.selectionList().map(function(sel, i) {
editor._selection.index = i;
var r = range(sel);
return {
selection: r,
selectedText: r.substring(info.content),
caret: r.start,
syntax: info.syntax,
profile: info.profile || null,
counter: i + 1,
contextNode: actionUtils.captureContext(editor, r.start)
};
});
return this.wrapWithAbbreviation(editor, selCtx);
},
wrapWithAbbreviation: function(editor, selCtx) {
selCtx = selCtx || selectionContext(editor);
// show prompt dialog that will wrap each selection
// on user typing
prompt.show({
label: 'Enter Abbreviation',
editor: editor.editor,
editorView: editor.editorView,
update: function(abbr) {
var result, replaced;
for (var i = selCtx.length - 1, ctx; i >= 0; i--) {
ctx = selCtx[i];
result = '';
try {
if (abbr) {
result = parser.expand(abbr, ctx);
} else {
result = ctx.pastedContent;
}
} catch (e) {
console.error(e);
result = ctx.pastedContent;
}
editor._selection.index = i;
replaced = editor.replaceContent(result, ctx.selection.start, ctx.selection.end);
ctx.finalCaret = getCaret(editor.editor);
updateFinalCarets(selCtx, i, lineDelta(ctx.selectedText, replaced));
}
},
confirm: function() {
setFinalCarets(selCtx, editor.editor);
}
});
},
updateTag: function(editor) {
var info = editorUtils.outputInfo(editor);
var selCtx = selectionContext(editor, info);
// show prompt dialog that will update each
// tag from selection
prompt.show({
label: 'Enter Abbreviation',
editor: editor.editor,
editorView: editor.editorView,
update: function(abbr) {
var tag, replaced, delta;
for (var i = selCtx.length - 1, ctx; i >= 0; i--) {
ctx = selCtx[i];
tag = null;
try {
tag = updateTag.getUpdatedTag(abbr, {match: ctx.tag}, info.content, {
counter: ctx.counter
});
} catch (e) {
console.error(e);
}
if (!tag) {
continue;
}
replaced = [{
start: ctx.tag.open.range.start,
end: ctx.tag.open.range.end,
content: tag.source
}];
if (tag.name() != ctx.tag.name && ctx.tag.close) {
replaced.unshift({
start: ctx.tag.close.range.start,
end: ctx.tag.close.range.end,
content: '</' + tag.name() + '>'
});
}
replaced.forEach(function(data) {
editor.replaceContent(data.content, data.start, data.end);
ctx.finalCaret = editor.editor.getBuffer().positionForCharacterIndex(data.start);
});
}
},
confirm: function() {
setFinalCarets(selCtx, editor.editor);
}
});
}
};