Skip to content

Commit 4a5eb82

Browse files
committed
Fix #281: Allow setting custom JSHint options using .jshintrc
1 parent af0d6b9 commit 4a5eb82

File tree

4 files changed

+150
-88
lines changed

4 files changed

+150
-88
lines changed

addons/cb.files.editor/client.js

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,17 @@
11
define([
22
"ace",
3-
"views/file",
4-
"codecomplete"
5-
], function(ace, FileEditorView, codecomplete) {
3+
"editor",
4+
"codecomplete",
5+
"settings"
6+
], function(ace, FileEditorView, codecomplete, userSettings) {
67
var $ = codebox.require("hr/dom");
78
var commands = codebox.require("core/commands/toolbar");
8-
var settings = codebox.require("core/settings");
99
var files = codebox.require("core/files");
1010
var languages = codebox.require("utils/languages");
1111

1212
var aceconfig = ace.require("ace/config");
1313
aceconfig.set("basePath", "static/addons/cb.files.editor/ace");
1414

15-
// Add settings
16-
settings.add({
17-
'namespace': "editor",
18-
'title': "Code Editor",
19-
'defaults': {
20-
'theme': "github",
21-
'fontsize': "12",
22-
'printmargincolumn': 80,
23-
'showinvisibles': false,
24-
'showprintmargin': false,
25-
'highlightactiveline': false,
26-
'wraplimitrange': 80,
27-
'enablesoftwrap': false,
28-
'enablesofttabs': true,
29-
'autocollaboration': true,
30-
'tabsize': 4,
31-
'keyboard': "textinput"
32-
},
33-
'fields': {
34-
'keyboard': {
35-
'label': "Keyboard mode",
36-
'type': "select",
37-
'options': {
38-
"vim": "Vim",
39-
"emacs": "Emacs",
40-
"textinput": "Default"
41-
}
42-
},
43-
'fontsize': {
44-
'label': "Font Size",
45-
'type': "number",
46-
'min': 10,
47-
'max': 30,
48-
'step': 1
49-
},
50-
'printmargincolumn': {
51-
'label': "Print Margin Column",
52-
'type': "number",
53-
'min': 0,
54-
'max': 1000,
55-
'step': 1
56-
},
57-
'wraplimitrange': {
58-
'label': "Wrap Limit Range",
59-
'type': "number",
60-
'min': 0,
61-
'max': 1000,
62-
'step': 1
63-
},
64-
'autocollaboration': {
65-
'label': "Auto enable realtime collaboration",
66-
'type': "checkbox"
67-
},
68-
'showprintmargin': {
69-
'label': "Show Print Margin",
70-
'type': "checkbox"
71-
},
72-
'showinvisibles': {
73-
'label': "Show Invisibles",
74-
'type': "checkbox"
75-
},
76-
'highlightactiveline': {
77-
'label': "Highlight Active Line",
78-
'type': "checkbox"
79-
},
80-
'enablesoftwrap': {
81-
'label': "Enable Soft Wrap",
82-
'type': "checkbox"
83-
},
84-
'enablesofttabs': {
85-
'label': "Use Soft Tabs",
86-
'type': "checkbox"
87-
},
88-
'tabsize': {
89-
'label': "Tab Size",
90-
'type': "number",
91-
'min': 0,
92-
'max': 1000,
93-
'step': 1
94-
}
95-
}
96-
});
97-
9815
// Build code files extensions list
9916
var textExts = _.reduce(languages.LIST, function(list, language) {
10017
list = list.concat(language.extensions || []);

addons/cb.files.editor/views/file.js renamed to addons/cb.files.editor/editor.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
define([
22
"ace",
3+
"jshint",
34
"theme/textmate",
45
"text!templates/file.html",
56
"less!stylesheets/file.less",
6-
], function(ace, aceDefaultTheme, templateFile) {
7+
], function(ace, jshint, aceDefaultTheme, templateFile) {
78
var _ = codebox.require("hr/utils");
89
var $ = codebox.require("hr/dom");
910
var hr = codebox.require("hr/hr");
@@ -192,6 +193,14 @@ define([
192193
that.sync.updateUserCursor(cursor.column, cursor.row);
193194
that.editorStatusCommand.set("title", "Line "+(cursor.row+1)+", Column "+(cursor.column+1));
194195
});
196+
this.editor.getSession().on("changeMode", function() {
197+
jshint.applySettings(that.editor)
198+
.then(function(state) {
199+
console.log("jshint config", state);
200+
}, function(state) {
201+
console.error("jshint error", state);
202+
});
203+
});
195204

196205
var $doc = this.editor.session.doc;
197206

addons/cb.files.editor/jshint.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
define([], function() {
2+
var File = codebox.require("models/file");
3+
var box = codebox.require("core/box");
4+
var Q = codebox.require("hr/promise");
5+
var _ = codebox.require("hr/utils");
6+
7+
var jshintFile = new File();
8+
9+
// Read the JSHint configuration from a file
10+
var readJshintSettings = _.memoize(function() {
11+
// Get file
12+
return jshintFile.getByPath("/.jshintrc")
13+
.then(function() {
14+
//Read file
15+
return jshintFile.read();
16+
})
17+
.then(function(content) {
18+
// Parse JSON
19+
return JSON.parse(content);
20+
});
21+
});
22+
23+
// Apply JSHint config from a file to an editor
24+
var applyJshintSettings = function(editor) {
25+
var session = editor.session;
26+
if (session.getMode().$id != "ace/mode/javascript") return Q.reject(new Error("Not a javascript editor"));
27+
if (!session.$worker) return Q.reject(new Error("No JSHint worker"));
28+
29+
return readJshintSettings()
30+
.then(function(options) {
31+
if (session.$worker) {
32+
//session.$worker.send("changeOptions",[ {undef: true}])
33+
// or
34+
session.$worker.send("setOptions",[options])
35+
}
36+
return options;
37+
});
38+
};
39+
40+
// Clear memoize cache when file change
41+
jshintFile.on("file:change", function(e) {
42+
readJshintSettings.cache = {};
43+
});
44+
45+
return {
46+
'applySettings': applyJshintSettings
47+
};
48+
});

addons/cb.files.editor/settings.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
define([], function() {
2+
var settings = codebox.require("core/settings");
3+
4+
// Add settings
5+
var userSettings = settings.add({
6+
'namespace': "editor",
7+
'title': "Code Editor",
8+
'defaults': {
9+
'theme': "github",
10+
'fontsize': "12",
11+
'printmargincolumn': 80,
12+
'showinvisibles': false,
13+
'showprintmargin': false,
14+
'highlightactiveline': false,
15+
'wraplimitrange': 80,
16+
'enablesoftwrap': false,
17+
'enablesofttabs': true,
18+
'autocollaboration': true,
19+
'tabsize': 4,
20+
'keyboard': "textinput"
21+
},
22+
'fields': {
23+
'keyboard': {
24+
'label': "Keyboard mode",
25+
'type': "select",
26+
'options': {
27+
"vim": "Vim",
28+
"emacs": "Emacs",
29+
"textinput": "Default"
30+
}
31+
},
32+
'fontsize': {
33+
'label': "Font Size",
34+
'type': "number",
35+
'min': 10,
36+
'max': 30,
37+
'step': 1
38+
},
39+
'printmargincolumn': {
40+
'label': "Print Margin Column",
41+
'type': "number",
42+
'min': 0,
43+
'max': 1000,
44+
'step': 1
45+
},
46+
'wraplimitrange': {
47+
'label': "Wrap Limit Range",
48+
'type': "number",
49+
'min': 0,
50+
'max': 1000,
51+
'step': 1
52+
},
53+
'autocollaboration': {
54+
'label': "Auto enable realtime collaboration",
55+
'type': "checkbox"
56+
},
57+
'showprintmargin': {
58+
'label': "Show Print Margin",
59+
'type': "checkbox"
60+
},
61+
'showinvisibles': {
62+
'label': "Show Invisibles",
63+
'type': "checkbox"
64+
},
65+
'highlightactiveline': {
66+
'label': "Highlight Active Line",
67+
'type': "checkbox"
68+
},
69+
'enablesoftwrap': {
70+
'label': "Enable Soft Wrap",
71+
'type': "checkbox"
72+
},
73+
'enablesofttabs': {
74+
'label': "Use Soft Tabs",
75+
'type': "checkbox"
76+
},
77+
'tabsize': {
78+
'label': "Tab Size",
79+
'type': "number",
80+
'min': 0,
81+
'max': 1000,
82+
'step': 1
83+
}
84+
}
85+
});
86+
87+
return userSettings;
88+
});

0 commit comments

Comments
 (0)