Skip to content

Commit 4a038cd

Browse files
authored
Filter definitions by imports (#12)
1 parent fe61fba commit 4a038cd

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@
8686
"typescript": "^3.1.3"
8787
},
8888
"dependencies": {
89-
"@sourcegraph/basic-code-intel": "6.0.7",
89+
"@sourcegraph/basic-code-intel": "6.0.12",
9090
"@types/ws": "^6.0.1",
9191
"concurrently": "^4.1.0",
92+
"path-browserify": "^1.0.0",
9293
"rxjs": "^6.3.3",
9394
"tagged-template-noop": "^2.1.0",
9495
"vscode-jsonrpc": "^4.0.0",

‎src/sourcegraph-python.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { getOrTryToCreateAccessToken } from './access-token'
2323
import { createUriConverter, UriConverter } from './converters'
2424
import { LanguageServerConnectionManager } from './lsp'
2525

26+
const path = require('path-browserify')
27+
2628
function 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+
/^import ([\.\w]*)/.exec(line) ||
71+
/^from ([\.\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

‎yarn.lock‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,15 @@
739739
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.7.0.tgz#9a06f4f137ee84d7df0460c1fdb1135ffa6c50fd"
740740
integrity sha512-ONhaKPIufzzrlNbqtWFFd+jlnemX6lJAgq9ZeiZtS7I1PIf/la7CW4m83rTXRnVnsMbW2k56pGYu7AUFJD9Pow==
741741

742-
"@sourcegraph/basic-code-intel@6.0.7":
743-
version "6.0.7"
744-
resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.7.tgz#6ba8623c1c5e71f780a059ccea2120ffdc1e3600"
745-
integrity sha512-+amGIq54N3aVOj7AxNtW6QtDfUVvtT253Ha+i9sofyo217L/7Ucwo/WoDGuded9Ng9FUxUM6UOS3MREW+lkUGQ==
742+
"@sourcegraph/basic-code-intel@6.0.12":
743+
version "6.0.12"
744+
resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.12.tgz#5d6c1a301c73a19bf28f6d25b5f4846cc3db844f"
745+
integrity sha512-G8IUx29UV1asiU3SZ3AMYKlZSZxX63nC7IR4ux+Jd+TBg2OW9WrRKOEaYKp2MWlwyWBc7Dpix3hv6GqMSMJNyA==
746746
dependencies:
747747
lodash "^4.17.11"
748748
rxjs "^6.3.3"
749749
sourcegraph "^23.0.0"
750+
sprintf-js "^1.1.2"
750751

751752
"@sourcegraph/tsconfig@^3.0.0":
752753
version "3.0.0"
@@ -3924,6 +3925,11 @@ path-browserify@0.0.0:
39243925
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.0.tgz#a0b870729aae214005b7d5032ec2cbbb0fb4451a"
39253926
integrity sha1-oLhwcpquIUAFt9UDLsLLuw+0RRo=
39263927

3928+
path-browserify@^1.0.0:
3929+
version "1.0.0"
3930+
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.0.tgz#40702a97af46ae00b0ea6fa8998c0b03c0af160d"
3931+
integrity sha512-Hkavx/nY4/plImrZPHRk2CL9vpOymZLgEbMNX1U0bjcBL7QN9wODxyx0yaMZURSQaUtSEvDrfAvxa9oPb0at9g==
3932+
39273933
path-dirname@^1.0.0:
39283934
version "1.0.2"
39293935
resolved "https://registry.yarnpkg.com/path-dirname/-/path-dirname-1.0.2.tgz#cc33d24d525e099a5388c0336c6e32b9160609e0"
@@ -5223,6 +5229,11 @@ split-string@^3.0.1, split-string@^3.0.2:
52235229
dependencies:
52245230
extend-shallow "^3.0.0"
52255231

5232+
sprintf-js@^1.1.2:
5233+
version "1.1.2"
5234+
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.1.2.tgz#da1765262bf8c0f571749f2ad6c26300207ae673"
5235+
integrity sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug==
5236+
52265237
sprintf-js@~1.0.2:
52275238
version "1.0.3"
52285239
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"

0 commit comments

Comments
 (0)