fix: Refactor getCurrentPartialTag and getTagFromCursorPosition to utilize getCurrentTagRange for improved tag extraction
This commit is contained in:
+18
-20
@@ -155,33 +155,24 @@ function searchCompletionCandidates(textareaElement) {
|
||||
|
||||
/**
|
||||
* Extracts the current tag being typed before the cursor.
|
||||
* Assumes tags are separated by commas.
|
||||
* @param {HTMLTextAreaElement} inputElement
|
||||
* @returns {string} The current partial tag.
|
||||
*/
|
||||
function getCurrentPartialTag(inputElement) {
|
||||
const text = inputElement.value;
|
||||
const cursorPos = inputElement.selectionStart;
|
||||
|
||||
// Find the last newline or comma before the cursor
|
||||
const lastNewLine = text.lastIndexOf('\n', cursorPos - 1);
|
||||
const lastComma = text.lastIndexOf(',', cursorPos - 1);
|
||||
|
||||
// Get the position of the last separator (newline or comma) before cursor
|
||||
const lastSeparator = Math.max(lastNewLine, lastComma);
|
||||
const start = lastSeparator === -1 ? 0 : lastSeparator + 1;
|
||||
|
||||
// Check if the cursor is inside a prompt strength/weight modifier (e.g., :1.2, :.5, :1.)
|
||||
const segmentBeforeCursor = text.substring(start, cursorPos);
|
||||
const lastColon = segmentBeforeCursor.lastIndexOf(':');
|
||||
if (lastColon !== -1) {
|
||||
const partAfterColon = segmentBeforeCursor.substring(lastColon + 1);
|
||||
if (partAfterColon.length > 0 && /^[0-9\.]+$/.test(partAfterColon) && /[0-9]/.test(partAfterColon)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Get the tag range at the cursor position
|
||||
const tagRange = getCurrentTagRange(text, cursorPos);
|
||||
|
||||
// If no tag is found or the cursor is at the start of the tag
|
||||
if (!tagRange) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const partial = text.substring(start, cursorPos).trimStart();
|
||||
|
||||
// Extract the part of the tag up to the cursor position
|
||||
const partial = text.substring(tagRange.start, cursorPos).trimStart();
|
||||
|
||||
return normalizeTagToSearch(partial);
|
||||
}
|
||||
|
||||
@@ -865,3 +856,10 @@ export class AutocompleteEventHandler {
|
||||
handleClick(event) {
|
||||
}
|
||||
}
|
||||
|
||||
// Public test hooks - export non-exported functions for testing only
|
||||
export const __test_hooks__ = {
|
||||
getCurrentPartialTag,
|
||||
searchCompletionCandidates,
|
||||
// Add other internal functions you want to test
|
||||
};
|
||||
+16
-35
@@ -1,12 +1,13 @@
|
||||
import { settingValues } from './settings.js';
|
||||
import { TagCategory, TagData, autoCompleteData } from './data.js';
|
||||
import { settingValues } from './settings.js';
|
||||
import {
|
||||
extractTagsFromTextArea,
|
||||
findAllTagPositions,
|
||||
getViewportMargin,
|
||||
isValidTag,
|
||||
normalizeTagToInsert,
|
||||
normalizeTagToSearch,
|
||||
isValidTag,
|
||||
findAllTagPositions,
|
||||
extractTagsFromTextArea,
|
||||
getViewportMargin
|
||||
getCurrentTagRange
|
||||
} from './utils.js';
|
||||
|
||||
// --- RelatedTags Logic ---
|
||||
@@ -38,42 +39,22 @@ function calculateJaccardSimilarity(tagA, tagB) {
|
||||
|
||||
/**
|
||||
* Extracts the tag at the current cursor position.
|
||||
* Handles tags separated by commas or newlines.
|
||||
* Utilizes getCurrentTagRange to properly handle tags with weights and parentheses.
|
||||
* @param {HTMLTextAreaElement} inputElement The textarea element
|
||||
* @returns {string|null} The tag at cursor or null
|
||||
*/
|
||||
export function getTagFromCursorPosition(inputElement) {
|
||||
const text = inputElement.value;
|
||||
const cursorPos = inputElement.selectionStart;
|
||||
|
||||
// Find the start position of the current tag
|
||||
// Look for the last comma or newline before the cursor
|
||||
const lastComma = text.lastIndexOf(',', cursorPos - 1);
|
||||
const lastNewline = text.lastIndexOf('\n', cursorPos - 1);
|
||||
let startPos = Math.max(lastComma, lastNewline);
|
||||
startPos = startPos === -1 ? 0 : startPos + 1; // If no separator found, start from the beginning
|
||||
|
||||
// Find the end position of the current tag
|
||||
// Look for the next comma or newline after the start position (or cursor position if more appropriate)
|
||||
// We search from startPos to correctly handle cases where the cursor is at the beginning of a tag
|
||||
let searchEndFrom = Math.max(cursorPos, startPos);
|
||||
let endPosComma = text.indexOf(',', searchEndFrom);
|
||||
let endPosNewline = text.indexOf('\n', searchEndFrom);
|
||||
|
||||
// If a separator is not found, treat it as the end of the text
|
||||
if (endPosComma === -1) endPosComma = text.length;
|
||||
if (endPosNewline === -1) endPosNewline = text.length;
|
||||
|
||||
// Choose the closer separator as the end position
|
||||
let endPos = Math.min(endPosComma, endPosNewline);
|
||||
|
||||
// Extract and trim the tag
|
||||
const tag = text.substring(startPos, endPos).trim();
|
||||
|
||||
// If no tag found, return null
|
||||
if (!tag) return null;
|
||||
|
||||
return normalizeTagToSearch(tag);
|
||||
|
||||
// Use getCurrentTagRange to get the tag at the cursor position
|
||||
const tagRange = getCurrentTagRange(text, cursorPos);
|
||||
|
||||
// If no tag was found at the cursor position
|
||||
if (!tagRange) return null;
|
||||
|
||||
// Return the normalized tag for searching
|
||||
return normalizeTagToSearch(tagRange.tag);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user