New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lintingrefactoring #1808

Closed
wants to merge 21 commits into
base: master
from
Commits
Jump to file or symbol
Failed to load files and symbols.
+52 −43
Diff settings

Always

Just for now

Viewing a subset of changes. View all

fixed issue where line numbers were being reported incorrectly

  • Loading branch information...
mikechambers committed Oct 9, 2012
commit 265298744823bdac671e012f391c09c25ce6b310
@@ -11,48 +11,47 @@ define(function (require, exports, module) {
var Strings = brackets.getModule("strings");
var BottomPanel = brackets.getModule("widgets/BottomPanel");
require("../../../thirdparty/jslint/jslint");
//todo: should this both run the linter and display results?
//todo: remove JSLintUtils.js
//todo: do this on first use
require("Handlebars/handlebars");
var outputTemplateSource = require("text!erroroutput.template");
var outputTemplate = Handlebars.compile(outputTemplateSource);
//todo: move jslint source to Linter extension
require("../../../thirdparty/jslint/jslint");
var DOCUMENT_SAVED = "documentSaved";
var CURRENT_DOCUMENT_CHANGED = "currentDocumentChange";
var LINTER = "LINTER";
var enabled = true;
var ignoreEmptyLines = true;
var outputErrorsToConsole = false;
//todo: should this both run the linter and display results?
//todo: need to lint file on startup
//todo: need to lint file on document change
//todo: move jslint source to Linter extension
//todo: remove JSLintUtils.js
var outputTemplate;
function _stripEmptyLines(text) {
//todo: precompile this regex
//todo: check this works with multiple line endings
return text.replace(/^\s*$[\n\r]{1,}/gm, "");
//return text.replace(/^\s*$[\n\r]{1,}/gm, "");
//todo: need to find more optimal way to do this. if we use
//regex above, it works, but then line numbers are off.
var i, arr = text.split("\n");
for (i = 0; i < arr.length; i++) {
if (!arr[i].match(/\S/)) {
arr[i] = "";
}
}
return arr.join("\n");
}
function lintDocument(document) {
var text = document.getText();
if (ignoreEmptyLines) {
text = _stripEmptyLines(text);
}
var hasErrors = !JSLINT(text, null);
function _displayLintErrors(errors) {
if (!hasErrors) {
if (!errors) {
BottomPanel.clearContent();
BottomPanel.close();
return;
}
return JSLINT.errors;
}
function _displayLintErrors(errors) {
var len = errors.length;
if (outputErrorsToConsole) {
@@ -69,42 +68,52 @@ define(function (require, exports, module) {
JSLINT_ERRORS: Strings.JSLINT_ERRORS
};
var output = $(outputTemplate(context));
if (!outputTemplate) {
//if the Handlebars template hasnt been compiled yet, compile it
//and then cache it for future use.
require("Handlebars/handlebars");
var outputTemplateSource = require("text!erroroutput.template");
outputTemplate = Handlebars.compile(outputTemplateSource);
}
/*
$("#bottom-panel").empty();
$("#bottom-panel").append(output);
$("#bottom-panel").show();
*/
var output = $(outputTemplate(context));
BottomPanel.loadContent(output);
}
function _lintCurrentDocument() {
var errors = lintDocument(DocumentManager.getCurrentDocument());
function lintDocument(document) {
var text = document.getText();
if (!errors) {
console.log("no errors clearing content");
BottomPanel.clearContent();
BottomPanel.close();
if (ignoreEmptyLines) {
text = _stripEmptyLines(text);
}
var hasErrors = !JSLINT(text, null);
if (!hasErrors) {
return;
}
return JSLINT.errors;
}
function _lintCurrentDocument() {
var errors = lintDocument(DocumentManager.getCurrentDocument());
_displayLintErrors(errors);
}
function _onDocumentUpdated(event) {
console.log("updated");
_lintCurrentDocument();
}
function _toggleLinting() {
enabled = !enabled;
if (enabled) {
$(DocumentManager).on("documentSaved documentChanged", _onDocumentUpdated);
$(DocumentManager).on(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
} else {
$(DocumentManager).off("documentSaved documentChanged", _onDocumentUpdated);
$(DocumentManager).off(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
}
}
@@ -121,7 +130,7 @@ define(function (require, exports, module) {
//todo: can we do this on a module loaded event? and not on initialization?
//do we know document has loaded yet?
if (enabled) {
$(DocumentManager).on("documentSaved currentDocumentChange", _onDocumentUpdated);
$(DocumentManager).on(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
_lintCurrentDocument();
}
});
@@ -7,11 +7,11 @@ define(function (require, exports, module) {
var EditorManager = require("editor/EditorManager");
var open = function() {
//foo
console.log("open");
};
var close = function() {
//foo
console.log("close");
};
var clearContent = function() {
ProTip! Use n and p to navigate between commits in a pull request.