-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusertype-strategy.js
151 lines (115 loc) · 3.2 KB
/
usertype-strategy.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
"use strict";
const path = require("path");
const {CompositeDisposable} = require("atom");
const {escapeRegExp} = require("../../utils.js");
const IconTables = require("../../icons/icon-tables.js");
const Strategy = require("../strategy.js");
class UsertypeStrategy extends Strategy {
constructor(){
super({
name: "usertypes",
priority: 3,
matchesFiles: true,
matchesDirs: false,
ignoreVirtual: false,
});
this.configDisposable = null;
this.customTypes = null;
this.customTypeIcons = null;
this.hasCustomTypes = false;
}
enable(){
if(!this.enabled && !this.configDisposable){
this.customTypes = new Map();
this.customTypeIcons = new Map();
this.hasCustomTypes = false;
this.configDisposable = new CompositeDisposable(
atom.config.onDidChange("core.customFileTypes", types => {
this.updateCustomTypes(types.newValue);
this.checkAll(false);
})
);
const types = atom.config.get("core.customFileTypes");
this.updateCustomTypes(types);
}
return super.enable();
}
disable(){
if(this.enabled && this.configDisposable){
this.configDisposable.dispose();
this.configDisposable = null;
this.customTypes = null;
this.customTypeIcons = null;
this.hasCustomTypes = false;
}
return super.disable();
}
matchIcon(resource){
if(!this.hasCustomTypes)
return null;
for(const [scopeName, patterns] of this.customTypes){
const {names, paths} = patterns;
if((null !== names && names.test(resource.name))
|| null !== paths && paths.test(resource.path))
return this.customTypeIcons.get(scopeName);
}
return null;
}
updateCustomTypes(types){
this.customTypes.clear();
this.customTypeIcons.clear();
for(const scopeName in types){
const icon = IconTables.matchScope(scopeName);
if(!icon)
continue; // Skip types without icons
this.customTypeIcons.set(scopeName, icon);
const names = [];
const paths = [];
const patterns = types[scopeName];
for(let i = 0, l = patterns.length; i < l; ++i){
const pattern = patterns[i];
(-1 === pattern.indexOf(path.sep))
? names.push(pattern)
: paths.push(pattern);
}
this.customTypes.set(scopeName, {
names: this.makeNamePattern(names),
paths: this.makePathPattern(paths),
});
}
this.hasCustomTypes = !!this.customTypes.size;
}
/**
* Compile an expression to match one or more file extensions.
*
* @param {String[]} names
* @return {RegExp|null}
* @private
*/
makeNamePattern(names){
const {length} = names;
if(!length)
return null;
names = names.map(s => escapeRegExp(s.replace(/^\./, ""))).join("|");
if(length > 1)
names = `(?:${names})`;
return new RegExp("(?:^|\\.)" + names + "$", "i");
}
/**
* Compile an expression to match one or more pathnames.
*
* @param {String[]} paths
* @return {RegExp|null}
* @private
*/
makePathPattern(paths){
const {length} = paths;
if(!length)
return null;
paths = paths.map(s => escapeRegExp(s)).join("|");
if(length > 1)
paths = `(?:${paths})`;
return new RegExp("(?:^|[\\/\\\\])" + paths + "$", "i");
}
}
module.exports = new UsertypeStrategy();