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

Commit

Permalink
Use a boolean preference
Browse files Browse the repository at this point in the history
  • Loading branch information
ficristo committed Apr 5, 2017
1 parent 99479bf commit f959789
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
69 changes: 29 additions & 40 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,28 @@ define(function (require, exports, module) {
TokenUtils = require("utils/TokenUtils"),
ValidationUtils = require("utils/ValidationUtils"),
ViewUtils = require("utils/ViewUtils"),
DeprecationWarning = require("utils/DeprecationWarning"),
MainViewManager = require("view/MainViewManager"),
_ = require("thirdparty/lodash");

/** Editor preferences */
var CLOSE_BRACKETS = "closeBrackets",
CLOSE_TAGS = "closeTags",
DRAG_DROP = "dragDropText",
HIGHLIGHT_MATCHES = "highlightMatches",
LINEWISE_COPY_CUT = "lineWiseCopyCut",
SCROLL_PAST_END = "scrollPastEnd",
SHOW_CURSOR_SELECT = "showCursorWhenSelecting",
SHOW_LINE_NUMBERS = "showLineNumbers",
SMART_INDENT = "smartIndent",
SOFT_TABS = "softTabs",
SPACE_UNITS = "spaceUnits",
STYLE_ACTIVE_LINE = "styleActiveLine",
TAB_SIZE = "tabSize",
UPPERCASE_COLORS = "uppercaseColors",
USE_TAB_CHAR = "useTabChar",
WORD_WRAP = "wordWrap",
INDENT_LINE_COMMENT = "indentLineComment",
COMMENTS = "comments";
var CLOSE_BRACKETS = "closeBrackets",
CLOSE_TAGS = "closeTags",
DRAG_DROP = "dragDropText",
HIGHLIGHT_MATCHES = "highlightMatches",
LINEWISE_COPY_CUT = "lineWiseCopyCut",
SCROLL_PAST_END = "scrollPastEnd",
SHOW_CURSOR_SELECT = "showCursorWhenSelecting",
SHOW_LINE_NUMBERS = "showLineNumbers",
SMART_INDENT = "smartIndent",
SOFT_TABS = "softTabs",
SPACE_UNITS = "spaceUnits",
STYLE_ACTIVE_LINE = "styleActiveLine",
TAB_SIZE = "tabSize",
UPPERCASE_COLORS = "uppercaseColors",
USE_TAB_CHAR = "useTabChar",
WORD_WRAP = "wordWrap",
INDENT_LINE_COMMENT = "indentLineComment",
INDENT_BLOCK_COMMENT = "indentBlockComment";

/**
* A list of gutter name and priorities currently registered for editors.
Expand Down Expand Up @@ -230,15 +229,8 @@ define(function (require, exports, module) {
description: Strings.DESCRIPTION_INDENT_LINE_COMMENT
});

PreferencesManager.definePreference(COMMENTS, "object", { indent: false }, {
description: Strings.DESCRIPTION_COMMENTS,
keys: {
indent: {
type: "boolean",
description: Strings.DESCRIPTION_COMMENTS_INDENT,
initial: false
}
}
PreferencesManager.definePreference(INDENT_BLOCK_COMMENT, "boolean", false, {
description: Strings.DESCRIPTION_INDENT_BLOCK_COMMENT
});

var editorOptions = Object.keys(cmOptions);
Expand Down Expand Up @@ -2695,48 +2687,45 @@ define(function (require, exports, module) {
};

/**
* Sets lineCommentIndent option.
*
* @deprecated
* Sets indentLineComment option.
* Affects any editors that share the same preference location.
* @param {boolean} value
* @param {string=} fullPath Path to file to get preference for
* @return {boolean} true if value was valid
*/
Editor.setIndentLineComment = function (value, fullPath) {
DeprecationWarning.deprecationWarning("Editor.setIndentLineComment has been deprecated. Use Editor.setCommentsPreferences instead.");
var options = fullPath && {context: fullPath};
return PreferencesManager.set(INDENT_LINE_COMMENT, value, options);
};

/**
* Returns true if lineCommentIndent is enabled for the specified or current file
* Returns true if indentLineComment is enabled for the specified or current file
* @param {string=} fullPath Path to file to get preference for
* @return {boolean}
*/
Editor.getIndentLineComment = function (fullPath) {
DeprecationWarning.deprecationWarning("Editor.getIndentLineComment has been deprecated. Use Editor.getCommentsPreferences instead.");
return PreferencesManager.get(INDENT_LINE_COMMENT, _buildPreferencesContext(fullPath));
};

/**
* Sets comments option.
* Returns true if blockCommentIndent is enabled for the specified or current file
* Affects any editors that share the same preference location.
* @param {boolean} value
* @param {string=} fullPath Path to file to get preference for
* @return {boolean} true if value was valid
*/
Editor.setCommentsPreferences = function (value, fullPath) {
Editor.setIndentBlockComment = function (value, fullPath) {
var options = fullPath && {context: fullPath};
return PreferencesManager.set(COMMENTS, value, options);
return PreferencesManager.set(INDENT_BLOCK_COMMENT, value, options);
};

/**
* Returns the comments preferences for the specified or current file
* Returns true if indentBlockComment is enabled for the specified or current file
* @param {string=} fullPath Path to file to get preference for
* @return {boolean}
*/
Editor.getCommentsPreferences = function (fullPath) {
return PreferencesManager.get(COMMENTS, _buildPreferencesContext(fullPath));
Editor.getIndentBlockComment = function (fullPath) {
return PreferencesManager.get(INDENT_BLOCK_COMMENT, _buildPreferencesContext(fullPath));
};

/**
Expand Down
16 changes: 7 additions & 9 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,10 @@ define(function (require, exports, module) {
var originalCursorPosition = line.search(/\S|$/);

var firstCharPosition, cursorPosition = originalCursorPosition;

var commentsPref = Editor.getCommentsPreferences();


for (i = startLine; i <= endLine; i++) {
//check if preference for indent line comment is available otherwise go back to default indentation
if (commentsPref.indent || Editor.getIndentLineComment()) {
if (Editor.getIndentLineComment()) {
//ignore the first line and recalculate cursor position for first non white space char of every line
if (i !== startLine) {
line = doc.getLine(i);
Expand Down Expand Up @@ -363,7 +361,7 @@ define(function (require, exports, module) {

var searchCtx, atSuffix, suffixEnd, initialPos, endLine;

var commentsPref = Editor.getCommentsPreferences();
var indentBlockComment = Editor.getIndentBlockComment();

if (!selectionsToTrack) {
// Track the original selection.
Expand Down Expand Up @@ -489,7 +487,7 @@ define(function (require, exports, module) {
var completeLineSel = sel.start.ch === 0 && sel.end.ch === 0 && sel.start.line < sel.end.line;
var startCh = _firstNotWs(doc, sel.start.line);
if (completeLineSel) {
if (commentsPref.indent) {
if (indentBlockComment) {
var endCh = _firstNotWs(doc, sel.end.line - 1);
editGroup.push({
text: _.repeat(" ", endCh) + suffix + "\n",
Expand All @@ -505,7 +503,7 @@ define(function (require, exports, module) {
}
} else {
editGroup.push({text: suffix, start: sel.end});
if (commentsPref.indent) {
if (indentBlockComment) {
editGroup.push({text: prefix, start: { line: sel.start.line, ch: startCh }});
} else {
editGroup.push({text: prefix, start: sel.start});
Expand Down Expand Up @@ -550,14 +548,14 @@ define(function (require, exports, module) {
// If both are found we assume that a complete line selection comment added new lines, so we remove them.
var line = doc.getLine(prefixPos.line).trim(),
prefixAtStart = prefixPos.ch === 0 && prefix.length === line.length,
prefixIndented = commentsPref.indent && prefix.length === line.length,
prefixIndented = indentBlockComment && prefix.length === line.length,
suffixAtStart = false,
suffixIndented = false;

if (suffixPos) {
line = doc.getLine(suffixPos.line).trim();
suffixAtStart = suffixPos.ch === 0 && suffix.length === line.length;
suffixIndented = commentsPref.indent && suffix.length === line.length;
suffixIndented = indentBlockComment && suffix.length === line.length;
}

// Remove the suffix if there is one
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 @@ -795,6 +795,7 @@ define({
"DEFAULT_PREFERENCES_JSON_DEFAULT" : "Default",
"DESCRIPTION_PURE_CODING_SURFACE" : "true to enable code only mode and hide all other UI elements in {APP_NAME}",
"DESCRIPTION_INDENT_LINE_COMMENT" : "true to enable indenting of line comments",
"DESCRIPTION_INDENT_BLOCK_COMMENT" : "true to enable indenting of block comments",
"DESCRIPTION_RECENT_FILES_NAV" : "Enable/disable navigation in recent files",
"DESCRIPTION_LIVEDEV_WEBSOCKET_PORT" : "Port on which WebSocket Server runs for Live Preview"
});
8 changes: 4 additions & 4 deletions test/spec/EditorCommandHandlers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ define(function (require, exports, module) {
require("editor/EditorCommandHandlers");

var shouldIndentLineComment = Editor.getIndentLineComment(),
commentsPrefDefault = Editor.getCommentsPreferences();
shouldIndentBlockComment = Editor.getIndentBlockComment();

describe("EditorCommandHandlers", function () {

Expand Down Expand Up @@ -936,7 +936,7 @@ define(function (require, exports, module) {
});
});

describe("Line comment/uncomment in languages with only block comments and with comments.indent enabled", function () {
describe("Line comment/uncomment in languages with only block comments and with indentBlockComment enabled", function () {
var htmlContent = "<html>\n" +
" <body>\n" +
" <p>Hello</p>\n" +
Expand All @@ -945,11 +945,11 @@ define(function (require, exports, module) {

beforeEach(function () {
setupFullEditor(htmlContent, "html");
PreferencesManager.set("comments", { indent: true });
PreferencesManager.set("indentBlockComment", true);
});

afterEach(function () {
PreferencesManager.set("comments", commentsPrefDefault);
PreferencesManager.set("indentBlockComment", shouldIndentBlockComment);
});

it("should comment/uncomment a single line, cursor at start", function () {
Expand Down

0 comments on commit f959789

Please sign in to comment.