Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to select and set cursor on vars #883

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/features/lightspeed/inlineSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import * as vscode from "vscode";
import { v4 as uuidv4 } from "uuid";
import _ from "lodash";

import { adjustInlineSuggestionIndent } from "../utils/lightspeed";
import {
adjustInlineSuggestionIndent,
convertToSnippetString,
} from "../utils/lightspeed";
import { getCurrentUTCDateTime } from "../utils/dateTime";
import { lightSpeedManager } from "../../extension";
import {
Expand Down Expand Up @@ -179,7 +182,14 @@ export async function getInlineSuggestionItems(
insertText = adjustInlineSuggestionIndent(prediction, currentPosition);
insertTexts.push(insertText);

const inlineSuggestionItem = new vscode.InlineCompletionItem(insertText);
// convert into snippet string to move cursor on jinja variables directly
const insertTextSnippetString = new vscode.SnippetString(
convertToSnippetString(insertText)
);

const inlineSuggestionItem = new vscode.InlineCompletionItem(
insertTextSnippetString
);
inlineSuggestionUserActionItems.push(inlineSuggestionItem);
});
// currentSuggestion is used in user action handlers
Expand Down
4 changes: 2 additions & 2 deletions src/features/utils/lightspeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export function adjustInlineSuggestionIndent(

/* A utility function to convert plain text to snippet string */
export function convertToSnippetString(suggestion: string): string {
// this regex matches all content inside {{ }}
// this regex matches the content inside {{ }} with decided vars, i.e., {{ _var_ }}
// TODO: once a prefix is decided for using it in from of variable names, the regex
// can be changed to match it
const regex = /(?<=\{\{ ).+?(?= \}\})/gm;
const regex = /(?:^|)[^@#\s_]*(_([^_]+)_)/gm;

let counter = 0;
const convertedSuggestion = suggestion.replace(regex, (item) => {
Expand Down