mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
Add getTypeDefinitionAtPosition to tsserver
This commit is contained in:
parent
6f1c307319
commit
b6905aff0c
@ -301,7 +301,29 @@ module ts.server {
|
||||
}
|
||||
|
||||
getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {
|
||||
throw new Error("Not Implemented Yet.");
|
||||
var lineOffset = this.positionToOneBasedLineOffset(fileName, position);
|
||||
var args: protocol.FileLocationRequestArgs = {
|
||||
file: fileName,
|
||||
line: lineOffset.line,
|
||||
offset: lineOffset.offset,
|
||||
};
|
||||
|
||||
var request = this.processRequest<protocol.TypeDefinitionRequest>(CommandNames.Type, args);
|
||||
var response = this.processResponse<protocol.TypeDefinitionResponse>(request);
|
||||
|
||||
return response.body.map(entry => {
|
||||
var fileName = entry.file;
|
||||
var start = this.lineOffsetToPosition(fileName, entry.start);
|
||||
var end = this.lineOffsetToPosition(fileName, entry.end);
|
||||
return {
|
||||
containerKind: "",
|
||||
containerName: "",
|
||||
fileName: fileName,
|
||||
textSpan: ts.createTextSpanFromBounds(start, end),
|
||||
kind: "",
|
||||
name: ""
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
findReferences(fileName: string, position: number): ReferencedSymbol[]{
|
||||
|
||||
@ -772,7 +772,7 @@ module ts.server {
|
||||
findConfigFile(searchPath: string): string {
|
||||
while (true) {
|
||||
var fileName = ts.combinePaths(searchPath, "tsconfig.json");
|
||||
if (sys.fileExists(fileName)) {
|
||||
if (this.host.fileExists(fileName)) {
|
||||
return fileName;
|
||||
}
|
||||
var parentPath = ts.getDirectoryPath(searchPath);
|
||||
@ -922,7 +922,7 @@ module ts.server {
|
||||
var proj = this.createProject(configFilename, projectOptions);
|
||||
for (var i = 0, len = parsedCommandLine.fileNames.length; i < len; i++) {
|
||||
var rootFilename = parsedCommandLine.fileNames[i];
|
||||
if (ts.sys.fileExists(rootFilename)) {
|
||||
if (this.host.fileExists(rootFilename)) {
|
||||
var info = this.openFile(rootFilename, clientFileName == rootFilename);
|
||||
proj.addRoot(info);
|
||||
}
|
||||
|
||||
15
src/server/protocol.d.ts
vendored
15
src/server/protocol.d.ts
vendored
@ -125,6 +125,14 @@ declare module ts.server.protocol {
|
||||
export interface DefinitionRequest extends FileLocationRequest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Go to type request; value of command field is
|
||||
* "type". Return response giving the file locations that
|
||||
* define the type for the symbol found in file at location line, col.
|
||||
*/
|
||||
export interface TypeDefinitionRequest extends FileLocationRequest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Location in source code expressed as (one-based) line and character offset.
|
||||
*/
|
||||
@ -165,6 +173,13 @@ declare module ts.server.protocol {
|
||||
body?: FileSpan[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Definition response message. Gives text range for definition.
|
||||
*/
|
||||
export interface TypeDefinitionResponse extends Response {
|
||||
body?: FileSpan[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get occurrences request; value of command field is
|
||||
* "occurrences". Return response giving spans that are relevant
|
||||
|
||||
@ -97,6 +97,7 @@ module ts.server {
|
||||
export var Rename = "rename";
|
||||
export var Saveto = "saveto";
|
||||
export var SignatureHelp = "signatureHelp";
|
||||
export var Type = "type";
|
||||
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[]{
|
||||
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.Type: {
|
||||
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);
|
||||
|
||||
12
tests/cases/fourslash/server/typedefinition01.ts
Normal file
12
tests/cases/fourslash/server/typedefinition01.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/// <reference path="../fourslash.ts"/>
|
||||
|
||||
// @Filename: b.ts
|
||||
////import n = require('a');
|
||||
////var x/*1*/ = new n.Foo();
|
||||
|
||||
// @Filename: a.ts
|
||||
//// /*2*/export class Foo {}
|
||||
|
||||
goTo.marker('1');
|
||||
goTo.type();
|
||||
verify.caretAtMarker('2');
|
||||
Loading…
x
Reference in New Issue
Block a user