-
Notifications
You must be signed in to change notification settings - Fork 718
Persistently cached declaration maps #1871
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
base: main
Are you sure you want to change the base?
Changes from all commits
c920059
8423ecf
ca93817
672434a
bb7a5b3
cef9f67
c80e7a3
4a6c6ea
8debdea
bd07e42
75267d1
c26faa9
459ebfe
e2fb018
4eab0b7
166b687
b285539
78e4e23
bb1f175
1a4114a
dd76d55
5d37720
a66fe91
a68e5e1
32f5cb5
c6cf35f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package fourslash_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/fourslash" | ||
"github.com/microsoft/typescript-go/internal/testutil" | ||
) | ||
|
||
func TestDeclarationMapGoToDefinitionChanges(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
const content = `// @Filename: index.ts | ||
export class Foo { | ||
member: string; | ||
methodName(propName: SomeType): void {} | ||
otherMethod() { | ||
if (Math.random() > 0.5) { | ||
return {x: 42}; | ||
} | ||
return {y: "yes"}; | ||
} | ||
} | ||
|
||
export interface SomeType { | ||
member: number; | ||
} | ||
// @Filename: index.d.ts.map | ||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,qBAAa,GAAG;IACZ,MAAM,EAAE,MAAM,CAAC;IACV,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;CAC5C;AAED,MAAM,WAAW,QAAQ;IACrB,MAAM,EAAE,MAAM,CAAC;CAClB"} | ||
// @Filename: index.d.ts | ||
export declare class Foo { | ||
member: string; | ||
methodName(propName: SomeType): void; | ||
} | ||
export interface SomeType { | ||
member: number; | ||
} | ||
//# sourceMappingURL=index.d.ts.map | ||
// @Filename: mymodule.ts | ||
import * as mod from "./index"; | ||
const instance = new mod.Foo(); | ||
instance.[|/*1*/methodName|]({member: 12}); | ||
instance.[|/*2*/otherMethod|]();` | ||
f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
f.VerifyBaselineGoToDefinition(t, "1") | ||
f.GoToFile(t, "index.d.ts") | ||
f.ReplaceAll(t, `export declare class Foo { | ||
member: string; | ||
methodName(propName: SomeType): void; | ||
otherMethod(): { | ||
x: number; | ||
y?: undefined; | ||
} | { | ||
y: string; | ||
x?: undefined; | ||
}; | ||
} | ||
export interface SomeType { | ||
member: number; | ||
} | ||
//# sourceMappingURL=index.d.ts.map`) | ||
f.GoToFile(t, "index.d.ts.map") | ||
f.ReplaceAll(t, `{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,qBAAa,GAAG;IACZ,MAAM,EAAE,MAAM,CAAC;IACV,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;IACzC,WAAW;;;;;;;CAMd;AAED,MAAM,WAAW,QAAQ;IACrB,MAAM,EAAE,MAAM,CAAC;CAClB"}`) | ||
f.VerifyBaselineGoToDefinition(t, "2") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,49 @@ func (l *LanguageService) ProvideDefinition(ctx context.Context, documentURI lsp | |
return l.createLocationsFromDeclarations(declarations), nil | ||
} | ||
|
||
func (l *LanguageService) GetFilesToMapFromDefinition(response lsproto.DefinitionResponse) []string { | ||
var files []string | ||
|
||
if response.Location != nil { | ||
files = append(files, response.Location.Uri.FileName()) | ||
} | ||
|
||
if response.Locations != nil { | ||
for _, location := range *response.Locations { | ||
files = core.AppendIfUnique(files, location.Uri.FileName()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perf nit: AppendIfUnique is an order of magnitude slower than building a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More importantly, I was expecting to see a |
||
} | ||
} | ||
|
||
if response.DefinitionLinks != nil { | ||
for _, link := range *response.DefinitionLinks { | ||
files = core.AppendIfUnique(files, link.TargetUri.FileName()) | ||
} | ||
} | ||
|
||
return files | ||
} | ||
|
||
func (l *LanguageService) GetMappedDefinition(definitions lsproto.DefinitionResponse) lsproto.DefinitionResponse { | ||
if definitions.Location != nil { | ||
definitions.Location = l.getMappedLocation(definitions.Location) | ||
} | ||
if definitions.Locations != nil { | ||
for i, loc := range *definitions.Locations { | ||
(*definitions.Locations)[i] = *l.getMappedLocation(&loc) | ||
} | ||
} | ||
if definitions.DefinitionLinks != nil { | ||
for i, link := range *definitions.DefinitionLinks { | ||
mappedTarget := l.getMappedLocation(&lsproto.Location{Uri: link.TargetUri, Range: link.TargetRange}) | ||
mappedSelection := l.getMappedLocation(&lsproto.Location{Uri: link.TargetUri, Range: link.TargetSelectionRange}) | ||
(*definitions.DefinitionLinks)[i].TargetUri = mappedTarget.Uri | ||
(*definitions.DefinitionLinks)[i].TargetRange = mappedTarget.Range | ||
(*definitions.DefinitionLinks)[i].TargetSelectionRange = mappedSelection.Range | ||
} | ||
} | ||
return definitions | ||
} | ||
|
||
func (l *LanguageService) ProvideTypeDefinition(ctx context.Context, documentURI lsproto.DocumentUri, position lsproto.Position) (lsproto.DefinitionResponse, error) { | ||
program, file := l.getProgramAndFile(documentURI) | ||
node := astnav.GetTouchingPropertyName(file, int(l.converters.LineAndCharacterToPosition(file, position))) | ||
|
@@ -104,17 +147,20 @@ func (l *LanguageService) createLocationsFromDeclarations(declarations []*ast.No | |
for _, decl := range declarations { | ||
file := ast.GetSourceFileOfNode(decl) | ||
name := core.OrElse(ast.GetNameOfDeclaration(decl), decl) | ||
nodeRange := createRangeFromNode(name, file) | ||
mappedLocation := l.getMappedLocation(file.FileName(), nodeRange) | ||
locations = core.AppendIfUnique(locations, mappedLocation) | ||
locations = core.AppendIfUnique(locations, lsproto.Location{ | ||
Uri: FileNameToDocumentURI(file.FileName()), | ||
Range: *l.createLspRangeFromNode(name, file), | ||
}) | ||
} | ||
return lsproto.LocationOrLocationsOrDefinitionLinksOrNull{Locations: &locations} | ||
} | ||
|
||
func (l *LanguageService) createLocationFromFileAndRange(file *ast.SourceFile, textRange core.TextRange) lsproto.DefinitionResponse { | ||
mappedLocation := l.getMappedLocation(file.FileName(), textRange) | ||
return lsproto.LocationOrLocationsOrDefinitionLinksOrNull{ | ||
Location: &mappedLocation, | ||
Location: &lsproto.Location{ | ||
Uri: FileNameToDocumentURI(file.FileName()), | ||
Range: *l.createLspRangeFromBounds(textRange.Pos(), textRange.End(), file), | ||
}, | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed to avoid crashes in the tests, since a file name for a bundled file doesn't round trip with the current implementations of
FileNameToDocumentURI
anddocumentUri.FileName()
.