Skip to content

Commit

Permalink
Document code: Add tree-sitter query for CPP (#4392)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeatrix authored May 30, 2024
1 parent 8f85eeb commit dc14cf4
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 15 deletions.
12 changes: 9 additions & 3 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ This is a log of all notable changes to Cody for VS Code. [Unreleased] changes a
- Chat: Integerated OpenCtx providers with @-mention context menu. [pull/4201](https://github.com/sourcegraph/cody/pull/4201)
- Enterprise: Adds support for the `completions.smartContextWindow` (available in Sourcegraph v5.5.0+) site configuration. [pull/4236](https://github.com/sourcegraph/cody/pull/4236)
- Chat: Integerated OpenCtx providers with @-mention context menu. [pull/4201](https://github.com/sourcegraph/cody/pull/4201/files)
- Keybinding: Assign the same keyboard shortcut for starting a new chat to the "New Chat with Selection" command.
- Keybinding: Assign the same keyboard shortcut for starting a new chat to the "New Chat with Selection" command. [pull/4255](https://github.com/sourcegraph/cody/pull/4255)
- Telemetry: Adds a new telemetry event when users uninstall the extension. [pull/4246](https://github.com/sourcegraph/cody/pull/4246)
- Chat: Added @-mention remote repositories search provider for enterprise. [pull/4311](https://github.com/sourcegraph/cody/pull/4311)
- Chat: Editor selection is now included in all chats by default. []()
- Chat: Editor selection is now included in all chats by default. [pull/4292](https://github.com/sourcegraph/cody/pull/4292)
- Chat: Assistant responses now have a "Try again with different context" line at the bottom with ways you can improve the context used to generate the response. [pull/4317](https://github.com/sourcegraph/cody/pull/4317)
- Document Code: Added additional Java support for range expansion. [pull/4353](https://github.com/sourcegraph/cody/pull/4353)
- Document Code: Adds additional languages support for range expansion:
- Java: [pull/4353](https://github.com/sourcegraph/cody/pull/4353)
- Kotlin: [pull/4355](https://github.com/sourcegraph/cody/pull/4355)
- Rust: [pull/4358](https://github.com/sourcegraph/cody/pull/4358)
- PHP: [pull/4359](https://github.com/sourcegraph/cody/pull/4359)
- C: [pull/4391](https://github.com/sourcegraph/cody/pull/4391)
- C++: [pull/4392](https://github.com/sourcegraph/cody/pull/4392)

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions vscode/src/tree-sitter/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SupportedLanguage } from './grammars'
import { cQueries } from './queries/c'
import { cppQueries } from './queries/cpp'
import { goQueries } from './queries/go'
import { javaQueries } from './queries/java'
import { javascriptQueries } from './queries/javascript'
Expand Down Expand Up @@ -53,4 +54,5 @@ export const languages: Partial<Record<SupportedLanguage, Record<QueryName, stri
...phpQueries,
...rustQueries,
...cQueries,
...cppQueries,
} as const
51 changes: 51 additions & 0 deletions vscode/src/tree-sitter/queries/cpp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import dedent from 'dedent'

import { SupportedLanguage } from '../grammars'
import type { QueryName } from '../queries'

const DOCUMENTABLE_NODES = dedent`
; Function definitions
;--------------------------------
(function_definition
type: (primitive_type)
declarator: (function_declarator)
body: (compound_statement) @symbol.function) @range.function
; Class definitions
;--------------------------------
(class_specifier
name: (type_identifier)
body: (_)) @range.class
(struct_specifier
name: (type_identifier)
body: (_)) @range.struct
(declaration
type: (union_specifier
name: (type_identifier) @symbol.union)) @range.union
; Variables
;--------------------------------
(declaration) @symbol.identifier @range.identifier
; Types
;--------------------------------
(type_definition
type: (struct_specifier) @symbol.identifier) @range.identifier
(enum_specifier
name: (type_identifier) @symbol.identifier) @range.identifier
`

const ENCLOSING_FUNCTION_QUERY = dedent`
(function_definition declarator: (function_declarator) @symbol.function) @range.function
`

export const cppQueries = {
[SupportedLanguage.cpp]: {
singlelineTriggers: '',
intents: '',
documentableNodes: DOCUMENTABLE_NODES,
identifiers: '',
graphContextIdentifiers: '',
enclosingFunction: ENCLOSING_FUNCTION_QUERY,
},
} satisfies Partial<Record<SupportedLanguage, Record<QueryName, string>>>
28 changes: 16 additions & 12 deletions vscode/src/tree-sitter/query-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ describe('getDocumentQuerySDK', () => {
{ languageId: SupportedLanguage.typescriptreact },
{ languageId: SupportedLanguage.go },
{ languageId: SupportedLanguage.python },
{ languageId: SupportedLanguage.java },
{ languageId: SupportedLanguage.kotlin },
{ languageId: SupportedLanguage.rust },
{ languageId: SupportedLanguage.php },
{ languageId: SupportedLanguage.c },
{ languageId: SupportedLanguage.cpp },
])('returns valid SDK for $languageId', async ({ languageId }) => {
const nonInitializedSDK = getDocumentQuerySDK(languageId)
expect(nonInitializedSDK).toBeNull()
Expand All @@ -29,18 +34,17 @@ describe('getDocumentQuerySDK', () => {
expect(sdk?.queries.intents).toBeTruthy()
})

it.each([
{ languageId: SupportedLanguage.csharp },
{ languageId: SupportedLanguage.cpp },
{ languageId: SupportedLanguage.csharp },
])('returns null for $languageId because queries are not defined', async ({ languageId }) => {
const nonInitializedSDK = getDocumentQuerySDK(languageId)
expect(nonInitializedSDK).toBeNull()
it.each([{ languageId: SupportedLanguage.csharp }])(
'returns null for $languageId because queries are not defined',
async ({ languageId }) => {
const nonInitializedSDK = getDocumentQuerySDK(languageId)
expect(nonInitializedSDK).toBeNull()

const parser = await initTreeSitterParser(languageId)
expect(parser).toBeTruthy()
const parser = await initTreeSitterParser(languageId)
expect(parser).toBeTruthy()

const sdk = getDocumentQuerySDK(languageId)
expect(sdk).toBeNull()
})
const sdk = getDocumentQuerySDK(languageId)
expect(sdk).toBeNull()
}
)
})
11 changes: 11 additions & 0 deletions vscode/src/tree-sitter/query-tests/documentable-nodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,15 @@ describe('getDocumentableNode', () => {
sourcesPath: 'test-data/documentable-node.c',
})
})

it('cpp', async () => {
const { language, parser, queries } = await initTreeSitterSDK(SupportedLanguage.cpp)

await annotateAndMatchSnapshot({
parser,
language,
captures: queryWrapper(queries.getDocumentableNode),
sourcesPath: 'test-data/documentable-node.cpp',
})
})
})
125 changes: 125 additions & 0 deletions vscode/src/tree-sitter/query-tests/test-data/documentable-node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* wrapper */
void wrapper()
{
void test()
{
// |
}
}

// ------------------------------------

void test()
{
// |
}

// ------------------------------------

static void twoSum(int *nums, int numsSize, int target, int *returnSize)
{
if (numsSize < 2)
{
return;
}
else
{
for (int i = 0; i < numsSize; i++)
{
for (int j = i + 1; j < numsSize; j++)
{
if (nums[i] + nums[j] == target)
{
// |
returnSize[0] = i;
returnSize[1] = j;
return;
}
}
}
}
}

// ------------------------------------

void test_multiline_func_declaration(
// |
int val,
int val2)
{
wrapper();
}

// ------------------------------------

void test_parameter(int val)
{
// |
wrapper();
}

// ------------------------------------

typedef struct
{
// |
} Agent;

// ------------------------------------

typedef struct
{
// |
void (*__init__)(struct AgentMultiLine *self, char *name);
} AgentMultiLine;

// ------------------------------------

void AgentMultiLine__init__(AgentMultiLine *self, char *name)
{
// |
self->name = name;
}

// ------------------------------------

typedef struct
{
char *name;
// |
} Agent;

// ------------------------------------

void Agent_test(Agent *self)
{
// |
}

// ------------------------------------

void return_statement()
{
return;
// |
}

// ------------------------------------

return_statement('value');
// |

// ------------------------------------

char *user_name = "Tom";
// |

// ------------------------------------

enum Level
{
// |
LOW,
MEDIUM,
HIGH
};
Loading

0 comments on commit dc14cf4

Please sign in to comment.