Skip to content

Commit

Permalink
fix: query hunks only if decorations enabled (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
taras-yemets authored Nov 16, 2022
1 parent b990dce commit a1e2624
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
29 changes: 24 additions & 5 deletions src/blame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,24 @@ export const getAllBlameDecorations = (
) => hunks.map(hunk => getDecorationFromHunk(hunk, now, hunk.startLine - 1, settings, sourcegraph))

export const queryBlameHunks = memoizeAsync(
async ({ uri, sourcegraph }: { uri: string; sourcegraph: typeof import('sourcegraph') }): Promise<Hunk[]> => {
async ({
uri,
sourcegraph,
selections,
}: {
uri: string
sourcegraph: typeof import('sourcegraph')
selections: Selection[] | null
}): Promise<Hunk[]> => {
const { repo, rev, path } = resolveURI(uri)
const { data, errors } = await sourcegraph.commands.executeCommand(
'queryGraphQL',
gql`
query GitBlame($repo: String!, $rev: String!, $path: String!) {
query GitBlame($repo: String!, $rev: String!, $path: String!, $startLine: Int!, $endLine: Int!) {
repository(name: $repo) {
commit(rev: $rev) {
blob(path: $path) {
blame(startLine: 0, endLine: 0) {
blame(startLine: $startLine, endLine: $endLine) {
startLine
endLine
author {
Expand All @@ -160,7 +168,13 @@ export const queryBlameHunks = memoizeAsync(
}
}
`,
{ repo, rev, path }
{
repo,
rev,
path,
startLine: selections ? selections[0].start.line + 1 : 0,
endLine: selections ? selections[0].end.line + 1 : 0,
}
)
if (errors && errors.length > 0) {
throw new Error(errors.join('\n'))
Expand All @@ -170,7 +184,12 @@ export const queryBlameHunks = memoizeAsync(
}
return data.repository.commit.blob.blame
},
({ uri }) => uri
({ uri, selections }) => {
if (selections) {
return [uri, selections[0].start.line, selections[0].end.line].join(':')
}
return uri
}
)

/**
Expand Down
11 changes: 10 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@ export function activate(context: sourcegraph.ExtensionContext): void {
// brief flicker of the old state when the file is reopened.
async function decorate(editor: sourcegraph.CodeEditor, selections: sourcegraph.Selection[] | null): Promise<void> {
const settings = sourcegraph.configuration.get<Settings>().value
const decorations = settings['git.blame.decorations'] || 'none'
const shouldQueryBlameHunks = Boolean(decorations === 'file' || (decorations === 'line' && selections?.length))

try {
const hunks = await queryBlameHunks({ uri: editor.document.uri, sourcegraph })
const hunks = shouldQueryBlameHunks
? await queryBlameHunks({
uri: editor.document.uri,
sourcegraph,
selections: decorations === 'line' ? selections : null,
})
: []
const now = Date.now()

// Check if the extension host supports status bar items (Introduced in Sourcegraph version 3.26.0).
Expand Down

0 comments on commit a1e2624

Please sign in to comment.