Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions locales/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
4 changes: 4 additions & 0 deletions locales/ja/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,9 @@
},
"AutocompletePlus_AutoFormatter_EnableAutoFormat": {
"name": "自動フォーマット機能の有効化"
},
"AutocompletePlus_AutoFormatter_TrimSurroundingSpaces": {
"name": "先頭・末尾の空白を削除",
"tooltip": "有効時、プロンプトの先頭と末尾にある空行やスペースをすべて削除します。"
}
}
4 changes: 4 additions & 0 deletions locales/zh-TW/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,9 @@
},
"AutocompletePlus_AutoFormatter_EnableAutoFormat": {
"name": "啟用自動格式化"
},
"AutocompletePlus_AutoFormatter_TrimSurroundingSpaces": {
"name": "去除首尾空白",
"tooltip": "啟用時,清除提示詞開頭和結尾的所有空行和空格。"
}
}
4 changes: 4 additions & 0 deletions locales/zh/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,9 @@
},
"AutocompletePlus_AutoFormatter_EnableAutoFormat": {
"name": "启用自动格式化"
},
"AutocompletePlus_AutoFormatter_TrimSurroundingSpaces": {
"name": "去除首尾空白",
"tooltip": "启用时,清除提示词开头和结尾的所有空行和空格。"
}
}
57 changes: 57 additions & 0 deletions tests/js/auto-formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions web/js/auto-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
11 changes: 11 additions & 0 deletions web/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions web/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading