Created
February 26, 2020 13:54
-
-
Save andreberg/3b563ff901782c3392d1beb455620d69 to your computer and use it in GitHub Desktop.
[VSCode: Slasher] TextEditor command extenison for converting between forward and backward slashes in a text selection. #vscode #extension #typescript #command #conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The module 'vscode' contains the VS Code extensibility API | |
// Import the module and reference it with the alias vscode in your code below | |
import * as vscode from 'vscode'; | |
// this method is called when your extension is activated | |
// your extension is activated the very first time the command is executed | |
export function activate(context: vscode.ExtensionContext) { | |
// Use the console to output diagnostic information (console.log) and errors (console.error) | |
// This line of code will only be executed once when your extension is activated | |
console.log('Congratulations, your extension "slasher" is now active!'); | |
// The command has been defined in the package.json file | |
// Now provide the implementation of the command with registerCommand | |
// The commandId parameter must match the command field in package.json | |
let disposable = vscode.commands.registerTextEditorCommand('extension.slasher', (textEditor, edit) => { | |
// The code you place here will be executed every time your command is executed | |
const doc = textEditor.document; | |
textEditor.selections.forEach((value, index, array) => { | |
const range = new vscode.Range(value.start, value.end); | |
const text = textEditor.document.getText(range); | |
const matchFrontSlashes = text.match(/\//g); | |
const matchBackSlashes = text.match(/\\/g); | |
let numFrontSlashes = 0; | |
let numBackSlashes = 0; | |
if (matchFrontSlashes !== null) { | |
numFrontSlashes = matchFrontSlashes.length; | |
} | |
if (matchBackSlashes !== null) { | |
numBackSlashes = matchBackSlashes.length; | |
} | |
const file = vscode.Uri.file(text); | |
const path = file.fsPath; | |
if (numBackSlashes > numFrontSlashes) { | |
edit.replace(range, path.replace(/\\/g, "/")); | |
} else if (numFrontSlashes > numBackSlashes) { | |
edit.replace(range, path.replace(/\//g, "\\")); | |
} else if (numBackSlashes == numFrontSlashes) { | |
if (numBackSlashes == 0) { | |
vscode.window.showInformationMessage("No slashes detected (not a path?)"); | |
} else { | |
vscode.window.showWarningMessage("Cannot determine intended conversion direction. (reason: path contains front and backslashes of equal amount)"); | |
} | |
} | |
}) | |
}); | |
context.subscriptions.push(disposable); | |
} | |
// this method is called when your extension is deactivated | |
export function deactivate() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment