-
Notifications
You must be signed in to change notification settings - Fork 718
[LSP] fix go to definition #1627
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
Closed
Closed
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
655a949
fix go to definition
rgoomar 89b1994
clean up implementation
rgoomar e67298a
simplify to just go to file
rgoomar f0a74ac
remove os lookups and stick to vfs and fix lookup
rgoomar 6a70dfe
Use vlq decoding to get line position
rgoomar 6a2fe24
fix linting and format
rgoomar f117f1d
fix baselines and logic to get passing tests
rgoomar a2675d0
remove comments
rgoomar a593f67
fix regression
rgoomar 9156feb
fix positioning
rgoomar 47331cd
adjust logic to use methods that exist
rgoomar 80fc178
adjust readFile approach
rgoomar b4370af
remove alternative declarations call
rgoomar d9f3125
fix paths
rgoomar 3124114
Merge branch 'main' into fix-go-to-def
rgoomar f44ea36
adjust logic
rgoomar 7d1a877
adjust logic to use ls conversion
rgoomar b984eb4
avoid using core methods
rgoomar cb9083a
change converter
rgoomar 32177b9
fix new line
rgoomar 3283874
adjust declfile
rgoomar af794f4
simplify types
rgoomar d720037
simplify types and interfaces
rgoomar e7732eb
fix formatting
rgoomar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package ls | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/microsoft/typescript-go/internal/compiler" | ||
"github.com/microsoft/typescript-go/internal/core" | ||
"github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
"github.com/microsoft/typescript-go/internal/sourcemap" | ||
"github.com/microsoft/typescript-go/internal/tspath" | ||
) | ||
|
||
type sourcemapHost struct { | ||
program *compiler.Program | ||
} | ||
|
||
|
||
func (h *sourcemapHost) GetSourceFileLike(fileName string) sourcemap.SourceFileLike { | ||
if content, ok := h.readFileWithFallback(fileName); ok { | ||
return &sourcemapSourceFile{ | ||
text: content, | ||
lineStarts: core.ComputeLineStarts(content), | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (h *sourcemapHost) GetCanonicalFileName(path string) string { | ||
return tspath.GetCanonicalFileName(path, h.program.UseCaseSensitiveFileNames()) | ||
} | ||
|
||
func (h *sourcemapHost) Log(text string) { | ||
} | ||
|
||
func (h *sourcemapHost) UseCaseSensitiveFileNames() bool { | ||
return h.program.UseCaseSensitiveFileNames() | ||
} | ||
|
||
func (h *sourcemapHost) GetCurrentDirectory() string { | ||
return h.program.Host().GetCurrentDirectory() | ||
} | ||
|
||
func (h *sourcemapHost) ReadFile(path string) (string, bool) { | ||
return h.readFileWithFallback(path) | ||
} | ||
|
||
func (h *sourcemapHost) FileExists(path string) bool { | ||
return h.program.Host().FS().FileExists(path) | ||
} | ||
|
||
// Tries to read a file from the program's FS first, and falls back to underlying FS for files not tracked by the program. | ||
func (h *sourcemapHost) readFileWithFallback(fileName string) (string, bool) { | ||
if content, ok := h.program.Host().FS().ReadFile(fileName); ok { | ||
return content, true | ||
} | ||
|
||
if fallbackFS, ok := h.program.Host().FS().(sourcemap.FallbackFileReader); ok { | ||
return fallbackFS.ReadFileWithFallback(fileName) | ||
} | ||
|
||
return "", false | ||
} | ||
|
||
|
||
type sourcemapSourceFile struct { | ||
text string | ||
lineStarts []core.TextPos | ||
} | ||
|
||
func (f *sourcemapSourceFile) Text() string { | ||
return f.text | ||
} | ||
|
||
func (f *sourcemapSourceFile) LineStarts() []core.TextPos { | ||
return f.lineStarts | ||
} | ||
|
||
type sourcemapFileReader struct { | ||
host *sourcemapHost | ||
} | ||
|
||
func (r *sourcemapFileReader) ReadFile(path string) (string, bool) { | ||
return r.host.readFileWithFallback(path) | ||
} | ||
|
||
// Creates a SourceMapper for the given program. | ||
func CreateSourceMapperForProgram(program *compiler.Program) sourcemap.SourceMapper { | ||
host := &sourcemapHost{program: program} | ||
fileReader := &sourcemapFileReader{host: host} | ||
return sourcemap.CreateSourceMapper(host, fileReader) | ||
} | ||
|
||
|
||
// Maps a single definition location using source maps. | ||
func MapSingleDefinitionLocation(program *compiler.Program, location lsproto.Location) *lsproto.Location { | ||
fileName := location.Uri.FileName() | ||
|
||
if !strings.HasSuffix(fileName, ".d.ts") { | ||
return nil | ||
} | ||
|
||
if strings.HasPrefix(fileName, "^/bundled/") { | ||
return nil | ||
} | ||
|
||
host := &sourcemapHost{program: program} | ||
sourceMapper := CreateSourceMapperForProgram(program) | ||
|
||
return tryMapLocation(sourceMapper, host, location) | ||
} | ||
|
||
func tryMapLocation(sourceMapper sourcemap.SourceMapper, host *sourcemapHost, location lsproto.Location) *lsproto.Location { | ||
fileName := location.Uri.FileName() | ||
|
||
declContent, ok := host.readFileWithFallback(fileName) | ||
rgoomar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if !ok { | ||
return nil | ||
} | ||
|
||
declLineStarts := core.ComputeLineStarts(declContent) | ||
rgoomar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if int(location.Range.Start.Line) >= len(declLineStarts) || int(location.Range.End.Line) >= len(declLineStarts) { | ||
return nil | ||
} | ||
|
||
declStartPos := int(declLineStarts[location.Range.Start.Line]) + int(location.Range.Start.Character) | ||
declEndPos := int(declLineStarts[location.Range.End.Line]) + int(location.Range.End.Character) | ||
|
||
startInput := sourcemap.DocumentPosition{ | ||
FileName: fileName, | ||
Pos: declStartPos, | ||
} | ||
|
||
startResult := sourceMapper.TryGetSourcePosition(startInput) | ||
if startResult == nil { | ||
return nil | ||
} | ||
|
||
sourceContent, ok := host.readFileWithFallback(startResult.FileName) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
sourceLineStarts := core.ComputeLineStarts(sourceContent) | ||
|
||
sourceStartLine, sourceStartChar := core.PositionToLineAndCharacter(startResult.Pos, sourceLineStarts) | ||
|
||
originalRangeLength := declEndPos - declStartPos | ||
|
||
if declStartPos >= 0 && declEndPos <= len(declContent) { | ||
originalText := strings.TrimSpace(declContent[declStartPos:declEndPos]) | ||
|
||
if isSimpleIdentifier(originalText) { | ||
sourceEndPos := startResult.Pos + len(originalText) | ||
|
||
if sourceEndPos > len(sourceContent) { | ||
sourceEndPos = len(sourceContent) | ||
} | ||
|
||
sourceEndLine, sourceEndChar := core.PositionToLineAndCharacter(sourceEndPos, sourceLineStarts) | ||
|
||
return &lsproto.Location{ | ||
Uri: FileNameToDocumentURI(startResult.FileName), | ||
Range: lsproto.Range{ | ||
Start: lsproto.Position{Line: uint32(sourceStartLine), Character: uint32(sourceStartChar)}, | ||
End: lsproto.Position{Line: uint32(sourceEndLine), Character: uint32(sourceEndChar)}, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
sourceEndPos := startResult.Pos + originalRangeLength | ||
|
||
if sourceEndPos > len(sourceContent) { | ||
sourceEndPos = len(sourceContent) | ||
} | ||
|
||
sourceEndLine, sourceEndChar := core.PositionToLineAndCharacter(sourceEndPos, sourceLineStarts) | ||
|
||
return &lsproto.Location{ | ||
Uri: FileNameToDocumentURI(startResult.FileName), | ||
Range: lsproto.Range{ | ||
Start: lsproto.Position{Line: uint32(sourceStartLine), Character: uint32(sourceStartChar)}, | ||
End: lsproto.Position{Line: uint32(sourceEndLine), Character: uint32(sourceEndChar)}, | ||
rgoomar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}, | ||
} | ||
} | ||
|
||
|
||
func isIdentifierChar(ch byte) bool { | ||
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || | ||
(ch >= '0' && ch <= '9') || ch == '_' || ch == '$' | ||
} | ||
|
||
func isSimpleIdentifier(text string) bool { | ||
if len(text) == 0 { | ||
return false | ||
} | ||
|
||
first := text[0] | ||
if !((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_' || first == '$') { | ||
return false | ||
} | ||
|
||
for i := 1; i < len(text); i++ { | ||
if !isIdentifierChar(text[i]) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.