Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 5f18bc7

Browse files
committed
Decaf find-options
1 parent ef1e7f3 commit 5f18bc7

File tree

2 files changed

+103
-83
lines changed

2 files changed

+103
-83
lines changed

lib/find-options.coffee

Lines changed: 0 additions & 83 deletions
This file was deleted.

lib/find-options.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const _ = require('underscore-plus');
2+
const {Emitter} = require('atom');
3+
4+
const Params = [
5+
'findPattern',
6+
'replacePattern',
7+
'pathsPattern',
8+
'useRegex',
9+
'wholeWord',
10+
'caseSensitive',
11+
'inCurrentSelection',
12+
'leadingContextLineCount',
13+
'trailingContextLineCount'
14+
];
15+
16+
module.exports = class FindOptions {
17+
constructor(state) {
18+
let left, left1, left2, left3, left4, left5;
19+
if (state == null) { state = {}; }
20+
this.emitter = new Emitter;
21+
22+
this.findPattern = '';
23+
this.replacePattern = state.replacePattern != null ? state.replacePattern : '';
24+
this.pathsPattern = state.pathsPattern != null ? state.pathsPattern : '';
25+
this.useRegex = (left = state.useRegex != null ? state.useRegex : atom.config.get('find-and-replace.useRegex')) != null ? left : false;
26+
this.caseSensitive = (left1 = state.caseSensitive != null ? state.caseSensitive : atom.config.get('find-and-replace.caseSensitive')) != null ? left1 : false;
27+
this.wholeWord = (left2 = state.wholeWord != null ? state.wholeWord : atom.config.get('find-and-replace.wholeWord')) != null ? left2 : false;
28+
this.inCurrentSelection = (left3 = state.inCurrentSelection != null ? state.inCurrentSelection : atom.config.get('find-and-replace.inCurrentSelection')) != null ? left3 : false;
29+
this.leadingContextLineCount = (left4 = state.leadingContextLineCount != null ? state.leadingContextLineCount : atom.config.get('find-and-replace.leadingContextLineCount')) != null ? left4 : 0;
30+
this.trailingContextLineCount = (left5 = state.trailingContextLineCount != null ? state.trailingContextLineCount : atom.config.get('find-and-replace.trailingContextLineCount')) != null ? left5 : 0;
31+
}
32+
33+
onDidChange(callback) {
34+
this.emitter.on('did-change', callback);
35+
}
36+
37+
onDidChangeUseRegex(callback) {
38+
this.emitter.on('did-change-useRegex', callback);
39+
}
40+
41+
onDidChangeReplacePattern(callback) {
42+
this.emitter.on('did-change-replacePattern', callback);
43+
}
44+
45+
serialize() {
46+
const result = {};
47+
for (let param of Params) {
48+
result[param] = this[param];
49+
}
50+
return result;
51+
}
52+
53+
set(newParams) {
54+
if (newParams == null) { newParams = {}; }
55+
let changedParams = {};
56+
for (let key of Params) {
57+
if ((newParams[key] != null) && (newParams[key] !== this[key])) {
58+
if (changedParams == null) { changedParams = {}; }
59+
this[key] = (changedParams[key] = newParams[key]);
60+
}
61+
}
62+
63+
if (Object.keys(changedParams).length) {
64+
for (let param in changedParams) {
65+
const val = changedParams[param];
66+
this.emitter.emit(`did-change-${param}`);
67+
}
68+
this.emitter.emit('did-change', changedParams);
69+
}
70+
return changedParams;
71+
}
72+
73+
getFindPatternRegex(forceUnicode) {
74+
let expression;
75+
if (forceUnicode == null) { forceUnicode = false; }
76+
for (let i = 0, end = this.findPattern.length, asc = 0 <= end; asc ? i <= end : i >= end; asc ? i++ : i--) {
77+
if (this.findPattern.charCodeAt(i) > 128) {
78+
forceUnicode = true;
79+
break;
80+
}
81+
}
82+
83+
let flags = 'gm';
84+
if (!this.caseSensitive) { flags += 'i'; }
85+
if (forceUnicode) { flags += 'u'; }
86+
87+
if (this.useRegex) {
88+
expression = this.findPattern;
89+
} else {
90+
expression = escapeRegExp(this.findPattern);
91+
}
92+
93+
if (this.wholeWord) { expression = `\\b${expression}\\b`; }
94+
95+
return new RegExp(expression, flags);
96+
}
97+
});
98+
99+
// This is different from _.escapeRegExp, which escapes dashes. Escaped dashes
100+
// are not allowed outside of character classes in RegExps with the `u` flag.
101+
//
102+
// See atom/find-and-replace#1022
103+
var escapeRegExp = string => string.replace(/[\/\\^$*+?.()|[\]{}]/g, '\\$&');

0 commit comments

Comments
 (0)