MCPcopy Create free account
hub / github.com/nodejs/node / formatOnEnter

Function formatOnEnter

test/fixtures/snapshot/typescript.js:146971–146998  ·  view source on GitHub ↗
(position, sourceFile, formatContext)

Source from the content-addressed store, hash-verified

146969 Constants[Constants["Unknown"] = -1] = "Unknown";
146970 })(Constants || (Constants = {}));
146971 function formatOnEnter(position, sourceFile, formatContext) {
146972 var line = sourceFile.getLineAndCharacterOfPosition(position).line;
146973 if (line === 0) {
146974 return [];
146975 }
146976 // After the enter key, the cursor is now at a new line. The new line may or may not contain non-whitespace characters.
146977 // If the new line has only whitespaces, we won't want to format this line, because that would remove the indentation as
146978 // trailing whitespaces. So the end of the formatting span should be the later one between:
146979 // 1. the end of the previous line
146980 // 2. the last non-whitespace character in the current line
146981 var endOfFormatSpan = ts.getEndLinePosition(line, sourceFile);
146982 while (ts.isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(endOfFormatSpan))) {
146983 endOfFormatSpan--;
146984 }
146985 // if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to
146986 // touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the
146987 // previous character before the end of format span is line break character as well.
146988 if (ts.isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) {
146989 endOfFormatSpan--;
146990 }
146991 var span = {
146992 // get start position for the previous line
146993 pos: ts.getStartPositionOfLine(line - 1, sourceFile),
146994 // end value is exclusive so add 1 to the result
146995 end: endOfFormatSpan + 1
146996 };
146997 return formatSpan(span, sourceFile, formatContext, 2 /* FormattingRequestKind.FormatOnEnter */);
146998 }
146999 formatting.formatOnEnter = formatOnEnter;
147000 function formatOnSemicolon(position, sourceFile, formatContext) {
147001 var semicolon = findImmediatelyPrecedingTokenOfKind(position, 26 /* SyntaxKind.SemicolonToken */, sourceFile);

Callers

nothing calls this directly

Calls 1

formatSpanFunction · 0.85

Tested by

no test coverage detected