@@ -4,55 +4,61 @@ import { DocumentSymbol, SymbolKind } from "vscode-languageclient";
44import { IMethodExtractor , SymbolInfo } from "../extractors" ;
55import { Logger } from '../../logger' ;
66
7- export class GoMethodExtractor implements IMethodExtractor {
7+ export class GoMethodExtractor implements IMethodExtractor {
8+ private async getModuleName ( modFile :vscode . Uri ) : Promise < string | undefined > {
9+ const modDocument = await vscode . workspace . openTextDocument ( modFile ) ;
10+ const match = modDocument . getText ( ) . match ( / ^ m o d u l e ( .+ ) $ / m) ;
11+ if ( ! match ) {
12+ Logger . warn ( `Could not found module name in '${ modFile . path } '` ) ;
13+ return undefined ;
14+ }
15+ return match [ 1 ] ;
16+ }
17+
18+ private getPackageDefinationName ( document : vscode . TextDocument ) : string | undefined {
19+ const match = document . getText ( ) . match ( / ^ p a c k a g e ( .+ ) $ / m) ;
20+ if ( ! match ) {
21+ Logger . warn ( `Could not found packakge name in '${ document . uri . path } '` ) ;
22+ return undefined ;
23+ }
24+ return match [ 1 ] ;
25+ }
26+
827 public async extractMethods ( document : vscode . TextDocument , docSymbols : DocumentSymbol [ ] ) : Promise < SymbolInfo [ ] > {
9- const methodSymbols = docSymbols . filter ( s => s . kind + 1 === SymbolKind . Method || s . kind + 1 === SymbolKind . Function ) ;
10- if ( ! methodSymbols . length ) {
28+ const methodSymbols = docSymbols . filter ( s => s . kind + 1 === SymbolKind . Method || s . kind + 1 === SymbolKind . Function ) ;
29+ if ( ! methodSymbols . length ) {
1130 return [ ] ;
1231 }
13-
14- const modFiles = await ( await vscode . workspace . findFiles ( '**/go.mod' ) ) . map ( x => x . fsPath ) ;
15- const normDocPath = document . uri . fsPath ;
16- const modFile = modFiles . find ( f => normDocPath . startsWith ( path . dirname ( f ) ) ) ;
17- if ( ! modFile ) {
32+
33+ const modFiles = await vscode . workspace . findFiles ( '**/go.mod' ) ;
34+ const modFile = modFiles . find ( f => document . uri . path . startsWith ( path . dirname ( f . path ) ) ) ;
35+ if ( ! modFile ) {
1836 Logger . warn ( `Could not resolve mod file for '${ document . uri . path } '` ) ;
1937 return [ ] ;
2038 }
2139
22- const modFolder = path . dirname ( modFile ) ;
23- const docFolder = path . dirname ( normDocPath ) ;
24- let packageName = '' ;
25- if ( modFolder === docFolder ) {
26- const match = document . getText ( ) . match ( / ^ p a c k a g e ( .+ ) $ / m) ;
27- if ( ! match ) {
28- Logger . warn ( `Could not found packakge name in '${ document . uri . path } '` ) ;
29- return [ ] ;
30- }
31- packageName = match [ 1 ] ;
40+ const moduleName = await this . getModuleName ( modFile ) ;
41+ if ( ! moduleName ) {
42+ return [ ] ;
3243 }
33- if ( ! packageName || packageName !== 'main' ) {
34- const modDocument = await vscode . workspace . openTextDocument ( modFile ) ;
35- const match = modDocument . getText ( ) . match ( / ^ m o d u l e ( .+ ) $ / m) ;
36- if ( ! match ) {
37- Logger . warn ( `Could not found module name in '${ modFile } '` ) ;
38- return [ ] ;
39- }
40- packageName = match [ 1 ] ;
41-
42- if ( modFolder !== docFolder ) {
43- const relative = path . relative ( modFolder , docFolder )
44- . replaceAll ( '\\' , '/' ) ; // get rid of windows backslashes
45- packageName += '/' + relative ;
46- }
44+ const packageDefinitionName = this . getPackageDefinationName ( document ) ;
45+ if ( ! packageDefinitionName ) {
46+ return [ ] ;
4747 }
48+ const modFolder = path . dirname ( modFile . path ) ;
49+ const docFolder = path . dirname ( document . uri . path ) ;
4850
51+ let packagePath = `${ moduleName } /${ path . relative ( modFolder , docFolder . replaceAll ( '\\' , '/' ) ) } ` ; // get rid of windows backslashes
52+ if ( packageDefinitionName === "main" ) {
53+ packagePath = moduleName ;
54+ }
4955 const methods : SymbolInfo [ ] = methodSymbols . map ( s => {
5056 return {
51- id : packageName + '$_$' + s . name ,
57+ id : packageDefinitionName === "main" ? packagePath + '$_$' + `main. ${ s . name } ` : packagePath + '$_$' + s . name ,
5258 name : s . name ,
53- displayName : packageName . split ( '/' ) . lastOrDefault ( ) + '.' + s . name ,
59+ displayName : packagePath . split ( '/' ) . lastOrDefault ( ) + '.' + s . name ,
5460 documentUri : document . uri ,
55- codeLocation : packageName ,
61+ codeLocation : packagePath ,
5662 range : new vscode . Range (
5763 new vscode . Position ( s . range . start . line , s . range . start . character ) ,
5864 new vscode . Position ( s . range . end . line , s . range . end . character )
0 commit comments