Skip to content

Format on type deletes valid range #3084

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module ts {
declare var process: any;
declare var global: any;
declare var __filename: string;
declare var Buffer: {
new (str: string, encoding ?: string): any;
}

declare class Enumerator {
public atEnd(): boolean;
Expand Down Expand Up @@ -252,8 +255,15 @@ module ts {
newLine: _os.EOL,
useCaseSensitiveFileNames: useCaseSensitiveFileNames,
write(s: string): void {
var buffer = new Buffer(s, 'utf8');
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this changing as part of a formatting change?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree this needs to be in a separate change.

Copy link
Contributor

Choose a reason for hiding this comment

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

agree

var offset: number = 0;
var toWrite: number = buffer.length
var written = 0;
// 1 is a standard descriptor for stdout
_fs.writeSync(1, s);
while ((written = _fs.writeSync(1, buffer, offset, toWrite)) < toWrite) {
offset += written;
toWrite -= written;
}
},
readFile,
writeFile,
Expand Down
41 changes: 27 additions & 14 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ module ts.server {
}
return spaceCache[n];
}

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

Choose a reason for hiding this comment

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

we already have code for that, check getIndentationString

if (editorOptions.ConvertTabsToSpaces) {
return generateSpaces(n);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

else on the next line

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;
Expand Down Expand Up @@ -541,31 +556,29 @@ module ts.server {
var editorOptions: ts.EditorOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we really need to copy formatOptions? it should be assignable to EditorOptions so original formatOptions can be used directly (assuming that nobody changes them)

IndentSize: formatOptions.IndentSize,
TabSize: formatOptions.TabSize,
NewLineCharacter: "\n",
NewLineCharacter: formatOptions.NewLineCharacter,
ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces,
};
var indentPosition =
compilerService.languageService.getIndentationAtPosition(file, position, editorOptions);
var preferredIndent = compilerService.languageService.getIndentationAtPosition(file, position, editorOptions);
Copy link
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 code that determines first non-whitespace column\character in the line, check SmartIndenter.findFirstNonWhitespaceCharacterAndColumn

var hasIndent = 0;
for (var i = 0, len = lineText.length; i < len; i++) {
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) {
edits.push({
span: ts.createTextSpanFromBounds(position, position - indentPosition),
newText: ""
});
// i points to the first non whitespace character
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm finding this quite worrying how much extra logic is here in the server to handle this sort of conversion. It seems like far too much complexit in this layer.

if (preferredIndent !== hasIndent) {
var firstNoWhiteSpacePosition = lineInfo.offset + i;
Copy link
Member

Choose a reason for hiding this comment

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

firstNoWhiteSpacePosition -> firstNonWhitespacePosition

edits.push({
span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition),
newText: generateIndentString(preferredIndent, editorOptions)
});
}
}
}
Expand Down