Skip to content
Merged
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
28 changes: 17 additions & 11 deletions internal/fourslash/_scripts/convertFourslash.mts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
// - `verify.baselineGoToDefinition(...)` called getDefinitionAndBoundSpan
// - `verify.baselineGetDefinitionAtPosition(...)` called getDefinitionAtPosition
// LSP doesn't have two separate commands though.
// It's unclear how we would model bound spans though.
return parseBaselineGoToDefinitionArgs(func.text, callExpression.arguments);
case "baselineRename":
case "baselineRenameAtRangesWithText":
Expand Down Expand Up @@ -1081,6 +1080,10 @@ function parseBaselineGoToDefinitionArgs(
funcName: "baselineGoToDefinition" | "baselineGoToType" | "baselineGetDefinitionAtPosition",
args: readonly ts.Expression[],
): [VerifyBaselineGoToDefinitionCmd] | undefined {
let boundSpan: true | undefined;
if (funcName === "baselineGoToDefinition") {
boundSpan = true;
}
let kind: "verifyBaselineGoToDefinition" | "verifyBaselineGoToType";
switch (funcName) {
case "baselineGoToDefinition":
Expand All @@ -1105,6 +1108,7 @@ function parseBaselineGoToDefinitionArgs(
kind,
markers: [],
ranges: true,
boundSpan,
}];
}
else {
Expand All @@ -1116,6 +1120,7 @@ function parseBaselineGoToDefinitionArgs(
return [{
kind,
markers: newArgs,
boundSpan,
}];
}

Expand Down Expand Up @@ -1592,6 +1597,7 @@ interface VerifyBaselineFindAllReferencesCmd {
interface VerifyBaselineGoToDefinitionCmd {
kind: "verifyBaselineGoToDefinition" | "verifyBaselineGoToType";
markers: string[];
boundSpan?: true;
ranges?: boolean;
}

Expand Down Expand Up @@ -1710,20 +1716,20 @@ function generateBaselineDocumentHighlights({ args, preferences }: VerifyBaselin
return `f.VerifyBaselineDocumentHighlights(t, ${preferences}, ${args.join(", ")})`;
}

function generateBaselineGoToDefinition({ markers, ranges, kind }: VerifyBaselineGoToDefinitionCmd): string {
let goFunc;
function generateBaselineGoToDefinition({ markers, ranges, kind, boundSpan }: VerifyBaselineGoToDefinitionCmd): string {
const originalSelectionRange = boundSpan ? "true" : "false";
switch (kind) {
case "verifyBaselineGoToDefinition":
goFunc = "VerifyBaselineGoToDefinition";
break;
if (ranges || markers.length === 0) {
return `f.VerifyBaselineGoToDefinition(t, ${originalSelectionRange})`;
}
return `f.VerifyBaselineGoToDefinition(t, ${originalSelectionRange}, ${markers.join(", ")})`;
case "verifyBaselineGoToType":
goFunc = "VerifyBaselineGoToTypeDefinition";
break;
}
if (ranges || markers.length === 0) {
return `f.${goFunc}(t)`;
if (ranges || markers.length === 0) {
return `f.VerifyBaselineGoToTypeDefinition(t)`;
}
return `f.VerifyBaselineGoToTypeDefinition(t, ${markers.join(", ")})`;
}
return `f.${goFunc}(t, ${markers.join(", ")})`;
}

function generateGoToCommand({ funcName, args }: GoToCmd): string {
Expand Down
23 changes: 22 additions & 1 deletion internal/fourslash/baselineutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ type baselineFourslashLocationsOptions struct {

startMarkerPrefix func(span lsproto.Location) *string
endMarkerSuffix func(span lsproto.Location) *string

additionalLocation *lsproto.Location
}

func (f *FourslashTest) getBaselineForLocationsWithFileContents(spans []lsproto.Location, options baselineFourslashLocationsOptions) string {
Expand All @@ -152,6 +154,7 @@ func (f *FourslashTest) getBaselineForGroupedLocationsWithFileContents(groupedRa
// but don't want to print it twice at the end if it already
// found in a file with ranges.
foundMarker := false
foundAdditionalLocation := false

baselineEntries := []string{}
err := f.vfs.WalkDir("/", func(path string, d vfs.DirEntry, e error) error {
Expand Down Expand Up @@ -179,6 +182,10 @@ func (f *FourslashTest) getBaselineForGroupedLocationsWithFileContents(groupedRa
foundMarker = true
}

if options.additionalLocation != nil && options.additionalLocation.Uri == fileName {
foundAdditionalLocation = true
}

baselineEntries = append(baselineEntries, f.getBaselineContentForFile(path, content, ranges, nil, options))
return nil
})
Expand All @@ -187,6 +194,21 @@ func (f *FourslashTest) getBaselineForGroupedLocationsWithFileContents(groupedRa
panic("walkdir error during fourslash baseline: " + err.Error())
}

// In Strada, there is a bug where we only ever add additional spans to baselines if we haven't
// already added the file to the baseline.
if options.additionalLocation != nil && !foundAdditionalLocation {
fileName := options.additionalLocation.Uri.FileName()
if content, ok := f.vfs.ReadFile(fileName); ok {
baselineEntries = append(
baselineEntries,
f.getBaselineContentForFile(fileName, content, []lsproto.Range{options.additionalLocation.Range}, nil, options),
)
if options.marker != nil && options.marker.FileName() == fileName {
foundMarker = true
}
}
}

if !foundMarker && options.marker != nil {
// If we didn't find the marker in any file, we need to add it.
markerFileName := options.marker.FileName()
Expand All @@ -195,7 +217,6 @@ func (f *FourslashTest) getBaselineForGroupedLocationsWithFileContents(groupedRa
}
}

// !!! foundAdditionalSpan
// !!! skipDocumentContainingOnlyMarker

return strings.Join(baselineEntries, "\n\n")
Expand Down
44 changes: 40 additions & 4 deletions internal/fourslash/fourslash.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ var (
ItemDefaults: &[]string{"commitCharacters", "editRange"},
},
}
defaultDefinitionCapabilities = &lsproto.DefinitionClientCapabilities{
LinkSupport: ptrTrue,
}
defaultTypeDefinitionCapabilities = &lsproto.TypeDefinitionClientCapabilities{
LinkSupport: ptrTrue,
}
)

func getCapabilitiesWithDefaults(capabilities *lsproto.ClientCapabilities) *lsproto.ClientCapabilities {
Expand Down Expand Up @@ -278,6 +284,12 @@ func getCapabilitiesWithDefaults(capabilities *lsproto.ClientCapabilities) *lspr
if capabilitiesWithDefaults.Workspace.Configuration == nil {
capabilitiesWithDefaults.Workspace.Configuration = ptrTrue
}
if capabilitiesWithDefaults.TextDocument.Definition == nil {
capabilitiesWithDefaults.TextDocument.Definition = defaultDefinitionCapabilities
}
if capabilitiesWithDefaults.TextDocument.TypeDefinition == nil {
capabilitiesWithDefaults.TextDocument.TypeDefinition = defaultTypeDefinitionCapabilities
}
return &capabilitiesWithDefaults
}

Expand Down Expand Up @@ -1032,6 +1044,7 @@ func (f *FourslashTest) VerifyBaselineFindAllReferences(

func (f *FourslashTest) VerifyBaselineGoToDefinition(
t *testing.T,
includeOriginalSelectionRange bool,
markers ...string,
) {
referenceLocations := f.lookupMarkersOrGetRanges(t, markers)
Expand Down Expand Up @@ -1064,17 +1077,35 @@ func (f *FourslashTest) VerifyBaselineGoToDefinition(
}

var resultAsLocations []lsproto.Location
var additionalLocation *lsproto.Location
if result.Locations != nil {
resultAsLocations = *result.Locations
} else if result.Location != nil {
resultAsLocations = []lsproto.Location{*result.Location}
} else if result.DefinitionLinks != nil {
t.Fatalf("Unexpected definition response type at marker '%s': %T", *f.lastKnownMarkerName, result.DefinitionLinks)
var originRange *lsproto.Range
resultAsLocations = core.Map(*result.DefinitionLinks, func(link *lsproto.LocationLink) lsproto.Location {
if originRange != nil && originRange != link.OriginSelectionRange {
panic("multiple different origin ranges in definition links")
}
originRange = link.OriginSelectionRange
return lsproto.Location{
Uri: link.TargetUri,
Range: link.TargetSelectionRange,
}
})
if originRange != nil && includeOriginalSelectionRange {
additionalLocation = &lsproto.Location{
Uri: lsconv.FileNameToDocumentURI(f.activeFilename),
Range: *originRange,
}
}
}

f.addResultToBaseline(t, "goToDefinition", f.getBaselineForLocationsWithFileContents(resultAsLocations, baselineFourslashLocationsOptions{
marker: markerOrRange,
markerName: "/*GOTO DEF*/",
marker: markerOrRange,
markerName: "/*GOTO DEF*/",
additionalLocation: additionalLocation,
}))
}
}
Expand Down Expand Up @@ -1118,7 +1149,12 @@ func (f *FourslashTest) VerifyBaselineGoToTypeDefinition(
} else if result.Location != nil {
resultAsLocations = []lsproto.Location{*result.Location}
} else if result.DefinitionLinks != nil {
t.Fatalf("Unexpected type definition response type at marker '%s': %T", *f.lastKnownMarkerName, result.DefinitionLinks)
resultAsLocations = core.Map(*result.DefinitionLinks, func(link *lsproto.LocationLink) lsproto.Location {
return lsproto.Location{
Uri: link.TargetUri,
Range: link.TargetSelectionRange,
}
})
}

f.addResultToBaseline(t, "goToType", f.getBaselineForLocationsWithFileContents(resultAsLocations, baselineFourslashLocationsOptions{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ import [|/*importBang*/bang|] = require("jquery");
f.VerifyQuickInfoAt(t, "useBar", "(alias) module \"jquery\"\nimport bar", "")
f.VerifyQuickInfoAt(t, "useBaz", "(alias) module \"jquery\"\nimport baz", "")
f.VerifyQuickInfoAt(t, "useBang", "(alias) module \"jquery\"\nimport bang = require(\"jquery\")", "")
f.VerifyBaselineGoToDefinition(t, "useFoo", "importFoo", "useBar", "useBaz", "importBaz", "useBang", "importBang")
f.VerifyBaselineGoToDefinition(t, true, "useFoo", "importFoo", "useBar", "useBaz", "importBaz", "useBang", "importBang")
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ import * as mod from "./indexdef";
const instance = new mod.Foo();
instance.[|/*1*/methodName|]({member: 12});`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "1")
f.VerifyBaselineGoToDefinition(t, true, "1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ import * as mod from "./out/indexdef";
const instance = new mod.Foo();
instance.[|/*1*/methodName|]({member: 12});`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "1")
f.VerifyBaselineGoToDefinition(t, true, "1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ class Button extends [|/*1*/Control|] {
}
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "1", "3")
f.VerifyBaselineGoToDefinition(t, true, "1", "3")
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ export class /*2*/Foo {
import { Foo/*1*/ } from "a";`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.GoToFile(t, "/home/src/workspaces/project/index.ts")
f.VerifyBaselineGoToDefinition(t, "1")
f.VerifyBaselineGoToDefinition(t, false, "1")
}
2 changes: 1 addition & 1 deletion internal/fourslash/tests/gen/definition01_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ var x = new n.Foo();
// @Filename: a.ts
/*2*/export class Foo {}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "1")
f.VerifyBaselineGoToDefinition(t, true, "1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func TestDefinitionNameOnEnumMember(t *testing.T) {
}
var enumMember = e.[|/*1*/thirdMember|];`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "1")
f.VerifyBaselineGoToDefinition(t, false, "1")
}
2 changes: 1 addition & 1 deletion internal/fourslash/tests/gen/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ var x = new n.Foo();
// @Filename: a.ts
/*2*/export class Foo {}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "1")
f.VerifyBaselineGoToDefinition(t, true, "1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import /*deg*/g from "./a";
import { f } from "./a";`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineFindAllReferences(t, "def", "deg")
f.VerifyBaselineGoToDefinition(t, "ref")
f.VerifyBaselineGoToDefinition(t, true, "ref")
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ var /*def4*/x: number;
/// <reference path="d.ts" />
[|/*use*/x|]++;`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "use")
f.VerifyBaselineGoToDefinition(t, true, "use")
}
2 changes: 1 addition & 1 deletion internal/fourslash/tests/gen/goToDefinitionAlias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export interface IFoo {
x;
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "alias1Type", "alias1Value", "alias2Type", "alias2Value")
f.VerifyBaselineGoToDefinition(t, true, "alias1Type", "alias1Value", "alias2Type", "alias2Value")
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ var ambientClassVariable = new /*constructorReference*/ambientClass();
ambientClass./*staticMethodReference*/method();
ambientClassVariable./*instanceMethodReference*/method();`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "ambientVariableReference", "ambientFunctionReference", "constructorReference", "staticMethodReference", "instanceMethodReference")
f.VerifyBaselineGoToDefinition(t, false, "ambientVariableReference", "ambientFunctionReference", "constructorReference", "staticMethodReference", "instanceMethodReference")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ var o = 0;
o.[|/*reference1*/myObjectMethod|]();
o[[|"/*reference2*/myObjectMethod"|]]();`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "reference1", "reference2")
f.VerifyBaselineGoToDefinition(t, true, "reference1", "reference2")
}
2 changes: 1 addition & 1 deletion internal/fourslash/tests/gen/goToDefinitionAwait1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ function notAsync() {
[|/*start2*/await|] Promise.resolve(0);
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "start1", "start2")
f.VerifyBaselineGoToDefinition(t, true, "start1", "start2")
}
2 changes: 1 addition & 1 deletion internal/fourslash/tests/gen/goToDefinitionAwait2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func TestGoToDefinitionAwait2(t *testing.T) {
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `[|/*start*/await|] Promise.resolve(0);`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "start")
f.VerifyBaselineGoToDefinition(t, true, "start")
}
2 changes: 1 addition & 1 deletion internal/fourslash/tests/gen/goToDefinitionAwait3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func TestGoToDefinitionAwait3(t *testing.T) {
}
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "start1", "start2")
f.VerifyBaselineGoToDefinition(t, true, "start1", "start2")
}
2 changes: 1 addition & 1 deletion internal/fourslash/tests/gen/goToDefinitionAwait4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func TestGoToDefinitionAwait4(t *testing.T) {
}
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "start")
f.VerifyBaselineGoToDefinition(t, true, "start")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ var s: /*string*/string;
var b: /*boolean*/boolean;
var v: /*void*/void;`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, f.MarkerNames()...)
f.VerifyBaselineGoToDefinition(t, true, f.MarkerNames()...)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ var a = function() { return /*arguments*/arguments; };
var t = /*true*/true;
var f = /*false*/false;`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, f.MarkerNames()...)
f.VerifyBaselineGoToDefinition(t, true, f.MarkerNames()...)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ declare module /*2b*/"*.css" {
// @Filename: index.ts
import styles from [|/*1*/"./index.css"|];`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "1")
f.VerifyBaselineGoToDefinition(t, true, "1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ class HasConstructor extends Base {
}
const hasConstructor = new [|/*HasConstructor*/HasConstructor|](cArg)`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "Derived", "SameFile", "HasConstructor", "Base")
f.VerifyBaselineGoToDefinition(t, true, "Derived", "SameFile", "HasConstructor", "Base")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func TestGoToDefinitionClassStaticBlocks(t *testing.T) {
[|/*classStaticBocks3*/static|] {}
}`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "classStaticBocks1", "classStaticBocks2", "classStaticBocks3")
f.VerifyBaselineGoToDefinition(t, true, "classStaticBocks1", "classStaticBocks2", "classStaticBocks3")
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ var x2 = new [|/*xref*/x|]();
var y1 = new [|/*yref*/y|]();
var z1 = new [|/*zref*/z|]();`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "xusage", "yusage", "zusage", "cref", "xref", "yref", "zref")
f.VerifyBaselineGoToDefinition(t, true, "xusage", "yusage", "zusage", "cref", "xref", "yref", "zref")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class Foo {

var x = new [|/*usage*/Foo|]();`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "usage")
f.VerifyBaselineGoToDefinition(t, true, "usage")
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ class Extended extends ConstructorOverload {
var extended1 = new [|/*extendedRef1*/Extended|]();
var extended2 = new [|/*extendedRef2*/Extended|]("foo");`
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
f.VerifyBaselineGoToDefinition(t, "constructorOverloadReference1", "constructorOverloadReference2", "constructorOverload1", "extendedRef1", "extendedRef2")
f.VerifyBaselineGoToDefinition(t, true, "constructorOverloadReference1", "constructorOverloadReference2", "constructorOverload1", "extendedRef1", "extendedRef2")
}
Loading