From bb74fa03924d1ce65d1c09624961b6f066adbfeb Mon Sep 17 00:00:00 2001 From: fakihap Date: Thu, 4 Aug 2022 09:29:04 +0700 Subject: [PATCH] added functionality Made it so there will be 2 available modes, - the inline wrapTag (ctrl+i) - the new-line wrapTagNewLine (ctrl+shift+i) --- package.json | 15 +++++--- src/extension.ts | 97 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 73 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index ee3752a..48a38e8 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,12 @@ "key": "ctrl+i", "command": "extension.wrapTag", "when": "editorTextFocus" - } + }, + { + "key": "ctrl+shift+i", + "command": "extension.wrapTagNewLine", + "when": "editorTextFocus" + } ] }, "scripts": { @@ -37,11 +42,9 @@ "postinstall": "node ./node_modules/vscode/bin/install" }, "devDependencies": { - "typescript": "^2.0.3", - "vscode": "^1.0.0", + "@types/mocha": "^2.2.32", "@types/node": "^6.0.40", - "@types/mocha": "^2.2.32" - }, - "dependencies": { + "typescript": "^2.0.3", + "vscode": "^1.1.37" } } diff --git a/src/extension.ts b/src/extension.ts index b18ec24..8223a15 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,24 +5,37 @@ import { commands, window, TextDocument, TextEditorEdit, ExtensionContext, Posit // your extension is activated the very first time the command is executed export function activate(context: ExtensionContext) { - let disposable = commands.registerCommand('extension.wrapTag', () => { - + let wrapTag = commands.registerCommand('extension.wrapTag', () => { const editor = window.activeTextEditor; - // if (!editor || !(editor.document.languageId === 'html')) return; - if (!editor) return; - + if (!editor) + return; let selection = editor.selection; let selectedText = editor.document.getText(selection); let wrapper = new TagWrapper(selectedText, selection); - if (wrapper.isAvaliableTag) { - editor.insertSnippet(wrapper.snippet); //insert snippet to replace the selection text + editor.insertSnippet(wrapper.snippet); //insert snippet to replace the selection text } - - }) - - context.subscriptions.push(disposable); + }); + + let wrapTagNewLine = commands.registerCommand('extension.wrapTagNewLine', () => { + const editor = window.activeTextEditor; + // if (!editor || !(editor.document.languageId === 'html')) return; + if (!editor) + return; + let selection = editor.selection; + let selectedText = editor.document.getText(selection); + let wrapper = new TagWrapper(selectedText, selection, true); + + // turned off availableTag check because i still cant find its uses + // and personally i think it's better this way + + // if (wrapper.isAvaliableTag) { + editor.insertSnippet(wrapper.snippet); //insert snippet to replace the selection text + // } + }); + + context.subscriptions.push(wrapTag, wrapTagNewLine); } @@ -35,8 +48,8 @@ class TagWrapper { private replacementTag = 'div'; private selectedText: string; - constructor(selectedText: string, selection: Selection) { - this.selectedText = this.formatSelectedText(selectedText, selection); + constructor(selectedText: string, selection: Selection, addNewLine : boolean = false) { + this.selectedText = this.formatSelectedText(selectedText, selection, addNewLine); } get snippet(): SnippetString { @@ -50,37 +63,55 @@ class TagWrapper { private generateSnippet(): SnippetString { let sn = new SnippetString(); - sn.appendText('<') + sn.appendText('<'); // sn.appendTabstop(1) - sn.appendPlaceholder(`${this.replacementTag}`, 1) - sn.appendText(`>\n${this.selectedText}') - + sn.appendPlaceholder(`${this.replacementTag}`, 1); + sn.appendText(`>${this.selectedText}'); + return sn; } //format multi line selected text - private formatSelectedText(selectedText: string, selection: Selection): string { + private formatSelectedText(selectedText: string, selection: Selection, addNewLine : boolean): string { let start = selection.start.character; let textArr; let endLine; - - if (selectedText.indexOf('\n') > -1) { - textArr = selectedText.split('\n') - endLine = '\n' + let formated; + + if (!addNewLine){ + if (selectedText.indexOf('\n') > -1) { + textArr = selectedText.split('\n'); + endLine = '\n'; + } + else { + textArr = selectedText.split('\r'); + endLine = '\r'; + } + formated = ''; + textArr.forEach((line, index) => { + formated += index === 0 ? `${line}` : `\t${line.substr(start)}`; + if (index+1 < textArr.length){ + formated += `${endLine}` + } + }); } else { - textArr = selectedText.split('\r') - endLine = '\r' + if (selectedText.indexOf('\n') > -1) { + textArr = selectedText.split('\n'); + endLine = '\n'; + } + else { + textArr = selectedText.split('\r'); + endLine = '\r'; + } + formated = `${endLine}` + textArr.forEach((line, index) => { + formated += index === 0 ? `\t${line}${endLine}` : `\t${line.substr(start)}${endLine}`; + }); } - let formated = ''; - textArr.forEach((line, index) => { - - formated += index === 0 ? `\t${line}${endLine}` : `\t${line.substr(start)}${endLine}`; - - }) - return formated; };