forked from emmetio/brackets-emmet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
152 lines (130 loc) · 4.75 KB
/
editor.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
define(['./emmet'], function(emmet) {
var require = emmet.require;
var _ = require('_');
var modeMap = {
"text/html": "html",
"application/xml": "xml",
"text/xsl": "xsl",
"text/css": "css",
"text/x-less": "less"
};
return {
context: null,
filePath: null,
setupContext: function (context, filePath) {
this.context = context;
this.filePath = filePath;
var indentation = "\t";
if (!context.getOption("indentWithTabs")) {
indentation = require("utils").repeatString(" ", context.getOption("indentUnit"));
}
require("resources").setVariable("indentation", indentation);
},
getSelectionRange: function () {
var caretPos = this.getCaretPos();
return {
start: caretPos,
end: caretPos + this.getSelection().length
};
},
createSelection: function (start, end) {
if (start == end) {
this.context.setCursor(this.context.posFromIndex(start));
} else {
this.context.setSelection(this.context.posFromIndex(start), this.context.posFromIndex(end));
}
},
getCurrentLineRange: function () {
var caret = this.context.getCursor(true);
return {
start: this.context.indexFromPos({line: caret.line, ch: 0}),
end: this.context.indexFromPos({line: caret.line, ch: this.context.getLine(caret.line).length})
};
},
getCaretPos: function () {
return this.context.indexFromPos(this.context.getCursor(true));
},
setCaretPos: function (pos) {
this.createSelection(pos, pos);
},
getCurrentLine: function () {
return this.context.getLine(this.context.getCursor(true).line) || "";
},
replaceContent: function (value, start, end, noIndent) {
if (_.isUndefined(end))
end = _.isUndefined(start) ? value.length : start;
if (_.isUndefined(start)) start = 0;
var utils = require("utils");
// indent new value
if (!noIndent) {
value = utils.padString(value, utils.getLinePaddingFromPosition(this.getContent(), start));
}
// find new caret position
var tabstopData = require("tabStops").extract(value, {
escape: function (ch) {
return ch;
}
});
value = tabstopData.text;
var firstTabStop = tabstopData.tabstops[0];
if (firstTabStop) {
firstTabStop.start += start;
firstTabStop.end += start;
} else {
firstTabStop = {
start: value.length + start,
end: value.length + start
};
}
// do a compound change to record all changes into single undo event
var that = this;
this.context.compoundChange(function () {
that.context.replaceRange(value, that.context.posFromIndex(start), that.context.posFromIndex(end));
that.createSelection(firstTabStop.start, firstTabStop.end);
});
},
getContent: function () {
return this.context.getValue();
},
getSyntax: function () {
var syntax = this.context.getOption("mode");
if (syntax in modeMap)
syntax = modeMap[syntax];
return require('actionUtils').detectSyntax(this, syntax);
},
/**
* Returns current output profile name (@see emmet#setupProfile)
* @return {String}
*/
getProfileName: function () {
if (this.context.getOption("profile"))
return this.context.getOption("profile");
return require('actionUtils').detectProfile(this);
},
/**
* Ask user to enter something
* @param {String} title Dialog title
* @return {String} Entered data
* @since 0.65
*/
prompt: function (title) {
return prompt(title);
},
/**
* Returns current selection
* @return {String}
* @since 0.65
*/
getSelection: function () {
return this.context.getSelection() || "";
},
/**
* Returns current editor"s file path
* @return {String}
* @since 0.65
*/
getFilePath: function () {
return this.filePath;
}
};
});