This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Add new comments preference. Support indent block comments on line co… #13254
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7ed43d
Add indentBlockComment preference. Support indent block comments on l…
ficristo 79c2bfd
Check for useTabChar and correct a comment. Add some tests
ficristo 15588c2
Use a single preference
ficristo bd392a6
Add test for block comments
ficristo cd78916
Correct a test
ficristo dc9f3af
Check also if it is a line comment command
ficristo 8a71b9f
Return 0 from _firstNotWs
ficristo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
return text.search(/\S|$/); | ||
} | ||
|
||
/** | ||
* Generates an edit that adds or removes block-comment tokens to the selection, preserving selection | ||
|
@@ -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)]; | ||
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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; | ||
} | ||
} | ||
|
@@ -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}}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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".