forked from ajaxorg/ace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
occur.js
146 lines (133 loc) · 5.43 KB
/
occur.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
"use strict";
var oop = require("./lib/oop");
var Search = require("./search").Search;
var EditSession = require("./edit_session").EditSession;
var SearchHighlight = require("./search_highlight").SearchHighlight;
/**
* @class Occur
*
* Finds all lines matching a search term in the current [[Document
* `Document`]] and displays them instead of the original `Document`. Keeps
* track of the mapping between the occur doc and the original doc.
*
**/
class Occur extends Search {
/**
* Enables occur mode. expects that `options.needle` is a search term.
* This search term is used to filter out all the lines that include it
* and these are then used as the content of a new [[Document
* `Document`]]. The current cursor position of editor will be translated
* so that the cursor is on the matching row/column as it was before.
* @param {Editor} editor
* @param {Object} options options.needle should be a String
* @return {Boolean} Whether occur activation was successful
*
**/
enter(editor, options) {
if (!options.needle) return false;
var pos = editor.getCursorPosition();
this.displayOccurContent(editor, options);
var translatedPos = this.originalToOccurPosition(editor.session, pos);
editor.moveCursorToPosition(translatedPos);
return true;
}
/**
* Disables occur mode. Resets the [[Sessions `EditSession`]] [[Document
* `Document`]] back to the original doc. If options.translatePosition is
* truthy also maps the [[Editors `Editor`]] cursor position accordingly.
* @param {Editor} editor
* @param {Object} options options.translatePosition
* @return {Boolean} Whether occur deactivation was successful
*
**/
exit(editor, options) {
var pos = options.translatePosition && editor.getCursorPosition();
var translatedPos = pos && this.occurToOriginalPosition(editor.session, pos);
this.displayOriginalContent(editor);
if (translatedPos)
editor.moveCursorToPosition(translatedPos);
return true;
}
highlight(sess, regexp) {
var hl = sess.$occurHighlight = sess.$occurHighlight || sess.addDynamicMarker(
new SearchHighlight(null, "ace_occur-highlight", "text"));
hl.setRegexp(regexp);
sess._emit("changeBackMarker"); // force highlight layer redraw
}
displayOccurContent(editor, options) {
// this.setSession(session || new EditSession(""))
this.$originalSession = editor.session;
var found = this.matchingLines(editor.session, options);
var lines = found.map(function(foundLine) { return foundLine.content; });
var occurSession = new EditSession(lines.join('\n'));
occurSession.$occur = this;
occurSession.$occurMatchingLines = found;
editor.setSession(occurSession);
this.$useEmacsStyleLineStart = this.$originalSession.$useEmacsStyleLineStart;
occurSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
this.highlight(occurSession, options.re);
occurSession._emit('changeBackMarker');
}
displayOriginalContent(editor) {
editor.setSession(this.$originalSession);
this.$originalSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
}
/**
* Translates the position from the original document to the occur lines in
* the document or the beginning if the doc {row: 0, column: 0} if not
* found.
* @param {EditSession} session The occur session
* @param {Object} pos The position in the original document
* @return {Object} position in occur doc
**/
originalToOccurPosition(session, pos) {
var lines = session.$occurMatchingLines;
var nullPos = {row: 0, column: 0};
if (!lines) return nullPos;
for (var i = 0; i < lines.length; i++) {
if (lines[i].row === pos.row)
return {row: i, column: pos.column};
}
return nullPos;
}
/**
* Translates the position from the occur document to the original document
* or `pos` if not found.
* @param {EditSession} session The occur session
* @param {Object} pos The position in the occur session document
* @return {Object} position
**/
occurToOriginalPosition(session, pos) {
var lines = session.$occurMatchingLines;
if (!lines || !lines[pos.row])
return pos;
return {row: lines[pos.row].row, column: pos.column};
}
matchingLines(session, options) {
options = oop.mixin({}, options);
if (!session || !options.needle) return [];
var search = new Search();
search.set(options);
return search.findAll(session).reduce(function(lines, range) {
var row = range.start.row;
var last = lines[lines.length-1];
return last && last.row === row ?
lines :
lines.concat({row: row, content: session.getLine(row)});
}, []);
}
}
var dom = require('./lib/dom');
dom.importCssString(".ace_occur-highlight {\n\
border-radius: 4px;\n\
background-color: rgba(87, 255, 8, 0.25);\n\
position: absolute;\n\
z-index: 4;\n\
box-sizing: border-box;\n\
box-shadow: 0 0 4px rgb(91, 255, 50);\n\
}\n\
.ace_dark .ace_occur-highlight {\n\
background-color: rgb(80, 140, 85);\n\
box-shadow: 0 0 4px rgb(60, 120, 70);\n\
}\n", "incremental-occur-highlighting", false);
exports.Occur = Occur;