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

Add new comments preference. Support indent block comments on line co… #13254

Merged
merged 7 commits into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 48 additions & 23 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,27 @@ define(function (require, exports, module) {
_ = 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",
INPUT_STYLE = "inputStyle";

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",
INPUT_STYLE = "inputStyle";


/**
* A list of gutter name and priorities currently registered for editors.
Expand Down Expand Up @@ -225,11 +228,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
});

PreferencesManager.definePreference(INDENT_BLOCK_COMMENT, "boolean", false, {
description: Strings.DESCRIPTION_INDENT_BLOCK_COMMENT
});
PreferencesManager.definePreference(INPUT_STYLE, "string", "textarea", {
description: Strings.DESCRIPTION_INPUT_STYLE
});
Expand Down Expand Up @@ -2688,8 +2692,8 @@ define(function (require, exports, module) {
};

/**
* Sets lineCommentIndent option.
*
* 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
Expand All @@ -2700,14 +2704,35 @@ define(function (require, exports, module) {
};

/**
* Returns true if word wrap 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) {
return PreferencesManager.get(INDENT_LINE_COMMENT, _buildPreferencesContext(fullPath));
};

/**
* Returns true if blockCommentIndent is enabled for the specified or current file
Copy link
Contributor

Choose a reason for hiding this comment

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

First line of this comment is true for "get", but not "set".

* 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.setIndentBlockComment = function (value, fullPath) {
var options = fullPath && {context: fullPath};
return PreferencesManager.set(INDENT_BLOCK_COMMENT, value, options);
};

/**
* 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.getIndentBlockComment = function (fullPath) {
return PreferencesManager.get(INDENT_BLOCK_COMMENT, _buildPreferencesContext(fullPath));
};

/**
* Runs callback for every Editor instance that currently exists
* @param {!function(!Editor)} callback
Expand Down
56 changes: 49 additions & 7 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ define(function (require, exports, module) {
return false;
}

/**
* Return the column of the first non whitespace char in the given line.
*
* @private
* @param {!Document} doc
* @param {number} lineNum
* @returns {number} the column index or null
*/
function _firstNotWs(doc, lineNum) {
var text = doc.getLine(lineNum);
if (text === null || text === undefined) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

The return value from this function is not validated -- it gets used directly as a char position. Seems like it would be more consistent with CodeMirror APIs to return -1 here (but my CM API knowledge is rusty...).

}

return text.search(/\S|$/);
}

/**
* Generates an edit that adds or removes block-comment tokens to the selection, preserving selection
Expand Down Expand Up @@ -345,6 +361,8 @@ define(function (require, exports, module) {

var searchCtx, atSuffix, suffixEnd, initialPos, endLine;

var indentBlockComment = Editor.getIndentBlockComment();

if (!selectionsToTrack) {
// Track the original selection.
selectionsToTrack = [_.cloneDeep(sel)];
Expand Down Expand Up @@ -467,12 +485,29 @@ define(function (require, exports, module) {
// Comment out - add the suffix to the start and the prefix to the end.
if (canComment) {
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) {
editGroup.push({text: suffix + "\n", start: sel.end});
editGroup.push({text: prefix + "\n", start: sel.start});
if (indentBlockComment) {
var endCh = _firstNotWs(doc, sel.end.line - 1);
editGroup.push({
text: _.repeat(" ", endCh) + suffix + "\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't always insert spaces -- need to use indent preference here in case it's Tabs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great catch! Fixed.

start: {line: sel.end.line, ch: 0}
});
editGroup.push({
text: prefix + "\n" + _.repeat(" ", startCh),
start: {line: sel.start.line, ch: startCh}
});
} else {
editGroup.push({text: suffix + "\n", start: sel.end});
editGroup.push({text: prefix + "\n", start: sel.start});
}
} else {
editGroup.push({text: suffix, start: sel.end});
editGroup.push({text: prefix, start: sel.start});
if (indentBlockComment) {
editGroup.push({text: prefix, start: { line: sel.start.line, ch: startCh }});
} else {
editGroup.push({text: prefix, start: sel.start});
}
}

// Correct the tracked selections. We can't just use the default selection fixup,
Expand All @@ -497,7 +532,7 @@ define(function (require, exports, module) {
if (completeLineSel) {
// Just move the line down.
pos.line++;
} else if (pos.line === sel.start.line) {
} else if (!indentBlockComment && pos.line === sel.start.line) {
pos.ch += prefix.length;
}
}
Expand All @@ -513,24 +548,31 @@ 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,
suffixAtStart = false;
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 = indentBlockComment && suffix.length === line.length;
}

// Remove the suffix if there is one
if (suffixPos) {
if (prefixAtStart && suffixAtStart) {
if (prefixIndented) {
editGroup.push({text: "", start: {line: suffixPos.line, ch: 0}, end: {line: suffixPos.line + 1, ch: 0}});
} else if (prefixAtStart && suffixAtStart) {
editGroup.push({text: "", start: suffixPos, end: {line: suffixPos.line + 1, ch: 0}});
} else {
editGroup.push({text: "", start: suffixPos, end: {line: suffixPos.line, ch: suffixPos.ch + suffix.length}});
}
}

// Remove the prefix
if (prefixAtStart && suffixAtStart) {
if (suffixIndented) {
editGroup.push({text: "", start: {line: prefixPos.line, ch: 0}, end: {line: prefixPos.line + 1, ch: 0}});
} else if (prefixAtStart && suffixAtStart) {
editGroup.push({text: "", start: prefixPos, end: {line: prefixPos.line + 1, ch: 0}});
} else {
editGroup.push({text: "", start: prefixPos, end: {line: prefixPos.line, ch: prefixPos.ch + prefix.length}});
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 @@ -797,6 +797,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"
});
Loading