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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ Therefore text transformation handlers can be registered at the core of the exte

---

Currently there are three transformations included:
Currently there are four transformations included:
- to uppercase
- to lowercase
- to capitalcase (capitalizing the first letter of each word<sup>*</sup>)
- reverse words

<sup>*</sup> a word is always starting after a common whitespace character + at the start of the selection

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
{
"command": "capitalcase",
"title": "To capitalcase"
},
{
"command": "reversewords",
"title": "Reverse words"
}
]
},
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {ExtensionContext, window, commands, Selection, Range, TextEditorEdit} from "vscode";
import Transformation from "./transformation";
import {UppercaseTransformer, LowercaseTransformer, CapitalcaseTransformer} from "./simple-transformations";
import {UppercaseTransformer, LowercaseTransformer, CapitalcaseTransformer, ReverseWordsTransformer} from "./simple-transformations";

export const transformers = new Array<Transformation>();
transformers.push(new UppercaseTransformer());
transformers.push(new LowercaseTransformer());
transformers.push(new CapitalcaseTransformer());
transformers.push(new ReverseWordsTransformer());

//This is needed to be able to inject a spy transformer during testing
let context: ExtensionContext;
Expand Down
17 changes: 17 additions & 0 deletions src/simple-transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,21 @@ export class CapitalcaseTransformer extends Transformation {

cb(newString);
}
}

export class ReverseWordsTransformer extends Transformation {
getCommandName(): string {
return "reversewords";
}

transform(input: string, cb: (output: string) => void): void {
let reverse = (str: string) => str.split(" ").reverse().join(" ");

let outString = input.split("\n").map((line) => {
// Trim the string in order to maintain indentation
return line.replace(line.trim(), reverse(line.trim()));
}).join("\n");

cb(outString);
}
}
23 changes: 22 additions & 1 deletion test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ suite("integration test", () => {



import {CapitalcaseTransformer} from "../src/simple-transformations";
import {CapitalcaseTransformer, ReverseWordsTransformer} from "../src/simple-transformations";

suite("unit tests", () => {
suite("capitalcase", () => {
Expand All @@ -68,6 +68,27 @@ suite("unit tests", () => {
});
});
});

suite("reversewords", () => {
let instance = new ReverseWordsTransformer();
const SAMPLE_CODE_USE = "foo === true";
const SAMPLE_MULTILINE_SENTENCE = "\t\tThis is some text\n And some in new line!";

test("correct handling of operators", (done: MochaDone) => {
instance.transform(SAMPLE_CODE_USE, (output: string) => {
assert.equal(output, "true === foo");
done();
});
});

// We want to reverse only words in line, and preserve indentation across multiple lines
test("correct handling of multiline input", (done: MochaDone) => {
instance.transform(SAMPLE_MULTILINE_SENTENCE, (output: string) => {
assert.equal(output, "\t\ttext some is This\n line! new in some And");
done();
});
});
});
});

function singleFire(fn: Function) {
Expand Down