Intelligent Code Completion for Vue Components
This challenge focuses on building a simplified code completion system for Vue components, specifically targeting the <template> section. Code completion is a crucial feature in modern IDEs, significantly boosting developer productivity by suggesting available properties, methods, and components. Your task is to create a TypeScript function that analyzes a Vue component's <script setup> section and generates a list of potential code completion suggestions for a given cursor position within its <template>.
Problem Description
You are tasked with creating a TypeScript function getCodeCompletionSuggestions that takes a Vue component's code (as a string) and a cursor position (as a number representing the character index within the code) as input. The function should parse the <script setup> section of the component, extract all declared variables, properties, methods, and imported components, and then return a list of relevant code completion suggestions based on the cursor position within the <template>.
What needs to be achieved:
- Parse the Vue component code to identify the
<script setup>section. - Extract all variables, properties, methods, and imported components declared within the
<script setup>section. - Analyze the cursor position within the
<template>section. - Generate a list of code completion suggestions relevant to the context around the cursor position.
Key Requirements:
- The function must be written in TypeScript.
- The function should handle basic Vue component syntax, including variable declarations, property assignments, method definitions, and component imports.
- The function should return an array of strings, where each string represents a potential code completion suggestion.
- The function should prioritize suggestions that are syntactically valid and contextually relevant to the cursor position.
- The function should ignore comments and strings when parsing.
Expected Behavior:
Given a Vue component's code and a cursor position within the <template>, the function should return a list of suggestions that are likely to be valid and useful for the developer. The suggestions should be based on the variables, properties, methods, and components declared in the <script setup> section.
Edge Cases to Consider:
- Component code without a
<script setup>section. - Component code with invalid syntax.
- Cursor position outside of the
<template>section. - Complex expressions within the
<template>section. - Nested components.
- Dynamic variable names.
Examples
Example 1:
Input:
```typescript
<template>
<div>
<p>{{ message }}</p>
<button @click="handleClick">Click Me</button>
<MyComponent :data="myData" />
</div>
</template>
<script setup>
import MyComponent from './MyComponent.vue';
const message = 'Hello, Vue!';
const myData = { name: 'World' };
function handleClick() {
console.log('Button clicked!');
}
</script>
cursorPosition: 32 (inside {{ message }})
Output:
["message"]
Explanation: The cursor is inside the `{{ message }}` interpolation. The function should suggest the variable `message` declared in the `<script setup>` section.
Example 2:
Input:
```typescript
<template>
<div>
<p>{{ message }}</p>
<button @click="handleClick">Click Me</button>
<MyComponent :data="myData" />
</div>
</template>
<script setup>
import MyComponent from './MyComponent.vue';
const message = 'Hello, Vue!';
const myData = { name: 'World' };
function handleClick() {
console.log('Button clicked!');
}
</script>
cursorPosition: 52 (inside handleClick)
Output:
["handleClick"]
Explanation: The cursor is inside the `@click` event handler, suggesting the method `handleClick`.
Example 3:
Input:
```typescript
<template>
<div>
<MyComponent :data="myData" />
</div>
</template>
<script setup>
import MyComponent from './MyComponent.vue';
const myData = { name: 'World' };
</script>
cursorPosition: 25 (inside :data="myData")
Output:
["myData"]
Explanation: The cursor is inside the `:data` prop binding, suggesting the variable `myData`.
Constraints
- The input component code string will be at least 100 characters long.
- The cursor position will be a non-negative integer less than the length of the component code string.
- The function must return an array of strings. The array can be empty if no suggestions are found.
- The function should execute within 200ms for typical Vue component code.
- The function should only consider variables, properties, methods, and imported components declared within the
<script setup>section.
Notes
- You don't need to implement a full-fledged parser. A simple regular expression-based approach is acceptable for this challenge.
- Focus on identifying the relevant keywords and patterns within the
<script setup>section. - Consider the context around the cursor position to prioritize suggestions. For example, if the cursor is inside a variable interpolation (
{{ ... }}), suggest variables. If the cursor is inside an event handler (@click="..."), suggest methods. - Error handling is not required. Assume the input component code is well-formed.
- This is a simplified version of code completion. A real-world implementation would involve more sophisticated parsing and semantic analysis.