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

Commit 92cc76b

Browse files
committed
Implement regex based rules
1 parent 7d22dab commit 92cc76b

File tree

3 files changed

+147
-118
lines changed

3 files changed

+147
-118
lines changed

init.js

+16-116
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
path: curpath,
2222
files: null,
2323
cache: [],
24+
rules: [],
2425

2526
//////////////////////////////////////////////////////////
2627
//
@@ -38,6 +39,10 @@
3839
amplify.subscribe("Include.Callback", function(obj){
3940
_this.replacePrefix(obj);
4041
});
42+
//Load rules
43+
$.getJSON(this.path + 'rules.json', function(json){
44+
_this.rules = json;
45+
});
4146
//Check requirements
4247
setTimeout(function(){
4348
if (typeof(codiad.Complete) == 'undefined') {
@@ -88,123 +93,18 @@
8893
getSuggestions: function(obj) {
8994
var text = obj.before.replace(new RegExp(" ", "g"), "");
9095
text = text.replace(new RegExp("\t", "g"), "");
91-
switch (obj.syntax) {
92-
case "c_cpp":
93-
//Include statement
94-
if (this.isAtEnd(text, "#include\"")) {
95-
this.sendSuggestion("c", obj.file);
96-
this.sendSuggestion("cpp", obj.file);
97-
this.sendSuggestion("h", obj.file);
98-
this.sendSuggestion("hpp", obj.file);
99-
}
100-
break;
101-
case "csharp":
102-
//Comes later
103-
break;
104-
case "php":
105-
//include
106-
if (this.isAtEnd(text, "include\""+obj.prefix)) {
107-
this.sendSuggestion("php", obj.file);
108-
}
109-
//include_once
110-
if (this.isAtEnd(text, "include_once\""+obj.prefix)) {
111-
this.sendSuggestion("php", obj.file);
112-
}
113-
//require
114-
if (this.isAtEnd(text, "require\""+obj.prefix)) {
115-
this.sendSuggestion("php", obj.file);
116-
}
117-
//require_once
118-
if (this.isAtEnd(text, "require_once\""+obj.prefix)) {
119-
this.sendSuggestion("php", obj.file);
120-
}
121-
//include
122-
if (this.isAtEnd(text, "include(\""+obj.prefix)) {
123-
this.sendSuggestion("php", obj.file);
124-
}
125-
//include_once
126-
if (this.isAtEnd(text, "include_once(\""+obj.prefix)) {
127-
this.sendSuggestion("php", obj.file);
128-
}
129-
//require
130-
if (this.isAtEnd(text, "require(\""+obj.prefix)) {
131-
this.sendSuggestion("php", obj.file);
132-
}
133-
//require_once
134-
if (this.isAtEnd(text, "require_once(\""+obj.prefix)) {
135-
this.sendSuggestion("php", obj.file);
136-
}
137-
case "html":
138-
//Script
139-
if ((text.lastIndexOf("<script") > text.lastIndexOf(">")) && this.isAtEnd(text, "src=\""+obj.prefix)) {
140-
this.sendSuggestion("js", obj.file);
141-
}
142-
//CSS, favicon, touch-icon
143-
if ((text.lastIndexOf("<link") > text.lastIndexOf(">")) && this.isAtEnd(text, "href=\""+obj.prefix)) {
144-
this.sendSuggestion("css", obj.file);
145-
this.sendSuggestion("ico", obj.file);
146-
this.sendSuggestion("png", obj.file);
96+
97+
var rule = [];
98+
for (var i = 0; i < this.rules.length; i++) {
99+
rule = this.rules[i];
100+
if (rule.scope.indexOf(obj.syntax) !== -1) {
101+
if (new RegExp(rule.regex).test(obj.before)) {
102+
for (var j = 0; j < rule.ext.length; j++) {
103+
console.log(rule.ext[j]);
104+
this.sendSuggestion(rule.ext[j], obj.file);
105+
}
147106
}
148-
//<a href=""></a>
149-
if ((text.lastIndexOf("<a") > text.lastIndexOf(">")) && this.isAtEnd(text, "href=\""+obj.prefix)) {
150-
this.sendSuggestion("html", obj.file);
151-
this.sendSuggestion("htm", obj.file);
152-
this.sendSuggestion("php", obj.file);
153-
}
154-
//<img src="">
155-
if ((text.lastIndexOf("<img") > text.lastIndexOf(">")) && this.isAtEnd(text, "src=\""+obj.prefix)) {
156-
this.sendSuggestion("jpg", obj.file);
157-
this.sendSuggestion("jpeg", obj.file);
158-
this.sendSuggestion("gif", obj.file);
159-
this.sendSuggestion("png", obj.file);
160-
this.sendSuggestion("bmp", obj.file);
161-
this.sendSuggestion("pcx", obj.file);
162-
this.sendSuggestion("tif", obj.file);
163-
this.sendSuggestion("tiff", obj.file);
164-
}
165-
case "javascript":
166-
//$.get
167-
if (this.isAtEnd(text, "$.get(\""+obj.prefix)) {
168-
this.sendSuggestion("html", obj.file);
169-
this.sendSuggestion("htm", obj.file);
170-
this.sendSuggestion("php", obj.file);
171-
}
172-
//$.post
173-
if (this.isAtEnd(text, "$.post(\""+obj.prefix)) {
174-
this.sendSuggestion("php", obj.file);
175-
}
176-
//$.getJSON
177-
if (this.isAtEnd(text, "$.getJSON(\""+obj.prefix)) {
178-
this.sendSuggestion("php", obj.file);
179-
this.sendSuggestion("json", obj.file);
180-
}
181-
//$.getScript
182-
if (this.isAtEnd(text, "$.getScript(\""+obj.prefix)) {
183-
this.sendSuggestion("js", obj.file);
184-
}
185-
//Support integrated files
186-
if (obj.syntax == "javascript") {
187-
break;
188-
}
189-
case "css":
190-
//Import
191-
if (this.isAtEnd(text, "@importurl("+obj.prefix)) {
192-
this.sendSuggestion("css", obj.file);
193-
} else if (this.isAtEnd(text, "background-image:url(")) { //background-image
194-
this.sendSuggestion("jpg", obj.file);
195-
this.sendSuggestion("jpeg", obj.file);
196-
this.sendSuggestion("gif", obj.file);
197-
this.sendSuggestion("png", obj.file);
198-
}
199-
break;
200-
case "twig":
201-
if(this.isAtEnd(text, "{%include\""+obj.prefix)) {
202-
this.sendSuggestion("twig", obj.file);
203-
this.sendSuggestion("html", obj.file);
204-
}
205-
break;
206-
default:
207-
break;
107+
}
208108
}
209109
},
210110

plugin.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[{
22
"author": "Andr3as",
3-
"version": "0.37",
3+
"version": "1.0.0",
44
"name": "Include",
5-
"url": "https://github.com/Andr3as/Codiad-Include.git"
5+
"url": "https://github.com/Andr3as/Codiad-Include"
66
}]

rules.json

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
[
2+
{
3+
"scope": ["html", "php"],
4+
"regex": "(<script[^>]*src\\='[^']*)$",
5+
"char": "'",
6+
"ext": ["js"]
7+
},{
8+
"scope": ["html", "php"],
9+
"regex": "(<script[^>]*src\\=\"[^\"]*)$",
10+
"char": "\"",
11+
"ext": ["js"]
12+
},{
13+
"scope": ["html", "php"],
14+
"regex": "(<link[^>]*href\\='[^']*)$",
15+
"char": "'",
16+
"ext": ["css", "png", "ico"]
17+
},{
18+
"scope": ["html", "php"],
19+
"regex": "(<link[^>]*href\\=\"[^\"]*)$",
20+
"char": "\"",
21+
"ext": ["css", "png", "ico"]
22+
},{
23+
"scope": ["html", "php"],
24+
"regex": "(<a[^>]*href\\='[^']*)$",
25+
"char": "'",
26+
"ext": ["html", "php", "htm"]
27+
},{
28+
"scope": ["html", "php"],
29+
"regex": "(<a[^>]*href\\=\"[^\"]*)$",
30+
"char": "\"",
31+
"ext": ["html", "php", "htm"]
32+
},{
33+
"scope": ["html", "php"],
34+
"regex": "(<img[^>]*src\\='[^']*)$",
35+
"char": "'",
36+
"ext": ["png", "jpeg", "jpg", "gif", "bmp", "pcx", "tif", "tiff"]
37+
},{
38+
"scope": ["html", "php"],
39+
"regex": "(<img[^>]*src\\=\"[^\"]*)$",
40+
"char": "\"",
41+
"ext": ["png", "jpeg", "jpg", "gif", "bmp", "pcx", "tif", "tiff"]
42+
},{
43+
"scope": ["php"],
44+
"regex": "([include|include_once|require|require_once][ ]*[(]?[ ]*'[^']*)$",
45+
"char": "'",
46+
"ext": ["php", "html", "htm"]
47+
},
48+
{
49+
"scope": ["php"],
50+
"regex": "([include|include_once|require|require_once][ ]*[(]?[ ]*\"[^\"]*)$",
51+
"char": "\"",
52+
"ext": ["php", "html", "htm"]
53+
},{
54+
"scope": ["php","html","javascript"],
55+
"regex": "(\\$\\.get\\('[^']*)$",
56+
"char": "'",
57+
"ext": ["php", "html", "htm", "json"]
58+
},{
59+
"scope": ["php", "html", "javascript"],
60+
"regex": "(\\$\\.get\\(\"[^\"]*)$",
61+
"char": "\"",
62+
"ext": ["php", "html", "htm", "json"]
63+
},{
64+
"scope": ["php", "html", "javascript"],
65+
"regex": "(\\$\\.post\\('[^']*)$",
66+
"char": "'",
67+
"ext": ["php"]
68+
},{
69+
"scope": ["php", "html", "javascript"],
70+
"regex": "(\\$\\.post\\(\"[^\"]*)$",
71+
"char": "\"",
72+
"ext": ["php"]
73+
},{
74+
"scope": ["php", "html", "javascript"],
75+
"regex": "(\\$\\.getJSON\\('[^']*)$",
76+
"char": "'",
77+
"ext": ["php", "json"]
78+
},{
79+
"scope": ["php", "html", "javascript"],
80+
"regex": "(\\$\\.getJSON\\(\"[^\"]*)$",
81+
"char": "\"",
82+
"ext": ["php", "json"]
83+
},{
84+
"scope": ["php", "html", "javascript"],
85+
"regex": "(\\$\\.getScript\\('[^']*)$",
86+
"char": "'",
87+
"ext": ["js"]
88+
},{
89+
"scope": ["php", "html", "javascript"],
90+
"regex": "(\\$\\.getScript\\(\"[^\"]*)$",
91+
"char": "\"",
92+
"ext": ["js"]
93+
},{
94+
"scope": ["php", "hmtl", "css"],
95+
"regex": "(@import[ ]*(url\\()?[ ]*'[^']*)$",
96+
"char": "'",
97+
"ext": ["css"]
98+
},{
99+
"scope": ["php", "html", "css"],
100+
"regex": "(@import[ ]*(url\\()?[ ]*\"[^\"]*)$",
101+
"char": "\"",
102+
"ext": ["css"]
103+
},{
104+
"scope": ["php", "html", "css"],
105+
"regex": "(background-image[ ]*:[ ]*url\\([ ]*'[^']*)$",
106+
"char": "'",
107+
"ext": ["png", "jpeg", "jpg", "gif", "bmp", "pcx", "tif", "tiff"]
108+
},{
109+
"scope": ["php", "html", "css"],
110+
"regex": "(background-image[ ]*:[ ]*url\\([ ]*\"[^\"]*)$",
111+
"char": "\"",
112+
"ext": ["png", "jpeg", "jpg", "gif", "bmp", "pcx", "tif", "tiff"]
113+
},{
114+
"scope": ["c_cpp"],
115+
"regex": "(#include[ ]*\"[^\"]*)$",
116+
"char": "\"",
117+
"ext": ["cpp", "hpp", "c", "h"]
118+
},{
119+
"scope": ["twig"],
120+
"regex": "({%[ ]*include[ ]*'[^']*)$",
121+
"char": "'",
122+
"ext": ["html"]
123+
},{
124+
"scope": ["twig"],
125+
"regex": "({%[ ]*include[ ]*\"[^\"]*)$",
126+
"char": "\"",
127+
"ext": ["html", "twig"]
128+
}
129+
]

0 commit comments

Comments
 (0)