How to use sourcegraph - 10 common examples

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 / packageJsonDependency.ts View on Github external
function computeRemoveDependencyEdit(
    diag: sourcegraph.Diagnostic,
    doc: sourcegraph.TextDocument,
    edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
    const dep = getDiagnosticData(diag)
    const range = findDependencyMatchRange(doc.text, dep.name)
    // TODO!(sqs): assumes dependency key-value is all on one line and only appears once
    edit.delete(
        new URL(doc.uri),
        new sourcegraph.Range(
            range.start.with({ character: 0 }),
            range.end.with({ line: range.end.line + 1, character: 0 })
        )
    )
    return edit
}
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / codeDuplication.ts View on Github external
function createWorkspaceEditForIgnore(
    doc: sourcegraph.TextDocument,
    diagnostic: sourcegraph.Diagnostic,
    edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
    // TODO!(sqs): get indent of previous line - in vscode this is inserted on the client
    // automatically, check out how they do it because that seems neat
    // (https://sourcegraph.com/github.com/microsoft/vscode-tslint@30d1a7ae25b0331466f1a54b4f7d23d60fa2da30/-/blob/tslint-server/src/tslintServer.ts#L618)

    const range = diagnostic.range // TODO!(sqs): get other duplication instance too

    const startIndent = doc.text.slice(doc.offsetAt(range.start.with(undefined, 0))).match(/[ \t]*/)
    edit.insert(
        new URL(doc.uri),
        range.start.with(undefined, 0),
        `${startIndent ? startIndent[0] : ''}// jscpd:ignore-start\n`
    )

    const endIndent = doc.text.slice(doc.offsetAt(range.end.with(undefined, 0))).match(/[ \t]*/)
    edit.insert(
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / importStar.ts View on Github external
function computeIgnoreEdit(
    diag: sourcegraph.Diagnostic,
    doc: sourcegraph.TextDocument,
    edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
    const { binding, module } = getDiagnosticData(diag)
    for (const range of findMatchRanges(doc.text, binding, module)) {
        edit.insert(
            new URL(doc.uri),
            range.end,
            ' // sourcegraph:ignore-line React lint https://sourcegraph.example.com/ofYRz6NFzj'
        )
    }
    return edit
}
github sourcegraph / sourcegraph / extensions / enterprise / sandbox / src / findReplace.ts View on Github external
vars: {
            repositoryNames: context.repositoryNames || ['github.com/sd9/guava19to21-sample'],
            matchTemplate: context.matchTemplate,
            rule: context.rule,
            rewriteTemplate: context.rewriteTemplate,
        },
    })
    if (errors && errors.length > 0) {
        throw new Error(`GraphQL response error: ${errors[0].message}`)
    }
    const canonicalURLs: string[] = data.comby.results.map(
        (r: any) => `git://${r.file.commit.repository.name}?${r.file.commit.oid}#${r.file.path}`
    )
    const docs = await Promise.all(canonicalURLs.map(url => sourcegraph.workspace.openTextDocument(new URL(url))))

    const edit = new sourcegraph.WorkspaceEdit()
    for (const [i, doc] of docs.entries()) {
        if (doc.text!.length > 15000) {
            continue // TODO!(sqs): skip too large
        }
        edit.set(new URL(doc.uri), [sourcegraph.TextEdit.patch(data.comby.results[i].rawDiff)])
    }

    return (edit as any).toJSON()
}
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / packageJsonDependency.ts View on Github external
function computeRemoveDependencyEdit(
    diag: sourcegraph.Diagnostic,
    doc: sourcegraph.TextDocument,
    edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
    const dep = getDiagnosticData(diag)
    const range = findDependencyMatchRange(doc.text, dep.name)
    // TODO!(sqs): assumes dependency key-value is all on one line and only appears once
    edit.delete(
        new URL(doc.uri),
        new sourcegraph.Range(
            range.start.with({ character: 0 }),
            range.end.with({ line: range.end.line + 1, character: 0 })
        )
    )
    return edit
}
github sourcegraph / sourcegraph / extensions / enterprise / check-search / src / dependencyRules.ts View on Github external
function computeRemoveDependencyEdit(
    diag: sourcegraph.Diagnostic,
    doc: sourcegraph.TextDocument,
    edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
    const dep = getDiagnosticData(diag)
    const range = findDependencyMatchRange(doc.text, dep.name)
    // TODO!(sqs): assumes dependency key-value is all on one line and only appears once
    edit.delete(
        new URL(doc.uri),
        new sourcegraph.Range(
            range.start.with({ character: 0 }),
            range.end.with({ line: range.end.line + 1, character: 0 })
        )
    )
    return edit
}
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 / 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 / sandbox / src / packageJsonDependency / packageJsonDependency.ts View on Github external
export function register(): Unsubscribable {
    const subscriptions = new Subscription()
    subscriptions.add(
        sourcegraph.workspace.registerDiagnosticProvider('packageJsonDependency', {
            provideDiagnostics: (_scope, context) =>
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                provideDiagnostics((context as any) as PackageJsonDependencyCampaignContext).pipe(
                    filter((diagnostics): diagnostics is sourcegraph.Diagnostic[] => diagnostics !== LOADING)
                ),
        })
    )
    subscriptions.add(sourcegraph.languages.registerCodeActionProvider(['*'], createCodeActionProvider()))
    subscriptions.add(
        sourcegraph.commands.registerActionEditCommand(COMMAND_ID, diagnostic => {
            if (!diagnostic || (diagnostic.tags && !diagnostic.tags.includes('fix'))) {
                return Promise.resolve(new sourcegraph.WorkspaceEdit())
            }
            return editForDependencyAction(diagnostic).toPromise()
        })
    )
    return subscriptions
}
github sourcegraph / sourcegraph-go / src / convert-lsp-to-sea.ts View on Github external
let definitionURI: sourcegraph.URI
    if (/^file:\/\/\//.test(uriFromLangServer)) {
        // The definition is in a file in the same repo
        const docURL = new URL(currentDocURI)
        docURL.hash = uriFromLangServer.slice('file:///'.length)
        definitionURI = new sourcegraph.URI(docURL.href)
    } else {
        definitionURI = new sourcegraph.URI(uriFromLangServer)
    }

    return new sourcegraph.Location(
        definitionURI,
        range &&
            new sourcegraph.Range(
                new sourcegraph.Position(range.start.line, range.start.character),
                new sourcegraph.Position(range.end.line, range.end.character)
            )
    )
}