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

Truncating lineText in references #51137

Closed
wants to merge 1 commit 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
21 changes: 20 additions & 1 deletion src/server/session.ts
Expand Up @@ -3522,7 +3522,26 @@ namespace ts.server {
const scriptInfo = Debug.checkDefined(projectService.getScriptInfo(fileName));
const span = toProtocolTextSpanWithContext(textSpan, contextSpan, scriptInfo);
const lineSpan = scriptInfo.lineToTextSpan(span.start.line - 1);
const lineText = scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
if (lineSpan.length > 1024) {
Copy link
Member

Choose a reason for hiding this comment

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

Since 1024 is just a number I made up, it may need to be tuned. That would be easier if it were a constant.

Choose a reason for hiding this comment

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

To avoid using a magic number, it would be better to create a new variable with a proper name and store 1024 into it.

var start = (span.start.offset > 500) ? (span.start.offset - 501 + lineSpan.start) : lineSpan.start;
Copy link
Member

Choose a reason for hiding this comment

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

We never use var because it has weird scoping (e.g. lineText below is visible outside the if block).

var end = (lineSpan.length -2 - span.end.offset > 500) ? (span.end.offset + 499 + lineSpan.start) : ts.textSpanEnd(lineSpan)-2; // need to ask why length is 2 more
Copy link
Member

Choose a reason for hiding this comment

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

I probably said "500", but I meant "half of the max length" (easy, if you have a constant for the max length). Also, you may discover after playing with it, that you want less text before and more text after the reference.

Copy link
Member

Choose a reason for hiding this comment

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

Usually when we subtract two from the length of a line, it's for \r and \n, but line endings vary by platform, so it's usually necessary to use a helper (we probably have one in core.ts or utilities.ts.

var lineText;
const sourceFile = scriptInfo.getDefaultProject().getLanguageService().getProgram()!.getSourceFile(fileName)!;
let tokenAtPosition = getTokenAtPosition(sourceFile, start);
let tokenAtPositionEnd = getTokenAtPosition(sourceFile, end - 1);
if (tokenAtPosition.getStart() < start) {
Copy link
Member

Choose a reason for hiding this comment

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

feed in the optional sourceFile parameter to getStart.

const nextToken = findNextToken(tokenAtPosition, tokenAtPosition.parent, sourceFile)?.getStart()!;
Copy link
Member

Choose a reason for hiding this comment

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

I might call this "nextTokenStart", to make it clearer that it's not a token.

Copy link
Member

Choose a reason for hiding this comment

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

Does getStart return the position before or after the leading trivia?

Copy link
Member Author

Choose a reason for hiding this comment

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

In here, getStart returns the start position of the token that was a result of findNextToken

Copy link
Member

Choose a reason for hiding this comment

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

But if the token looks like /*comment*/identifier, is getStart before or after the comment?

Copy link
Member

Choose a reason for hiding this comment

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

Also, the ?. followed by the ! is very questionable. Please handle the undefined explicitly.

Also, if you are calling getStart, feed in the optional sourceFile parameter.

start = (nextToken - start) > 10 ? start : nextToken;
Copy link
Member

Choose a reason for hiding this comment

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

As this gets more polished, it might help to add comments.

}
if (tokenAtPositionEnd.getEnd() > end) {
const precedingToken = findPrecedingToken(end, sourceFile, tokenAtPositionEnd)?.getStart()!;
Copy link
Member

Choose a reason for hiding this comment

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

Same comment with ?./! and the sourceFile argument to getStart

end = (end - precedingToken) > 10 ? end: precedingToken;
}
lineText = scriptInfo.getSnapshot().getText(start, end).replace(/\r|\n/g, "")!;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we want to add "..." or something when we truncate?

Copy link
Member

Choose a reason for hiding this comment

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

Aside from the ... issue, we have a trimString (or possibly trimStringEnd) helper which you should probably use instead.

}
else {
lineText = scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
}
return {
file: fileName,
...span,
Expand Down