-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Get type definition at position #2966
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dda598a
Refactor getDefinition code
mhegazy e9f76d0
Add implementation for getTypeDefinitionAtPosition
mhegazy 6f1c307
Add test to shims
mhegazy b6905af
Add getTypeDefinitionAtPosition to tsserver
mhegazy 1de5ea8
Respond to code review comments
mhegazy 70c2d58
Remove erronious change to shims.ts in previous commit
mhegazy f073981
Update command name
mhegazy 7fc6142
Merge branch 'master' into getTypeDefinitionAtPosition
mhegazy 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
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
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
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 |
---|---|---|
|
@@ -97,6 +97,7 @@ module ts.server { | |
export var Rename = "rename"; | ||
export var Saveto = "saveto"; | ||
export var SignatureHelp = "signatureHelp"; | ||
export var TypeDefinition = "typeDefinition"; | ||
export var Unknown = "unknown"; | ||
} | ||
|
||
|
@@ -285,7 +286,29 @@ module ts.server { | |
})); | ||
} | ||
|
||
getOccurrences(line: number, offset: number, fileName: string): protocol.OccurrencesResponseItem[] { | ||
getTypeDefinition(line: number, offset: number, fileName: string): protocol.FileSpan[] { | ||
var file = ts.normalizePath(fileName); | ||
var project = this.projectService.getProjectForFile(file); | ||
if (!project) { | ||
throw Errors.NoProject; | ||
} | ||
|
||
var compilerService = project.compilerService; | ||
var position = compilerService.host.lineOffsetToPosition(file, line, offset); | ||
|
||
var definitions = compilerService.languageService.getTypeDefinitionAtPosition(file, position); | ||
if (!definitions) { | ||
return undefined; | ||
} | ||
|
||
return definitions.map(def => ({ | ||
file: def.fileName, | ||
start: compilerService.host.positionToLineOffset(def.fileName, def.textSpan.start), | ||
end: compilerService.host.positionToLineOffset(def.fileName, ts.textSpanEnd(def.textSpan)) | ||
})); | ||
} | ||
|
||
getOccurrences(line: number, offset: number, fileName: string): protocol.OccurrencesResponseItem[]{ | ||
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. Space before |
||
fileName = ts.normalizePath(fileName); | ||
let project = this.projectService.getProjectForFile(fileName); | ||
|
||
|
@@ -817,6 +840,11 @@ module ts.server { | |
response = this.getDefinition(defArgs.line, defArgs.offset, defArgs.file); | ||
break; | ||
} | ||
case CommandNames.TypeDefinition: { | ||
var defArgs = <protocol.FileLocationRequestArgs>request.arguments; | ||
response = this.getTypeDefinition(defArgs.line, defArgs.offset, defArgs.file); | ||
break; | ||
} | ||
case CommandNames.References: { | ||
var refArgs = <protocol.FileLocationRequestArgs>request.arguments; | ||
response = this.getReferences(refArgs.line, refArgs.offset, refArgs.file); | ||
|
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.
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.
What is the difference between getDefinitionAtPositoin and getTypeDefinitionAtPosition?
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.
One gets you the place where the "symbol" is defined, where the other takes you to the definition location of the "type".
it is a very useful feature when you want to know what is the shape of
value.property
, hovering gives you a type name, but to know the shape of the type you need to first jump to the declaration ofproperty
, then jump to the declaration of its type. it gets even harder with inferred types, cause you need to go to the definition of the function, or variable that produces the contextual type then go to the type from there.editors like Eclipse and monaco have had this feature for long time. also @basarat has added support for it recently on the atom plugin if i am not mistaken.
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.
That makes sense. We shoudl drive this through on the Roslyn side as well though. Definitely open up a bug on them to support this. That way we can get a command in for VB/C#/TS for this scenario, and the experience can be consistent between all three languages.