Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Add support for filtering definitions by import path #67

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
38 changes: 30 additions & 8 deletions package/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -517,6 +530,7 @@ export class Handler {
commentStyle,
docstringIgnore,
sourcegraph,
filterDefinitions: filterDefinitions = ({ results }) => results,
}: HandlerArgs) {
this.sourcegraph = sourcegraph
this.api = new API(sourcegraph)
Expand All @@ -525,6 +539,7 @@ export class Handler {
this.commentStyle = commentStyle
this.docstringIgnore = docstringIgnore
this.debugAnnotatedURIs = []
this.filterDefinitions = filterDefinitions
}

/**
Expand Down Expand Up @@ -605,11 +620,15 @@ export class Handler {
doc: TextDocument,
pos: Position
): Promise<Location[] | null> {
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,
})
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions template/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down