|
1 | | -// The module 'vscode' contains the VS Code extensibility API |
2 | | -// Import the module and reference it with the alias vscode in your code below |
| 1 | +'use strict'; |
3 | 2 | import * as vscode from 'vscode'; |
| 3 | +import { TextDocument, Position, Selection } from 'vscode'; |
| 4 | +// import { commands, window, TextDocument, TextEditorEdit, ExtensionContext, Position, SnippetString, Selection } from 'vscode'; |
4 | 5 |
|
5 | | -// this method is called when your extension is activated |
6 | | -// your extension is activated the very first time the command is executed |
7 | | -export function activate(context: vscode.ExtensionContext) { |
8 | 6 |
|
9 | | - // Use the console to output diagnostic information (console.log) and errors (console.error) |
10 | | - // This line of code will only be executed once when your extension is activated |
11 | | - console.log('Congratulations, your extension "pasteregex" is now active!'); |
| 7 | +// declaring constants |
| 8 | +const editor = vscode.window.activeTextEditor; |
| 9 | + |
| 10 | +function getSelectedText(): string { |
| 11 | + /* |
| 12 | + This function is meant to get the text that is currently selected |
| 13 | + */ |
| 14 | + let document = editor?.document; |
| 15 | + let selection = editor?.selection; |
| 16 | + let selectedText = document?.getText(selection); |
| 17 | + return selectedText as string |
| 18 | +} |
| 19 | + |
| 20 | +function insertNewString(modifiedString: string) { |
| 21 | + /* |
| 22 | + This function is meant to get the text that is currently selected |
| 23 | + */ |
| 24 | + if (editor) { |
| 25 | + let selection = editor.selection; |
| 26 | + editor.edit(editBuilder => { |
| 27 | + editBuilder.replace(selection, modifiedString); |
| 28 | + }); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// export the vscode commands that'll show up in the command pallette |
| 33 | +export function activate(context: vscode.ExtensionContext) { |
| 34 | + let disposable = vscode.commands.registerCommand("PasteRegEx.rmHangingSpaces", async () => { |
| 35 | + /* |
| 36 | + This function removes all of the spaces that appear at the end of sentences |
| 37 | + */ |
| 38 | + var selectionText: String = getSelectedText(); |
| 39 | + let editor = vscode.window.activeTextEditor; |
| 40 | + let RegEx = new RegExp(" $", "gm") |
| 41 | + var modifiedString: string = selectionText.replace(RegEx, ""); |
| 42 | + insertNewString(modifiedString) |
| 43 | + }); |
12 | 44 |
|
13 | | - // The command has been defined in the package.json file |
14 | | - // Now provide the implementation of the command with registerCommand |
15 | | - // The commandId parameter must match the command field in package.json |
16 | | - let disposable = vscode.commands.registerCommand('pasteregex.helloWorld', () => { |
17 | | - // The code you place here will be executed every time your command is executed |
| 45 | + disposable = vscode.commands.registerCommand("PasteRegEx.rmNewLines", async () => { |
| 46 | + /* |
| 47 | + This function removes all of the new lines in the selection |
| 48 | + */ |
| 49 | + var selectionText: String = getSelectedText(); |
| 50 | + let editor = vscode.window.activeTextEditor; |
| 51 | + let RegEx = new RegExp("\n", "gm") |
| 52 | + var modifiedString: string = selectionText.replace(RegEx, " "); |
| 53 | + insertNewString(modifiedString) |
| 54 | + }); |
18 | 55 |
|
19 | | - // Display a message box to the user |
20 | | - vscode.window.showInformationMessage('Hello World from PasteRegex!'); |
21 | | - }); |
| 56 | + disposable = vscode.commands.registerCommand("PasteRegEx.rmHyphenLineEnd", async () => { |
| 57 | + /* |
| 58 | + This function removes all of the hyphens that appear at the end of the line |
| 59 | + */ |
| 60 | + var selectionText: String = getSelectedText(); |
| 61 | + let editor = vscode.window.activeTextEditor; |
| 62 | + let RegEx = new RegExp("(?<=[a-z])-$\n(?=[a-z])", "gm") |
| 63 | + var modifiedString: string = selectionText.replace(RegEx, ""); |
| 64 | + insertNewString(modifiedString) |
| 65 | + }); |
22 | 66 |
|
23 | | - context.subscriptions.push(disposable); |
| 67 | + disposable = vscode.commands.registerCommand("PasteRegEx.rmSentenceBreaks", async () => { |
| 68 | + /* |
| 69 | + This function removes all of the newlines that appear to break up a sentence |
| 70 | + */ |
| 71 | + var selectionText: String = getSelectedText(); |
| 72 | + let editor = vscode.window.activeTextEditor; |
| 73 | + let RegEx = new RegExp("[a-z]$\n+[a-z]", "gm") |
| 74 | + var modifiedString: string = selectionText.replace(RegEx, " "); |
| 75 | + insertNewString(modifiedString) |
| 76 | + }); |
24 | 77 | } |
25 | 78 |
|
26 | | -// this method is called when your extension is deactivated |
27 | | -export function deactivate() {} |
| 79 | +export function deactivate() { } |
| 80 | + |
| 81 | +/* |
| 82 | +Getting access to the clipboard text |
| 83 | +// var clipboardText = await vscode.env.clipboard.readText(); |
| 84 | +*/ |
0 commit comments