|
| 1 | +package ls |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/microsoft/typescript-go/internal/core" |
| 5 | + "github.com/microsoft/typescript-go/internal/debug" |
| 6 | + "github.com/microsoft/typescript-go/internal/lsp/lsproto" |
| 7 | + "github.com/microsoft/typescript-go/internal/sourcemap" |
| 8 | + "github.com/microsoft/typescript-go/internal/tspath" |
| 9 | +) |
| 10 | + |
| 11 | +func (l *LanguageService) getMappedLocation(fileName string, fileRange core.TextRange) lsproto.Location { |
| 12 | + startPos := l.tryGetSourcePosition(fileName, core.TextPos(fileRange.Pos())) |
| 13 | + if startPos == nil { |
| 14 | + lspRange := l.createLspRangeFromRange(fileRange, l.getScript(fileName)) |
| 15 | + return lsproto.Location{ |
| 16 | + Uri: FileNameToDocumentURI(fileName), |
| 17 | + Range: *lspRange, |
| 18 | + } |
| 19 | + } |
| 20 | + endPos := l.tryGetSourcePosition(fileName, core.TextPos(fileRange.End())) |
| 21 | + debug.Assert(endPos.FileName == startPos.FileName, "start and end should be in same file") |
| 22 | + newRange := core.NewTextRange(startPos.Pos, endPos.Pos) |
| 23 | + lspRange := l.createLspRangeFromRange(newRange, l.getScript(startPos.FileName)) |
| 24 | + return lsproto.Location{ |
| 25 | + Uri: FileNameToDocumentURI(startPos.FileName), |
| 26 | + Range: *lspRange, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +type script struct { |
| 31 | + fileName string |
| 32 | + text string |
| 33 | +} |
| 34 | + |
| 35 | +func (s *script) FileName() string { |
| 36 | + return s.fileName |
| 37 | +} |
| 38 | + |
| 39 | +func (s *script) Text() string { |
| 40 | + return s.text |
| 41 | +} |
| 42 | + |
| 43 | +func (l *LanguageService) getScript(fileName string) *script { |
| 44 | + text, ok := l.host.ReadFile(fileName) |
| 45 | + if !ok { |
| 46 | + return nil |
| 47 | + } |
| 48 | + return &script{fileName: fileName, text: text} |
| 49 | +} |
| 50 | + |
| 51 | +func (l *LanguageService) tryGetSourcePosition( |
| 52 | + fileName string, |
| 53 | + position core.TextPos, |
| 54 | +) *sourcemap.DocumentPosition { |
| 55 | + newPos := l.tryGetSourcePositionWorker(fileName, position) |
| 56 | + if newPos != nil { |
| 57 | + if _, ok := l.ReadFile(newPos.FileName); !ok { // File doesn't exist |
| 58 | + return nil |
| 59 | + } |
| 60 | + } |
| 61 | + return newPos |
| 62 | +} |
| 63 | + |
| 64 | +func (l *LanguageService) tryGetSourcePositionWorker( |
| 65 | + fileName string, |
| 66 | + position core.TextPos, |
| 67 | +) *sourcemap.DocumentPosition { |
| 68 | + if !tspath.IsDeclarationFileName(fileName) { |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + positionMapper := l.GetDocumentPositionMapper(fileName) |
| 73 | + documentPos := positionMapper.GetSourcePosition(&sourcemap.DocumentPosition{FileName: fileName, Pos: int(position)}) |
| 74 | + if documentPos == nil { |
| 75 | + return nil |
| 76 | + } |
| 77 | + if newPos := l.tryGetSourcePositionWorker(documentPos.FileName, core.TextPos(documentPos.Pos)); newPos != nil { |
| 78 | + return newPos |
| 79 | + } |
| 80 | + return documentPos |
| 81 | +} |
0 commit comments