@@ -7,23 +7,29 @@ import {
7
7
Selection ,
8
8
Uri ,
9
9
ViewColumn ,
10
- commands ,
10
+ workspace ,
11
11
type TextDocumentShowOptions ,
12
12
} from 'vscode' ;
13
13
14
14
import { Context } from '../Context.js' ;
15
15
import { Item , Options , QuickPick } from './QuickPick.js' ;
16
16
17
+ import { getMethodLine , parseApex } from '../salesforce/ApexParser/ApexSymbolLocator.js' ;
18
+
17
19
export class OpenFileInPackage {
18
- static async openFileForSymbol (
19
- context : Context ,
20
- name : string ,
21
- lineNumber ?: number ,
22
- ) : Promise < void > {
23
- const paths = await context . findSymbol ( name ) ;
20
+ static async openFileForSymbol ( context : Context , symbolName : string ) : Promise < void > {
21
+ if ( ! symbolName ?. trim ( ) ) {
22
+ return ;
23
+ }
24
+
25
+ const parts = symbolName . split ( '.' ) ;
26
+ const fileName = parts [ 0 ] ?. trim ( ) ;
27
+
28
+ const paths = await context . findSymbol ( fileName as string ) ;
24
29
if ( ! paths . length ) {
25
30
return ;
26
31
}
32
+
27
33
const matchingWs = context . workspaces . filter ( ( ws ) => {
28
34
const found = paths . findIndex ( ( p ) => p . startsWith ( ws . path ( ) ) ) ;
29
35
if ( found > - 1 ) {
@@ -38,24 +44,39 @@ export class OpenFileInPackage {
38
44
new Options ( 'Select a workspace:' ) ,
39
45
)
40
46
: [ new Item ( matchingWs [ 0 ] ?. name ( ) || '' , matchingWs [ 0 ] ?. path ( ) || '' , '' ) ] ;
47
+ if ( ! wsPath ) {
48
+ return ;
49
+ }
41
50
42
- if ( wsPath && lineNumber ) {
43
- const zeroBasedLine = lineNumber - 1 ;
44
- const linePosition = new Position ( zeroBasedLine , 0 ) ;
45
-
46
- const options : TextDocumentShowOptions = {
47
- preserveFocus : false ,
48
- preview : false ,
49
- viewColumn : ViewColumn . Active ,
50
- selection : new Selection ( linePosition , linePosition ) ,
51
- } ;
52
-
53
- const wsPathTrimmed = wsPath . description . trim ( ) ;
54
- const path =
55
- paths . find ( ( e ) => {
56
- return e . startsWith ( wsPathTrimmed + sep ) ;
57
- } ) || '' ;
58
- commands . executeCommand ( 'vscode.open' , Uri . file ( path ) , options ) ;
51
+ const wsPathTrimmed = wsPath . description . trim ( ) ;
52
+ const path =
53
+ paths . find ( ( e ) => {
54
+ return e . startsWith ( wsPathTrimmed + sep ) ;
55
+ } ) || '' ;
56
+
57
+ const uri = Uri . file ( path ) ;
58
+ const document = await workspace . openTextDocument ( uri ) ;
59
+
60
+ const parsedRoot = parseApex ( document . getText ( ) ) ;
61
+
62
+ const symbolLocation = getMethodLine ( parsedRoot , parts ) ;
63
+
64
+ if ( ! symbolLocation . isExactMatch ) {
65
+ context . display . showErrorMessage (
66
+ `Symbol ' ${ symbolLocation . missingSymbol } ' could not be found in file ' ${ fileName } '` ,
67
+ ) ;
59
68
}
69
+ const zeroIndexedLineNumber = symbolLocation . line - 1 ;
70
+
71
+ const pos = new Position ( zeroIndexedLineNumber , 0 ) ;
72
+
73
+ const options : TextDocumentShowOptions = {
74
+ preserveFocus : false ,
75
+ preview : false ,
76
+ viewColumn : ViewColumn . Active ,
77
+ selection : new Selection ( pos , pos ) ,
78
+ } ;
79
+
80
+ context . display . showFile ( path , options ) ;
60
81
}
61
82
}
0 commit comments