Skip to content
Merged
Changes from all commits
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
35 changes: 24 additions & 11 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ namespace ts.server {
return spaceCache[n];
}

export function generateIndentString(n: number, editorOptions: EditorOptions): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we already have similar function getIndentationString. Can it be used instead to avoid code duplication?

if (editorOptions.ConvertTabsToSpaces) {
return generateSpaces(n);
} else {
var result = "";
for (var i = 0; i < Math.floor(n / editorOptions.TabSize); i++) {
result += "\t";
}
for (var i = 0; i < n % editorOptions.TabSize; i++) {
result += " ";
}
return result;
}
}

interface FileStart {
file: string;
start: ILineInfo;
Expand Down Expand Up @@ -607,27 +622,25 @@ namespace ts.server {
NewLineCharacter: "\n",
ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces,
};
var indentPosition =
compilerService.languageService.getIndentationAtPosition(file, position, editorOptions);
var preferredIndent = compilerService.languageService.getIndentationAtPosition(file, position, editorOptions);
var hasIndent = 0;
for (var i = 0, len = lineText.length; i < len; i++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should already have something that is kind of doing the same thing findFirstNonWhitespaceCharacterAndColumn. Can you check if it can be reused here?

if (lineText.charAt(i) == " ") {
indentPosition--;
hasIndent++;
}
else if (lineText.charAt(i) == "\t") {
indentPosition -= editorOptions.IndentSize;
hasIndent += editorOptions.TabSize;
}
else {
break;
}
}
if (indentPosition > 0) {
var spaces = generateSpaces(indentPosition);
edits.push({ span: ts.createTextSpanFromBounds(position, position), newText: spaces });
}
else if (indentPosition < 0) {
// i points to the first non whitespace character
if (preferredIndent !== hasIndent) {
var firstNoWhiteSpacePosition = lineInfo.offset + i;
edits.push({
span: ts.createTextSpanFromBounds(position, position - indentPosition),
newText: ""
span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition),
newText: generateIndentString(preferredIndent, editorOptions)
});
}
}
Expand Down