1
+ import path from 'path'
2
+
1
3
import * as ts from 'typescript'
2
4
5
+ import { ProjectOptions } from './CommandLineOptions'
3
6
import { Counter } from './Counter'
4
7
import {
5
8
metaDescriptor ,
@@ -25,22 +28,48 @@ export class FileIndexer {
25
28
private localCounter = new Counter ( )
26
29
private propertyCounters : Map < string , Counter > = new Map ( )
27
30
private localSymbolTable : Map < ts . Node , LsifSymbol > = new Map ( )
31
+ private workingDirectoryRegExp : RegExp
28
32
constructor (
29
33
public readonly checker : ts . TypeChecker ,
34
+ public readonly options : ProjectOptions ,
30
35
public readonly input : Input ,
31
36
public readonly document : lsif . lib . codeintel . lsiftyped . Document ,
32
37
public readonly globalSymbolTable : Map < ts . Node , LsifSymbol > ,
33
38
public readonly packages : Packages ,
34
39
public readonly sourceFile : ts . SourceFile
35
- ) { }
40
+ ) {
41
+ this . workingDirectoryRegExp = new RegExp ( options . cwd , 'g' )
42
+ }
36
43
public index ( ) : void {
44
+ this . emitSourceFileOccurrence ( )
37
45
this . visit ( this . sourceFile )
38
46
}
47
+ private emitSourceFileOccurrence ( ) : void {
48
+ const symbol = this . lsifSymbol ( this . sourceFile )
49
+ if ( symbol . isEmpty ( ) ) {
50
+ return
51
+ }
52
+ this . document . occurrences . push (
53
+ new lsif . lib . codeintel . lsiftyped . Occurrence ( {
54
+ range : [ 0 , 0 , 0 ] ,
55
+ symbol : symbol . value ,
56
+ symbol_roles : lsiftyped . SymbolRole . Definition ,
57
+ } )
58
+ )
59
+ const moduleName =
60
+ this . sourceFile . moduleName || path . basename ( this . sourceFile . fileName )
61
+ this . document . symbols . push (
62
+ new lsiftyped . SymbolInformation ( {
63
+ symbol : symbol . value ,
64
+ documentation : [ '```ts\nmodule "' + moduleName + '"\n```' ] ,
65
+ } )
66
+ )
67
+ }
39
68
private visit ( node : ts . Node ) : void {
40
- if ( ts . isIdentifier ( node ) ) {
69
+ if ( ts . isIdentifier ( node ) || ts . isStringLiteralLike ( node ) ) {
41
70
const sym = this . getTSSymbolAtLocation ( node )
42
71
if ( sym ) {
43
- this . visitIdentifier ( node , sym )
72
+ this . visitSymbolOccurrence ( node , sym )
44
73
}
45
74
}
46
75
ts . forEachChild ( node , node => this . visit ( node ) )
@@ -52,6 +81,7 @@ export class FileIndexer {
52
81
// This code is directly based off src/services/goToDefinition.ts.
53
82
private getTSSymbolAtLocation ( node : ts . Node ) : ts . Symbol | undefined {
54
83
const symbol = this . checker . getSymbolAtLocation ( node )
84
+
55
85
// If this is an alias, and the request came at the declaration location
56
86
// get the aliased symbol instead. This allows for goto def on an import e.g.
57
87
// import {A, B} from "mod";
@@ -71,10 +101,10 @@ export class FileIndexer {
71
101
return symbol
72
102
}
73
103
74
- private visitIdentifier ( identifier : ts . Identifier , sym : ts . Symbol ) : void {
75
- const range = Range . fromNode ( identifier ) . toLsif ( )
104
+ private visitSymbolOccurrence ( node : ts . Node , sym : ts . Symbol ) : void {
105
+ const range = Range . fromNode ( node ) . toLsif ( )
76
106
let role = 0
77
- const isDefinition = this . declarationName ( identifier . parent ) === identifier
107
+ const isDefinition = this . declarationName ( node . parent ) === node
78
108
if ( isDefinition ) {
79
109
role |= lsiftyped . SymbolRole . Definition
80
110
}
@@ -92,7 +122,7 @@ export class FileIndexer {
92
122
} )
93
123
)
94
124
if ( isDefinition ) {
95
- this . addSymbolInformation ( identifier , sym , declaration , lsifSymbol )
125
+ this . addSymbolInformation ( node , sym , declaration , lsifSymbol )
96
126
this . handleShorthandPropertyDefinition ( declaration , range )
97
127
// Only emit one symbol for definitions sites, see https://github.com/sourcegraph/lsif-typescript/issues/45
98
128
break
@@ -137,14 +167,19 @@ export class FileIndexer {
137
167
}
138
168
}
139
169
170
+ private hideWorkingDirectory ( value : string ) : string {
171
+ return value . replace ( this . workingDirectoryRegExp , '' )
172
+ }
140
173
private addSymbolInformation (
141
174
node : ts . Node ,
142
175
sym : ts . Symbol ,
143
176
declaration : ts . Node ,
144
177
symbol : LsifSymbol
145
178
) : void {
146
179
const documentation = [
147
- '```ts\n' + this . signatureForDocumentation ( node , sym ) + '\n```' ,
180
+ '```ts\n' +
181
+ this . hideWorkingDirectory ( this . signatureForDocumentation ( node , sym ) ) +
182
+ '\n```' ,
148
183
]
149
184
const docstring = sym . getDocumentationComment ( this . checker )
150
185
if ( docstring . length > 0 ) {
0 commit comments