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

Saving the tabs settings per file/project #3039

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
167 changes: 94 additions & 73 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,24 @@ define(function (require, exports, module) {
"use strict";

var CodeHintManager = require("editor/CodeHintManager"),
ProjectManager = require("project/ProjectManager"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
Menus = require("command/Menus"),
PerfUtils = require("utils/PerfUtils"),
PreferencesManager = require("preferences/PreferencesManager"),
Strings = require("strings"),
TextRange = require("document/TextRange").TextRange,
CollectionUtils = require("utils/CollectionUtils"),
TokenUtils = require("utils/TokenUtils"),
ViewUtils = require("utils/ViewUtils");

var PREFERENCES_CLIENT_ID = "com.adobe.brackets.Editor",
defaultPrefs = { useTabChar: false, tabSize: 4, indentUnit: 4, closeBrackets: false };
defaultPrefs = { closeBrackets: false };

/** Editor preferences */
var _prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_CLIENT_ID, defaultPrefs);

/** @type {boolean} Global setting: When inserting new text, use tab characters? (instead of spaces) */
var _useTabChar = _prefs.getValue("useTabChar");

/** @type {number} Global setting: Tab size */
var _tabSize = _prefs.getValue("tabSize");

/** @type {number} Global setting: Indent unit (i.e. number of spaces when indenting) */
var _indentUnit = _prefs.getValue("indentUnit");

/** @type {boolean} Global setting: Auto closes (, {, [, " and ' */
var _closeBrackets = _prefs.getValue("closeBrackets");

Expand Down Expand Up @@ -143,7 +136,7 @@ define(function (require, exports, module) {
if (instance.getOption("indentWithTabs")) {
CodeMirror.commands.insertTab(instance);
} else {
var i, ins = "", numSpaces = _indentUnit;
var i, ins = "", numSpaces = instance.getOption("indentUnit");
numSpaces -= to.ch % numSpaces;
for (i = 0; i < numSpaces; i++) {
ins += " ";
Expand All @@ -164,12 +157,13 @@ define(function (require, exports, module) {
function _handleSoftTabNavigation(instance, direction, functionName) {
var handled = false;
if (!instance.getOption("indentWithTabs")) {
var cursor = instance.getCursor(),
jump = cursor.ch % _indentUnit,
var indentUnit = instance.getOption("indentUnit"),
cursor = instance.getCursor(),
jump = cursor.ch % indentUnit,
line = instance.getLine(cursor.line);

if (direction === 1) {
jump = _indentUnit - jump;
jump = indentUnit - jump;

if (cursor.ch + jump > line.length) { // Jump would go beyond current line
return false;
Expand All @@ -188,7 +182,7 @@ define(function (require, exports, module) {
// If we are on the tab boundary, jump by the full amount,
// but not beyond the start of the line.
if (jump === 0) {
jump = _indentUnit;
jump = indentUnit;
}

// Search backwards to the first non-space character
Expand Down Expand Up @@ -341,9 +335,9 @@ 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()
indentWithTabs: _useTabChar,
tabSize: _tabSize,
indentUnit: _indentUnit,
indentWithTabs: this._getTabPreference("indentWithTabs"),
tabSize: this._getTabPreference("tabSize"),
indentUnit: this._getTabPreference("indentUnit"),
lineNumbers: true,
matchBrackets: true,
dragDrop: false, // work around issue #1123
Expand Down Expand Up @@ -694,7 +688,7 @@ define(function (require, exports, module) {

if (expandTabs) {
var line = this._codeMirror.getRange({line: cursor.line, ch: 0}, cursor),
tabSize = Editor.getTabSize(),
tabSize = this.getTabSize(),
column = 0,
i;

Expand Down Expand Up @@ -1253,6 +1247,68 @@ define(function (require, exports, module) {
return this._codeMirror.getOption("mode");
};


/**
* @private
* Sets the given preference option to the current editor and saves it
* @param {string} preference
* @param {number|boolean} value
*/
Editor.prototype._setTabPreference = function (preference, value) {
this._codeMirror.setOption(preference, value);
_prefs.setValue(preference + "_" + this.document.file.fullPath, value);
};

/*
* @private
* Returns the value for the given preference key first checking with the file settings and then with the project settings
* @param {string} preference
* @return {number|boolean}
*/
Editor.prototype._getTabPreference = function (preference) {
var value = _prefs.getValue(preference + "_" + this.document.file.fullPath);
return value !== undefined ? value : ProjectManager.getTabSettings()[preference];
};

/**
* Sets whether to use tab characters (vs. spaces) when inserting new text.
*/
Editor.prototype.setUseTabChar = function (value) {
this._setTabPreference("indentWithTabs", Boolean(value));
};

/** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
Editor.prototype.getUseTabChar = function () {
return this._codeMirror.getOption("indentWithTabs");
};

/**
* Sets the tab character width.
* @param {number} value
*/
Editor.prototype.setTabSize = function (value) {
this._setTabPreference("tabSize", value);
};

/** @type {number} Get indent unit */
Editor.prototype.getTabSize = function (value) {
return this._codeMirror.getOption("tabSize");
};

/**
* Sets the indentation width.
* @param {number} value
*/
Editor.prototype.setIndentUnit = function (value) {
this._setTabPreference("indentUnit", value);
};

/** @type {number} Get indentation width */
Editor.prototype.getIndentUnit = function (value) {
return this._codeMirror.getOption("indentUnit");
};


/**
* The Document we're bound to
* @type {!Document}
Expand Down Expand Up @@ -1291,60 +1347,6 @@ define(function (require, exports, module) {
// Global settings that affect all Editor instances (both currently open Editors as well as those created
// in the future)

/**
* Sets whether to use tab characters (vs. spaces) when inserting new text. Affects all Editors.
* @param {boolean} value
*/
Editor.setUseTabChar = function (value) {
_useTabChar = value;
_instances.forEach(function (editor) {
editor._codeMirror.setOption("indentWithTabs", _useTabChar);
});

_prefs.setValue("useTabChar", Boolean(_useTabChar));
};

/** @type {boolean} Gets whether all Editors use tab characters (vs. spaces) when inserting new text */
Editor.getUseTabChar = function () {
return _useTabChar;
};

/**
* Sets tab character width. Affects all Editors.
* @param {number} value
*/
Editor.setTabSize = function (value) {
_tabSize = value;
_instances.forEach(function (editor) {
editor._codeMirror.setOption("tabSize", _tabSize);
});

_prefs.setValue("tabSize", _tabSize);
};

/** @type {number} Get indent unit */
Editor.getTabSize = function () {
return _tabSize;
};

/**
* Sets indentation width. Affects all Editors.
* @param {number} value
*/
Editor.setIndentUnit = function (value) {
_indentUnit = value;
_instances.forEach(function (editor) {
editor._codeMirror.setOption("indentUnit", _indentUnit);
});

_prefs.setValue("indentUnit", _indentUnit);
};

/** @type {number} Get indentation width */
Editor.getIndentUnit = function () {
return _indentUnit;
};

/**
* Sets the auto close brackets. Affects all Editors.
* @param {boolean} value
Expand All @@ -1363,6 +1365,25 @@ define(function (require, exports, module) {
return _closeBrackets;
};


/**
* Changes the Tab Settings of all the opened editors within the project with no saved tab preferences after a
* tab preference change and triggers a StatusBar update
*/
Editor.setProjectTabs = function () {
var preferences = ProjectManager.getTabSettings();
_instances.forEach(function (editor) {
if (ProjectManager.isWithinProject(editor.document.file.fullPath)) {
CollectionUtils.forEach(preferences, function (value, key) {
if (_prefs.getValue(key + "_" + editor.document.file.fullPath) === undefined) {
editor._codeMirror.setOption(key, value);
}
});
}
});
};


// Define public API
exports.Editor = Editor;
exports.BOUNDARY_CHECK_NORMAL = BOUNDARY_CHECK_NORMAL;
Expand Down
30 changes: 23 additions & 7 deletions src/editor/EditorStatusBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ define(function (require, exports, module) {
}

function _updateIndentType() {
var indentWithTabs = Editor.getUseTabChar();
var indentWithTabs = EditorManager.getActiveEditor().getUseTabChar();
$indentType.text(indentWithTabs ? Strings.STATUSBAR_TAB_SIZE : Strings.STATUSBAR_SPACES);
$indentType.attr("title", indentWithTabs ? Strings.STATUSBAR_INDENT_TOOLTIP_SPACES : Strings.STATUSBAR_INDENT_TOOLTIP_TABS);
$indentWidthLabel.attr("title", indentWithTabs ? Strings.STATUSBAR_INDENT_SIZE_TOOLTIP_TABS : Strings.STATUSBAR_INDENT_SIZE_TOOLTIP_SPACES);
}

function _getIndentSize() {
return Editor.getUseTabChar() ? Editor.getTabSize() : Editor.getIndentUnit();
var editor = EditorManager.getActiveEditor();
return editor.getUseTabChar() ? editor.getTabSize() : editor.getIndentUnit();
}

function _updateIndentSize() {
Expand All @@ -75,7 +76,8 @@ define(function (require, exports, module) {
}

function _toggleIndentType() {
Editor.setUseTabChar(!Editor.getUseTabChar());
var editor = EditorManager.getActiveEditor();
editor.setUseTabChar(!editor.getUseTabChar());
_updateIndentType();
_updateIndentSize();
}
Expand Down Expand Up @@ -103,10 +105,11 @@ define(function (require, exports, module) {
return;
}

if (Editor.getUseTabChar()) {
Editor.setTabSize(Math.max(Math.min(value, 10), 1));
var editor = EditorManager.getActiveEditor();
if (editor.getUseTabChar()) {
editor.setTabSize(Math.max(Math.min(value, 10), 1));
} else {
Editor.setIndentUnit(Math.max(Math.min(value, 10), 1));
editor.setIndentUnit(Math.max(Math.min(value, 10), 1));
}

// update indicator
Expand Down Expand Up @@ -177,9 +180,22 @@ define(function (require, exports, module) {

_onActiveEditorChange(null, EditorManager.getActiveEditor(), null);
}


/**
* Updates the Editors tab settings and the StatusBar content
*/
function updateProjectTabs() {
Editor.setProjectTabs();
_updateIndentType();
_updateIndentSize();
}


// Initialize: status bar focused listener
$(EditorManager).on("activeEditorChange", _onActiveEditorChange);

AppInit.htmlReady(_init);


exports.updateProjectTabs = updateProjectTabs;
});
16 changes: 14 additions & 2 deletions src/htmlContent/project-settings-dialog.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<div class="project-settings-dialog modal">
<div class="modal-header">
<h1 class="dialog-title"></h1>
<h1 class="dialog-title">{{title}}</h1>
</div>
<div class="modal-body">
<div class="settings-list">
<label>{{PROJECT_SETTING_BASE_URL}}: <input type="text" placeholder="{{PROJECT_SETTING_BASE_URL_HINT}}" class="base-url" /></label>
<label>{{PROJECT_SETTING_BASE_URL}}: <input type="text" placeholder="{{PROJECT_SETTING_BASE_URL_HINT}}" value="{{baseUrl}}" class="base-url" /></label>
{{#errorMessage}}<div class="alert-message" style="margin-bottom: 0">{{errorMessage}}</div>{{/errorMessage}}
</div>
{{#tabSettings}}
<div class="settings-list">
{{PROJECT_SETTING_TABS}}:
<select class="indent-type">
<option value="tabs" {{#indentWithTabs}}selected{{/indentWithTabs}}>{{STATUSBAR_TAB_SIZE}}</option>
<option value="spaces" {{^indentWithTabs}}selected{{/indentWithTabs}}>{{STATUSBAR_SPACES}}</option>
</select>
{{#indentWithTabs}}<input type="text" value="{{tabSize}}" class="indent-size" />{{/indentWithTabs}}
{{^indentWithTabs}}<input type="text" value="{{indentUnit}}" class="indent-size" />{{/indentWithTabs}}
</div>
{{/tabSettings}}
</div>
<div class="modal-footer">
<a href="#" class="dialog-button btn left" data-button-id="cancel">{{CANCEL}}</a>
Expand Down
1 change: 1 addition & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ define({
"PROJECT_SETTINGS_TITLE" : "Project Settings for: {0}",
"PROJECT_SETTING_BASE_URL" : "Live Preview Base URL",
"PROJECT_SETTING_BASE_URL_HINT" : "(to use a local server, specify url)",
"PROJECT_SETTING_TABS" : "Project Tabs Settings",
"BASEURL_ERROR_INVALID_PROTOCOL" : "The {0} protocol isn't supported by Live Preview&mdash;please use http: or https: .",
"BASEURL_ERROR_SEARCH_DISALLOWED" : "The base URL can't contain search parameters like \"{0}\".",
"BASEURL_ERROR_HASH_DISALLOWED" : "The base URL can't contain hashes like \"{0}\".",
Expand Down
Loading