Skip to content

Commit

Permalink
Finishing autocomplete Cody Ignore (#2961)
Browse files Browse the repository at this point in the history
This is the Autocomplete step of my context hardening for Cody Ignore
https://github.com/sourcegraph/sourcegraph/issues/59683

[Document](https://docs.google.com/document/d/1LUNSOuwwhgycfCvqoI3AETS-sDIjCpZknFv2ueQ8Ctw/edit)
 
Snippet
<img width="698" alt="image"
src="https://github.com/sourcegraph/cody/assets/11155207/309404fa-52dd-4e78-92b5-e69939d1ff1c">


## Test plan

Try it in the debugger UI. USe this file for instructions of Cody Ignore
https://sourcegraph.com/github.com/sourcegraph/cody@adding-autocomplete-ignore/-/blob/vscode/doc/codyignore.md?L16

---------

Co-authored-by: Beatrix <[email protected]>
  • Loading branch information
arafatkatze and abeatrix authored Feb 1, 2024
1 parent e29bf03 commit 2167395
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
69 changes: 67 additions & 2 deletions vscode/src/completions/context/context-mixer.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { describe, expect, it, vi } from 'vitest'
import { beforeAll, describe, expect, it, vi } from 'vitest'

import { testFileUri, uriBasename } from '@sourcegraph/cody-shared'
import { ignores, isCodyIgnoredFile, testFileUri, uriBasename } from '@sourcegraph/cody-shared'

import { getCurrentDocContext } from '../get-current-doc-context'
import { documentAndPosition } from '../test-helpers'
import type { ContextRetriever, ContextSnippet } from '../types'

import { ContextMixer } from './context-mixer'
import type { ContextStrategyFactory } from './context-strategy'
import { Utils } from 'vscode-uri'
import { CODY_IGNORE_URI_PATH } from '@sourcegraph/cody-shared/src/cody-ignore/ignore-helper'

function createMockStrategy(resultSets: ContextSnippet[][]): ContextStrategyFactory {
const retrievers = []
Expand Down Expand Up @@ -217,6 +219,69 @@ describe('ContextMixer', () => {
totalChars: 136,
})
})

describe('retrived context is filtered by .cody/ignore', () => {
const workspaceRoot = testFileUri('')
beforeAll(() => {
ignores.setActiveState(true)
// all foo.ts files will be ignored
ignores.setIgnoreFiles(workspaceRoot, [
{
uri: Utils.joinPath(workspaceRoot, '.', CODY_IGNORE_URI_PATH),
content: '**/foo.ts',
},
])
})
it('mixes results are filtered', async () => {
const mixer = new ContextMixer(
createMockStrategy([
[
{
uri: testFileUri('foo.ts'),
content: 'function foo1() {}',
startLine: 0,
endLine: 0,
},
{
uri: testFileUri('foo/bar.ts'),
content: 'function bar1() {}',
startLine: 0,
endLine: 0,
},
],
[
{
uri: testFileUri('test/foo.ts'),
content: 'function foo3() {}',
startLine: 10,
endLine: 10,
},
{
uri: testFileUri('foo.ts'),
content: 'function foo1() {}\nfunction foo2() {}',
startLine: 0,
endLine: 1,
},
{
uri: testFileUri('example/bar.ts'),
content: 'function bar1() {}\nfunction bar2() {}',
startLine: 0,
endLine: 1,
},
],
])
)
const { context } = await mixer.getContext(defaultOptions)
const contextFiles = normalize(context)
// returns 2 bar.ts context
expect(contextFiles?.length).toEqual(2)
for (const context of contextFiles) {
expect(
isCodyIgnoredFile(Utils.joinPath(workspaceRoot, context.fileName))
).toBeFalsy()
}
})
})
})
})

Expand Down
7 changes: 4 additions & 3 deletions vscode/src/completions/context/context-mixer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as vscode from 'vscode'

import { wrapInActiveSpan } from '@sourcegraph/cody-shared'
import { wrapInActiveSpan, isCodyIgnoredFile } from '@sourcegraph/cody-shared'

import type { DocumentContext } from '../get-current-doc-context'
import type { ContextSnippet } from '../types'
Expand Down Expand Up @@ -80,7 +80,7 @@ export class ContextMixer implements vscode.Disposable {
const results = await Promise.all(
retrievers.map(async retriever => {
const retrieverStart = performance.now()
const snippets = await wrapInActiveSpan(
const allSnippets = await wrapInActiveSpan(
`autocomplete.retrieve.${retriever.identifier}`,
() =>
retriever.retrieve({
Expand All @@ -91,11 +91,12 @@ export class ContextMixer implements vscode.Disposable {
},
})
)
const filteredSnippets = allSnippets.filter(snippet => !isCodyIgnoredFile(snippet.uri))

return {
identifier: retriever.identifier,
duration: performance.now() - retrieverStart,
snippets: new Set(snippets),
snippets: new Set(filteredSnippets),
}
})
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as vscode from 'vscode'
import type { URI } from 'vscode-uri'

import { isCodyIgnoredFile } from '@sourcegraph/cody-shared'

import type { ContextRetriever, ContextRetrieverOptions, ContextSnippet } from '../../../types'
import { baseLanguageId } from '../../utils'

Expand Down Expand Up @@ -38,7 +36,7 @@ export class JaccardSimilarityRetriever implements ContextRetriever {
const matches: JaccardMatchWithFilename[] = []
for (const { uri, contents } of files) {
const match = bestJaccardMatch(targetText, contents, SNIPPET_WINDOW_SIZE)
if (!match || abortSignal?.aborted || isCodyIgnoredFile(uri)) {
if (!match || abortSignal?.aborted) {
continue
}

Expand Down Expand Up @@ -99,11 +97,6 @@ async function getRelevantFiles(
return
}

// Do not add files that are on the codyignore list
if (isCodyIgnoredFile(document.uri)) {
return
}

if (baseLanguageId(document.languageId) !== baseLanguageId(curLang)) {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as vscode from 'vscode'
import type { URI } from 'vscode-uri'

import { isCodyIgnoredFile } from '@sourcegraph/cody-shared'

import { getContextRange } from '../../../doc-context-getters'
import type { ContextRetriever, ContextRetrieverOptions } from '../../../types'
import { baseLanguageId } from '../../utils'
Expand Down Expand Up @@ -53,7 +51,7 @@ export class JaccardSimilarityRetriever implements ContextRetriever {

const matches: JaccardMatchWithFilename[] = []
for (const { uri, contents } of files) {
if (isCodyIgnoredFile(uri) || abortSignal?.aborted) {
if (abortSignal?.aborted) {
continue
}
const fileMatches = bestJaccardMatches(
Expand Down Expand Up @@ -132,11 +130,6 @@ async function getRelevantFiles(
return
}

// Do not add files that are on the codyignore list
if (isCodyIgnoredFile(document.uri)) {
return
}

if (baseLanguageId(document.languageId) !== baseLanguageId(curLang)) {
return
}
Expand Down

0 comments on commit 2167395

Please sign in to comment.