@@ -23,6 +23,8 @@ import { getOrTryToCreateAccessToken } from './access-token'
2323import { createUriConverter , UriConverter } from './converters'
2424import { LanguageServerConnectionManager } from './lsp'
2525
26+ const path = require ( 'path-browserify' )
27+
2628function fromSubscribable < T > ( sub : {
2729 subscribe ( next : ( value ?: T ) => void ) : sourcegraph . Unsubscribable
2830} ) : Observable < T > {
@@ -51,7 +53,6 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
5153 return activateBasicCodeIntel ( {
5254 languageID : 'python' ,
5355 fileExts : [ 'py' ] ,
54- definitionPatterns : [ '\\b%s\\b=' ] ,
5556 commentStyle : {
5657 docPlacement : 'below the definition' ,
5758 lineRegex : / # \s ? / ,
@@ -60,6 +61,49 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
6061 endRegex : / " " " / ,
6162 } ,
6263 } ,
64+ filterDefinitions : ( { filePath, fileContent, results } ) => {
65+ const imports = fileContent
66+ . split ( '\n' )
67+ . map ( line => {
68+ // Matches the import at index 1
69+ const match =
70+ / ^ i m p o r t ( [ \. \w ] * ) / . exec ( line ) ||
71+ / ^ f r o m ( [ \. \w ] * ) / . exec ( line )
72+ return match ? match [ 1 ] : undefined
73+ } )
74+ . filter ( ( x ) : x is string => Boolean ( x ) )
75+
76+ /**
77+ * Converts a relative import to a relative path, or undefined
78+ * if the import is not relative.
79+ */
80+ function relativeImportToPath ( i : string ) : string | undefined {
81+ const match = / ^ ( \. ) ( \. * ) ( .* ) / . exec ( i )
82+ if ( ! match ) {
83+ return undefined
84+ }
85+ const parentDots = match [ 2 ]
86+ const pkg = match [ 3 ]
87+ return (
88+ parentDots . replace ( / \. / g, '../' ) +
89+ pkg . replace ( / \. / g, '/' )
90+ )
91+ }
92+
93+ const filteredResults = results . filter ( result =>
94+ imports . some (
95+ i =>
96+ relativeImportToPath ( i )
97+ ? path . join (
98+ path . dirname ( filePath ) ,
99+ relativeImportToPath ( i )
100+ ) === result . file . replace ( / \. [ ^ / . ] + $ / , '' )
101+ : result . file . includes ( i . replace ( / \. / g, '/' ) )
102+ )
103+ )
104+
105+ return filteredResults . length === 0 ? results : filteredResults
106+ } ,
63107 } ) ( ctx )
64108 }
65109
0 commit comments