-
Notifications
You must be signed in to change notification settings - Fork 714
Port source map position mapping + go to definition source mapping #1767
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
Changes from 6 commits
c920059
8423ecf
ca93817
672434a
bb7a5b3
cef9f67
c80e7a3
4a6c6ea
8debdea
bd07e42
75267d1
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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
package ls | ||
|
||
import ( | ||
"github.com/microsoft/typescript-go/internal/compiler" | ||
) | ||
|
||
type Host interface { | ||
GetProgram() *compiler.Program | ||
UseCaseSensitiveFileNames() bool | ||
ReadFile(path string) (contents string, ok bool) | ||
FileExists(path string) bool | ||
Converters() *Converters | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ls | ||
|
||
import ( | ||
"github.com/microsoft/typescript-go/internal/core" | ||
"github.com/microsoft/typescript-go/internal/debug" | ||
"github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
"github.com/microsoft/typescript-go/internal/sourcemap" | ||
"github.com/microsoft/typescript-go/internal/tspath" | ||
) | ||
|
||
func (l *LanguageService) getMappedLocation(fileName string, fileRange core.TextRange) lsproto.Location { | ||
startPos := l.tryGetSourcePosition(fileName, core.TextPos(fileRange.Pos())) | ||
if startPos == nil { | ||
lspRange := l.createLspRangeFromRange(fileRange, l.getScript(fileName)) | ||
return lsproto.Location{ | ||
Uri: FileNameToDocumentURI(fileName), | ||
Range: *lspRange, | ||
} | ||
} | ||
endPos := l.tryGetSourcePosition(fileName, core.TextPos(fileRange.End())) | ||
debug.Assert(endPos.FileName == startPos.FileName, "start and end should be in same file") | ||
newRange := core.NewTextRange(startPos.Pos, endPos.Pos) | ||
lspRange := l.createLspRangeFromRange(newRange, l.getScript(startPos.FileName)) | ||
return lsproto.Location{ | ||
Uri: FileNameToDocumentURI(startPos.FileName), | ||
Range: *lspRange, | ||
} | ||
} | ||
|
||
type script struct { | ||
fileName string | ||
text string | ||
} | ||
|
||
func (s *script) FileName() string { | ||
return s.fileName | ||
} | ||
|
||
func (s *script) Text() string { | ||
return s.text | ||
} | ||
|
||
func (l *LanguageService) getScript(fileName string) *script { | ||
text, ok := l.host.ReadFile(fileName) | ||
if !ok { | ||
return nil | ||
} | ||
return &script{fileName: fileName, text: text} | ||
} | ||
|
||
func (l *LanguageService) tryGetSourcePosition( | ||
fileName string, | ||
position core.TextPos, | ||
) *sourcemap.DocumentPosition { | ||
newPos := l.tryGetSourcePositionWorker(fileName, position) | ||
if newPos != nil { | ||
if !l.host.FileExists(newPos.FileName) { | ||
return nil | ||
} | ||
} | ||
return newPos | ||
} | ||
|
||
func (l *LanguageService) tryGetSourcePositionWorker( | ||
fileName string, | ||
position core.TextPos, | ||
) *sourcemap.DocumentPosition { | ||
if !tspath.IsDeclarationFileName(fileName) { | ||
return nil | ||
} | ||
|
||
positionMapper := l.GetDocumentPositionMapper(fileName) | ||
documentPos := positionMapper.GetSourcePosition(&sourcemap.DocumentPosition{FileName: fileName, Pos: int(position)}) | ||
if documentPos == nil { | ||
return nil | ||
} | ||
if newPos := l.tryGetSourcePositionWorker(documentPos.FileName, core.TextPos(documentPos.Pos)); newPos != nil { | ||
return newPos | ||
} | ||
return documentPos | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package project | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/microsoft/typescript-go/internal/collections" | ||
"github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
"github.com/microsoft/typescript-go/internal/project/dirty" | ||
"github.com/microsoft/typescript-go/internal/tspath" | ||
|
@@ -24,8 +27,11 @@ type snapshotFS struct { | |
fs vfs.FS | ||
overlays map[tspath.Path]*overlay | ||
diskFiles map[tspath.Path]*diskFile | ||
readFiles collections.SyncMap[tspath.Path, memoizedDiskFile] | ||
} | ||
|
||
type memoizedDiskFile func() *diskFile | ||
|
||
func (s *snapshotFS) FS() vfs.FS { | ||
return s.fs | ||
} | ||
|
@@ -37,6 +43,15 @@ func (s *snapshotFS) GetFile(fileName string) FileHandle { | |
if file, ok := s.diskFiles[s.toPath(fileName)]; ok { | ||
return file | ||
} | ||
newEntry := memoizedDiskFile(sync.OnceValue(func() *diskFile { | ||
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. This is probably fine, but I had imagined a change to 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. There were other usages of 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. yeah caching at cached.vfs level means you need to clear it on "writeFile" otherwise tsc -b will run into issues if we dont handle that well - which would add to cost right? and normally in those scenarios the text gets cached in "sourceFile" so another cache is generally not needed 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. Thanks for clarifying! 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. I guess that makes sense; there are already some weird inconsistencies with cachedvfs (FileExists can be out of sync with Stat can be out of sync with ReadFile) but I guess we can avoid growing those any more. |
||
if contents, ok := s.fs.ReadFile(fileName); ok { | ||
return newDiskFile(fileName, contents) | ||
} | ||
return nil | ||
})) | ||
if entry, ok := s.readFiles.LoadOrStore(s.toPath(fileName), newEntry); ok { | ||
return entry() | ||
} | ||
return nil | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.