├── .gitmodules ├── .editorconfig ├── package.json ├── README.md ├── node └── domain.js └── main.js /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "node/editorconfig"] 2 | path = node/lib 3 | url = https://github.com/editorconfig/editorconfig-core-js.git 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 4 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "EditorConfig", 3 | "name": "brackets-editorconfig", 4 | "description": "Supporting EditorConfig features", 5 | "keywords": ["editorconfig", "whitespace", "indentation", "newline"], 6 | "version": "0.0.4", 7 | "author": "Chen-Heng Chang", 8 | "homepage": "http://editorconfig.org/", 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/kidwm/brackets-editorconfig.git" 12 | }, 13 | "engines": { 14 | "brackets": ">=1.1.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EditorConfig for Brackets 2 | 3 | > [EditorConfig](http://editorconfig.org) helps developers maintain consistent coding styles between different editors. 4 | 5 | Based on @MiguelCastillo's [Brackets-wsSanitizer](https://github.com/MiguelCastillo/Brackets-wsSanitizer) and utilizing [EditorConfig JavaScript Core](https://github.com/editorconfig/editorconfig-core-js) library. 6 | 7 | ## Supported properties 8 | 9 | - root 10 | - indent_style 11 | - indent_size 12 | - trim_trailing_whitespace 13 | - insert_final_newline 14 | 15 | 16 | ## Features 17 | 18 | - Applies the above settings from your `.editorconfig` file 19 | - Syntax highlights `.editorconfig` files 20 | 21 | 22 | ## License 23 | 24 | [MIT](http://opensource.org/licenses/MIT) © [Chen-Heng Chang](http://kidwm.net) 25 | -------------------------------------------------------------------------------- /node/domain.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | "use strict"; 3 | var editorconfig = require("./lib/editorconfig"); 4 | function parse(path) { 5 | return editorconfig.parse(path); 6 | } 7 | /** 8 | * Initializes the test domain with several test commands. 9 | * @param {DomainManager} domainManager The DomainManager for the server 10 | */ 11 | function init(domainManager) { 12 | if (!domainManager.hasDomain("editorconfig")) { 13 | domainManager.registerDomain("editorconfig", { 14 | major: 0, 15 | minor: 1 16 | }); 17 | } 18 | domainManager.registerCommand( 19 | "editorconfig", // domain name 20 | "parse", // command name 21 | parse, // command handler function 22 | false, // this command is synchronous in Node 23 | "Returns the editorconfig settings", [{ 24 | name: "path", // parameters 25 | type: "string", 26 | description: "the file path" 27 | }], [{ 28 | name: "config", // return values 29 | type: "json", 30 | description: "editorconfig values in json format" 31 | }] 32 | ); 33 | } 34 | exports.init = init; 35 | }()); 36 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | /** 2 | * EditorConfig for Brackets Copyright (c) 2014 Chen-Heng Chang. 3 | * 4 | * Licensed under MIT 5 | * 6 | * Based on https://github.com/MiguelCastillo/Brackets-wsSanitizer 7 | */ 8 | 9 | 10 | define(function (require, exports, module) { 11 | 'use strict'; 12 | 13 | var CommandManager = brackets.getModule('command/CommandManager'), 14 | Commands = brackets.getModule('command/Commands'), 15 | DocumentManager = brackets.getModule('document/DocumentManager'), 16 | Editor = brackets.getModule('editor/Editor').Editor, 17 | MainViewManager = brackets.getModule('view/MainViewManager'), 18 | Menus = brackets.getModule('command/Menus'), 19 | PreferencesManager = brackets.getModule('preferences/PreferencesManager'), 20 | AppInit = brackets.getModule("utils/AppInit"), 21 | LanguageManager = brackets.getModule("language/LanguageManager"), 22 | ExtensionUtils = brackets.getModule("utils/ExtensionUtils"), 23 | NodeDomain = brackets.getModule("utils/NodeDomain"), 24 | PREFERENCES_KEY = 'brackets-editorconfig', 25 | prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY); 26 | 27 | var _prefLocation = { 28 | location: { 29 | scope: "session" 30 | } 31 | }; 32 | var configDomain = new NodeDomain("editorconfig", ExtensionUtils.getModulePath(module, "node/domain")); 33 | var trim_trailing_whitespace = false; 34 | var insert_final_newline = false; 35 | 36 | LanguageManager.defineLanguage("editorconfig", { 37 | name: "EditorConfig", 38 | mode: "properties", 39 | fileNames: [".editorconfig"] 40 | }); 41 | 42 | // Set default value 43 | prefs.definePreference("enabled", "boolean", "true"); 44 | 45 | // Set up the menu and callback for it 46 | (function() { 47 | var COMMAND_ID = PREFERENCES_KEY, 48 | menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU), 49 | command = CommandManager.register('EditorConfig', COMMAND_ID, setEnabled); 50 | 51 | menu.addMenuDivider(); 52 | menu.addMenuItem(COMMAND_ID); 53 | 54 | function setEnabled() { 55 | var enabled = !command.getChecked(); 56 | command.setChecked(enabled); 57 | prefs.set('enabled', enabled); 58 | DocumentManager[enabled ? 'on' : 'off']('documentSaved', sanitize); 59 | MainViewManager[enabled ? 'on' : 'off']("currentFileChange", apply); 60 | if (enabled) apply(); 61 | } 62 | 63 | command.setChecked(!prefs.get('enabled')); 64 | setEnabled(); 65 | })(); 66 | 67 | 68 | function sanitize(event, doc) { 69 | doc.batchOperation(function () { 70 | var line, pattern, match; 71 | var lineIndex = 0, 72 | wsPattern = getReplacePattern(Editor); 73 | 74 | //trim trailing whitespaces 75 | while ((line = doc.getLine(lineIndex)) !== undefined) { 76 | pattern = /[ \t]+$/g; 77 | match = pattern.exec(line); 78 | if (trim_trailing_whitespace && match) { 79 | doc.replaceRange( 80 | '', 81 | {line: lineIndex, ch: match.index}, 82 | {line: lineIndex, ch: pattern.lastIndex} 83 | ); 84 | } 85 | /* 86 | match = wsPattern.sanitizeLine(line); 87 | if ( match.replaceWith ) { 88 | doc.replaceRange( 89 | match.replaceWith, 90 | {line: lineIndex, ch: match.start}, 91 | {line: lineIndex, ch: match.end} 92 | ); 93 | } 94 | */ 95 | lineIndex += 1; 96 | } 97 | 98 | //ensure newline at the end of file 99 | line = doc.getLine(lineIndex - 1); 100 | if (insert_final_newline && line !== undefined && line.length > 0 && line.slice(-1) !== '\n') { 101 | doc.replaceRange( 102 | '\n', 103 | {line: lineIndex, ch: line.slice(-1)} 104 | ); 105 | } 106 | }); 107 | 108 | CommandManager.execute(Commands.FILE_SAVE, {doc: doc}); 109 | } 110 | 111 | 112 | function getReplacePattern(editor) { 113 | var pattern = editor.getUseTabChar() ? { 114 | units: editor.getTabSize(), 115 | matchPattern: /^[ ]+/g, 116 | replaceWith: '\t', 117 | getIndent: function(length) { 118 | return Math.round(length / pattern.units); 119 | } 120 | }: { 121 | units: editor.getSpaceUnits(), 122 | matchPattern: /^[\t]+/g, 123 | replaceWith: ' ', 124 | getIndent: function(length) { 125 | return length * pattern.units; 126 | } 127 | }; 128 | 129 | 130 | function sanitizeLine(line) { 131 | var regMatch = line.match(pattern.matchPattern); 132 | var matches = (regMatch || [''])[0]; 133 | var indent = pattern.getIndent(matches.length); 134 | 135 | return { 136 | replaceWith: new Array(indent + 1).join(pattern.replaceWith), 137 | start: 0, 138 | end: matches.length 139 | }; 140 | } 141 | 142 | return { 143 | sanitizeLine: sanitizeLine 144 | }; 145 | } 146 | 147 | function apply() { 148 | trim_trailing_whitespace = false; 149 | insert_final_newline = false; 150 | if (DocumentManager.getCurrentDocument()) 151 | configDomain.exec("parse", DocumentManager.getCurrentDocument().file.fullPath) 152 | .done(function (config) { 153 | if (JSON.stringify(config) !== "{}") { 154 | PreferencesManager.set("useTabChar", (config.indent_style === 'tab' ) ? true : false, _prefLocation); 155 | if (config.indent_style === 'tab' && config.indent_size) { 156 | PreferencesManager.set("tabSize", config.indent_size, _prefLocation); 157 | } 158 | if (config.indent_style === 'space' && config.indent_size) { 159 | PreferencesManager.set("spaceUnits", config.indent_size, _prefLocation); 160 | } 161 | trim_trailing_whitespace = config.trim_trailing_whitespace ? true : false; 162 | insert_final_newline = config.insert_final_newline ? true : false; 163 | } 164 | }).fail(function (err) { 165 | console.error("[brackets-editorconfig] failed to parse configuration", err); 166 | }); 167 | } 168 | }); 169 | --------------------------------------------------------------------------------