Skip to content

Commit 2b518c1

Browse files
Muhammed Emin TİFTİKÇİrebornix
authored andcommitted
Transform to Title Case
microsoft#70990
1 parent b71932d commit 2b518c1

2 files changed

Lines changed: 99 additions & 10 deletions

File tree

src/vs/editor/contrib/linesOperations/linesOperations.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ export abstract class AbstractCaseAction extends EditorAction {
886886
return;
887887
}
888888

889+
let wordSeparators = editor.getConfiguration().wordSeparators;
890+
889891
let commands: ICommand[] = [];
890892

891893
for (let i = 0, len = selections.length; i < len; i++) {
@@ -900,12 +902,12 @@ export abstract class AbstractCaseAction extends EditorAction {
900902

901903
let wordRange = new Range(cursor.lineNumber, word.startColumn, cursor.lineNumber, word.endColumn);
902904
let text = model.getValueInRange(wordRange);
903-
commands.push(new ReplaceCommandThatPreservesSelection(wordRange, this._modifyText(text),
905+
commands.push(new ReplaceCommandThatPreservesSelection(wordRange, this._modifyText(text, wordSeparators),
904906
new Selection(cursor.lineNumber, cursor.column, cursor.lineNumber, cursor.column)));
905907

906908
} else {
907909
let text = model.getValueInRange(selection);
908-
commands.push(new ReplaceCommandThatPreservesSelection(selection, this._modifyText(text), selection));
910+
commands.push(new ReplaceCommandThatPreservesSelection(selection, this._modifyText(text, wordSeparators), selection));
909911
}
910912
}
911913

@@ -914,7 +916,7 @@ export abstract class AbstractCaseAction extends EditorAction {
914916
editor.pushUndoStop();
915917
}
916918

917-
protected abstract _modifyText(text: string): string;
919+
protected abstract _modifyText(text: string, wordSeparators: string): string;
918920
}
919921

920922
export class UpperCaseAction extends AbstractCaseAction {
@@ -927,7 +929,7 @@ export class UpperCaseAction extends AbstractCaseAction {
927929
});
928930
}
929931

930-
protected _modifyText(text: string): string {
932+
protected _modifyText(text: string, wordSeparators: string): string {
931933
return text.toLocaleUpperCase();
932934
}
933935
}
@@ -942,11 +944,48 @@ export class LowerCaseAction extends AbstractCaseAction {
942944
});
943945
}
944946

945-
protected _modifyText(text: string): string {
947+
protected _modifyText(text: string, wordSeparators: string): string {
946948
return text.toLocaleLowerCase();
947949
}
948950
}
949951

952+
export class TitleCaseAction extends AbstractCaseAction {
953+
constructor() {
954+
super({
955+
id: 'editor.action.transformToTitlecase',
956+
label: nls.localize('editor.transformToTitlecase', "Transform to Title Case"),
957+
alias: 'Transform to Title Case',
958+
precondition: EditorContextKeys.writable
959+
});
960+
}
961+
962+
protected _modifyText(text: string, wordSeparators: string): string {
963+
const separators = '\r\n\t ' + wordSeparators;
964+
const excludedChars = separators.split('');
965+
966+
let title = '';
967+
let startUpperCase = true;
968+
969+
for (let i = 0; i < text.length; i++) {
970+
let currentChar = text[i];
971+
972+
if (excludedChars.indexOf(currentChar) >= 0) {
973+
startUpperCase = true;
974+
975+
title += currentChar;
976+
} else if (startUpperCase) {
977+
startUpperCase = false;
978+
979+
title += currentChar.toLocaleUpperCase();
980+
} else {
981+
title += currentChar.toLocaleLowerCase();
982+
}
983+
}
984+
985+
return title;
986+
}
987+
}
988+
950989
registerEditorAction(CopyLinesUpAction);
951990
registerEditorAction(CopyLinesDownAction);
952991
registerEditorAction(MoveLinesUpAction);
@@ -965,3 +1004,4 @@ registerEditorAction(JoinLinesAction);
9651004
registerEditorAction(TransposeAction);
9661005
registerEditorAction(UpperCaseAction);
9671006
registerEditorAction(LowerCaseAction);
1007+
registerEditorAction(TitleCaseAction);

src/vs/editor/contrib/linesOperations/test/linesOperations.test.ts

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Position } from 'vs/editor/common/core/position';
99
import { Selection } from 'vs/editor/common/core/selection';
1010
import { Handler } from 'vs/editor/common/editorCommon';
1111
import { ITextModel } from 'vs/editor/common/model';
12-
import { DeleteAllLeftAction, DeleteAllRightAction, IndentLinesAction, InsertLineAfterAction, InsertLineBeforeAction, JoinLinesAction, LowerCaseAction, SortLinesAscendingAction, SortLinesDescendingAction, TransposeAction, UpperCaseAction, DeleteLinesAction } from 'vs/editor/contrib/linesOperations/linesOperations';
12+
import { TitleCaseAction, DeleteAllLeftAction, DeleteAllRightAction, IndentLinesAction, InsertLineAfterAction, InsertLineBeforeAction, JoinLinesAction, LowerCaseAction, SortLinesAscendingAction, SortLinesDescendingAction, TransposeAction, UpperCaseAction, DeleteLinesAction } from 'vs/editor/contrib/linesOperations/linesOperations';
1313
import { withTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
1414
import { createTextModel } from 'vs/editor/test/common/editorTestUtils';
1515

@@ -529,6 +529,7 @@ suite('Editor Contrib - Line Operations', () => {
529529
let model = editor.getModel()!;
530530
let uppercaseAction = new UpperCaseAction();
531531
let lowercaseAction = new LowerCaseAction();
532+
let titlecaseAction = new TitleCaseAction();
532533

533534
editor.setSelection(new Selection(1, 1, 1, 12));
534535
uppercaseAction.run(null!, editor);
@@ -550,15 +551,63 @@ suite('Editor Contrib - Line Operations', () => {
550551
assert.equal(model.getLineContent(1), 'hello world', '007');
551552
assert.deepEqual(editor.getSelection()!.toString(), new Selection(1, 4, 1, 4).toString(), '008');
552553

554+
editor.setSelection(new Selection(1, 1, 1, 12));
555+
titlecaseAction.run(null!, editor);
556+
assert.equal(model.getLineContent(1), 'Hello World', '009');
557+
assert.deepEqual(editor.getSelection()!.toString(), new Selection(1, 1, 1, 12).toString(), '010');
558+
553559
editor.setSelection(new Selection(2, 1, 2, 6));
554560
uppercaseAction.run(null!, editor);
555-
assert.equal(model.getLineContent(2), 'ÖÇŞĞÜ', '009');
556-
assert.deepEqual(editor.getSelection()!.toString(), new Selection(2, 1, 2, 6).toString(), '010');
561+
assert.equal(model.getLineContent(2), 'ÖÇŞĞÜ', '011');
562+
assert.deepEqual(editor.getSelection()!.toString(), new Selection(2, 1, 2, 6).toString(), '012');
557563

558564
editor.setSelection(new Selection(2, 1, 2, 6));
559565
lowercaseAction.run(null!, editor);
560-
assert.equal(model.getLineContent(2), 'öçşğü', '011');
561-
assert.deepEqual(editor.getSelection()!.toString(), new Selection(2, 1, 2, 6).toString(), '012');
566+
assert.equal(model.getLineContent(2), 'öçşğü', '013');
567+
assert.deepEqual(editor.getSelection()!.toString(), new Selection(2, 1, 2, 6).toString(), '014');
568+
569+
editor.setSelection(new Selection(2, 1, 2, 6));
570+
titlecaseAction.run(null!, editor);
571+
assert.equal(model.getLineContent(2), 'Öçşğü', '015');
572+
assert.deepEqual(editor.getSelection()!.toString(), new Selection(2, 1, 2, 6).toString(), '016');
573+
}
574+
);
575+
576+
withTestCodeEditor(
577+
[
578+
'foO baR BaZ',
579+
'foO\'baR\'BaZ',
580+
'foO[baR]BaZ',
581+
'foO`baR~BaZ',
582+
'foO^baR%BaZ',
583+
'foO$baR!BaZ'
584+
], {}, (editor) => {
585+
let model = editor.getModel()!;
586+
let titlecaseAction = new TitleCaseAction();
587+
588+
editor.setSelection(new Selection(1, 1, 1, 12));
589+
titlecaseAction.run(null!, editor);
590+
assert.equal(model.getLineContent(1), 'Foo Bar Baz');
591+
592+
editor.setSelection(new Selection(2, 1, 2, 12));
593+
titlecaseAction.run(null!, editor);
594+
assert.equal(model.getLineContent(2), 'Foo\'Bar\'Baz');
595+
596+
editor.setSelection(new Selection(3, 1, 3, 12));
597+
titlecaseAction.run(null!, editor);
598+
assert.equal(model.getLineContent(3), 'Foo[Bar]Baz');
599+
600+
editor.setSelection(new Selection(4, 1, 4, 12));
601+
titlecaseAction.run(null!, editor);
602+
assert.equal(model.getLineContent(4), 'Foo`Bar~Baz');
603+
604+
editor.setSelection(new Selection(5, 1, 5, 12));
605+
titlecaseAction.run(null!, editor);
606+
assert.equal(model.getLineContent(5), 'Foo^Bar%Baz');
607+
608+
editor.setSelection(new Selection(6, 1, 6, 12));
609+
titlecaseAction.run(null!, editor);
610+
assert.equal(model.getLineContent(6), 'Foo$Bar!Baz');
562611
}
563612
);
564613

0 commit comments

Comments
 (0)