Skip to content

Commit

Permalink
Merge pull request adobe#11954 from borax12/lineCommentBugFix
Browse files Browse the repository at this point in the history
Line comment bug fix
  • Loading branch information
abose committed Feb 13, 2016
2 parents 57e18b9 + c8b3b5f commit 8e49f35
Show file tree
Hide file tree
Showing 4 changed files with 542 additions and 52 deletions.
32 changes: 30 additions & 2 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ define(function (require, exports, module) {
TAB_SIZE = "tabSize",
UPPERCASE_COLORS = "uppercaseColors",
USE_TAB_CHAR = "useTabChar",
WORD_WRAP = "wordWrap";
WORD_WRAP = "wordWrap",
INDENT_LINE_COMMENT = "indentLineComment";


var cmOptions = {};

Expand Down Expand Up @@ -207,7 +209,12 @@ define(function (require, exports, module) {
PreferencesManager.definePreference(WORD_WRAP, "boolean", true, {
description: Strings.DESCRIPTION_WORD_WRAP
});


PreferencesManager.definePreference(INDENT_LINE_COMMENT, "boolean", false, {
description: Strings.DESCRIPTION_INDENT_LINE_COMMENT
});


var editorOptions = Object.keys(cmOptions);

/** Editor preferences */
Expand Down Expand Up @@ -2514,6 +2521,27 @@ define(function (require, exports, module) {
return PreferencesManager.get(WORD_WRAP, _buildPreferencesContext(fullPath));
};

/**
* Sets lineCommentIndent option.
*
* @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) {
var options = fullPath && {context: fullPath};
return PreferencesManager.set(INDENT_LINE_COMMENT, value, options);
};

/**
* Returns true if word wrap is enabled for the specified or current file
* @param {string=} fullPath Path to file to get preference for
* @return {boolean}
*/
Editor.getIndentLineComment = function (fullPath) {
return PreferencesManager.get(INDENT_LINE_COMMENT, _buildPreferencesContext(fullPath));
};

/**
* Runs callback for every Editor instance that currently exists
* @param {!function(!Editor)} callback
Expand Down
25 changes: 24 additions & 1 deletion src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ define(function (require, exports, module) {
// Load dependent modules
var Commands = require("command/Commands"),
Strings = require("strings"),
Editor = require("editor/Editor").Editor,
CommandManager = require("command/CommandManager"),
EditorManager = require("editor/EditorManager"),
StringUtils = require("utils/StringUtils"),
Expand Down Expand Up @@ -206,8 +207,30 @@ define(function (require, exports, module) {

if (containsNotLineComment) {
// Comment out - prepend the first prefix to each line
line = doc.getLine(startLine);
var originalCursorPosition = line.search(/\S|$/);

var firstCharPosition,cursorPosition = originalCursorPosition;

for (i = startLine; i <= endLine; i++) {
editGroup.push({text: prefixes[0], start: {line: i, ch: 0}});
//check if preference for indent line comment is available otherwise go back to default indentation
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);
firstCharPosition = line.search(/\S|$/);
}
//if the non space first character position is before original start position , put comment at the new position otherwise older pos
if (firstCharPosition < originalCursorPosition) {
cursorPosition = firstCharPosition;
} else {
cursorPosition = originalCursorPosition;
}

editGroup.push({text: prefixes[0], start: {line: i, ch: cursorPosition}});
} else {
editGroup.push({text: prefixes[0], start: {line: i, ch: 0}});
}
}

// Make sure tracked selections include the prefix that was added at start of range
Expand Down
3 changes: 2 additions & 1 deletion src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,5 +778,6 @@ define({
"DESCRIPTION_SHOW_PANE_HEADER_BUTTONS" : "Toggle when to show the close and flip-view buttons on the header.",
"DEFAULT_PREFERENCES_JSON_HEADER_COMMENT" : "/*\n * This is a read-only file with the preferences supported\n * by {APP_NAME}.\n * Use this file as a reference to modify your preferences\n * file \"brackets.json\" opened in the other pane.\n * For more information on how to use preferences inside\n * {APP_NAME}, refer to the web page at https://github.com/adobe/brackets/wiki/How-to-Use-Brackets#preferences\n */",
"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_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"
});
Loading

0 comments on commit 8e49f35

Please sign in to comment.