-
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 4 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 |
---|---|---|
@@ -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.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.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 | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
|
||
"github.com/microsoft/typescript-go/internal/ast" | ||
"github.com/microsoft/typescript-go/internal/core" | ||
"github.com/microsoft/typescript-go/internal/debug" | ||
"github.com/microsoft/typescript-go/internal/diagnostics" | ||
"github.com/microsoft/typescript-go/internal/jsnum" | ||
"github.com/microsoft/typescript-go/internal/stringutil" | ||
|
@@ -2451,31 +2452,42 @@ func GetPositionOfLineAndCharacter(sourceFile *ast.SourceFile, line int, charact | |
} | ||
|
||
func ComputePositionOfLineAndCharacter(lineStarts []core.TextPos, line int, character int) int { | ||
/// !!! debugText, allowEdits | ||
return ComputePositionOfLineAndCharacterEx(lineStarts, line, character, nil, false) | ||
} | ||
|
||
func ComputePositionOfLineAndCharacterEx(lineStarts []core.TextPos, line int, character int, text *string, allowEdits bool) int { | ||
if line < 0 || line >= len(lineStarts) { | ||
// if (allowEdits) { | ||
// // Clamp line to nearest allowable value | ||
// line = line < 0 ? 0 : line >= lineStarts.length ? lineStarts.length - 1 : line; | ||
// } | ||
panic(fmt.Sprintf("Bad line number. Line: %d, lineStarts.length: %d.", line, len(lineStarts))) | ||
if allowEdits { | ||
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 a direct port of Strada and allows cases where the source file and source map are out of sync, see test declarationMapsOutOfDateMapping. |
||
// Clamp line to nearest allowable value | ||
if line < 0 { | ||
line = 0 | ||
} else if line >= len(lineStarts) { | ||
line = len(lineStarts) - 1 | ||
} | ||
} else { | ||
panic(fmt.Sprintf("Bad line number. Line: %d, lineStarts.length: %d.", line, len(lineStarts))) | ||
} | ||
} | ||
|
||
res := int(lineStarts[line]) + character | ||
|
||
// !!! | ||
// if (allowEdits) { | ||
// // Clamp to nearest allowable values to allow the underlying to be edited without crashing (accuracy is lost, instead) | ||
// // TODO: Somehow track edits between file as it was during the creation of sourcemap we have and the current file and | ||
// // apply them to the computed position to improve accuracy | ||
// return res > lineStarts[line + 1] ? lineStarts[line + 1] : typeof debugText === "string" && res > debugText.length ? debugText.length : res; | ||
// } | ||
if allowEdits { | ||
// Clamp to nearest allowable values to allow the underlying to be edited without crashing (accuracy is lost, instead) | ||
// TODO: Somehow track edits between file as it was during the creation of sourcemap we have and the current file and | ||
// apply them to the computed position to improve accuracy | ||
if line+1 < len(lineStarts) && res > int(lineStarts[line+1]) { | ||
return int(lineStarts[line+1]) | ||
} | ||
if text != nil && res > len(*text) { | ||
return len(*text) | ||
} | ||
return res | ||
} | ||
if line < len(lineStarts)-1 && res >= int(lineStarts[line+1]) { | ||
panic("Computed position is beyond that of the following line.") | ||
} else if text != nil { | ||
debug.Assert(res <= len(*text)) // Allow single character overflow for trailing newline | ||
} | ||
// !!! | ||
// else if (debugText !== undefined) { | ||
// Debug.assert(res <= debugText.length); // Allow single character overflow for trailing newline | ||
// } | ||
return res | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.