Skip to content

Commit

Permalink
added lorem ipsum manager
Browse files Browse the repository at this point in the history
  • Loading branch information
theteladras committed Apr 11, 2022
1 parent bbf6d0e commit 4ab959e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ the selected snake case text will be turned to kebab case _(eg. call\_sync -> ca
---
#### >Length of Selection
pops an alert, in the bottom right corner, with the information of the length of the selected text

---
#### >Lorem Ipsum on Selection
replaces the selection with lorem ipsum, if nothing is selected will replace the whole line where the cursor is placed
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Selection Manager",
"description": "Manage selected text of a file.",
"icon": "images/manage-selection.png",
"version": "0.0.18",
"version": "0.0.19",
"license": "MIT",
"engines": {
"vscode": "^1.66.0"
Expand All @@ -28,7 +28,8 @@
"onCommand:selection-manager.camel-to-kebab",
"onCommand:selection-manager.kebab-to-snake",
"onCommand:selection-manager.snake-to-kebab",
"onCommand:selection-manager.length"
"onCommand:selection-manager.length",
"onCommand:selection-manager.loremIpsum"
],
"main": "./dist/extension.js",
"contributes": {
Expand Down Expand Up @@ -100,6 +101,10 @@
{
"command": "selection-manager.length",
"title": "Length of Selection"
},
{
"command": "selection-manager.loremIpsum",
"title": "Lorem Ipsum on Selection"
}
]
},
Expand Down Expand Up @@ -135,5 +140,8 @@
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2"
},
"publisher": "ManageTextSelection"
"publisher": "ManageTextSelection",
"dependencies": {
"lorem-ipsum": "^2.0.4"
}
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
kebabToCamelManager,
camelToKebabManager,
kebabToSnakeManager,
loremIpsumManager,
} from './managers';
import { snakeToKebabManager } from './managers/snakeToKebab';

Expand All @@ -42,6 +43,7 @@ export function activate(context: vscode.ExtensionContext) {
['selection-manager.kebab-to-snake', kebabToSnakeManager],
['selection-manager.snake-to-kebab', snakeToKebabManager],
['selection-manager.length', lengthManager],
['selection-manager.loremIpsum', loremIpsumManager],
];

dispose.forEach(([key, manager]) => context.subscriptions.push(vscode.commands.registerCommand(key, manager)));
Expand Down
1 change: 1 addition & 0 deletions src/managers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './kebabToCamel';
export * from './camelToKebab';
export * from './kebabToSnake';
export * from './snakeToCamel';
export * from './loremIpsum';
28 changes: 28 additions & 0 deletions src/managers/loremIpsum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as vscode from 'vscode';
import { generateLoremIpsum } from '../utils';

export async function loremIpsumManager(): Promise<void> {
const editor = vscode.window.activeTextEditor;

if (!editor) return;

const text = generateLoremIpsum();

const selectedText = editor.document.getText(editor.selection);

if (!selectedText) {
const cursorOnLine = editor.selection.start.line;

const line = editor.document.lineAt(cursorOnLine);

editor.edit(edit => edit.replace(line.range, text));

return;
}

const docText = editor.document.getText().replace(selectedText, text);

editor.edit(edit => {
edit.replace(new vscode.Range(0, 0, editor.document.lineCount, 0), docText);
});
}
19 changes: 15 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { LoremIpsum } from 'lorem-ipsum';

const lorem = new LoremIpsum({
wordsPerSentence: {
max: 16,
min: 3
}
});

export const range = (start: number, end: number): number[] => {
return Array(end - start + 1).fill(null).map((_, idx) => start + idx);
};
Expand All @@ -11,10 +20,12 @@ export const snakeToCamelCase = (str: string): string => str.toLowerCase().repla

export const camelToSnakeCase = (str: string): string => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);

export const kebabToCamel = (str: string) => str.replace(/-./g, x=>x[1].toUpperCase());
export const kebabToCamel = (str: string): string => str.replace(/-./g, x=>x[1].toUpperCase());

export const camelToKebab = (str: string): string => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? "-" : "") + $.toLowerCase());

export const camelToKebab = (str: string) => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? "-" : "") + $.toLowerCase());
export const kebabToSnake = (str: string): string => str.replace(/-/g, "_");

export const kebabToSnake = (str: string) => str.replace(/-/g, "_");
export const snakeToKebab = (str: string): string => str.replace(/_/g, "-");

export const snakeToKebab = (str: string) => str.replace(/_/g, "-");
export const generateLoremIpsum = (): string => lorem.generator.generateRandomSentence();
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ colorette@^2.0.14:
resolved "https://registry.npmjs.org/colorette/-/colorette-2.0.16.tgz"
integrity sha512-hUewv7oMjCp+wkBv5Rm0v87eJhq4woh5rSR+42YSQJKecCqgIqNkZ6lAlQms/BwHPJA5NKMRlpxPRv0n8HQW6g==

commander@^2.20.0:
commander@^2.17.1, commander@^2.20.0:
version "2.20.3"
resolved "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz"
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
Expand Down Expand Up @@ -1267,6 +1267,13 @@ [email protected]:
chalk "^4.1.0"
is-unicode-supported "^0.1.0"

lorem-ipsum@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/lorem-ipsum/-/lorem-ipsum-2.0.4.tgz#5feedb37a14e9cb96c3e994d4a2edd4cb0c299db"
integrity sha512-TD+ERYfxjYiUfOyaKU6OH4euumNVeKoo3BxIhokb7bGmoCULsME48onF9NVxYK3CU1z9L5ALnkDkW8lIkHvMNQ==
dependencies:
commander "^2.17.1"

lru-cache@^7.4.0:
version "7.8.0"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-7.8.0.tgz"
Expand Down

0 comments on commit 4ab959e

Please sign in to comment.