Skip to content

Add edit functionality to fourslash #1310

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 60 additions & 1 deletion internal/fourslash/_scripts/convertFourslash.mts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,64 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
if (namespace.text === "goTo" && func.text === "marker") {
return parseGoToMarkerArgs(callExpression.arguments);
}
// `edit....`
if (namespace.text === "edit") {
const result = parseEditStatement(func.text, callExpression.arguments);
if (!result) {
return undefined;
}
return [result];
}
// !!! other fourslash commands
}
console.error(`Unrecognized fourslash statement: ${statement.getText()}`);
return undefined;
}

function parseEditStatement(funcName: string, args: readonly ts.Expression[]): EditCmd | undefined {
switch (funcName) {
case "insert":
case "paste":
case "insertLine":
if (args.length !== 1 || !ts.isStringLiteralLike(args[0])) {
console.error(`Expected a single string literal argument in edit.${funcName}, got ${args.map(arg => arg.getText()).join(", ")}`);
return undefined;
}
return {
kind: "edit",
goStatement: `f.${funcName.charAt(0).toUpperCase() + funcName.slice(1)}(t, ${getGoStringLiteral(args[0].text)})`,
};
case "replaceLine":
if (args.length !== 2 || !ts.isNumericLiteral(args[0]) || !ts.isStringLiteral(args[1])) {
console.error(`Expected a single string literal argument in edit.insert, got ${args.map(arg => arg.getText()).join(", ")}`);
return undefined;
}
return {
kind: "edit",
goStatement: `f.ReplaceLine(t, ${args[0].text}, ${getGoStringLiteral(args[1].text)})`,
};
case "backspace":
const arg = args[0];
if (arg) {
if (!ts.isNumericLiteral(arg)) {
console.error(`Expected numeric literal argument in edit.backspace, got ${arg.getText()}`);
return undefined;
}
return {
kind: "edit",
goStatement: `f.Backspace(t, ${arg.text})`,
};
}
return {
kind: "edit",
goStatement: `f.Backspace(t, 1)`,
};
default:
console.error(`Unrecognized edit function: ${funcName}`);
return undefined;
}
}

function getGoStringLiteral(text: string): string {
return `${JSON.stringify(text)}`;
}
Expand Down Expand Up @@ -614,7 +666,12 @@ interface GoToMarkerCmd {
marker: string;
}

type Cmd = VerifyCompletionsCmd | GoToMarkerCmd;
interface EditCmd {
kind: "edit";
goStatement: string;
}

type Cmd = VerifyCompletionsCmd | GoToMarkerCmd | EditCmd;

function generateVerifyCompletions({ marker, args, isNewIdentifierLocation }: VerifyCompletionsCmd): string {
let expectedList = "nil";
Expand Down Expand Up @@ -649,6 +706,8 @@ function generateCmd(cmd: Cmd): string {
return generateVerifyCompletions(cmd as VerifyCompletionsCmd);
case "goToMarker":
return generateGoToMarker(cmd as GoToMarkerCmd);
case "edit":
return cmd.goStatement;
default:
throw new Error(`Unknown command kind: ${cmd}`);
}
Expand Down
25 changes: 24 additions & 1 deletion internal/fourslash/_scripts/failingTests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ TestCompletionListAndMemberListOnCommentedLine
TestCompletionListAndMemberListOnCommentedWhiteSpace
TestCompletionListAtInvalidLocations
TestCompletionListBuilderLocations_VariableDeclarations
TestCompletionListCladule
TestCompletionListForExportEquals
TestCompletionListForTransitivelyExportedMembers01
TestCompletionListForTransitivelyExportedMembers04
TestCompletionListForUnicodeEscapeName
TestCompletionListFunctionExpression
TestCompletionListFunctionMembers
TestCompletionListInArrowFunctionInUnclosedCallSite01
TestCompletionListInClassStaticBlocks
Expand All @@ -74,10 +76,10 @@ TestCompletionListInComments
TestCompletionListInComments2
TestCompletionListInComments3
TestCompletionListInExtendsClause
TestCompletionListInFunctionDeclaration
TestCompletionListInImportClause01
TestCompletionListInImportClause05
TestCompletionListInImportClause06
TestCompletionListInObjectBindingPattern16
TestCompletionListInScope
TestCompletionListInTemplateLiteralParts1
TestCompletionListInUnclosedCommaExpression01
Expand Down Expand Up @@ -108,6 +110,7 @@ TestCompletionsAfterJSDoc
TestCompletionsBeforeRestArg1
TestCompletionsECMAPrivateMemberTriggerCharacter
TestCompletionsImportDefaultExportCrash1
TestCompletionsImport_computedSymbolName
TestCompletionsImport_umdDefaultNoCrash2
TestCompletionsInRequire
TestCompletionsInterfaceElement
Expand Down Expand Up @@ -161,15 +164,19 @@ TestGetJavaScriptCompletions12
TestGetJavaScriptCompletions13
TestGetJavaScriptCompletions14
TestGetJavaScriptCompletions15
TestGetJavaScriptCompletions18
TestGetJavaScriptCompletions19
TestGetJavaScriptCompletions2
TestGetJavaScriptCompletions20
TestGetJavaScriptCompletions21
TestGetJavaScriptCompletions22
TestGetJavaScriptCompletions3
TestGetJavaScriptCompletions4
TestGetJavaScriptCompletions5
TestGetJavaScriptCompletions8
TestGetJavaScriptCompletions9
TestGetJavaScriptGlobalCompletions1
TestGetJavaScriptQuickInfo8
TestImportCompletionsPackageJsonExportsSpecifierEndsInTs
TestImportCompletionsPackageJsonExportsTrailingSlash1
TestImportCompletionsPackageJsonImportsConditions1
Expand Down Expand Up @@ -197,6 +204,7 @@ TestJavaScriptModules14
TestJavascriptModules20
TestJavascriptModules21
TestJavascriptModulesTypeImport
TestJsDocFunctionSignatures3
TestJsDocGenerics1
TestJsdocExtendsTagCompletion
TestJsdocImplementsTagCompletion
Expand All @@ -220,12 +228,16 @@ TestMemberListOnConstructorType
TestNoCompletionListOnCommentsInsideObjectLiterals
TestNodeModulesImportCompletions1
TestPathCompletionsAllowModuleAugmentationExtensions
TestPathCompletionsAllowTsExtensions
TestPathCompletionsPackageJsonExportsBundlerNoNodeCondition
TestPathCompletionsPackageJsonExportsCustomConditions
TestPathCompletionsPackageJsonExportsWildcard1
TestPathCompletionsPackageJsonExportsWildcard10
TestPathCompletionsPackageJsonExportsWildcard11
TestPathCompletionsPackageJsonExportsWildcard12
TestPathCompletionsPackageJsonExportsWildcard2
TestPathCompletionsPackageJsonExportsWildcard3
TestPathCompletionsPackageJsonExportsWildcard4
TestPathCompletionsPackageJsonExportsWildcard5
TestPathCompletionsPackageJsonExportsWildcard6
TestPathCompletionsPackageJsonExportsWildcard7
Expand All @@ -236,6 +248,9 @@ TestPathCompletionsPackageJsonImportsCustomConditions
TestPathCompletionsPackageJsonImportsIgnoreMatchingNodeModule2
TestPathCompletionsPackageJsonImportsOnlyFromClosestScope1
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard1
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard2
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard3
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard4
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard5
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard6
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard7
Expand All @@ -245,13 +260,21 @@ TestPathCompletionsPackageJsonImportsWildcard1
TestPathCompletionsPackageJsonImportsWildcard10
TestPathCompletionsPackageJsonImportsWildcard11
TestPathCompletionsPackageJsonImportsWildcard12
TestPathCompletionsPackageJsonImportsWildcard2
TestPathCompletionsPackageJsonImportsWildcard3
TestPathCompletionsPackageJsonImportsWildcard4
TestPathCompletionsPackageJsonImportsWildcard5
TestPathCompletionsPackageJsonImportsWildcard6
TestPathCompletionsPackageJsonImportsWildcard7
TestPathCompletionsPackageJsonImportsWildcard8
TestPathCompletionsPackageJsonImportsWildcard9
TestPathCompletionsTypesVersionsLocal
TestPathCompletionsTypesVersionsWildcard1
TestPathCompletionsTypesVersionsWildcard2
TestPathCompletionsTypesVersionsWildcard3
TestPathCompletionsTypesVersionsWildcard4
TestPathCompletionsTypesVersionsWildcard5
TestPathCompletionsTypesVersionsWildcard6
TestSatisfiesOperatorCompletion
TestStringCompletionsImportOrExportSpecifier
TestStringCompletionsVsEscaping
Expand Down
Loading