stable version 0.2.0, now providing preview pressing double tab to accept preview

This commit is contained in:
Falko Victor Habel 2024-10-04 18:26:01 +03:00
parent b6241855fc
commit d8e97c2b4e
3 changed files with 252 additions and 72 deletions

View File

@ -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",

View File

@ -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);
// 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 content
await vscode.workspace.applyEdit(edit);
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);
// Split the preview content into lines
const previewLines = previewContent.split('\n');
// 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);
// 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 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;
};
// Set the new cursor position after inserting preview
textEditor.selection = new vscode.Selection(newPosition, newPosition);
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);
// 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);
}
}
});
// 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;
@ -197,8 +205,6 @@ const storeAndInsertPreview = async () => {
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");
@ -225,7 +231,6 @@ const storeAndInsertPreview = async () => {
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:}');

175
src/test.py Normal file
View File

@ -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]
right = [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)