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
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
"key": "ctrl+i",
"command": "extension.wrapTag",
"when": "editorTextFocus"
}
},
{
"key": "ctrl+shift+i",
"command": "extension.wrapTagNewLine",
"when": "editorTextFocus"
}
]
},
"scripts": {
Expand All @@ -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"
}
}
97 changes: 64 additions & 33 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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 {
Expand All @@ -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('>')

sn.appendPlaceholder(`${this.replacementTag}`, 1);
sn.appendText(`>${this.selectedText}</`);
sn.appendPlaceholder(`${this.replacementTag}`, 1) // [the said above]
//sn.appendText(`${this.replacementTag}`); // i use this one instead of [the above] since i use "auto rename tag" extension
sn.appendText('>');

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;
};

Expand Down