diff --git a/locales/en/settings.json b/locales/en/settings.json index f7ea594..25a33cc 100644 --- a/locales/en/settings.json +++ b/locales/en/settings.json @@ -85,5 +85,9 @@ }, "AutocompletePlus_AutoFormatter_EnableAutoFormat": { "name": "Enable Auto Format" + }, + "AutocompletePlus_AutoFormatter_TrimSurroundingSpaces": { + "name": "Trim Surrounding Spaces", + "tooltip": "When enabled, trim any blank lines from the beginning and end of the prompt." } } \ No newline at end of file diff --git a/locales/ja/settings.json b/locales/ja/settings.json index 2739ede..d380410 100644 --- a/locales/ja/settings.json +++ b/locales/ja/settings.json @@ -85,5 +85,9 @@ }, "AutocompletePlus_AutoFormatter_EnableAutoFormat": { "name": "自動フォーマット機能の有効化" + }, + "AutocompletePlus_AutoFormatter_TrimSurroundingSpaces": { + "name": "先頭・末尾の空白を削除", + "tooltip": "有効時、プロンプトの先頭と末尾にある空行やスペースをすべて削除します。" } } \ No newline at end of file diff --git a/locales/zh-TW/settings.json b/locales/zh-TW/settings.json index b0d930c..9aa0d66 100644 --- a/locales/zh-TW/settings.json +++ b/locales/zh-TW/settings.json @@ -85,5 +85,9 @@ }, "AutocompletePlus_AutoFormatter_EnableAutoFormat": { "name": "啟用自動格式化" + }, + "AutocompletePlus_AutoFormatter_TrimSurroundingSpaces": { + "name": "去除首尾空白", + "tooltip": "啟用時,清除提示詞開頭和結尾的所有空行和空格。" } } \ No newline at end of file diff --git a/locales/zh/settings.json b/locales/zh/settings.json index 7cb56b7..36317b2 100644 --- a/locales/zh/settings.json +++ b/locales/zh/settings.json @@ -85,5 +85,9 @@ }, "AutocompletePlus_AutoFormatter_EnableAutoFormat": { "name": "启用自动格式化" + }, + "AutocompletePlus_AutoFormatter_TrimSurroundingSpaces": { + "name": "去除首尾空白", + "tooltip": "启用时,清除提示词开头和结尾的所有空行和空格。" } } \ No newline at end of file diff --git a/tests/js/auto-formatter.test.js b/tests/js/auto-formatter.test.js index 02cda10..5e9b71c 100644 --- a/tests/js/auto-formatter.test.js +++ b/tests/js/auto-formatter.test.js @@ -47,15 +47,18 @@ describe('AutoFormatter Functions', () => { describe('formatPromptText', () => { // Store original setting value to restore after tests const originalUseTrailingComma = settingValues.useTrailingComma; + const originalTrimSurroundingSpaces = settingValues.trimSurroundingSpaces; afterEach(() => { // Restore original setting after each test settingValues.useTrailingComma = originalUseTrailingComma; + settingValues.trimSurroundingSpaces = originalTrimSurroundingSpaces; }); describe('with useTrailingComma enabled', () => { beforeEach(() => { settingValues.useTrailingComma = true; + settingValues.trimSurroundingSpaces = false; }); test('should format text by adding comma and space after tags', () => { @@ -101,6 +104,7 @@ describe('AutoFormatter Functions', () => { describe('with useTrailingComma disabled', () => { beforeEach(() => { settingValues.useTrailingComma = false; + settingValues.trimSurroundingSpaces = false; }); test('should format text by adding comma and space after tags without trailing comma', () => { @@ -143,6 +147,59 @@ describe('AutoFormatter Functions', () => { }); }); + describe('with trimSurroundingSpaces enabled', () => { + beforeEach(() => { + settingValues.trimSurroundingSpaces = true; + settingValues.useTrailingComma = false; + }); + + test('should remove trailing newlines and spaces', () => { + const input = 'tag1, tag2\n\n '; + const expected = 'tag1, tag2'; + expect(formatPromptText(input)).toBe(expected); + }); + + test('should remove leading newlines and spaces', () => { + const input = ' \n\ntag1, tag2'; + const expected = 'tag1, tag2'; + expect(formatPromptText(input)).toBe(expected); + }); + + test('should remove both leading and trailing whitespaces but keep middle empty lines', () => { + const input = ' \n\ntag1\n\ntag2\n\n '; + const expected = 'tag1\n\ntag2'; + expect(formatPromptText(input)).toBe(expected); + }); + + test('should return empty string if input is only whitespace', () => { + const input = ' \n '; + expect(formatPromptText(input)).toBe(''); + }); + }); + + describe('with trimSurroundingSpaces disabled', () => { + beforeEach(() => { + settingValues.trimSurroundingSpaces = false; + settingValues.useTrailingComma = false; + }); + + test('should preserve trailing newlines', () => { + const input = 'tag1, tag2\n\n'; + const expected = 'tag1, tag2\n\n'; + expect(formatPromptText(input)).toBe(expected); + }); + + test('should preserve leading spaces (as first line content)', () => { + const input = ' \ntag1'; + const expected = '\ntag1'; + expect(formatPromptText(input)).toBe(expected); + }); + + test('should handle input with only spaces', () => { + expect(formatPromptText(' ')).toBe(' '); + }); + }); + test('should handle empty input', () => { expect(formatPromptText('')).toBe(''); expect(formatPromptText(null)).toBe(null); diff --git a/web/js/auto-formatter.js b/web/js/auto-formatter.js index 44dca74..f016320 100644 --- a/web/js/auto-formatter.js +++ b/web/js/auto-formatter.js @@ -70,6 +70,11 @@ function shouldAutoFormat(text, nodeInfo) { export function formatPromptText(text) { if (!text || text.trim().length === 0) return text; + // Trim surrounding spaces of the entire prompt if the setting is enabled + if (settingValues.trimSurroundingSpaces) { + text = text.trim(); + } + // Split text into individual lines for processing const lines = text.split('\n'); const formattedLines = []; diff --git a/web/js/main.js b/web/js/main.js index 3d49c86..4673fa5 100644 --- a/web/js/main.js +++ b/web/js/main.js @@ -490,6 +490,17 @@ app.registerExtension({ }, // --- Auto format settings --- + { + id: id + '.AutoFormatter.TrimSurroundingSpaces', + name: 'Trim Surrounding Spaces', + tooltip: 'When enabled, trim any blank lines from the beginning and end of the prompt.', + type: 'boolean', + defaultValue: false, + category: [name, 'AutoFormatter', 'Trim Surrounding Spaces'], + onChange: (newVal, oldVal) => { + settingValues.trimSurroundingSpaces = newVal; + }, + }, { id: id + '.AutoFormatter.UseTrailingComma', name: 'Use Trailing Comma', diff --git a/web/js/settings.js b/web/js/settings.js index d02830f..726fc79 100644 --- a/web/js/settings.js +++ b/web/js/settings.js @@ -25,6 +25,7 @@ export const settingValues = { enableAutoFormat: true, autoFormatTrigger: 'auto', // Options: 'auto' (format on blur + shortcut), 'manual' (shortcut only) useTrailingComma: false, // Whether to add comma at the end of each line + trimSurroundingSpaces: false, // Trim spaces around each tag // Internal logic settings