Skip to content

Commit 746b133

Browse files
committed
Filetype identification fixes & improvements
Look to fileExt to determine the file type by extension rather than string in fileName which is the path. This means .c isn't mistakenly picked up in paths such as /httpdocs/mydomain.com/file.rb Simplified code with 2 improved ternary statements
1 parent c141fc2 commit 746b133

1 file changed

Lines changed: 64 additions & 49 deletions

File tree

processes/on-editor-load.php

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,39 @@
1313

1414
// Switch the CodeMirror mode on demand
1515
top.ICEcoder.switchMode = function(mode) {
16-
var cM, fileName;
16+
var cM, fileName, fileExt;
1717

1818
cM = top.ICEcoder.getcMInstance();
1919
fileName = top.ICEcoder.openFiles[top.ICEcoder.selectedTab-1];
2020
if (cM && mode) {
2121
cM.setOption("mode",mode);
2222
} else if (cM && fileName) {
23-
fileName.indexOf('.js')>0 ? cM.setOption("mode","text/javascript")
24-
: fileName.indexOf('.coffee')>0 ? cM.setOption("mode","text/x-coffeescript")
25-
: fileName.indexOf('.rb')>0 ? cM.setOption("mode","text/x-ruby")
26-
: fileName.indexOf('.py')>0 ? cM.setOption("mode","text/x-python")
27-
: fileName.indexOf('.css')>0 ? cM.setOption("mode","text/css")
28-
: fileName.indexOf('.less')>0 ? cM.setOption("mode","text/x-less")
29-
: fileName.indexOf('.md')>0 ? cM.setOption("mode","text/x-markdown")
30-
: fileName.indexOf('.xml')>0 ? cM.setOption("mode","application/xml")
31-
: fileName.indexOf('.sql')>0 ? cM.setOption("mode","text/x-mysql") // also text/x-sql, text/x-mariadb, text/x-cassandra or text/x-plsql
32-
: fileName.indexOf('.erl')>0 ? cM.setOption("mode","text/x-erlang")
33-
: fileName.indexOf('.yaml')>0 ? cM.setOption("mode","text/x-yaml")
34-
: fileName.indexOf('.java')>0 ? cM.setOption("mode","text/x-java")
35-
: fileName.indexOf('.jl')>0 ? cM.setOption("mode","text/x-julia")
36-
: fileName.indexOf('.c')>0 ? cM.setOption("mode","text/x-csrc")
37-
: fileName.indexOf('.cpp')>0 ? cM.setOption("mode","text/x-c++src")
38-
: fileName.indexOf('.cs')>0 ? cM.setOption("mode","text/x-csharp")
39-
: fileName.indexOf('.go')>0 ? cM.setOption("mode","text/x-go")
40-
: fileName.indexOf('.lua')>0 ? cM.setOption("mode","text/x-lua")
41-
: fileName.indexOf('.pl')>0 ? cM.setOption("mode","text/x-perl")
42-
: fileName.indexOf('.rs')>0 ? cM.setOption("mode","text/x-rustsrc")
43-
: fileName.indexOf('.scss')>0 ? cM.setOption("mode","text/x-sass")
44-
: cM.setOption("mode","application/x-httpd-php");
23+
fileExt = fileName.split(".");
24+
fileExt = fileExt[fileExt.length-1];
25+
cM.setOption("mode",
26+
fileExt == "js" ? "text/javascript"
27+
: fileExt == "coffee" ? "text/x-coffeescript"
28+
: fileExt == "rb" ? "text/x-ruby"
29+
: fileExt == "py" ? "text/x-python"
30+
: fileExt == "css" ? "text/css"
31+
: fileExt == "less" ? "text/x-less"
32+
: fileExt == "md" ? "text/x-markdown"
33+
: fileExt == "xml" ? "application/xml"
34+
: fileExt == "sql" ? "text/x-mysql" // also text/x-sql, text/x-mariadb, text/x-cassandra or text/x-plsql
35+
: fileExt == "erl" ? "text/x-erlang"
36+
: fileExt == "yaml" ? "text/x-yaml"
37+
: fileExt == "java" ? "text/x-java"
38+
: fileExt == "jl" ? "text/x-julia"
39+
: fileExt == "c" ? "text/x-csrc"
40+
: fileExt == "cpp" ? "text/x-c++src"
41+
: fileExt == "cs" ? "text/x-csharp"
42+
: fileExt == "go" ? "text/x-go"
43+
: fileExt == "lua" ? "text/x-lua"
44+
: fileExt == "pl" ? "text/x-perl"
45+
: fileExt == "rs" ? "text/x-rustsrc"
46+
: fileExt == "scss" ? "text/x-sass"
47+
: "application/x-httpd-php"
48+
);
4549
}
4650
}
4751

@@ -112,9 +116,12 @@
112116

113117
// Work out the nesting depth location on demand and update our display if required
114118
top.ICEcoder.getNestLocationSub = function(nestCheck, fileName) {
115-
var events;
119+
var events, fileExt;
116120

117-
if (["js","coffee","css","less","sql","erl","yaml","java","jl","c","cpp","cs","go","lua","pl","rs","scss"].indexOf(fileName.split(".")[1])<0 &&
121+
fileExt = fileName.split(".");
122+
fileExt = fileExt[fileExt.length-1];
123+
124+
if (["js","coffee","css","less","sql","erl","yaml","java","jl","c","cpp","cs","go","lua","pl","rs","scss"].indexOf(fileExt)<0 &&
118125
(nestCheck.indexOf("include(")==-1)&&(nestCheck.indexOf("include_once(")==-1)) {
119126

120127
// Then for all the array items, output as the nest display
@@ -134,12 +141,16 @@
134141

135142
// Indicate if the nesting structure of the code is OK
136143
top.ICEcoder.updateNestingIndicator = function() {
137-
var cM, testToken, nestOK, fileName;
144+
var cM, testToken, nestOK, fileName, fileExt;
138145

139146
cM = top.ICEcoder.getcMInstance();
140147
nestOK = true;
141148
fileName = top.ICEcoder.openFiles[top.ICEcoder.selectedTab-1];
142-
if (cM && fileName && ["js","coffee","css","less","sql","erl","yaml","java","jl","c","cpp","cs","go","lua","pl","rs","scss"].indexOf(fileName.split(".")[1])==-1) {
149+
if (fileName) {
150+
fileExt = fileName.split(".");
151+
fileExt = fileExt[fileExt.length-1];
152+
}
153+
if (cM && fileName && ["js","coffee","css","less","sql","erl","yaml","java","jl","c","cpp","cs","go","lua","pl","rs","scss"].indexOf(fileExt)==-1) {
143154
testToken = cM.getTokenAt({line:cM.lineCount(),ch:cM.lineInfo(cM.lineCount()-1).text.length});
144155
nestOK = testToken.type && testToken.type.indexOf("error") == -1 ? true : false;
145156
}
@@ -149,7 +160,7 @@
149160

150161
// Determine which area of the document we're in
151162
top.ICEcoder.caretLocationType = function() {
152-
var cM, caretLocType, caretChunk, fileName;
163+
var cM, caretLocType, caretChunk, fileName, fileExt;
153164

154165
cM = top.ICEcoder.getcMInstance();
155166
caretLocType = "Unknown";
@@ -163,27 +174,31 @@
163174

164175
fileName = top.ICEcoder.openFiles[top.ICEcoder.selectedTab-1];
165176
if (fileName) {
166-
if (fileName.indexOf(".js")>0) {caretLocType="JavaScript"}
167-
else if (fileName.indexOf(".coffee")>0) {caretLocType="CoffeeScript"}
168-
else if (fileName.indexOf(".py")>0) {caretLocType="Python"}
169-
else if (fileName.indexOf(".rb")>0) {caretLocType="Ruby"}
170-
else if (fileName.indexOf(".css")>0) {caretLocType="CSS"}
171-
else if (fileName.indexOf(".less")>0) {caretLocType="LESS"}
172-
else if (fileName.indexOf(".md")>0) {caretLocType="Markdown"}
173-
else if (fileName.indexOf(".xml")>0) {caretLocType="XML"}
174-
else if (fileName.indexOf(".sql")>0) {caretLocType="SQL"}
175-
else if (fileName.indexOf(".yaml")>0) {caretLocType="YAML"}
176-
else if (fileName.indexOf(".java")>0) {caretLocType="Java"}
177-
else if (fileName.indexOf(".erl")>0) {caretLocType="Erlang"}
178-
else if (fileName.indexOf(".jl")>0) {caretLocType="Julia"}
179-
else if (fileName.indexOf(".c")>0 && fileName.indexOf(".cpp")<0 && fileName.indexOf(".cs")<0) {caretLocType="C"}
180-
else if (fileName.indexOf(".cpp")>0) {caretLocType="C++"}
181-
else if (fileName.indexOf(".cs")>0) {caretLocType="C#"}
182-
else if (fileName.indexOf(".go")>0) {caretLocType="Go"}
183-
else if (fileName.indexOf(".lua")>0) {caretLocType="Lua"}
184-
else if (fileName.indexOf(".pl")>0) {caretLocType="Perl"}
185-
else if (fileName.indexOf(".rs")>0) {caretLocType="Rust"}
186-
else if (fileName.indexOf(".scss")>0) {caretLocType="Sass"};
177+
fileExt = fileName.split(".");
178+
fileExt = fileExt[fileExt.length-1];
179+
caretLocType =
180+
fileExt == "js" ? "JavaScript"
181+
: fileExt == "coffee" ? "CoffeeScript"
182+
: fileExt == "py" ? "Python"
183+
: fileExt == "rb" ? "Ruby"
184+
: fileExt == "css" ? "CSS"
185+
: fileExt == "less" ? "LESS"
186+
: fileExt == "md" ? "Markdown"
187+
: fileExt == "xml" ? "XML"
188+
: fileExt == "sql" ? "SQL"
189+
: fileExt == "yaml" ? "YAML"
190+
: fileExt == "java" ? "Java"
191+
: fileExt == "erl" ? "Erlang"
192+
: fileExt == "jl" ? "Julia"
193+
: fileExt == "c" ? "C"
194+
: fileExt == "cpp" ? "C++"
195+
: fileExt == "cs" ? "C#"
196+
: fileExt == "go" ? "Go"
197+
: fileExt == "lua" ? "Lua"
198+
: fileExt == "pl" ? "Perl"
199+
: fileExt == "rs" ? "Rust"
200+
: fileExt == "scss" ? "Sass"
201+
: "Content";
187202
}
188203

189204
top.ICEcoder.caretLocType = caretLocType;

0 commit comments

Comments
 (0)