Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { loadRequiredLanguageParsers } from "../languageParser"
import tsxQuery from "../queries/tsx"
import { initializeTreeSitter, testParseSourceCodeDefinitions, inspectTreeStructure, debugLog } from "./helpers"

// Sample component content
// Sample component content with enhanced TypeScript language constructs
const sampleTsxContent = `
// Original components
interface VSCodeCheckboxProps {
checked: boolean
onChange: (checked: boolean) => void
Expand Down Expand Up @@ -66,7 +67,236 @@ const TemperatureControl = ({
</>
)
}
}`

// Utility Types
type User = {
id: string;
username: string;
password: string;
email: string;
}

// Partial - Makes all properties optional
type PartialUser = Partial<User>;

// Required - Makes all properties required
type RequiredConfig = Required<{theme?: string, showHeader?: boolean}>;

// Readonly - Makes all properties readonly
type ReadonlyState = Readonly<{count: number, status: string}>;

// Function Overloads
function process(value: string): string;
function process(value: number): number;
function process(value: boolean): boolean;
function process(value: any): any {
return value;
}

// Async Function
async function fetchData(url: string): Promise<Response> {
const response = await fetch(url);
return response;
}

// Async Arrow Function
const fetchUser = async (id: string): Promise<User> => {
const response = await fetch(\`/api/users/\${id}\`);
return response.json();
};

// Class with Members and Properties
class AdvancedComponent {
// Public property
public name: string;

// Private property
private _count: number = 0;

// Protected property
protected status: 'active' | 'inactive' = 'active';

// Readonly property
readonly id: string;

// Static property
static defaultProps = {
theme: 'light',
showHeader: true
};

// Constructor
constructor(name: string, id: string) {
this.name = name;
this.id = id;
}

// Getter method
get count(): number {
return this._count;
}

// Setter method
set count(value: number) {
if (value >= 0) {
this._count = value;
}
}

// Public method
public updateName(newName: string): void {
this.name = newName;
}
}

// React Hooks and Context
import React, { createContext, useContext, useState, useEffect } from 'react';

// Create a context
const ThemeContext = createContext({
theme: 'light',
toggleTheme: () => {}
});

// Context provider and consumer
const ThemeProvider = ThemeContext.Provider;
const ThemeConsumer = ThemeContext.Consumer;

// Custom hook using context
function useTheme() {
const context = useContext(ThemeContext);
if (!context) {
throw new Error('useTheme must be used within a ThemeProvider');
}
return context;
}

// Component using hooks
function ThemeToggler() {
// useState hook
const [theme, setTheme] = useState('light');

// useEffect hook
useEffect(() => {
document.body.dataset.theme = theme;
return () => {
delete document.body.dataset.theme;
};
}, [theme]);

return (
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Switch to {theme === 'light' ? 'dark' : 'light'} theme
</button>
);
}

// Decorator Example
@Component({
selector: 'app-root',
template: '<div>App Component</div>'
})
class AppComponent {
title = 'My App';

@Input()
data: string[] = [];
}

// Enum Declaration
enum LogLevel {
Error = 1,
Warning = 2,
Info = 3,
Debug = 4
}

// Namespace Declaration
namespace Validation {
export function isValidEmail(email: string): boolean {
return email.includes('@');
}

export function isValidPhone(phone: string): boolean {
return phone.length >= 10;
}
}

// Complex Nested Components and Member Expressions
export const ComplexComponent = () => {
return (
<CustomHeader
title="Test"
subtitle={
<span className="text-gray-500">
Nested <strong>content</strong>
</span>
}
/>
);
};

export const NestedSelectors = () => (
<section>
<Select.Option>
<Group.Item>
<Text.Body>Deeply nested</Text.Body>
</Group.Item>
</Select.Option>
</section>
);

// Template Literal Types
type EventName<T extends string> = \`on\${Capitalize<T>}\`;
type CSSProperty<T extends string> = \`--\${T}\` | \`-webkit-\${T}\` | \`-moz-\${T}\` | \`-ms-\${T}\`;
type RouteParams<T extends string> = T extends \`\${string}:\${infer Param}/\${infer Rest}\`
? { [K in Param | keyof RouteParams<Rest>]: string }
: T extends \`\${string}:\${infer Param}\`
? { [K in Param]: string }
: {};

// Conditional Types
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type Parameters<T> = T extends (...args: infer P) => any ? P : never;
type InstanceType<T> = T extends new (...args: any[]) => infer R ? R : never;
type IsFunction<T> = T extends (...args: any[]) => any ? true : false;

// Generic Components with Constraints
type ComplexProps<T> = {
data: T[];
render: (item: T) => React.ReactNode;
};

export const GenericList = <T extends { id: string }>({
data,
render
}: ComplexProps<T>) => (
<div>
{data.map(item => render(item))}
</div>
);

export const ConditionalComponent = ({ condition }) =>
condition ? (
<PrimaryContent>
<h1>Main Content</h1>
</PrimaryContent>
) : (
<FallbackContent />
);

// Dictionary Interface with Constrained Key Types
interface Dictionary<K extends string | number, V> {
get(key: K): V | undefined;
set(key: K, value: V): void;
has(key: K): boolean;
}

type KeyValuePair<K extends string | number, V> = {
key: K;
value: V;
};
`

// We'll use the debug test to test the parser directly

Expand Down
85 changes: 80 additions & 5 deletions src/services/tree-sitter/queries/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,94 @@
- method declarations (with namespace scope)
- typedef declarations
- class declarations
- enum declarations (including enum class)
- namespace declarations (including nested namespaces)
- template declarations (including specializations and variadic templates)
- macro definitions
- constructor declarations
- destructor declarations
- operator overloading
- static member declarations
- friend declarations
- using declarations and directives
- alias declarations (using)
- constexpr functions and variables
- lambda expressions
- attributes
- inheritance relationships
- static variables
- virtual functions
- auto type deduction
- concepts (C++20)
- inline functions and variables
- nested namespaces (C++17)
- structured bindings (C++17)
- noexcept specifier
- default parameters
- variadic templates
- explicit template instantiation
*/
export default `
(struct_specifier name: (type_identifier) @name.definition.class body:(_)) @definition.class
; Struct declarations
(struct_specifier name: (type_identifier) @name.definition.class) @definition.class

(declaration type: (union_specifier name: (type_identifier) @name.definition.class)) @definition.class
; Union declarations
(union_specifier name: (type_identifier) @name.definition.class) @definition.class

; Function declarations
(function_declarator declarator: (identifier) @name.definition.function) @definition.function

; Method declarations (field identifier)
(function_declarator declarator: (field_identifier) @name.definition.function) @definition.function

(function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @scope name: (identifier) @name.definition.method)) @definition.method
; Class declarations
(class_specifier name: (type_identifier) @name.definition.class) @definition.class

(type_definition declarator: (type_identifier) @name.definition.type) @definition.type
; Enum declarations
(enum_specifier name: (type_identifier) @name.definition.enum) @definition.enum

(class_specifier name: (type_identifier) @name.definition.class) @definition.class
; Namespace declarations
(namespace_definition name: (namespace_identifier) @name.definition.namespace) @definition.namespace

; Template declarations
(template_declaration) @definition.template

; Template class declarations
(template_declaration (class_specifier name: (type_identifier) @name.definition.template_class)) @definition.template_class

; Template function declarations
(template_declaration (function_definition declarator: (function_declarator declarator: (identifier) @name.definition.template_function))) @definition.template_function

; Virtual functions
(function_definition (virtual)) @definition.virtual_function

; Auto type deduction
(declaration type: (placeholder_type_specifier (auto))) @definition.auto_variable

; Structured bindings (C++17) - using a text-based match
(declaration) @definition.structured_binding
(#match? @definition.structured_binding "\\[.*\\]")

; Inline functions and variables - using a text-based match
(function_definition) @definition.inline_function
(#match? @definition.inline_function "inline")

(declaration) @definition.inline_variable
(#match? @definition.inline_variable "inline")

; Noexcept specifier - using a text-based match
(function_definition) @definition.noexcept_function
(#match? @definition.noexcept_function "noexcept")

; Function with default parameters - using a text-based match
(function_declarator) @definition.function_with_default_params
(#match? @definition.function_with_default_params "=")

; Variadic templates - using a text-based match
(template_declaration) @definition.variadic_template
(#match? @definition.variadic_template "\\.\\.\\.")

; Explicit template instantiation - using a text-based match
(template_declaration) @definition.template_instantiation
(#match? @definition.template_instantiation "template\\s+class|template\\s+struct")
`
Loading