Skip to content
This repository was archived by the owner on Jun 7, 2022. It is now read-only.
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 extension/src/basic-code-intel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Handler, Providers } from '@sourcegraph/basic-code-intel'
import * as path from 'path'
import * as sourcegraph from 'sourcegraph'

export function initBasicCodeIntel(): Providers {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not important, maybe keep in mind for future code) Since this is only a factory function now, I'd recommend the create prefix instead of init. That's the conventional prefix used e.g. in the Node stdlib. With init, I personally wouldn't be sure if it might do side effects like register providers in the Sourcegraph API, while create communicates it's only creating an object but not causes side effects (assuming that is the case here)

return new Handler({
sourcegraph,
languageID: 'typescript',
fileExts: ['ts', 'tsx', 'js', 'jsx'],
commentStyle: {
lineRegex: /\/\/\s?/,
block: {
startRegex: /\/\*\*?/,
lineNoiseRegex: /(^\s*\*\s?)?/,
endRegex: /\*\//,
},
},
filterDefinitions: ({ filePath, fileContent, results }) => {
const imports = fileContent
.split('\n')
.map(line => {
// Matches the import at index 1
const match = /\bfrom ['"](.*)['"];?$/.exec(line) || /\brequire\(['"](.*)['"]\)/.exec(line)
return match ? match[1] : undefined
})
.filter((x): x is string => Boolean(x))

const filteredResults = results.filter(result =>
imports.some(i => path.join(path.dirname(filePath), i) === result.file.replace(/\.[^/.]+$/, ''))
)

return filteredResults.length === 0 ? results : filteredResults
},
})
}
Loading