feat/preview #3
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "fabelous-autocoder",
|
"name": "fabelous-autocoder",
|
||||||
"version": "0.2.74",
|
"version": "0.2.0",
|
||||||
"displayName": "Fabelous Autocoder",
|
"displayName": "Fabelous Autocoder",
|
||||||
"description": "A simple to use Ollama autocompletion Plugin",
|
"description": "A simple to use Ollama autocompletion Plugin",
|
||||||
"icon": "icon.png",
|
"icon": "icon.png",
|
||||||
|
@ -110,10 +110,9 @@
|
||||||
},
|
},
|
||||||
"commands": [
|
"commands": [
|
||||||
{
|
{
|
||||||
"command": "fabelous-autocoder.autocomplete",
|
"command": "fabelous-autocoder.autocomplete",
|
||||||
"title": "Fabelous autocompletion"
|
"title": "Fabelous Autocompletion"
|
||||||
}
|
}]
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"vscode:prepublish": "npm run compile",
|
"vscode:prepublish": "npm run compile",
|
||||||
|
|
118
src/extension.ts
118
src/extension.ts
|
@ -61,7 +61,6 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({
|
||||||
textDecoration: 'none; display: none;', // Hide the original text
|
textDecoration: 'none; display: none;', // Hide the original text
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
|
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
|
||||||
const document = textEditor.document;
|
const document = textEditor.document;
|
||||||
const position = textEditor.selection.active;
|
const position = textEditor.selection.active;
|
||||||
|
@ -117,65 +116,74 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
let previewInserted = false;
|
let previewInserted = false;
|
||||||
let originalContent: string;
|
let originalContent: string;
|
||||||
let previewStartLine: number;
|
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 previewContent = completionText + '\n'.repeat(1);
|
||||||
const storeAndInsertPreview = async () => {
|
const edit = new vscode.WorkspaceEdit();
|
||||||
previewStartLine = startLine;
|
edit.replace(document.uri, fullRange, previewContent); // Overwrite the content
|
||||||
const endLine = document.lineCount - 1;
|
await vscode.workspace.applyEdit(edit);
|
||||||
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);
|
// Split the preview content into lines
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const previewLines = previewContent.split('\n');
|
||||||
edit.replace(document.uri, fullRange, previewContent); // Overwrite the context
|
|
||||||
await vscode.workspace.applyEdit(edit);
|
|
||||||
|
|
||||||
// Move the cursor to the end of the inserted content
|
// Calculate the exact position of the last character of the preview content
|
||||||
const previewLines = previewContent.split('\n');
|
const lastPreviewLineIndex = previewStartLine + previewLines.length - 1;
|
||||||
const lastPreviewLineIndex = previewStartLine + previewLines.length - 1;
|
const lastPreviewLine = previewLines[previewLines.length - 2]; // Second to last line (last line is newline)
|
||||||
const lastPreviewLine = previewLines[previewLines.length - 1];
|
const newPosition = new vscode.Position(lastPreviewLineIndex - 1, lastPreviewLine.length); // Place cursor at end of last line
|
||||||
const newPosition = new vscode.Position(lastPreviewLineIndex, lastPreviewLine.length);
|
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
|
||||||
|
|
||||||
// Set decorations on the newly inserted lines
|
// Set the new cursor position after inserting preview
|
||||||
const previewRanges: vscode.DecorationOptions[] = [];
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
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) => {
|
// Explicitly scroll to reveal the cursor position at the end of the preview
|
||||||
if (event.document.uri.toString() === document.uri.toString() && previewInserted) {
|
textEditor.revealRange(new vscode.Range(newPosition, newPosition), vscode.TextEditorRevealType.InCenter);
|
||||||
console.log("URI matches and preview inserted");
|
|
||||||
if (event.contentChanges.length > 0) {
|
// Set decorations on the newly inserted lines
|
||||||
const change = event.contentChanges[0];
|
const previewRanges: vscode.DecorationOptions[] = [];
|
||||||
console.log("Change:", change);
|
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 () => {
|
const restoreOriginalContent = async () => {
|
||||||
if (!previewInserted) return;
|
if (!previewInserted) return;
|
||||||
|
|
||||||
|
@ -197,8 +205,6 @@ const storeAndInsertPreview = async () => {
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Modify the acceptPreview function to match the working version
|
// 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) => {
|
const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => {
|
||||||
console.log("Accepting preview");
|
console.log("Accepting preview");
|
||||||
|
@ -225,7 +231,6 @@ const storeAndInsertPreview = async () => {
|
||||||
console.log("Preview accepted");
|
console.log("Preview accepted");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Call this function to initiate the preview
|
// Call this function to initiate the preview
|
||||||
await storeAndInsertPreview();
|
await storeAndInsertPreview();
|
||||||
|
|
||||||
|
@ -240,6 +245,7 @@ const storeAndInsertPreview = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
|
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
|
||||||
const item = new vscode.CompletionItem("Fabelous autocompletion");
|
const item = new vscode.CompletionItem("Fabelous autocompletion");
|
||||||
item.insertText = new vscode.SnippetString('${1:}');
|
item.insertText = new vscode.SnippetString('${1:}');
|
||||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue