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.
+54 −45
Diff settings

Always

Just for now

Viewing a subset of changes. View all

changed default for ignoring lines with whitespace to false, so now e…

…rrors will be show for any lines with trailing whitespace. Removed all trailing whitespace from checked in files.
  • Loading branch information...
mikechambers committed Oct 9, 2012
commit 5d976a3536f63af08dc63b3d9b6dab6804be0215
@@ -3,7 +3,7 @@
define(function (require, exports, module) {
'use strict';
var CommandManager = brackets.getModule("command/CommandManager");
var DocumentManager = brackets.getModule("document/DocumentManager");
var Menus = brackets.getModule("command/Menus");
@@ -12,33 +12,43 @@ define(function (require, exports, module) {
var BottomPanel = brackets.getModule("widgets/BottomPanel");
var AppInit = brackets.getModule("utils/AppInit");
var StatusBar = brackets.getModule("widgets/StatusBar");
//todo: only lint support file extension
//todo: readd scrolling code
//todo: save enabled settings and update menu
//todo: move jslint source to Linter extension
require("../../../thirdparty/jslint/jslint");
var DOCUMENT_SAVED = "documentSaved";
var CURRENT_DOCUMENT_CHANGED = "currentDocumentChange";
var LINTER = "LINTER";
//whether or not linting is enabled
var enabled = true;
var ignoreEmptyLines = true;
//whether or not empty lines should be ignore. If false
//linting will report errors for empty lines
var ignoreEmptyLines = false;
//whether errors should be written to the console, in addition to
//the error panel
var outputErrorsToConsole = false;
var statusIcon = $("#gold-star");
var outputTemplate;
var currentErrors;
//precompile the regex.
var extRegEx = /^(\.js|\.json\.css\.htm|\.html)$/i;
function _isSupportedExtension(document) {
var ext = document ? PathUtils.filenameExtension(document.file.fullPath) : "";
return extRegEx.test(ext);
}
function _stripEmptyLines(text) {
//todo: precompile this regex
//todo: check this works with multiple line endings
@@ -55,117 +65,116 @@ define(function (require, exports, module) {
}
return arr.join("\n");
}
function _lintRowClickHandler(e) {
var index = parseInt($(e.currentTarget).data("index"), 10);
var error = currentErrors[index];
var editor = EditorManager.getCurrentFullEditor();
editor.setCursorPos(error.line - 1, error.character - 1);
EditorManager.focusEditor();
}
function _displayLintErrors(errors) {
currentErrors = errors;
if (!errors) {
statusIcon.hide();
$(".lint-error-row").off("click", _lintRowClickHandler);
BottomPanel.clearContent();
//BottomPanel.close();
return;
}
var len = errors.length;
var i;
if (outputErrorsToConsole) {
var error;
for (i = 0; i < len; i++) {
error = errors[i];
console.log(error.line + "\t" + error.reason + "\t" + error.evidence);
}
}
var errorData = [];
for (i = 0; i < len; i++) {
errorData.push({error: errors[i], index: i});
}
var context = {
errors: errorData,
JSLINT_ERRORS: Strings.JSLINT_ERRORS
};
if (!outputTemplate) {
//if the Handlebars template hasnt been compiled yet, compile it
//and then cache it for future use (for performance).
require("Handlebars/handlebars");
var outputTemplateSource = require("text!erroroutput.template");
outputTemplate = Handlebars.compile(outputTemplateSource);
}
var output = $(outputTemplate(context));
BottomPanel.loadContent(output);
statusIcon.show();
$(".lint-error-row").on("click", _lintRowClickHandler);
}
/* Runs linting on the specified document, and returns an array of errors found
by the linter
*/
function lintDocument(document) {
if (!_isSupportedExtension(document)) {
return;
}
var text = document.getText();
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) {
_lintCurrentDocument();
}
function _toggleLinting() {
enabled = !enabled;
if (enabled) {
$(DocumentManager).on(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
} else {
$(DocumentManager).off(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
}
}
//todo: externalize this for localization?
//todo: What tense should the menu item be?
CommandManager.register("Enable Linting", LINTER, _toggleLinting);
var menu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);
menu.addMenuDivider();
menu.addMenuItem(LINTER);
@@ -178,10 +187,10 @@ define(function (require, exports, module) {
$(DocumentManager).on(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
_lintCurrentDocument();
}
AppInit.htmlReady(function () {
StatusBar.addIndicator(module.id, $("#gold-star"), false);
});
exports.lintDocument = lintDocument;
});
View
@@ -3,14 +3,14 @@
define(function (require, exports, module) {
'use strict';
var EditorManager = require("editor/EditorManager");
/*
var open = function() {
console.log("open");
};
var close = function() {
console.log("close");
};
@@ -21,15 +21,15 @@ define(function (require, exports, module) {
$("#bottom-panel").hide();
EditorManager.resizeEditor();
}
function loadContent(html) {
clearContent();
$("#bottom-panel").append(html);
$("#bottom-panel").show();
EditorManager.resizeEditor();
}
//exports.open = open;
//exports.close = close;
exports.loadContent = loadContent;
ProTip! Use n and p to navigate between commits in a pull request.