Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in indenting multiline comments with tabs (issue #348) #990

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/vs/editor/common/commands/shiftCommand.ts
Expand Up @@ -71,6 +71,8 @@ export class ShiftCommand implements EditorCommon.ICommand {
}
}

var isMultilineComment = false;

for (lineNumber = startLine; lineNumber <= endLine; lineNumber++) {
var lineText = model.getLineContent(lineNumber);
var indentationEndIndex = Strings.firstNonWhitespaceIndex(lineText);
Expand All @@ -89,7 +91,11 @@ export class ShiftCommand implements EditorCommon.ICommand {

var desiredIndentCount: number;
if (this._opts.isUnshift) {
desiredIndentCount = ShiftCommand.unshiftIndentCount(lineText, indentationEndIndex + 1, tabSize);
if (isMultilineComment) {
desiredIndentCount = ShiftCommand.unshiftIndentCount(lineText, indentationEndIndex, tabSize);
} else {
desiredIndentCount = ShiftCommand.unshiftIndentCount(lineText, indentationEndIndex + 1, tabSize);
}
} else {
desiredIndentCount = ShiftCommand.shiftIndentCount(lineText, indentationEndIndex + 1, tabSize);
}
Expand All @@ -99,7 +105,23 @@ export class ShiftCommand implements EditorCommon.ICommand {
indents[j] = indents[j-1] + oneIndent;
}

builder.addEditOperation(new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), indents[desiredIndentCount]);
var desiredIndent = indents[desiredIndentCount];
if (isMultilineComment) {
// Add space to align the multiline comment
desiredIndent += " ";
}

// Check whether we're in a multiline comment or not
if (lineText.length >= 2 + indentationEndIndex) {
if (lineText.indexOf("/*") === indentationEndIndex) {
isMultilineComment = true;
}
if (lineText.indexOf("*/") !== -1) {

Choose a reason for hiding this comment

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

[minor] You can pass the start index in the call to indexOf. Will reduce atleast 2 comparisons.

Copy link

Choose a reason for hiding this comment

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

Hardcoding language-specific comment delimiters here seems wrong.

isMultilineComment = false;
}
}

builder.addEditOperation(new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), desiredIndent);
}

this._selectionId = builder.trackSelection(this._selection);
Expand Down