Skip to content

Commit ca78105

Browse files
committed
formatting
1 parent 4f60eeb commit ca78105

File tree

264 files changed

+12925
-281
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

264 files changed

+12925
-281
lines changed

internal/format/span.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (w *formatSpanWorker) execute(s *formattingScanner) []core.TextChange {
268268
w.insertIndentation(item.Loc.Pos(), indentation, false)
269269
})
270270

271-
if opt.TrimTrailingWhitespace != false {
271+
if opt.TrimTrailingWhitespace {
272272
w.trimTrailingWhitespacesForRemainingRange(remainingTrivia)
273273
}
274274
}
@@ -303,6 +303,9 @@ func (w *formatSpanWorker) execute(s *formattingScanner) []core.TextChange {
303303
// edit in the middle of a token where the range ended, so if we have a non-contiguous
304304
// pair here, we're already done and we can ignore it.
305305
parent := astnav.FindPrecedingToken(w.sourceFile, tokenInfo.Loc.End())
306+
if parent != nil {
307+
parent = parent.Parent
308+
}
306309
if parent == nil {
307310
parent = w.previousParent
308311
}

internal/fourslash/_scripts/convertFourslash.mts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
179179
case "importFixAtPosition":
180180
// `verify.importFixAtPosition(...)`
181181
return parseImportFixAtPositionArgs(callExpression.arguments);
182+
case "currentLineContentIs":
183+
case "currentFileContentIs":
184+
case "indentationIs":
185+
case "indentationAtPositionIs":
186+
case "textAtCaretIs":
187+
return parseCurrentContentIsArgs(func.text, callExpression.arguments);
182188
case "quickInfoAt":
183189
case "quickInfoExists":
184190
case "quickInfoIs":
@@ -251,6 +257,9 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
251257
}
252258
return [result];
253259
}
260+
if (namespace.text === "format") {
261+
return parseFormatStatement(func.text, callExpression.arguments);
262+
}
254263
// !!! other fourslash commands
255264
}
256265
console.error(`Unrecognized fourslash statement: ${statement.getText()}`);
@@ -307,6 +316,63 @@ function parseEditStatement(funcName: string, args: readonly ts.Expression[]): E
307316
}
308317
}
309318

319+
function parseFormatStatement(funcName: string, args: readonly ts.Expression[]): FormatCmd[] | undefined {
320+
switch (funcName) {
321+
case "document": {
322+
return [{
323+
kind: "format",
324+
goStatement: `f.FormatDocument(t, "")`,
325+
}];
326+
}
327+
case "setOption":
328+
const optName = getGoStringLiteral(getStringLiteralLike(args[0])!.text);
329+
return [{
330+
kind: "format",
331+
goStatement: `f.SetFormatOption(t, ${optName}, ${args[1].getText()})`,
332+
}];
333+
case "selection":
334+
case "onType":
335+
case "copyFormatOptions":
336+
case "setFormatOptions":
337+
console.error(`format function not implemented: ${funcName}`);
338+
break;
339+
default:
340+
console.error(`Unrecognized format function: ${funcName}`);
341+
}
342+
return undefined;
343+
}
344+
345+
function parseCurrentContentIsArgs(funcName: string, args: readonly ts.Expression[]): VerifyContentCmd[] | undefined {
346+
switch (funcName) {
347+
case "currentFileContentIs":
348+
return [{
349+
kind: "verifyContent",
350+
goStatement: `f.VerifyCurrentFileContent(t, ${getGoStringLiteralFromNode(args[0])!})`,
351+
}];
352+
case "currentLineContentIs":
353+
return [{
354+
kind: "verifyContent",
355+
goStatement: `f.VerifyCurrentLineContent(t, ${getGoStringLiteralFromNode(args[0])!})`,
356+
}];
357+
case "indentationIs":
358+
// return [{
359+
// kind: "verifyContent",
360+
// goStatement: `f.VerifyIndentation(t, ${getNumericLiteral(args[0])?.text})`,
361+
// }];
362+
case "indentationAtPositionIs":
363+
case "textAtCaretIs":
364+
console.error(`unimplemented verify content function: ${funcName}`);
365+
return undefined;
366+
// return [{
367+
// kind: "verifyContent",
368+
// goStatement: `f.VerifyTextAtCaret(t, ${getGoStringLiteral(args[0].getText())})`,
369+
// }];
370+
default:
371+
console.error(`Unrecognized verify content function: ${funcName}`);
372+
return undefined;
373+
}
374+
}
375+
310376
function getGoMultiLineStringLiteral(text: string): string {
311377
if (!text.includes("`") && !text.includes("\\")) {
312378
return "`" + text + "`";
@@ -318,6 +384,24 @@ function getGoStringLiteral(text: string): string {
318384
return `${JSON.stringify(text)}`;
319385
}
320386

387+
function getGoStringLiteralFromNode(node: ts.Node): string | undefined {
388+
const stringLiteralLike = getStringLiteralLike(node);
389+
if (stringLiteralLike) {
390+
return getGoMultiLineStringLiteral(stringLiteralLike.text);
391+
}
392+
switch (node.kind) {
393+
case ts.SyntaxKind.BinaryExpression: {
394+
const binaryExpr = node as ts.BinaryExpression;
395+
const left = getGoStringLiteralFromNode(binaryExpr.left);
396+
const right = getGoStringLiteralFromNode(binaryExpr.right);
397+
const op = binaryExpr.operatorToken.getText();
398+
return left + op + right;
399+
}
400+
default:
401+
console.error(`Unhandled case ${node.kind} in getGoStringLiteralFromNode: ${node.getText()}`);
402+
}
403+
}
404+
321405
function parseGoToArgs(args: readonly ts.Expression[], funcName: string): GoToCmd[] | undefined {
322406
switch (funcName) {
323407
case "marker": {
@@ -2498,6 +2582,16 @@ interface EditCmd {
24982582
goStatement: string;
24992583
}
25002584

2585+
interface FormatCmd {
2586+
kind: "format"; // | "formatSelection" | "formatOnType" | "copyFormatOptions" | "setFormatOptions" | "setOption";
2587+
goStatement: string;
2588+
}
2589+
2590+
interface VerifyContentCmd {
2591+
kind: "verifyContent";
2592+
goStatement: string;
2593+
}
2594+
25012595
interface VerifyQuickInfoCmd {
25022596
kind: "quickInfoIs" | "quickInfoAt" | "quickInfoExists" | "notQuickInfoExists";
25032597
marker?: string;
@@ -2582,7 +2676,9 @@ type Cmd =
25822676
| VerifyNoSignatureHelpForTriggerReasonCmd
25832677
| VerifyBaselineCallHierarchy
25842678
| GoToCmd
2679+
| FormatCmd
25852680
| EditCmd
2681+
| VerifyContentCmd
25862682
| VerifyQuickInfoCmd
25872683
| VerifyBaselineRenameCmd
25882684
| VerifyRenameInfoCmd
@@ -2860,6 +2956,8 @@ function generateCmd(cmd: Cmd): string {
28602956
case "goTo":
28612957
return generateGoToCommand(cmd);
28622958
case "edit":
2959+
case "format":
2960+
case "verifyContent":
28632961
return cmd.goStatement;
28642962
case "quickInfoAt":
28652963
case "quickInfoIs":

internal/fourslash/_scripts/failingTests.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ TestAmbientShorthandGotoDefinition
33
TestArgumentsAreAvailableAfterEditsAtEndOfFunction
44
TestAugmentedTypesClass1
55
TestAugmentedTypesClass3Fourslash
6+
TestAutoFormattingOnPasting
67
TestAutoImportCompletionAmbientMergedModule1
78
TestAutoImportCompletionExportListAugmentation1
89
TestAutoImportCompletionExportListAugmentation2
@@ -32,7 +33,10 @@ TestAutoImportProvider_pnpm
3233
TestAutoImportProvider_wildcardExports1
3334
TestAutoImportProvider_wildcardExports2
3435
TestAutoImportProvider_wildcardExports3
36+
TestAutoImportProvider1
3537
TestAutoImportProvider4
38+
TestAutoImportProvider7
39+
TestAutoImportProvider8
3640
TestAutoImportSortCaseSensitivity1
3741
TestAutoImportSymlinkCaseSensitive
3842
TestAutoImportTypeImport1
@@ -241,10 +245,87 @@ TestExportDefaultClass
241245
TestExportDefaultFunction
242246
TestFindReferencesBindingPatternInJsdocNoCrash1
243247
TestFindReferencesBindingPatternInJsdocNoCrash2
248+
TestForceIndentAfterNewLineInsert
249+
TestFormat01
250+
TestFormatAfterObjectLiteral
251+
TestFormatArrayLiteralExpression
252+
TestFormatAsyncClassMethod1
253+
TestFormatAsyncClassMethod2
254+
TestFormatColonAndQMark
255+
TestFormatComments
256+
TestFormatConflictDiff3Marker1
257+
TestFormatConflictMarker1
258+
TestFormatDebuggerStatement
259+
TestFormatDocumentPreserveTrailingWhitespace
260+
TestFormatDocumentWithJSDoc
261+
TestFormatDocumentWithTrivia
262+
TestFormatDotAfterNumber
263+
TestFormatEmptyParamList
264+
TestFormatIfTryCatchBlocks
265+
TestFormatIfWithEmptyCondition
266+
TestFormatImplicitModule
267+
TestFormatMultipleFunctionArguments
268+
TestFormatNoSpaceAfterTemplateHeadAndMiddle
269+
TestFormatObjectBindingPattern
270+
TestFormatOnSemiColonAfterBreak
271+
TestFormatRemoveNewLineAfterOpenBrace
272+
TestFormatSpaceAfterImplementsExtends
273+
TestFormatSpaceAfterTemplateHeadAndMiddle
274+
TestFormatSpaceBetweenFunctionAndArrayIndex
275+
TestFormattingAfterMultiLineIfCondition
276+
TestFormattingAwait
277+
TestFormattingChainingMethods
278+
TestFormattingDecorators
279+
TestFormattingDoubleLessThan
280+
TestFormattingForIn
281+
TestFormattingForLoopSemicolons
282+
TestFormattingForOfKeyword
283+
TestFormattingGlobalAugmentation2
284+
TestFormattingInExpressionsInTsx
285+
TestFormattingJsxTexts2
286+
TestFormattingJsxTexts3
287+
TestFormattingJsxTexts4
288+
TestFormattingMappedType
289+
TestFormattingMultilineCommentsWithTabs1
290+
TestFormattingNestedScopes
291+
TestFormattingNonNullAssertionOperator
292+
TestFormattingObjectLiteral
293+
TestFormattingObjectLiteralOpenCurlyNewline
294+
TestFormattingObjectLiteralOpenCurlyNewlineAssignment
295+
TestFormattingObjectLiteralOpenCurlyNewlineTyping
296+
TestFormattingObjectLiteralOpenCurlySingleLine
297+
TestFormattingOfExportDefault
298+
TestFormattingOnClasses
299+
TestFormattingOnClosingBracket
300+
TestFormattingOnDocumentReadyFunction
301+
TestFormattingOnDoWhileNoSemicolon
302+
TestFormattingOnEmptyInterfaceLiteral
303+
TestFormattingOnInvalidCodes
304+
TestFormattingOnModuleIndentation
305+
TestFormattingOnNestedDoWhileByEnter
306+
TestFormattingOnObjectLiteral
307+
TestFormattingOnSingleLineBlocks
308+
TestFormattingOnStatementsWithNoSemicolon
309+
TestFormattingOnTabAfterCloseCurly
310+
TestFormattingOnVariety
311+
TestFormattingReplaceTabsWithSpaces
312+
TestFormattingSpaceAfterCommaBeforeOpenParen
313+
TestFormattingSpaceBeforeFunctionParen
314+
TestFormattingSpaceBetweenParent
315+
TestFormattingSpacesAfterConstructor
316+
TestFormattingTemplates
317+
TestFormattingVoid
318+
TestFormatTryFinally
319+
TestFormatTsxClosingAfterJsxText
320+
TestFormatVariableDeclarationList
321+
TestFormatWithStatement
322+
TestFunctionIndentation
323+
TestFunctionTypePredicateFormatting
244324
TestGenericCombinators3
245325
TestGenericCombinatorWithConstraints1
246326
TestGenericFunctionWithGenericParams1
247327
TestGenericInterfacesWithConstraints1
328+
TestGenericsFormattingMultiline
248329
TestGenericTypeWithMultipleBases1MultiFile
249330
TestGetJavaScriptCompletions10
250331
TestGetJavaScriptCompletions12
@@ -285,6 +366,8 @@ TestImportCompletionsPackageJsonImportsPattern_ts_ts
285366
TestImportCompletionsPackageJsonImportsPattern2
286367
TestImportFixesGlobalTypingsCache
287368
TestImportNameCodeFix_avoidRelativeNodeModules
369+
TestImportNameCodeFix_externalNonRelateive2
370+
TestImportNameCodeFix_externalNonRelative1
288371
TestImportNameCodeFix_fileWithNoTrailingNewline
289372
TestImportNameCodeFix_HeaderComment1
290373
TestImportNameCodeFix_HeaderComment2
@@ -329,6 +412,7 @@ TestImportTypeCompletions6
329412
TestImportTypeCompletions7
330413
TestImportTypeCompletions8
331414
TestImportTypeCompletions9
415+
TestIndentationInJsx3
332416
TestIndexerReturnTypes1
333417
TestIndirectClassInstantiation
334418
TestInstanceTypesForGenericType1
@@ -378,11 +462,13 @@ TestMemberListOfExportedClass
378462
TestMemberListOnContextualThis
379463
TestModuleNodeNextAutoImport2
380464
TestModuleNodeNextAutoImport3
465+
TestMultilineCommentBeforeOpenBrace
381466
TestNgProxy1
382467
TestNodeModulesImportCompletions1
383468
TestNoQuickInfoForLabel
384469
TestNoQuickInfoInWhitespace
385470
TestNumericPropertyNames
471+
TestOptionalPropertyFormatting
386472
TestOverloadQuickInfo
387473
TestParameterWithDestructuring
388474
TestParameterWithNestedDestructuring
@@ -550,11 +636,14 @@ TestRenameFromNodeModulesDep4
550636
TestRenamePrivateFields
551637
TestReverseMappedTypeQuickInfo
552638
TestSelfReferencedExternalModule
639+
TestSemicolonFormattingNestedStatements
640+
TestSpaceAfterReturn
553641
TestStringCompletionsImportOrExportSpecifier
554642
TestStringCompletionsVsEscaping
555643
TestSymbolCompletionLowerPriority
556644
TestSyntheticImportFromBabelGeneratedFile1
557645
TestSyntheticImportFromBabelGeneratedFile2
646+
TestTabbingAfterNewlineInsertedBeforeWhile
558647
TestThisBindingInLambda
559648
TestThisPredicateFunctionQuickInfo01
560649
TestThisPredicateFunctionQuickInfo02
@@ -571,3 +660,7 @@ TestTsxQuickInfo5
571660
TestTsxQuickInfo6
572661
TestTsxQuickInfo7
573662
TestTypeOperatorNodeBuilding
663+
TestUnclosedStringLiteralAutoformating
664+
TestWhiteSpaceTrimming
665+
TestWhiteSpaceTrimming2
666+
TestWhiteSpaceTrimming4

0 commit comments

Comments
 (0)