Skip to content

Commit baf813f

Browse files
authored
Add edit functionality to fourslash (#1310)
1 parent 28b91fb commit baf813f

File tree

48 files changed

+2423
-27
lines changed

Some content is hidden

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

48 files changed

+2423
-27
lines changed

internal/fourslash/_scripts/convertFourslash.mts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,64 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
146146
if (namespace.text === "goTo" && func.text === "marker") {
147147
return parseGoToMarkerArgs(callExpression.arguments);
148148
}
149+
// `edit....`
150+
if (namespace.text === "edit") {
151+
const result = parseEditStatement(func.text, callExpression.arguments);
152+
if (!result) {
153+
return undefined;
154+
}
155+
return [result];
156+
}
149157
// !!! other fourslash commands
150158
}
151159
console.error(`Unrecognized fourslash statement: ${statement.getText()}`);
152160
return undefined;
153161
}
154162

163+
function parseEditStatement(funcName: string, args: readonly ts.Expression[]): EditCmd | undefined {
164+
switch (funcName) {
165+
case "insert":
166+
case "paste":
167+
case "insertLine":
168+
if (args.length !== 1 || !ts.isStringLiteralLike(args[0])) {
169+
console.error(`Expected a single string literal argument in edit.${funcName}, got ${args.map(arg => arg.getText()).join(", ")}`);
170+
return undefined;
171+
}
172+
return {
173+
kind: "edit",
174+
goStatement: `f.${funcName.charAt(0).toUpperCase() + funcName.slice(1)}(t, ${getGoStringLiteral(args[0].text)})`,
175+
};
176+
case "replaceLine":
177+
if (args.length !== 2 || !ts.isNumericLiteral(args[0]) || !ts.isStringLiteral(args[1])) {
178+
console.error(`Expected a single string literal argument in edit.insert, got ${args.map(arg => arg.getText()).join(", ")}`);
179+
return undefined;
180+
}
181+
return {
182+
kind: "edit",
183+
goStatement: `f.ReplaceLine(t, ${args[0].text}, ${getGoStringLiteral(args[1].text)})`,
184+
};
185+
case "backspace":
186+
const arg = args[0];
187+
if (arg) {
188+
if (!ts.isNumericLiteral(arg)) {
189+
console.error(`Expected numeric literal argument in edit.backspace, got ${arg.getText()}`);
190+
return undefined;
191+
}
192+
return {
193+
kind: "edit",
194+
goStatement: `f.Backspace(t, ${arg.text})`,
195+
};
196+
}
197+
return {
198+
kind: "edit",
199+
goStatement: `f.Backspace(t, 1)`,
200+
};
201+
default:
202+
console.error(`Unrecognized edit function: ${funcName}`);
203+
return undefined;
204+
}
205+
}
206+
155207
function getGoStringLiteral(text: string): string {
156208
return `${JSON.stringify(text)}`;
157209
}
@@ -614,7 +666,12 @@ interface GoToMarkerCmd {
614666
marker: string;
615667
}
616668

617-
type Cmd = VerifyCompletionsCmd | GoToMarkerCmd;
669+
interface EditCmd {
670+
kind: "edit";
671+
goStatement: string;
672+
}
673+
674+
type Cmd = VerifyCompletionsCmd | GoToMarkerCmd | EditCmd;
618675

619676
function generateVerifyCompletions({ marker, args, isNewIdentifierLocation }: VerifyCompletionsCmd): string {
620677
let expectedList = "nil";
@@ -649,6 +706,8 @@ function generateCmd(cmd: Cmd): string {
649706
return generateVerifyCompletions(cmd as VerifyCompletionsCmd);
650707
case "goToMarker":
651708
return generateGoToMarker(cmd as GoToMarkerCmd);
709+
case "edit":
710+
return cmd.goStatement;
652711
default:
653712
throw new Error(`Unknown command kind: ${cmd}`);
654713
}

internal/fourslash/_scripts/failingTests.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ TestCompletionListAndMemberListOnCommentedLine
6262
TestCompletionListAndMemberListOnCommentedWhiteSpace
6363
TestCompletionListAtInvalidLocations
6464
TestCompletionListBuilderLocations_VariableDeclarations
65+
TestCompletionListCladule
6566
TestCompletionListForExportEquals
6667
TestCompletionListForTransitivelyExportedMembers01
6768
TestCompletionListForTransitivelyExportedMembers04
6869
TestCompletionListForUnicodeEscapeName
70+
TestCompletionListFunctionExpression
6971
TestCompletionListFunctionMembers
7072
TestCompletionListInArrowFunctionInUnclosedCallSite01
7173
TestCompletionListInClassStaticBlocks
@@ -74,10 +76,10 @@ TestCompletionListInComments
7476
TestCompletionListInComments2
7577
TestCompletionListInComments3
7678
TestCompletionListInExtendsClause
79+
TestCompletionListInFunctionDeclaration
7780
TestCompletionListInImportClause01
7881
TestCompletionListInImportClause05
7982
TestCompletionListInImportClause06
80-
TestCompletionListInObjectBindingPattern16
8183
TestCompletionListInScope
8284
TestCompletionListInTemplateLiteralParts1
8385
TestCompletionListInUnclosedCommaExpression01
@@ -108,6 +110,7 @@ TestCompletionsAfterJSDoc
108110
TestCompletionsBeforeRestArg1
109111
TestCompletionsECMAPrivateMemberTriggerCharacter
110112
TestCompletionsImportDefaultExportCrash1
113+
TestCompletionsImport_computedSymbolName
111114
TestCompletionsImport_umdDefaultNoCrash2
112115
TestCompletionsInRequire
113116
TestCompletionsInterfaceElement
@@ -161,15 +164,19 @@ TestGetJavaScriptCompletions12
161164
TestGetJavaScriptCompletions13
162165
TestGetJavaScriptCompletions14
163166
TestGetJavaScriptCompletions15
167+
TestGetJavaScriptCompletions18
168+
TestGetJavaScriptCompletions19
164169
TestGetJavaScriptCompletions2
165170
TestGetJavaScriptCompletions20
166171
TestGetJavaScriptCompletions21
172+
TestGetJavaScriptCompletions22
167173
TestGetJavaScriptCompletions3
168174
TestGetJavaScriptCompletions4
169175
TestGetJavaScriptCompletions5
170176
TestGetJavaScriptCompletions8
171177
TestGetJavaScriptCompletions9
172178
TestGetJavaScriptGlobalCompletions1
179+
TestGetJavaScriptQuickInfo8
173180
TestImportCompletionsPackageJsonExportsSpecifierEndsInTs
174181
TestImportCompletionsPackageJsonExportsTrailingSlash1
175182
TestImportCompletionsPackageJsonImportsConditions1
@@ -197,6 +204,7 @@ TestJavaScriptModules14
197204
TestJavascriptModules20
198205
TestJavascriptModules21
199206
TestJavascriptModulesTypeImport
207+
TestJsDocFunctionSignatures3
200208
TestJsDocGenerics1
201209
TestJsdocExtendsTagCompletion
202210
TestJsdocImplementsTagCompletion
@@ -220,12 +228,16 @@ TestMemberListOnConstructorType
220228
TestNoCompletionListOnCommentsInsideObjectLiterals
221229
TestNodeModulesImportCompletions1
222230
TestPathCompletionsAllowModuleAugmentationExtensions
231+
TestPathCompletionsAllowTsExtensions
223232
TestPathCompletionsPackageJsonExportsBundlerNoNodeCondition
224233
TestPathCompletionsPackageJsonExportsCustomConditions
225234
TestPathCompletionsPackageJsonExportsWildcard1
226235
TestPathCompletionsPackageJsonExportsWildcard10
227236
TestPathCompletionsPackageJsonExportsWildcard11
228237
TestPathCompletionsPackageJsonExportsWildcard12
238+
TestPathCompletionsPackageJsonExportsWildcard2
239+
TestPathCompletionsPackageJsonExportsWildcard3
240+
TestPathCompletionsPackageJsonExportsWildcard4
229241
TestPathCompletionsPackageJsonExportsWildcard5
230242
TestPathCompletionsPackageJsonExportsWildcard6
231243
TestPathCompletionsPackageJsonExportsWildcard7
@@ -236,6 +248,9 @@ TestPathCompletionsPackageJsonImportsCustomConditions
236248
TestPathCompletionsPackageJsonImportsIgnoreMatchingNodeModule2
237249
TestPathCompletionsPackageJsonImportsOnlyFromClosestScope1
238250
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard1
251+
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard2
252+
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard3
253+
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard4
239254
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard5
240255
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard6
241256
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard7
@@ -245,13 +260,21 @@ TestPathCompletionsPackageJsonImportsWildcard1
245260
TestPathCompletionsPackageJsonImportsWildcard10
246261
TestPathCompletionsPackageJsonImportsWildcard11
247262
TestPathCompletionsPackageJsonImportsWildcard12
263+
TestPathCompletionsPackageJsonImportsWildcard2
264+
TestPathCompletionsPackageJsonImportsWildcard3
265+
TestPathCompletionsPackageJsonImportsWildcard4
248266
TestPathCompletionsPackageJsonImportsWildcard5
249267
TestPathCompletionsPackageJsonImportsWildcard6
250268
TestPathCompletionsPackageJsonImportsWildcard7
251269
TestPathCompletionsPackageJsonImportsWildcard8
252270
TestPathCompletionsPackageJsonImportsWildcard9
253271
TestPathCompletionsTypesVersionsLocal
272+
TestPathCompletionsTypesVersionsWildcard1
254273
TestPathCompletionsTypesVersionsWildcard2
274+
TestPathCompletionsTypesVersionsWildcard3
275+
TestPathCompletionsTypesVersionsWildcard4
276+
TestPathCompletionsTypesVersionsWildcard5
277+
TestPathCompletionsTypesVersionsWildcard6
255278
TestSatisfiesOperatorCompletion
256279
TestStringCompletionsImportOrExportSpecifier
257280
TestStringCompletionsVsEscaping

0 commit comments

Comments
 (0)