@@ -10,7 +10,7 @@ import * as ts from 'typescript'; // used as value, passed in by tsserver at run
1010import * as tss from 'typescript/lib/tsserverlibrary' ; // used as type only
1111
1212import { createLanguageService } from './language_service' ;
13- import { Completion , Diagnostic , DiagnosticMessageChain } from './types' ;
13+ import { Completion , Diagnostic , DiagnosticMessageChain , Location } from './types' ;
1414import { TypeScriptServiceHost } from './typescript_host' ;
1515
1616const projectHostMap = new WeakMap < tss . server . Project , TypeScriptServiceHost > ( ) ;
@@ -156,36 +156,61 @@ export function create(info: tss.server.PluginCreateInfo): ts.LanguageService {
156156 return base ;
157157 } ;
158158
159- proxy . getDefinitionAtPosition = function (
160- fileName : string , position : number ) : ReadonlyArray < ts . DefinitionInfo > {
161- let base = oldLS . getDefinitionAtPosition ( fileName , position ) ;
162- if ( base && base . length ) {
163- return base ;
164- }
159+ proxy . getDefinitionAtPosition = function ( fileName : string , position : number ) :
160+ ReadonlyArray < ts . DefinitionInfo > |
161+ undefined {
162+ const base = oldLS . getDefinitionAtPosition ( fileName , position ) ;
163+ if ( base && base . length ) {
164+ return base ;
165+ }
166+ const ours = ls . getDefinitionAt ( fileName , position ) ;
167+ if ( ours && ours . length ) {
168+ return ours . map ( ( loc : Location ) => {
169+ return {
170+ fileName : loc . fileName ,
171+ textSpan : {
172+ start : loc . span . start ,
173+ length : loc . span . end - loc . span . start ,
174+ } ,
175+ name : '' ,
176+ kind : ts . ScriptElementKind . unknown ,
177+ containerName : loc . fileName ,
178+ containerKind : ts . ScriptElementKind . unknown ,
179+ } ;
180+ } ) ;
181+ }
182+ } ;
165183
166- return tryOperation ( 'get definition' , ( ) => {
167- const ours = ls . getDefinitionAt ( fileName , position ) ;
168- let combined ;
169-
170- if ( ours && ours . length ) {
171- combined = base && base . concat ( [ ] ) || [ ] ;
172- for ( const loc of ours ) {
173- combined . push ( {
174- fileName : loc . fileName ,
175- textSpan : { start : loc . span . start , length : loc . span . end - loc . span . start } ,
176- name : '' ,
177- // TODO: remove any and fix type error.
178- kind : 'definition' as any ,
179- containerName : loc . fileName ,
180- containerKind : 'file' as any ,
181- } ) ;
182- }
183- } else {
184- combined = base ;
185- }
186- return combined ;
187- } ) || [ ] ;
188- } ;
184+ proxy . getDefinitionAndBoundSpan = function ( fileName : string , position : number ) :
185+ ts . DefinitionInfoAndBoundSpan |
186+ undefined {
187+ const base = oldLS . getDefinitionAndBoundSpan ( fileName , position ) ;
188+ if ( base && base . definitions && base . definitions . length ) {
189+ return base ;
190+ }
191+ const ours = ls . getDefinitionAt ( fileName , position ) ;
192+ if ( ours && ours . length ) {
193+ return {
194+ definitions : ours . map ( ( loc : Location ) => {
195+ return {
196+ fileName : loc . fileName ,
197+ textSpan : {
198+ start : loc . span . start ,
199+ length : loc . span . end - loc . span . start ,
200+ } ,
201+ name : '' ,
202+ kind : ts . ScriptElementKind . unknown ,
203+ containerName : loc . fileName ,
204+ containerKind : ts . ScriptElementKind . unknown ,
205+ } ;
206+ } ) ,
207+ textSpan : {
208+ start : ours [ 0 ] . span . start ,
209+ length : ours [ 0 ] . span . end - ours [ 0 ] . span . start ,
210+ } ,
211+ } ;
212+ }
213+ } ;
189214
190215 return proxy ;
191216}
0 commit comments