Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Added smart indent and auto close tags as preferences #6888

Merged
merged 2 commits into from
Feb 20, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,40 @@ define(function (require, exports, module) {
_ = require("thirdparty/lodash");

/** Editor preferences */
var USE_TAB_CHAR = "useTabChar",
var SMART_INDENT = "smartIndent",
USE_TAB_CHAR = "useTabChar",
TAB_SIZE = "tabSize",
SPACE_UNITS = "spaceUnits",
CLOSE_BRACKETS = "closeBrackets",
SHOW_LINE_NUMBERS = "showLineNumbers",
STYLE_ACTIVE_LINE = "styleActiveLine",
WORD_WRAP = "wordWrap",
CLOSE_TAGS = "closeTags",
cmOptions = {};

// Mappings from Brackets preferences to CodeMirror options
cmOptions[SMART_INDENT] = "smartIndent";
cmOptions[USE_TAB_CHAR] = "indentWithTabs";
cmOptions[TAB_SIZE] = "indentUnit";
cmOptions[SPACE_UNITS] = "indentUnit";
cmOptions[CLOSE_BRACKETS] = "autoCloseBrackets";
cmOptions[SHOW_LINE_NUMBERS] = "lineNumbers";
cmOptions[STYLE_ACTIVE_LINE] = "styleActiveLine";
cmOptions[WORD_WRAP] = "lineWrapping";
cmOptions[CLOSE_TAGS] = "autoCloseTags";

PreferencesManager.definePreference(SMART_INDENT, "boolean", true);
PreferencesManager.definePreference(USE_TAB_CHAR, "boolean", false);
PreferencesManager.definePreference(TAB_SIZE, "number", 4);
PreferencesManager.definePreference(SPACE_UNITS, "number", 4);
PreferencesManager.definePreference(CLOSE_BRACKETS, "boolean", false);
PreferencesManager.definePreference(SHOW_LINE_NUMBERS, "boolean", true);
PreferencesManager.definePreference(STYLE_ACTIVE_LINE, "boolean", false);
PreferencesManager.definePreference(WORD_WRAP, "boolean", true);
PreferencesManager.definePreference(CLOSE_TAGS, "Object", { whenOpening: true, whenClosing: true, indentTags: [] });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little uncertain about having this be an object pref vs. a boolean or some combination of other prefs. It makes this a bit more complex to use. That said, I looked at closetag.js and seeing how these options work, I can imagine people reasonably wanting to tweak these different values.

Aside: I wonder why we override indentTags?

One possibility is to start with just a closeTags boolean that's either all on (the definition above) or all off and then define other (less likely to be used) preferences to customize the behavior further. I could imagine indentTags being a separate pref, for example.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I just wanted to add whenOpening and whenClosing as boolean prefs, since those are the useful ones. People would like to disable one but not both and other disable both. But I wasn't sure how it would work when updating the preference in CodeMirror. I guess if I go that way it would require a special case to handle it.

So I am ok in using 2 boolean pref and a special case to handle the on change event.

We overwrite indentTags because if we don't then when you have <div| and write a > it auto-completes and indents the tag becoming:

<div>
    |
</div>

Which is really annoying, but some people might like it. So you can define for which tags this behavior is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dangoor Want me to do that instead of using the object?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomMalbran Sorry I didn't get back to this earlier. Given your description, I'd say we can just leave it as an object. We may make some changes when we do the UI, but I don't think it's worth adding a bunch of special case code at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Just trying to make sure to get this into Release 37 :)

I guess the UI might be a problem. A simple UI where we just edit a JavaScript Object with lots of comments and all the possible preference would just work fine. A more complex ones with inputs should have some sort of API where each module can add new input lines for each preference to add to the UI, and that API could handle inputs as objects.


var editorOptions = [USE_TAB_CHAR, TAB_SIZE, SPACE_UNITS, CLOSE_BRACKETS,
SHOW_LINE_NUMBERS, STYLE_ACTIVE_LINE, WORD_WRAP];
var editorOptions = [SMART_INDENT, USE_TAB_CHAR, TAB_SIZE, SPACE_UNITS, CLOSE_BRACKETS,
SHOW_LINE_NUMBERS, STYLE_ACTIVE_LINE, WORD_WRAP, CLOSE_TAGS];

/** Editor preferences */

Expand Down Expand Up @@ -381,6 +387,7 @@ define(function (require, exports, module) {
// (note: CodeMirror doesn't actually require using 'new', but jslint complains without it)
this._codeMirror = new CodeMirror(container, {
electricChars: false, // we use our own impl of this to avoid CodeMirror bugs; see _checkElectricChars()
smartIndent: currentOptions[SMART_INDENT],
indentWithTabs: currentOptions[USE_TAB_CHAR],
tabSize: currentOptions[TAB_SIZE],
indentUnit: currentOptions[USE_TAB_CHAR] ? currentOptions[TAB_SIZE] : currentOptions[SPACE_UNITS],
Expand All @@ -393,11 +400,7 @@ define(function (require, exports, module) {
dragDrop: false,
extraKeys: codeMirrorKeyMap,
autoCloseBrackets: currentOptions[CLOSE_BRACKETS],
autoCloseTags: {
whenOpening: true,
whenClosing: true,
indentTags: []
},
autoCloseTags: currentOptions[CLOSE_TAGS],
cursorScrollMargin: 3
});

Expand Down