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.
+2,222 −262
Diff settings

Always

Just for now

View
@@ -75,7 +75,6 @@ define(function (require, exports, module) {
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
CodeHintManager = require("editor/CodeHintManager"),
JSLintUtils = require("language/JSLintUtils"),
PerfUtils = require("utils/PerfUtils"),
FileIndexManager = require("project/FileIndexManager"),
QuickOpen = require("search/QuickOpen"),
@@ -91,8 +90,8 @@ define(function (require, exports, module) {
UrlParams = require("utils/UrlParams").UrlParams,
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
PreferencesManager = require("preferences/PreferencesManager"),
StatusBar = require("widgets/Statusbar");
StatusBar = require("widgets/Statusbar"),
BottomPanel = require("widgets/BottomPanel");
// Local variables
var params = new UrlParams(),
PREFERENCES_CLIENT_ID = "com.adobe.brackets.startup";
@@ -139,7 +138,6 @@ define(function (require, exports, module) {
EditorManager : EditorManager,
Commands : Commands,
WorkingSetView : WorkingSetView,
JSLintUtils : JSLintUtils,
PerfUtils : PerfUtils,
JSUtils : JSUtils,
CommandManager : require("command/CommandManager"),
View
@@ -72,7 +72,6 @@ define(function (require, exports, module) {
exports.VIEW_INCREASE_FONT_SIZE = "view.increaseFontSize";
exports.VIEW_DECREASE_FONT_SIZE = "view.decreaseFontSize";
exports.VIEW_RESTORE_FONT_SIZE = "view.restoreFontSize";
exports.TOGGLE_JSLINT = "debug.jslint";
// Navigate
exports.NAVIGATE_NEXT_DOC = "navigate.nextDoc";
View
@@ -903,8 +903,6 @@ define(function (require, exports, module) {
menu.addMenuItem(Commands.VIEW_INCREASE_FONT_SIZE, [{key: "Ctrl-=", displayKey: "Ctrl-+"}]);
menu.addMenuItem(Commands.VIEW_DECREASE_FONT_SIZE, [{key: "Ctrl--", displayKey: "Ctrl-\u2212"}]);
menu.addMenuItem(Commands.VIEW_RESTORE_FONT_SIZE, "Ctrl-0");
menu.addMenuDivider();
menu.addMenuItem(Commands.TOGGLE_JSLINT);
/*
* Navigate menu

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,18 @@
<div class="toolbar simple-toolbar-layout">
<div class="title">{{JSLINT_ERRORS}}</div>
</div>
<div class="table-container">
<table class="zebra-striped condensed-table" id="jslint-error-tab">
<tbody>
{{#each errors}}
<tr class="lint-error-row" data-index="{{this.index}}">
<td>{{this.error.line}}</td>
<td>{{this.error.reason}}</td>
<td>{{this.error.evidence}}</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
@@ -0,0 +1,15 @@
.lint-valid {
font-size: 1em;
color: limegreen;
/*color: lighten(@bc-yellow, @bc-color-step-size*2);*/
}
.lint-disabled {
font-size: 1em;
color: #808080; /*@bc-grey*/
}
.lint-errors {
font-size: 1em;
color: #dc322f/*@bc-red*/;
}
@@ -0,0 +1,228 @@
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, JSLINT, Handlebars, AppInit, StatusBar, PathUtils */
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");
var EditorManager = brackets.getModule("editor/EditorManager");
var Strings = brackets.getModule("strings");
var BottomPanel = brackets.getModule("widgets/BottomPanel");
var AppInit = brackets.getModule("utils/AppInit");
var StatusBar = brackets.getModule("widgets/StatusBar");
var PreferencesManager = brackets.getModule("preferences/PreferencesManager");
var StringUtils = brackets.getModule("utils/StringUtils");
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
//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 id = "brackets.linter";
var LINTER = "LINTER";
var _prefs;
var command;
//whether or not linting is enabled
var enabled = 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
//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 _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) {
console.log("lint-valid");
StatusBar.updateIndicator(id, true, "lint-valid", Strings.JSLINT_NO_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();
var errorString = (len === 1) ? Strings.JSLINT_ERROR_INFORMATION : StringUtils.format(Strings.JSLINT_ERRORS_INFORMATION, len);
console.log("lint-errors");
StatusBar.updateIndicator(id, true, "lint-errors", errorString);
$(".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) {
var text = document.getText();
if (ignoreEmptyLines) {
text = _stripEmptyLines(text);
}
var hasErrors = !JSLINT(text, null);
if (!hasErrors) {
return;
}
return JSLINT.errors;
}
function _lintCurrentDocument() {
var document = DocumentManager.getCurrentDocument();
if (!_isSupportedExtension(document)) {
console.log("lint-disabled");
StatusBar.updateIndicator(id, true, "lint-disabled", Strings.JSLINT_DISABLED);
return;
}
var errors = lintDocument(document);
_displayLintErrors(errors);
}
function _onDocumentUpdated(event) {
_lintCurrentDocument();
}
function _initPrefs() {
_prefs = PreferencesManager.getPreferenceStorage(id, { enabled: true });
enabled = _prefs.getValue("enabled");
command.setChecked(enabled);
}
function _initMenu() {
var menu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);
menu.addMenuDivider();
menu.addMenuItem(LINTER);
}
function _toggleLinting() {
enabled = !enabled;
command.setChecked(enabled);
_prefs.setValue("enabled", enabled);
if (enabled) {
$(DocumentManager).on(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
} else {
$(DocumentManager).off(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
console.log("lint-disabled");
StatusBar.updateIndicator(id, true, "lint-disabled", Strings.JSLINT_DISABLED);
}
}
//todo: externalize this for localization?
//todo: What tense should the menu item be?
command = CommandManager.register("Enable Linting", LINTER, _toggleLinting);
_initMenu();
_initPrefs();
AppInit.htmlReady(function () {
ExtensionUtils.loadStyleSheet(module, "linter.css");
StatusBar.addIndicator(id, statusIcon, true);
if (enabled) {
$(DocumentManager).on(DOCUMENT_SAVED + " " + CURRENT_DOCUMENT_CHANGED, _onDocumentUpdated);
_lintCurrentDocument();
}
});
exports.lintDocument = lintDocument;
});
@@ -89,13 +89,8 @@
<div id="not-editor">
</div>
</div>
<div id="jslint-results" class="bottom-panel">
<div class="toolbar simple-toolbar-layout">
<div class="title">{{JSLINT_ERRORS}}</div>
</div>
<div class="table-container"></div>
</div>
<div id="bottom-panel" class="bottom-panel"></div>
<div id="search-results" class="bottom-panel">
<div class="toolbar simple-toolbar-layout">
<div class="title">{{SEARCH_RESULTS}}</div>
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.