Skip to content

Commit a1e2624

Browse files
authored
fix: query hunks only if decorations enabled (#304)
1 parent b990dce commit a1e2624

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/blame.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,24 @@ export const getAllBlameDecorations = (
127127
) => hunks.map(hunk => getDecorationFromHunk(hunk, now, hunk.startLine - 1, settings, sourcegraph))
128128

129129
export const queryBlameHunks = memoizeAsync(
130-
async ({ uri, sourcegraph }: { uri: string; sourcegraph: typeof import('sourcegraph') }): Promise<Hunk[]> => {
130+
async ({
131+
uri,
132+
sourcegraph,
133+
selections,
134+
}: {
135+
uri: string
136+
sourcegraph: typeof import('sourcegraph')
137+
selections: Selection[] | null
138+
}): Promise<Hunk[]> => {
131139
const { repo, rev, path } = resolveURI(uri)
132140
const { data, errors } = await sourcegraph.commands.executeCommand(
133141
'queryGraphQL',
134142
gql`
135-
query GitBlame($repo: String!, $rev: String!, $path: String!) {
143+
query GitBlame($repo: String!, $rev: String!, $path: String!, $startLine: Int!, $endLine: Int!) {
136144
repository(name: $repo) {
137145
commit(rev: $rev) {
138146
blob(path: $path) {
139-
blame(startLine: 0, endLine: 0) {
147+
blame(startLine: $startLine, endLine: $endLine) {
140148
startLine
141149
endLine
142150
author {
@@ -160,7 +168,13 @@ export const queryBlameHunks = memoizeAsync(
160168
}
161169
}
162170
`,
163-
{ repo, rev, path }
171+
{
172+
repo,
173+
rev,
174+
path,
175+
startLine: selections ? selections[0].start.line + 1 : 0,
176+
endLine: selections ? selections[0].end.line + 1 : 0,
177+
}
164178
)
165179
if (errors && errors.length > 0) {
166180
throw new Error(errors.join('\n'))
@@ -170,7 +184,12 @@ export const queryBlameHunks = memoizeAsync(
170184
}
171185
return data.repository.commit.blob.blame
172186
},
173-
({ uri }) => uri
187+
({ uri, selections }) => {
188+
if (selections) {
189+
return [uri, selections[0].start.line, selections[0].end.line].join(':')
190+
}
191+
return uri
192+
}
174193
)
175194

176195
/**

src/extension.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,17 @@ export function activate(context: sourcegraph.ExtensionContext): void {
5353
// brief flicker of the old state when the file is reopened.
5454
async function decorate(editor: sourcegraph.CodeEditor, selections: sourcegraph.Selection[] | null): Promise<void> {
5555
const settings = sourcegraph.configuration.get<Settings>().value
56+
const decorations = settings['git.blame.decorations'] || 'none'
57+
const shouldQueryBlameHunks = Boolean(decorations === 'file' || (decorations === 'line' && selections?.length))
58+
5659
try {
57-
const hunks = await queryBlameHunks({ uri: editor.document.uri, sourcegraph })
60+
const hunks = shouldQueryBlameHunks
61+
? await queryBlameHunks({
62+
uri: editor.document.uri,
63+
sourcegraph,
64+
selections: decorations === 'line' ? selections : null,
65+
})
66+
: []
5867
const now = Date.now()
5968

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

0 commit comments

Comments
 (0)