From d8e97c2b4efb9944e89134f3365f1531c19fbb93 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 4 Oct 2024 18:26:01 +0300 Subject: [PATCH] stable version 0.2.0, now providing preview pressing double tab to accept preview --- package.json | 9 ++- src/extension.ts | 140 +++++++++++++++++++------------------ src/test.py | 175 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 252 insertions(+), 72 deletions(-) create mode 100644 src/test.py diff --git a/package.json b/package.json index 10be8e5..53d6a68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fabelous-autocoder", - "version": "0.2.74", + "version": "0.2.0", "displayName": "Fabelous Autocoder", "description": "A simple to use Ollama autocompletion Plugin", "icon": "icon.png", @@ -110,10 +110,9 @@ }, "commands": [ { - "command": "fabelous-autocoder.autocomplete", - "title": "Fabelous autocompletion" - } - ] + "command": "fabelous-autocoder.autocomplete", + "title": "Fabelous Autocompletion" + }] }, "scripts": { "vscode:prepublish": "npm run compile", diff --git a/src/extension.ts b/src/extension.ts index cfa1354..acaa133 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -61,7 +61,6 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({ textDecoration: 'none; display: none;', // Hide the original text }); - async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { const document = textEditor.document; const position = textEditor.selection.active; @@ -117,65 +116,74 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo let previewInserted = false; let originalContent: string; let previewStartLine: number; + const storeAndInsertPreview = async () => { + previewStartLine = startLine; + const endLine = document.lineCount - 1; + const endCharacter = document.lineAt(endLine).text.length; + const fullRange = new vscode.Range(startLine, 0, endLine, endCharacter); + originalContent = document.getText(fullRange); + + const previewContent = completionText + '\n'.repeat(1); + const edit = new vscode.WorkspaceEdit(); + edit.replace(document.uri, fullRange, previewContent); // Overwrite the content + await vscode.workspace.applyEdit(edit); + + // Split the preview content into lines + const previewLines = previewContent.split('\n'); + + // Calculate the exact position of the last character of the preview content + const lastPreviewLineIndex = previewStartLine + previewLines.length - 1; + const lastPreviewLine = previewLines[previewLines.length - 2]; // Second to last line (last line is newline) + const newPosition = new vscode.Position(lastPreviewLineIndex - 1, lastPreviewLine.length); // Place cursor at end of last line + + // Set the new cursor position after inserting preview + textEditor.selection = new vscode.Selection(newPosition, newPosition); + + // Explicitly scroll to reveal the cursor position at the end of the preview + textEditor.revealRange(new vscode.Range(newPosition, newPosition), vscode.TextEditorRevealType.InCenter); + + // Set decorations on the newly inserted lines + const previewRanges: vscode.DecorationOptions[] = []; + for (let i = 0; i < previewLines.length; i++) { + const range = new vscode.Range(previewStartLine + i, 0, previewStartLine + i, previewLines[i].length); + previewRanges.push({ + range, + renderOptions: { + after: { + contentText: previewLines[i], + } + } + }); + } + textEditor.setDecorations(previewDecorationType, previewRanges); + previewInserted = true; + }; + + const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { + const textEditor = vscode.window.activeTextEditor; + if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted) { + return; + } + + // Determine tab size setting + const tabSize = textEditor.options.tabSize as number || 4; + + for (const change of event.contentChanges) { + const changeStartLine = change.range.start.line; + + // Detect Backspace by a single-character deletion + if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) { + await restoreOriginalContent(); + } + + // Detect Tab by checking if the change is a multiple of spaces equal to the tab size + if (/^[ ]+$/.test(change.text) && change.text.length === tabSize && changeStartLine >= previewStartLine) { + await acceptPreview(textEditor, document, startLine, position, completionText); + } + } + }); - // Store the original content and insert preview -const storeAndInsertPreview = async () => { - previewStartLine = startLine; - const endLine = document.lineCount - 1; - const endCharacter = document.lineAt(endLine).text.length; - const fullRange = new vscode.Range(startLine, 0, endLine, endCharacter); - originalContent = document.getText(fullRange); - - const previewContent = completionText + '\n'.repeat(1); - const edit = new vscode.WorkspaceEdit(); - edit.replace(document.uri, fullRange, previewContent); // Overwrite the context - await vscode.workspace.applyEdit(edit); - - // Move the cursor to the end of the inserted content - const previewLines = previewContent.split('\n'); - const lastPreviewLineIndex = previewStartLine + previewLines.length - 1; - const lastPreviewLine = previewLines[previewLines.length - 1]; - const newPosition = new vscode.Position(lastPreviewLineIndex, lastPreviewLine.length); - textEditor.selection = new vscode.Selection(newPosition, newPosition); - - // Set decorations on the newly inserted lines - const previewRanges: vscode.DecorationOptions[] = []; - for (let i = 0; i < previewLines.length; i++) { - const range = new vscode.Range(previewStartLine + i, 0, previewStartLine + i + 1, 0); - previewRanges.push({ - range, - renderOptions: { - after: { - contentText: previewLines[i], - } - } - }); - } - textEditor.setDecorations(previewDecorationType, previewRanges); - previewInserted = true; -}; - - const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { - if (event.document.uri.toString() === document.uri.toString() && previewInserted) { - console.log("URI matches and preview inserted"); - if (event.contentChanges.length > 0) { - const change = event.contentChanges[0]; - console.log("Change:", change); - - // Accept preview with Tab key - if (change && change.text === '\t' && previewInserted) { - console.log("Tab key pressed, accepting preview"); - await acceptPreview(textEditor, document, startLine, position, completionText); - } - - // Keep the existing backspace functionality - if (change.text === '' && change.range.start.line >= previewStartLine) { - console.log("Backspace detected"); - await restoreOriginalContent(); - } - } - } - }); + const restoreOriginalContent = async () => { if (!previewInserted) return; @@ -196,36 +204,33 @@ const storeAndInsertPreview = async () => { const newPosition = new vscode.Position(lastOriginalLineNumber, lastLineLength); textEditor.selection = new vscode.Selection(newPosition, newPosition); }; - - // Modify the acceptPreview function to match the working version const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => { console.log("Accepting preview"); textEditor.setDecorations(previewDecorationType, []); const edit = new vscode.WorkspaceEdit(); - + // Insert the completion text const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines - + // Replace the entire range from the start of the context to the current position const replaceRange = new vscode.Range(startLine, 0, position.line, position.character); edit.replace(document.uri, replaceRange, insertText); - + await vscode.workspace.applyEdit(edit); await document.save(); // Optionally save after inserting - + // Move the cursor to the end of the inserted completion const totalNewLines = insertText.split('\n').length; const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0); textEditor.selection = new vscode.Selection(newPosition, newPosition); - + disposable.dispose(); previewInserted = false; console.log("Preview accepted"); }; - // Call this function to initiate the preview await storeAndInsertPreview(); @@ -240,6 +245,7 @@ const storeAndInsertPreview = async () => { } + async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) { const item = new vscode.CompletionItem("Fabelous autocompletion"); item.insertText = new vscode.SnippetString('${1:}'); diff --git a/src/test.py b/src/test.py new file mode 100644 index 0000000..8dfb3f9 --- /dev/null +++ b/src/test.py @@ -0,0 +1,175 @@ + + + + + +def quicksort(arr): + if len(arr) <= 1: + return arr + else: + pivot = arr[len(arr)//2] # choose the middle element as the pivot + left_partition = [i for i in arr if i < pivot] + right_partition = [i for i in arr if i > pivot] + + return quicksort(left_partition) + [pivot] + quicksort(right_partition) + + +def quicksort(arr): + if len(arr) <= 1: + return arr + mid = int(len(arr)/2) + left = [item for i, item in enumerate(arr) if i=mid] + + return quicksort(left)+[arr[mid]]+quicksort(right) +def is_fibunccai(num): + num = str(num) + size = len(num) + + # Create 1s array for DP table to store last 10 digits of Fibonacci sequence + fib = [1] * 10 + + # Initialize last two numbers in Fibonacci sequence as 1 and 2 +def binary_search(arr, num) : + left = 0; right = len(arr) - 1 + + while (left <= right) : + mid = int((right + left) / 2) + + if arr[mid] == num : + return mid + + elif arr[mid] > num : + right = mid - 1 + + else: + left = mid + 1 + + return -1 + +arr = [2, 4, 5, 7, 8, 9, 10, 11, 12] +num = 9 +index = binary_search(arr, num) + +if index!= -1: + print("Element is present at ", index) +else: + print("Element is not present in array") + +def quicksort(arr): + if len(arr) <= 1: + return arr + + pivot = arr[len(arr)//2] + left_partition, right_partition = [], [] + + for i in range(len(arr)): + if arr[i] < pivot: + left_partition.append(arr[i]) + else: + right_partition.append(arr[i]) + + return quicksort(left_partition) + [pivot] + quicksort(right_partition) + +def merge_sort(arr): + if len(arr) > 1: + mid = len(arr) // 2 + left_half = arr[:mid] + right_half = arr[mid:] + + # Recursive call for sorting the two halves + merge_sort(left_half) + merge_sort(right_half) + + i = j = k = 0 + + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + arr[k] = left_half[i] + i += 1 + else: + arr[k] = right_half[j] + j += 1 + k += 1 + + # Checking if any element was left + while i < len(left_half): + arr[k] = left_half[i] + i += 1 + k += 1 + + while j < len(right_half): + arr[k] = right_half[j] + j += 1 + k += 1 +# Driver code to test above +arr = [24, 32, 10, 67, 55, 38, 19] +print(arr) +merge_sort(arr) + +print("Sorted array is:") +print(arr) +r) // 2 + left_half = arr[:mid] + right_half = arr[mid:] + + # Recursive call for sorting the two halves + merge_sort(left_half) + merge_sort(right_half) + + i = j = k = 0 + + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + arr[k] = left_half[i] + i += 1 + else: + arr[k] = right_half[j] + j += 1 + k += 1 + + # Checking if any element was left + while i < len(left_half): + arr[k] = left_half[i] + i += 1 + k += 1 + + while j < len(right_half): + arr[k] = right_half[j] + j += 1 + k += 1 +# Driver code to test above +arr = [24, 32, 10, 67, 55, 38, 19] +print(arr) +merge_sort(arr) + +print("Sorted array is:") +print(arr) + +def is_fibuncacci(num): + if (num < 2): + return False + + # Base case + if num == 1: + return True + + for i in range(2, num + 1): + if is_fibonacci(i) and is_fibonacci(num - i): + return True + + # If we reach here, then n + + + # Base case + if num == 1: + return True + + for i in range(2, num + 1): + if is_fibonacci(i) and is_fibonacci(num - i): + return True + + # If we reach here, then n + + +def quicksort(arr) \ No newline at end of file