Skip to content

Commit d9d333b

Browse files
authored
Merge pull request #11 from asux/feat/configurable-focus-tag
Configurable RSpec focus tag
2 parents ede65e5 + 8dbdc69 commit d9d333b

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "rspec-focus",
33
"displayName": "RSpec Focus",
44
"description": "Add and clear focus mode in RSpec files",
5-
"version": "0.3.1",
5+
"version": "0.4.0",
66
"license": "MIT",
77
"publisher": "asux",
88
"icon": "rspec.png",
@@ -33,8 +33,18 @@
3333
"activationEvents": [
3434
"onLanguage:ruby"
3535
],
36-
"main": "./out/src/rspec-focus",
36+
"main": "./out/src/extension",
3737
"contributes": {
38+
"configuration": {
39+
"title": "RSpec Focus",
40+
"properties": {
41+
"rspec-focus.focusTag": {
42+
"type": "string",
43+
"default": "focus",
44+
"description": "RSpec tag used for focus (e.g. focus, wip). Inserted as :tag in examples."
45+
}
46+
}
47+
},
3848
"commands": [
3949
{
4050
"command": "rspec-focus.add",
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ const KEYWORDS = [
4242
'it_should_behave_like'
4343
];
4444

45-
const FOCUS_TAG = ', :focus';
45+
const DEFAULT_FOCUS_TAG = 'focus';
46+
47+
function getFocusTag(): string {
48+
const raw = vscode.workspace.getConfiguration('rspec-focus').get<string>('focusTag', DEFAULT_FOCUS_TAG);
49+
const sanitized = (raw ?? '').replace(/^:+/, '').replace(/[^a-zA-Z0-9_].*$/, '') || DEFAULT_FOCUS_TAG;
50+
return `, :${sanitized}`;
51+
}
4652

4753
function getKeywordsRegexp(): RegExp {
4854
return new RegExp(`(?:${KEYWORDS.join('|')})\\s['"].+['"]\\sdo$`, 'm');
@@ -58,6 +64,7 @@ async function add() {
5864
return;
5965
}
6066

67+
const focusTag = getFocusTag();
6168
await editor.edit((editBuilder) => {
6269
const activePosition = editor.selection.active;
6370
for (let i = activePosition.line; i >= 0; i--) {
@@ -66,13 +73,13 @@ async function add() {
6673
const matches = text.match(getKeywordsRegexp()) || text.match(getRSpecBlockRegexp());
6774

6875
if (matches) {
69-
if (text.includes(FOCUS_TAG)) {
76+
if (text.includes(focusTag)) {
7077
continue;
7178
} else {
7279
const doIndex = text.lastIndexOf('do');
7380
if (doIndex !== -1) {
7481
const position = new Position(i, doIndex - 1);
75-
editBuilder.insert(position, FOCUS_TAG);
82+
editBuilder.insert(position, focusTag);
7683
}
7784
break;
7885
}
@@ -87,15 +94,16 @@ async function clear() {
8794
return;
8895
}
8996

97+
const focusTag = getFocusTag();
9098
await editor.edit((editBuilder) => {
9199
for (let i = 0; i < editor.document.lineCount; i++) {
92100
const line = editor.document.lineAt(i);
93101
const text = line.text;
94-
const focusIndex = text.indexOf(FOCUS_TAG);
102+
const focusIndex = text.indexOf(focusTag);
95103

96104
if (focusIndex !== -1) {
97105
const start = new Position(i, focusIndex);
98-
const end = new Position(i, focusIndex + FOCUS_TAG.length);
106+
const end = new Position(i, focusIndex + focusTag.length);
99107
editBuilder.delete(new Range(start, end));
100108
}
101109
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,24 @@ suite('RSpec Focus Functional Tests', () => {
7373
assert.strictEqual(editor.document.lineAt(0).text, "describe 'User' do");
7474
assert.strictEqual(editor.document.lineAt(1).text, " it 'is valid' do");
7575
});
76+
77+
test('Uses configurable focus tag (e.g. wip)', async () => {
78+
const config = vscode.workspace.getConfiguration('rspec-focus');
79+
const original = config.get<string>('focusTag');
80+
try {
81+
await config.update('focusTag', 'wip', vscode.ConfigurationTarget.Global);
82+
83+
const content = "it 'work in progress' do\nend";
84+
const editor = await setupEditor(content);
85+
editor.selection = new vscode.Selection(new vscode.Position(0, 0), new vscode.Position(0, 0));
86+
87+
await vscode.commands.executeCommand('rspec-focus.add');
88+
assert.strictEqual(editor.document.lineAt(0).text, "it 'work in progress', :wip do");
89+
90+
await vscode.commands.executeCommand('rspec-focus.clear');
91+
assert.strictEqual(editor.document.lineAt(0).text, "it 'work in progress' do");
92+
} finally {
93+
await config.update('focusTag', original, vscode.ConfigurationTarget.Global);
94+
}
95+
});
7696
});

0 commit comments

Comments
 (0)