This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathresult-row-view.js
More file actions
333 lines (292 loc) · 9.4 KB
/
Copy pathresult-row-view.js
File metadata and controls
333 lines (292 loc) · 9.4 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
const getIconServices = require('../get-icon-services');
const { Point, Range } = require('atom');
const {
LeadingContextRow,
TrailingContextRow,
ResultPathRow,
MatchRow,
ResultRowGroup
} = require('./result-row');
const {showIf} = require('./util');
const _ = require('underscore-plus');
const path = require('path');
const assert = require('assert');
const etch = require('etch');
const $ = etch.dom;
class ResultPathRowView {
constructor({groupData, isSelected}) {
const props = {groupData, isSelected};
this.props = Object.assign({}, props);
etch.initialize(this);
getIconServices().updateIcon(this, groupData.filePath);
}
destroy() {
return etch.destroy(this)
}
update({groupData, isSelected}) {
const props = {groupData, isSelected};
if (!_.isEqual(props, this.props)) {
this.props = Object.assign({}, props);
etch.update(this);
}
}
writeAfterUpdate() {
getIconServices().updateIcon(this, this.props.groupData.filePath);
}
render() {
let relativePath = this.props.groupData.filePath;
if (atom.project) {
let rootPath;
[rootPath, relativePath] = atom.project.relativizePath(this.props.groupData.filePath);
if (rootPath && atom.project.getDirectories().length > 1) {
relativePath = path.join(path.basename(rootPath), relativePath);
}
}
const groupData = this.props.groupData;
return (
$.li(
{
className: [
// This triggers the CSS displaying the "expand / collapse" arrows
// See `styles/lists.less` in the atom-ui repository for details
'list-nested-item',
groupData.isCollapsed ? 'collapsed' : '',
this.props.isSelected ? 'selected' : ''
].join(' ').trim(),
key: groupData.filePath
},
$.div(
{
className: 'list-item path-row',
dataset: { filePath: groupData.filePath }
},
$.span({
dataset: {name: path.basename(groupData.filePath)},
ref: 'icon',
className: 'icon'
}),
$.span({className: 'path-name bright'}, relativePath),
$.span(
{ref: 'description', className: 'path-match-number'},
`(${groupData.matchCount} match${groupData.matchCount === 1 ? '' : 'es'})`
)
)
)
)
}
};
class MatchRowView {
constructor({rowData, groupData, isSelected, replacePattern, regex}) {
const props = {rowData, groupData, isSelected, replacePattern, regex};
const previewData = {matches: rowData.matches, replacePattern, regex};
this.props = Object.assign({}, props);
this.previewData = previewData;
this.previewNode = this.generatePreviewNode(previewData);
etch.initialize(this);
}
update({rowData, groupData, isSelected, replacePattern, regex}) {
const props = {rowData, groupData, isSelected, replacePattern, regex};
const previewData = {matches: rowData.matches, replacePattern, regex};
if (!_.isEqual(props, this.props)) {
if (!_.isEqual(previewData, this.previewData)) {
this.previewData = previewData;
this.previewNode = this.generatePreviewNode(previewData);
}
this.props = Object.assign({}, props);
etch.update(this);
}
}
generatePreviewNode({matches, replacePattern, regex}) {
const TYPE = {
MATCH: 1,
TEXT: 2,
ELLIPSIS: 3
};
const segments = [];
for (const match of matches) {
if (segments.length) {
const previousRange = segments[segments.length - 1].range;
const currentRange = new Range(
new Point(0, match.lineTextOffset),
new Point(0, match.lineTextOffset + match.lineText.length)
);
if (previousRange.intersectsWith(currentRange)) {
const segment = segments.pop();
// current range starts before previous range, therefore it should contain everything from previous range
if (previousRange.start.isGreaterThanOrEqual(currentRange.start)) {
// Delete everything up to previous match from the beginning of current range
match.lineText = match.lineText.substring(previousRange.start.column - currentRange.start.column);
match.lineTextOffset = previousRange.start.column;
} else { // current range starts midway through previous range
// Prepend non-overlapping part of previous range to current range
const preprefix = segment.content.substring(0, currentRange.start.column - previousRange.start.column);
match.lineText = preprefix + match.lineText;
match.lineTextOffset -= preprefix.length;
}
} else { // current range does not intersect and comes after previous range
segments.push({ type: TYPE.ELLIPSIS, range: new Range(previousRange.end, currentRange.start) });
}
}
const prefix = match.lineText.substring(0, match.range.start.column - match.lineTextOffset);
const suffix = match.lineText.substring(match.range.end.column - match.lineTextOffset);
if (prefix) {
segments.push({
type: TYPE.TEXT,
content: prefix,
range: new Range(
new Point(0, match.lineTextOffset),
new Point(0, match.range.start.column)
)
});
}
let replacementText
if (replacePattern) {
replacementText = regex ? match.matchText.replace(regex, replacePattern) : replacePattern;
}
segments.push({
type: TYPE.MATCH,
content: match.matchText,
range: new Range(
new Point(0, match.range.start.column),
new Point(0, match.range.end.column)
),
replacement: replacementText
});
if (suffix) {
segments.push({
type: TYPE.TEXT,
content: suffix,
range: new Range(
new Point(0, match.range.end.column),
new Point(0, match.lineTextOffset + match.lineText.length)
)
});
}
}
const subnodes = [];
for (const segment of segments) {
if (segment.type === TYPE.TEXT) {
subnodes.push($.span({}, segment.content));
}
if (segment.type === TYPE.MATCH) {
subnodes.push(
$.span({ className: `match ${segment.replacement ? 'highlight-error' : 'highlight-info'}` }, segment.content)
);
if (segment.replacement) {
subnodes.push($.span({ className: 'replacement highlight-success' }, segment.replacement));
}
}
if (segment.type === TYPE.ELLIPSIS) {
subnodes.push($.span({}, '…'));
}
}
return $.span({ className: 'preview' }, ...subnodes);
}
render() {
return (
$.li(
{
className: [
'list-item',
'match-row',
this.props.isSelected ? 'selected' : '',
this.props.rowData.separator ? 'separator' : ''
].join(' ').trim(),
dataset: {
filePath: this.props.groupData.filePath,
matchLineNumber: this.props.rowData.lineNumber,
}
},
$.span(
{className: 'line-number text-subtle'},
this.props.rowData.lineNumber + 1
),
this.previewNode
)
);
}
};
class ContextRowView {
constructor({rowData, groupData, isSelected}) {
const props = {rowData, groupData, isSelected};
this.props = Object.assign({}, props);
etch.initialize(this);
}
destroy() {
return etch.destroy(this)
}
update({rowData, groupData, isSelected}) {
const props = {rowData, groupData, isSelected};
if (!_.isEqual(props, this.props)) {
this.props = Object.assign({}, props);
etch.update(this);
}
}
render() {
return (
$.li(
{
className: [
'list-item',
'context-row',
this.props.rowData.separator ? 'separator' : ''
].join(' ').trim(),
dataset: {
filePath: this.props.groupData.filePath,
matchLineNumber: this.props.rowData.matchLineNumber
},
},
$.span({className: 'line-number text-subtle'}, this.props.rowData.lineNumber + 1),
$.span({className: 'preview'}, $.span({}, this.props.rowData.line))
)
)
}
}
function getRowViewType(row) {
if (row instanceof ResultPathRow) {
return ResultPathRowView;
}
if (row instanceof MatchRow) {
return MatchRowView;
}
if (row instanceof LeadingContextRow) {
return ContextRowView;
}
if (row instanceof TrailingContextRow) {
return ContextRowView;
}
assert(false);
}
module.exports =
class ResultRowView {
constructor({item}) {
const props = {
rowData: Object.assign({}, item.row.data),
groupData: Object.assign({}, item.row.group.data),
isSelected: item.isSelected,
replacePattern: item.replacePattern,
regex: item.regex
};
this.props = props;
this.rowViewType = getRowViewType(item.row);
etch.initialize(this);
}
destroy() {
return etch.destroy(this);
}
update({item}) {
const props = {
rowData: Object.assign({}, item.row.data),
groupData: Object.assign({}, item.row.group.data),
isSelected: item.isSelected,
replacePattern: item.replacePattern,
regex: item.regex
}
this.props = props;
this.rowViewType = getRowViewType(item.row);
etch.update(this);
}
render() {
return $(this.rowViewType, this.props);
}
};