@@ -3,7 +3,8 @@ import { Settings } from '../../settings';
33import { Logger } from '../logger' ;
44import { PathUtils } from '../common/pathUtils' ;
55import { integer } from 'vscode-languageclient' ;
6- import { DocumentInfoProvider } from '../documentInfoProvider' ;
6+ import { DocumentInfo , DocumentInfoProvider } from '../documentInfoProvider' ;
7+ import path = require( 'path' ) ;
78
89export interface PossibleCodeObjectLocation {
910
@@ -40,17 +41,66 @@ export interface IModulePathToUriConverter {
4041}
4142
4243export class LogicalModulePathToUriConverter implements IModulePathToUriConverter {
43-
44+ readonly _commonLogic : CommonConverterLogic = new CommonConverterLogic ( )
45+
46+ public constructor ( private _documentInfoProvider : DocumentInfoProvider ) {
47+
48+ }
4449 async convert ( pathInfo : PossibleCodeObjectLocation ) : Promise < vscode . Location | undefined > {
4550 const { moduleLogicalPath } = pathInfo ;
4651 if ( moduleLogicalPath ) {
52+ let range :vscode . Range | undefined = undefined ;
4753 let symbols = await this . lookupCodeObjectByFullName ( moduleLogicalPath ) ;
48- symbols = symbols . filter ( x => x . name . endsWith ( moduleLogicalPath ) ) ;
49- //We have a match
50- if ( symbols . length === 1 ) {
51- return symbols [ 0 ] . location ;
52-
54+ symbols = symbols . filter ( x => moduleLogicalPath . endsWith ( x . name ) ) ;
55+
56+ const classesOrModules = symbols . filter ( x => x . kind === vscode . SymbolKind . Class
57+ || x . kind === vscode . SymbolKind . Module ) ;
58+
59+
60+ //If the logical name resolves to a class or module, our job isn't done
61+ for ( const c of classesOrModules ) {
62+ const textDoc = await vscode . workspace . openTextDocument ( c . location . uri ) ;
63+ const docInfo = await this . _documentInfoProvider . getDocumentInfo ( textDoc ) ;
64+
65+ if ( docInfo ) {
66+ range = this . _commonLogic . findInDocument ( docInfo , pathInfo ) ;
67+ if ( range ) {
68+ return new vscode . Location ( textDoc . uri , range ) ;
69+ }
70+ }
71+ }
72+
73+ var methodMatches =
74+ symbols . filter ( symbol => ( symbol . kind === vscode . SymbolKind . Function ||
75+ symbol . kind === vscode . SymbolKind . Method ) ) ;
76+
77+
78+ if ( pathInfo . methodName ) {
79+ methodMatches = methodMatches . filter ( method => {
80+ if ( pathInfo . methodName ) {
81+ return method . name . endsWith ( pathInfo . methodName ) ;
82+ }
83+ } ) ;
84+ }
85+
86+ if ( pathInfo . moduleLogicalPath ) {
87+ methodMatches = methodMatches . filter ( method => {
88+ if ( pathInfo . moduleLogicalPath ) {
89+ return pathInfo . moduleLogicalPath . endsWith ( method . name ) ;
90+ }
91+ } ) ;
5392 }
93+
94+ if ( methodMatches . length === 1 ) {
95+ return methodMatches [ 0 ] . location ;
96+ }
97+
98+ if ( classesOrModules . length == 1 ) {
99+ return classesOrModules [ 0 ] . location ;
100+
101+ }
102+ //We have a match
103+
54104 }
55105 }
56106
@@ -59,9 +109,41 @@ export class LogicalModulePathToUriConverter implements IModulePathToUriConverte
59109 }
60110}
61111
112+ export class CommonConverterLogic {
113+ public findInDocument ( docInfo : DocumentInfo , pathInfo : PossibleCodeObjectLocation ) : vscode . Range | undefined {
114+ let range :vscode . Range | undefined = undefined ;
115+ if ( pathInfo . lineNumber ) {
116+ range =
117+ new vscode . Range (
118+ new vscode . Position ( pathInfo . lineNumber , 0 ) ,
119+ new vscode . Position ( pathInfo . lineNumber + 1 , 0 ) ) ;
120+ }
121+ else if ( pathInfo . spanName ) {
122+ var span = docInfo ?. spans . filter ( x => x . name == pathInfo . spanName ) . firstOrDefault ( ) ;
123+ if ( span ) {
124+ range = new vscode . Range ( new vscode . Position ( span . range . start . line + 1 , span . range . start . character ) ,
125+ new vscode . Position ( span . range . start . line + 2 , span . range . start . character + 1 ) ) ; }
126+ }
127+
128+ else if ( pathInfo . methodName ) {
129+ var method = docInfo ?. methods . filter ( x => {
130+ const methoId = x . id . split ( "$_$" ) . lastOrDefault ( ) ;
131+ if ( ! methoId ) { return false } ;
132+ return methoId === pathInfo . methodName ;
133+ } ) . firstOrDefault ( ) ;
134+ if ( method ) {
135+ range = new vscode . Range ( new vscode . Position ( method . range . start . line + 1 , method . range . start . character ) ,
136+ new vscode . Position ( method . range . start . line + 2 , method . range . start . character + 1 ) ) ;
137+ }
138+ }
139+
140+ return range ;
141+ }
142+ }
62143
63144export class PhysicalModulePathToUriConverter implements IModulePathToUriConverter {
64145
146+ readonly _commonLogic : CommonConverterLogic = new CommonConverterLogic ( )
65147 constructor (
66148 private _specialFolders : string [ ] ,
67149 private _documentInfoProvider : DocumentInfoProvider ) {
@@ -143,27 +225,13 @@ export class PhysicalModulePathToUriConverter implements IModulePathToUriConvert
143225
144226 const textDoc = await vscode . workspace . openTextDocument ( fileUri ) ;
145227 const docInfo = await this . _documentInfoProvider . getDocumentInfo ( textDoc ) ;
146- if ( pathInfo . lineNumber ) {
147- range =
148- new vscode . Range (
149- new vscode . Position ( pathInfo . lineNumber , 0 ) ,
150- new vscode . Position ( pathInfo . lineNumber + 1 , 0 ) ) ;
151- }
152- else if ( pathInfo . spanName ) {
153- var span = docInfo ?. spans . filter ( x => x . name == pathInfo . spanName ) . firstOrDefault ( ) ;
154- if ( span ) {
155- range = new vscode . Range ( new vscode . Position ( span . range . start . line + 1 , span . range . start . character ) ,
156- new vscode . Position ( span . range . start . line + 2 , span . range . start . character + 1 ) ) ; }
157- }
228+
229+ if ( docInfo ) {
230+ range = this . _commonLogic . findInDocument ( docInfo , pathInfo ) ;
158231
159- else if ( pathInfo . methodName ) {
160- var method = docInfo ?. methods . filter ( x => x . name == pathInfo . methodName ) . firstOrDefault ( ) ;
161- if ( method ) {
162- range = new vscode . Range ( new vscode . Position ( method . range . start . line + 1 , method . range . start . character ) ,
163- new vscode . Position ( method . range . start . line + 2 , method . range . start . character + 1 ) ) ;
164- }
165232 }
166- if ( ! fileUri || ! range ) {
233+
234+ if ( ! range ) {
167235 return undefined ;
168236 }
169237
0 commit comments