From 7c1d0f3f83037111cf6f40f1d149fbd281b9a6e8 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 20 Mar 2019 19:46:59 -0700 Subject: [PATCH] Add support for filtering definitions by import path --- languages.ts | 35 +++++++++++++++++++++++++++++++++++ package/package.json | 2 +- package/src/handler.ts | 38 ++++++++++++++++++++++++++++++-------- template/package.json | 2 +- template/yarn.lock | 8 ++++---- 5 files changed, 71 insertions(+), 14 deletions(-) diff --git a/languages.ts b/languages.ts index 9a8f43ee4..f20661118 100644 --- a/languages.ts +++ b/languages.ts @@ -40,6 +40,10 @@ const lispStyle: CommentStyle = { }, } +function dir(path: string) { + return path.slice(0, path.lastIndexOf('/')) +} + // The set of languages come from https://madnight.github.io/githut/#/pull_requests/2018/4 // The language names come from https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers // The extensions come from shared/src/languages.ts @@ -85,6 +89,37 @@ export const languageSpecs: LanguageSpec[] = [ commentStyle: { lineRegex: /\/\/\s?/, }, + filterDefinitions: ({ doc, pos, fileContent, results }) => { + const currentFileImportedPaths = fileContent + .split('\n') + .map(line => { + // Matches the import at index 3 + const match = /^(import |\t)(\w+ |\. )?"(.*)"$/.exec( + line + ) + return match ? match[3] : undefined + }) + .filter((x): x is string => Boolean(x)) + + const currentFileURL = new URL(doc.uri) + const currentRepository = + currentFileURL.hostname + currentFileURL.pathname + const currentFilePath = currentFileURL.hash.slice(1) + const currentFileImportPath = + currentRepository + '/' + dir(currentFilePath) + + const filteredResults = results.filter(result => { + const resultImportPath = + result.repo + '/' + dir(result.file) + return ( + currentFileImportedPaths.some(i => + resultImportPath.includes(i) + ) || resultImportPath === currentFileImportPath + ) + }) + + return filteredResults.length === 0 ? results : filteredResults + }, }, stylized: 'Go', hasLanguageServer: true, diff --git a/package/package.json b/package/package.json index fd9112a71..808c81782 100644 --- a/package/package.json +++ b/package/package.json @@ -1,6 +1,6 @@ { "name": "@sourcegraph/basic-code-intel", - "version": "6.0.8", + "version": "6.0.9", "description": "Common library for providing basic code intelligence in Sourcegraph extensions", "repository": { "type": "git", diff --git a/package/src/handler.ts b/package/src/handler.ts index 25aaa207e..83858c486 100644 --- a/package/src/handler.ts +++ b/package/src/handler.ts @@ -477,6 +477,13 @@ export type CommentStyle = { block?: BlockCommentStyle } +type FilterDefinitions = (args: { + doc: TextDocument + fileContent: string + pos: Position + results: Result[] +}) => Result[] + export interface HandlerArgs { /** * Used to label markdown code blocks. @@ -493,6 +500,11 @@ export interface HandlerArgs { docstringIgnore?: RegExp commentStyle?: CommentStyle sourcegraph: typeof import('sourcegraph') + /** + * Callback that filters the given symbol search results (e.g. to drop + * results from non-imported files). + */ + filterDefinitions?: FilterDefinitions } export class Handler { @@ -506,6 +518,7 @@ export class Handler { public commentStyle: CommentStyle | undefined public docstringIgnore: RegExp | undefined public debugAnnotatedURIs: string[] + public filterDefinitions: FilterDefinitions /** * Constructs a new Handler that provides code intelligence on files with the given @@ -517,6 +530,7 @@ export class Handler { commentStyle, docstringIgnore, sourcegraph, + filterDefinitions: filterDefinitions = ({ results }) => results, }: HandlerArgs) { this.sourcegraph = sourcegraph this.api = new API(sourcegraph) @@ -525,6 +539,7 @@ export class Handler { this.commentStyle = commentStyle this.docstringIgnore = docstringIgnore this.debugAnnotatedURIs = [] + this.filterDefinitions = filterDefinitions } /** @@ -605,11 +620,15 @@ export class Handler { doc: TextDocument, pos: Position ): Promise { - if (doc.text === undefined) { + const fileContent = await this.api.getFileContent( + new this.sourcegraph.Location(new URL(doc.uri)) + ) + if (!fileContent) { return null } + const tokenResult = findSearchToken({ - text: doc.text, + text: fileContent, position: pos, lineRegex: this.commentStyle && this.commentStyle.lineRegex, }) @@ -629,15 +648,18 @@ export class Handler { this.sourcegraph.internal.sourcegraphURL.href === 'https://sourcegraph.com/', })) { - const symbolResults = (await this.api.search(query)) - .filter( + const symbolResults = this.filterDefinitions({ + doc, + pos, + fileContent, + results: (await this.api.search(query)).filter( result => !result.fileLocal || result.file === new URL(doc.uri).hash.replace(/^#/, '') - ) - .map(result => - resultToLocation({ result, sourcegraph: this.sourcegraph }) - ) + ), + }).map(result => + resultToLocation({ result, sourcegraph: this.sourcegraph }) + ) if (symbolResults.length > 0) { return sortByProximity({ diff --git a/template/package.json b/template/package.json index 2ef1fbdb6..b41414a7c 100644 --- a/template/package.json +++ b/template/package.json @@ -107,7 +107,7 @@ "typescript": "^3.2.1" }, "dependencies": { - "@sourcegraph/basic-code-intel": "6.0.8", + "@sourcegraph/basic-code-intel": "6.0.9", "rxjs": "^6.3.3" }, "sideEffects": false diff --git a/template/yarn.lock b/template/yarn.lock index c222b86b1..aecc09762 100644 --- a/template/yarn.lock +++ b/template/yarn.lock @@ -737,10 +737,10 @@ resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.7.0.tgz#9a06f4f137ee84d7df0460c1fdb1135ffa6c50fd" integrity sha512-ONhaKPIufzzrlNbqtWFFd+jlnemX6lJAgq9ZeiZtS7I1PIf/la7CW4m83rTXRnVnsMbW2k56pGYu7AUFJD9Pow== -"@sourcegraph/basic-code-intel@6.0.8": - version "6.0.8" - resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.8.tgz#884a92762d65e09584283d3ff2723261cd1c24c6" - integrity sha512-EY217NLUMztnflmwLuWPpkvHKsuChV9wI9fE2iKBEaZAU78feXiwGZna8cr4TJk8y2Gp71uhJjEbJhwl+3KJGg== +"@sourcegraph/basic-code-intel@6.0.9": + version "6.0.9" + resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.9.tgz#7c76513a5cda622e86e966a5dd4128be677278bc" + integrity sha512-/pfZ1E8mhetl3wOVAD3nn8DfQoQHE+PzvezzPXF3P+sSZyn7p4oHErn+ezo4gR3d7UwjZ4UEn0UkbSdW5RWjnw== dependencies: lodash "^4.17.11" rxjs "^6.3.3"