Skip to content

Commit

Permalink
Merge pull request #77 from arshad-yaseen/check-backward-overlap
Browse files Browse the repository at this point in the history
Handle Backward Overlap
  • Loading branch information
arshad-yaseen authored Nov 17, 2024
2 parents c3efafc + 437ede1 commit ed5eab3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
55 changes: 47 additions & 8 deletions src/prompts/completion-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ ${textBeforeCursor}${CURSOR_TOKEN}${textAfterCursor}
${codeFile}
</context>
<critical_rules>
1. NEVER REPEAT ANY TEXT THAT APPEARS BEFORE THE CURSOR
2. Start your completion EXACTLY from the cursor position
3. If user types 'const ' and cursor is after it, DO NOT include 'const ' in your completion
4. ONLY provide the remaining part of the code that should appear after the cursor
5. Violation of these rules will cause code duplication and syntax errors
</critical_rules>
<primary_objectives>
1. Generate code that is syntactically correct and follows ${language || 'the language'}'s best practices
2. Ensure seamless integration with existing code structure
Expand All @@ -40,7 +48,8 @@ ${textBeforeCursor}${CURSOR_TOKEN}${textAfterCursor}
</primary_objectives>
<strict_requirements>
- Output MUST contain only the code to be inserted at cursor position
- Output MUST contain only the NEW code to be inserted at cursor position
- NEVER repeat any code that appears before the cursor position
- DO NOT include any code that appears before the cursor
- DO NOT include explanatory comments or documentation
- DO NOT wrap output in markdown code blocks
Expand All @@ -57,12 +66,40 @@ ${textBeforeCursor}${CURSOR_TOKEN}${textAfterCursor}
1. Analyze the code context before and after the cursor
2. Identify the current scope and available variables/functions
3. Determine the logical flow and required completion
4. Verify syntax compatibility with surrounding code
5. Ensure completion maintains code integrity
4. Remove any duplicate text that appears before cursor
5. Verify completion starts exactly at cursor position
</code_analysis_steps>
<examples>
[Previous examples section remains the same]
<example>
Context: "const <cursor>"
CORRECT COMPLETION: "myVariable = 42"
INCORRECT COMPLETION: "const myVariable = 42"
</example>
<example>
Context: "function hello<cursor>"
CORRECT COMPLETION: "(name: string) {\\n return 'Hello ' + name;\\n}"
INCORRECT COMPLETION: "function hello(name: string) {\\n return 'Hello ' + name;\\n}"
</example>
<example>
Context: "const randomNumber = Math.floor(Math.ran<cursor>00);"
CORRECT COMPLETION: "dom() * 1"
INCORRECT COMPLETION: "Math.random() * 1"
</example>
<example>
Context: "const result = 'Hello' + ' W<cursor>';"
CORRECT COMPLETION: "orld"
INCORRECT COMPLETION: "orld';"
</example>
<example>
Context: "function isPalindrome(<cursor>)"
CORRECT COMPLETION: "str) {\n return str === str.split('').reverse().join('');\n}"
INCORRECT COMPLETION: "(str) {\n return str === str.split('').reverse().join('');\n}"
</example>
</examples>
<error_prevention>
Expand All @@ -71,14 +108,16 @@ ${textBeforeCursor}${CURSOR_TOKEN}${textAfterCursor}
- Check for proper bracket and parenthesis matching
- Maintain consistent indentation with surrounding code
- Respect language-specific type safety requirements
- NEVER duplicate text that appears before cursor
</error_prevention>
<final_validation>
Before providing the completion:
1. Confirm the output contains ONLY the necessary code
2. Verify it fits seamlessly at the cursor position
3. Ensure it follows the active completion mode requirements
4. Check for consistency with existing code style
1. Confirm the output contains ONLY the new code after cursor position
2. Double-check no text before cursor is duplicated
3. Verify it fits seamlessly at the cursor position
4. Ensure it follows the active completion mode requirements
5. Check for consistency with existing code style
</final_validation>
</instructions>`.trim();

Expand Down
45 changes: 32 additions & 13 deletions src/utils/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ export const computeCompletionInsertionRange = (
}

const startOffset = mdl.getOffsetAt(pos);
const remainingText = mdl.getValue().substring(startOffset);
const textBeforeCursor = mdl.getValue().substring(0, startOffset);
const textAfterCursor = mdl.getValue().substring(startOffset);

let prefixOverlapLength = 0;
let suffixOverlapLength = 0;
let maxOverlapLength = 0;
let startOverlapLength = 0;

const completionLength = completion.length;
const remainingLength = remainingText.length;
const beforeLength = textBeforeCursor.length;
const afterLength = textAfterCursor.length;

// Handle cursor at the end of the document
if (startOffset >= mdl.getValue().length) {
Expand All @@ -53,7 +56,7 @@ export const computeCompletionInsertionRange = (
}

// Handle empty remaining text
if (remainingLength === 0) {
if (afterLength === 0) {
return new monaco.Range(
pos.lineNumber,
pos.column,
Expand All @@ -62,17 +65,28 @@ export const computeCompletionInsertionRange = (
);
}

const maxPossibleOverlap = Math.min(completionLength, remainingLength);
// Find overlap with text before cursor
const maxBeforeOverlap = Math.min(completionLength, beforeLength);
for (let i = 1; i <= maxBeforeOverlap; i++) {
const completionStart = completion.substring(0, i);
const textEnd = textBeforeCursor.slice(-i);
if (completionStart === textEnd) {
startOverlapLength = i;
}
}

// Find overlap with text after cursor
const maxAfterOverlap = Math.min(completionLength, afterLength);

// Find the longest prefix overlap
for (let i = 0; i < maxPossibleOverlap; i++) {
if (completion[i] !== remainingText[i]) break;
// Find the longest prefix overlap with text after cursor
for (let i = 0; i < maxAfterOverlap; i++) {
if (completion[i] !== textAfterCursor[i]) break;
prefixOverlapLength++;
}

// Find the longest suffix overlap
for (let i = 1; i <= maxPossibleOverlap; i++) {
if (completion.slice(-i) === remainingText.slice(0, i)) {
// Find the longest suffix overlap with text after cursor
for (let i = 1; i <= maxAfterOverlap; i++) {
if (completion.slice(-i) === textAfterCursor.slice(0, i)) {
suffixOverlapLength = i;
}
}
Expand All @@ -82,19 +96,24 @@ export const computeCompletionInsertionRange = (
// Check for internal overlaps if no prefix or suffix overlap
if (maxOverlapLength === 0) {
for (let i = 1; i < completionLength; i++) {
if (remainingText.startsWith(completion.substring(i))) {
if (textAfterCursor.startsWith(completion.substring(i))) {
maxOverlapLength = completionLength - i;
break;
}
}
}

// Calculate start and end positions
const startPosition =
startOverlapLength > 0
? mdl.getPositionAt(startOffset - startOverlapLength)
: pos;
const endOffset = startOffset + maxOverlapLength;
const endPosition = mdl.getPositionAt(endOffset);

return new monaco.Range(
pos.lineNumber,
pos.column,
startPosition.lineNumber,
startPosition.column,
endPosition.lineNumber,
endPosition.column,
);
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/src/app/api/complete/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {Copilot} from 'monacopilot';

const copilot = new Copilot(process.env.ANTHROPIC_API_KEY!);
const copilot = new Copilot(process.env.OPENAI_API_KEY!, {
provider: 'openai',
model: 'gpt-4o-mini',
});

export async function POST(req: Request) {
const body = await req.json();
Expand Down

0 comments on commit ed5eab3

Please sign in to comment.