How to use the sourcegraph.languages function in sourcegraph

To help you get started, we’ve selected a few sourcegraph examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / importStar.ts View on Github external
async function computeFixAllAction(): Promise> {
    // TODO!(sqs): Make this listen for new diagnostics and include those too, but that might be
    // super inefficient because it's n^2, so maybe an altogether better/different solution is
    // needed.
    const allImportStarDiags = sourcegraph.languages
        .getDiagnostics()
        .map(([uri, diagnostics]) => {
            const matchingDiags = diagnostics.filter(isImportStarDiagnostic)
            return matchingDiags.length > 0
                ? ([uri, matchingDiags] as ReturnType[0])
                : null
        })
        .filter(isDefined)
    const edit = new sourcegraph.WorkspaceEdit()
    for (const [uri, diags] of allImportStarDiags) {
        const doc = await sourcegraph.workspace.openTextDocument(uri)
        for (const diag of diags) {
            computeFixEdit(diag, doc, edit)
        }
    }
    return { edit, diagnostics: flatten(allImportStarDiags.map(([, diagnostics]) => diagnostics)) }
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / noInlineProps.ts View on Github external
disableRuleEdits.insert(
                new URL(doc.uri),
                new sourcegraph.Position(diag.range.start.line, 0),
                '// sourcegraph:ignore-next-line React lint https://sourcegraph.example.com/ofYRz6NFzj\n'
            )

            return [
                {
                    title: 'Extract Props type',
                    edit: fixEdits,
                    diagnostics: flatten(sourcegraph.languages.getDiagnostics().map(([, diagnostics]) => diagnostics)),
                },
                {
                    title: 'Ignore',
                    edit: disableRuleEdits,
                    diagnostics: flatten(sourcegraph.languages.getDiagnostics().map(([, diagnostics]) => diagnostics)),
                },
                ...OTHER_CODE_ACTIONS,
            ]
        },
    }
github sourcegraph / sourcegraph-go / src / lang-go.ts View on Github external
ctx.subscriptions.add(
            sourcegraph.languages.registerHoverProvider(goFiles, {
                provideHover: asyncFirst([lsif.hover, lsp.hover, wrapMaybe(basicCodeIntel.hover)], null),
            })
        )
        ctx.subscriptions.add(
            sourcegraph.languages.registerDefinitionProvider(goFiles, {
                provideDefinition: asyncFirst(
                    [lsif.definition, lsp.definition, wrapMaybe(basicCodeIntel.definition)],
                    null
                ),
            })
        )
        ctx.subscriptions.add(
            sourcegraph.languages.registerReferenceProvider(goFiles, {
                provideReferences: async (doc, pos) => {
                    if (isLSPEnabled()) {
                        const references = await lsp.references(doc, pos)
                        return references ? references.value : null
                    }

                    // Gets an opaque value that is the same for all locations
                    // within a file but different from other files.
                    const file = (loc: sourcegraph.Location) => `${loc.uri.host} ${loc.uri.pathname} ${loc.uri.hash}`

                    // Concatenates LSIF results (if present) with fuzzy results
                    // because LSIF data might be sparse.
                    const lsifReferences = await lsif.references(doc, pos)
                    const fuzzyReferences = (await basicCodeIntel.references(doc, pos)) || []

                    const lsifFiles = new Set((lsifReferences ? lsifReferences.value : []).map(file))
github sourcegraph / sourcegraph-basic-code-intel / template / src / extension.ts View on Github external
const lsif = initLSIF()
        const handler = new Handler({
            ...languageSpec.handlerArgs,
            sourcegraph,
        })
        const selector = documentSelector(languageSpec.handlerArgs.fileExts)
        ctx.subscriptions.add(
            sourcegraph.languages.registerHoverProvider(selector, {
                provideHover: asyncFirst(
                    [lsif.hover, wrapMaybe(handler.hover.bind(handler))],
                    null
                ),
            })
        )
        ctx.subscriptions.add(
            sourcegraph.languages.registerDefinitionProvider(selector, {
                provideDefinition: asyncFirst(
                    [
                        lsif.definition,
                        wrapMaybe(handler.definition.bind(handler)),
                    ],
                    null
                ),
            })
        )
        ctx.subscriptions.add(
            sourcegraph.languages.registerReferenceProvider(selector, {
                provideReferences: async (doc, pos) => {
                    // Gets an opaque value that is the same for all locations
                    // within a file but different from other files.
                    const file = (loc: sourcegraph.Location) =>
                        `${loc.uri.host} ${loc.uri.pathname} ${loc.uri.hash}`
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / packageJsonDependency.ts View on Github external
export function register(): Unsubscribable {
    const subscriptions = new Subscription()
    subscriptions.add(
        sourcegraph.workspace.registerDiagnosticProvider('packageJsonDependency', {
            provideDiagnostics: (_scope, context) =>
                provideDiagnostics(context).pipe(
                    filter((diagnostics): diagnostics is sourcegraph.Diagnostic[] => diagnostics !== LOADING)
                ),
        })
    )
    subscriptions.add(sourcegraph.languages.registerCodeActionProvider(['*'], createCodeActionProvider()))
    subscriptions.add(
        sourcegraph.commands.registerActionEditCommand(REMOVE_COMMAND, async diagnostic => {
            const doc = await sourcegraph.workspace.openTextDocument(diagnostic.resource)
            return computeRemoveDependencyEdit(diagnostic, doc)
        })
    )
    return subscriptions
}
github sourcegraph / sourcegraph / extensions / enterprise / sandbox / src / npmCredentials / providers.ts View on Github external
export function register(): Unsubscribable {
    const subscriptions = new Subscription()
    subscriptions.add(
        sourcegraph.workspace.registerDiagnosticProvider('npmCredentials', {
            provideDiagnostics: (_scope, context) =>
                provideDiagnostics((context as any) as NPMCredentialsCampaignContext).pipe(
                    filter((diagnostics): diagnostics is sourcegraph.Diagnostic[] => diagnostics !== LOADING)
                ),
        })
    )
    subscriptions.add(sourcegraph.languages.registerCodeActionProvider(['*'], createCodeActionProvider()))
    subscriptions.add(
        sourcegraph.commands.registerActionEditCommand(NPM_CREDENTIALS_FIX_COMMAND, diagnostic => {
            if (!diagnostic) {
                return new sourcegraph.WorkspaceEdit()
            }
            return computeFixEdit(diagnostic)
        })
    )
    return subscriptions
}
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / dependencyRules.ts View on Github external
function startDiagnostics(): Unsubscribable {
    const subscriptions = new Subscription()

    const diagnosticsCollection = sourcegraph.languages.createDiagnosticCollection('dependencyRules')
    subscriptions.add(diagnosticsCollection)
    subscriptions.add(
        diagnostics.subscribe(entries => {
            diagnosticsCollection.set(entries)
        })
    )

    return diagnosticsCollection
}
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / travisGo.ts View on Github external
export function registerTravisGo(): Unsubscribable {
    const subscriptions = new Subscription()
    subscriptions.add(startDiagnostics())
    subscriptions.add(registerStatusProvider(diagnostics, diagnosticCollection))
    subscriptions.add(sourcegraph.languages.registerCodeActionProvider(['*'], createCodeActionProvider()))
    return subscriptions
}
github sourcegraph / sourcegraph / lsif / extension / src / lsif.ts View on Github external
return null
                }
                const locations = Array.isArray(body) ? body : [body]
                return convertLocations(
                    sourcegraph,
                    locations.map((definition: LSP.Location) => ({
                        ...definition,
                        uri: setPath(doc, definition.uri),
                    }))
                )
            },
        })
    )

    ctx.subscriptions.add(
        sourcegraph.languages.registerReferenceProvider(['*'], {
            provideReferences: async (doc, position) => {
                if (!(await hasLSIF(doc))) {
                    return null
                }
                const locations: LSP.Location[] | null = await send({
                    doc,
                    method: 'references',
                    path: pathFromDoc(doc),
                    position,
                })
                if (!locations) {
                    return null
                }
                return convertLocations(
                    sourcegraph,
                    locations.map((reference: LSP.Location) => ({
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / codeOwnership.ts View on Github external
function startDiagnostics(): Unsubscribable {
    const subscriptions = new Subscription()

    const diagnosticsCollection = sourcegraph.languages.createDiagnosticCollection('codeOwnership')
    subscriptions.add(diagnosticsCollection)
    subscriptions.add(
        from(sourcegraph.workspace.rootChanges)
            .pipe(
                startWith(void 0),
                map(() => sourcegraph.workspace.roots.filter(propertyIsDefined('baseUri'))),
                switchMap(async roots => {
                    return combineLatestOrDefault(
                        roots
                            .map(async (root, rootI) => {
                                const base = parseRepoURI(root.baseUri.toString())
                                const head = parseRepoURI(root.uri.toString())
                                const data = dataOrThrowErrors(
                                    await queryGraphQL({
                                        query: gql`
                                            query ComparisonRawDiff(